Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ma.compress_nd behaves not as expected #13709

Open
oldnaari opened this issue Jun 4, 2019 · 4 comments
Open

ma.compress_nd behaves not as expected #13709

oldnaari opened this issue Jun 4, 2019 · 4 comments

Comments

@oldnaari
Copy link

oldnaari commented Jun 4, 2019

Reproducing code example:

from numpy import ma
arr = ma.masked_all((3, 3, 2))
arr[0,0] = 1
out = ma.compress_nd(arr, axis=(0, 1))

This code sets out to the following value:

array([], shape=(0, 0, 2), dtype=float64)

The expected value is:

array([[1., 1.]])

Numpy/Python version information:

1.16.2 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0]
@eric-wieser
Copy link
Member

I think this is working as intended - the goal is to suppress slices that contain any masked elements, and in your array every slice along axes 0 and 1 contains a masked element.

There is however a bug here if you skip arr[0,0] = 1, we return an array of the wrong shape.

@oldnaari
Copy link
Author

oldnaari commented Jun 4, 2019

I don't get it. All elements for arr[0, 0, :] contain unmasked values. I have tried:

ma.compress_nd(arr, axis=(0, 1, 2))
ma.compress_nd(arr, axis=(0, 1))
ma.compress_nd(arr, axis=(1, 2))
ma.compress_nd(arr, axis=(0, 2))
ma.compress_nd(arr, axis=(0, ))
ma.compress_nd(arr, axis=(1, ))
ma.compress_nd(arr, axis=(2, ))

All of those give empty arrays. Shouldn't one of them be non empty? Or it works like

ma.compress_nd(arr, axis=(0, 1)) = ma.compress_nd(ma.compress_nd(arr, axis=0), axis=0)

?

@eric-wieser
Copy link
Member

Or it works like ...

It should be the same as ma.compress_nd(ma.compress_nd(arr, axis=0), axis=1). The shape is wrong though, so you found another bug.

Turns out the two optimizations in that function are both wrong.

@seberg seberg added the 00 - Bug label Jun 4, 2019
@ddasilva
Copy link
Contributor

ddasilva commented Nov 19, 2021

@eric-wieser Is this as simple as removing the following optimizations? I authored this function a number of years ago at the advice of a core developer (@charris I think?). Would be happy to fix.

    # Nothing is masked: return x
    if m is nomask or not m.any():
        return x._data
    # All is masked: return empty
    if m.all():
        return nxarray([])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants