-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
np.tile modifies the mask of the inputted masked array #3140
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
Comments
Yes... I think there is an older bug report about this... The basic problem is that somewhere the masks shape is assigned using https://github.com/seberg/numpy/compare/safemask I did change the |
So the more thorough fix would probably include changing the ndarray.shape attribute setting logic (possible fixing #145 while at it. I am not certain as to the discussion, but since I messed with it recently, attempt_nocopy_reshape can easily be modified to cover all cases (if someone works on that, I have had done it, but it would be unused right now so it is not in numpy), so if it is exposed to the getset.c it could be just called directly then. But just saying if someone feels like looking at into finding a good solution. I don't think its difficult overall, and I am happy to give pointers. |
@seberg, thanks for a quick and thorough reply. I'm a bit confused right now. Do you think working on https://github.com/seberg/numpy/compare/safemask would be a good way to fix this or do you think this issue should be tackled by fixing #145? The latter is harder to understand for me and I don't know why it hasn't been merged yet? Also, I haven't contributed to Numpy yet, so I'm not very familiar with the workflow (though I've read the development workflow). Is this easy enough for a first contribution? |
Don't know, but I had a bit longer look, and I think fixing the latter (gh-145) has some tedious details, and maybe the approach in that PR is best. I just though that by changing ndarray.shape attribute setting, you may not need to give the masked array its own shape attribute. While I do not remember why I did that, there was probably some reason... So my guess is, the basic approach in that safemask branch is probably good, but needs at the very least some tests. Contributing is very easy, all you need to do is create your own branch in a github fork, push to it and then github will already show you a button to create a pull request. And once you have the pull request open, anyone can easily comment on it to smooth out the details. |
The bug is still present identically in recent numpy versions. >>> np.__version__
'1.14.3'
>>> A.mask.shape
(1, 2)
>>> A.shape
(2,)
>>> A
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/honnorat/usr/conda/lib/python3.6/site-packages/numpy/ma/core.py", line 3942, in __repr__
self._insert_masked_print(),
File "/Users/honnorat/usr/conda/lib/python3.6/site-packages/numpy/ma/core.py", line 3866, in _insert_masked_print
_recursive_printoption(res, mask, masked_print_option)
File "/Users/honnorat/usr/conda/lib/python3.6/site-packages/numpy/ma/core.py", line 2439, in _recursive_printoption
np.copyto(result, printopt, where=mask)
ValueError: could not broadcast where mask from shape (1,2) into shape (2) |
@mhvk: Wrong PR number? |
Thanks, corrected to #10314 |
Hello, I have run the snippet of code in the description and I am under the impression that this issue can be closed? Python 3.10.7 (main, Sep 7 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.__version__
'1.24.0.dev0+934.gdb7414b7f'
>>> A = ma.masked_array(np.array([1., 2.]), mask=np.array([False, False]))
>>> A
masked_array(data=[1.0, 2.0],
mask=[False, False],
fill_value=1e+20)
>>> B = np.tile(A, (2, 1))
>>> A
masked_array(data=[1.0, 2.0],
mask=[False, False],
fill_value=1e+20)
>>> A._data
array([1., 2.])
>>> A._mask
array([False, False]) Thanks for listening. |
Here is an example of the problem:
The issue is that the call to
np.tile
changedA._mask
fromarray([False, False], dtype=bool)
toarray([[False, False]], dtype=bool)
, which made the__repr___()
function return the error.Digging deeper, I found that the issue is with the call
_nx.array(A,copy=False,subok=True,ndmin=d)
in the tile function.Before this call,
A
has the correct_mask
and after this call,A
's_mask
changes.Does anyone have any ideas how to fix this?
This is on Fedora-rawhide with:
python-2.7.3-34.fc19.x86_64
numpy-1.7.0-1.fc19.x86_64
The text was updated successfully, but these errors were encountered: