10000 ma.empty_like copies reference to mask instead of making a new mask · Issue #3145 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ma.empty_like copies reference to mask instead of making a new mask #3145

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

Closed
AgilentGCMS opened this issue Mar 14, 2013 · 6 comments
Closed

Comments

@AgilentGCMS
Copy link

ma.empty_like on a masked array creates a new masked array with the mask pointing to the same mask as the source array, so if the target array mask is changed, the mask of the source array changes as well. Code sample:

from numpy import * # create some random data a = random.random((5,5)) # make this a masked array a = ma.masked_greater(a, 0.3) # use empty_like to create a new array b = ma.empty_like(a) # change b, in particular do something that sets the mask to False b[:,:] = 2.0 # check whether at least one element in a is still masked print a.mask.any()

You will see that this destroys the mask of a. Note that this issue is similar to #2572, except that this exists even for numpy.ma.empty_like, which, being from the numpy.ma module, should in principle know how to handle masked arrays.

@tomyun
Copy link
tomyun commented Jun 5, 2013

Any progress? As noted in #2572, numpy.zeros_like also does keep a reference of mask, leading to undesirable results. It is remarkable that numpy.ma.empty_like is called under the hood.

By the way, I use a below snippet to circumvent this issue.

# b = numpy.zeros_like(a)
b = a.copy()
b.fill(0)

@njsmith
Copy link
Member
njsmith commented Jun 5, 2013

No progress - it's a nasty bug but probably the only person who it's urgent
for is you. So your best bet on getting it fixed is to submit a pull
request. Probably an easy fix...
On 5 Jun 2013 01:43, "Kyungdahm Yun" notifications@github.com wrote:

Any progress? As noted in #2572#2572,
numpy.zeros_like also does keep a reference of mask, leading to
undesirable results. It is remarkable that numpy.ma.empty_like is called
under the hood.

By the way, I use a below snippet to circumvent this issue.

b = numpy.zeros_like(a)b = a.copy()b.fill(0)


Reply to this email directly or view it on GitHubhttps://github.com//issues/3145#issuecomment-18948923
.

tomyun added a commit to tomyun/numpy that referenced this issue Jun 5, 2013
As underlying `empty_like()` knows nothing about mask, it would be
`_convert2ma`'s job to properly set up the output mask. `copy` argument
is added to figure out which functions need this behavior.

`ones_like()` and `zeros_like()` which were mere aliases of regular
numpy functions now go through `_convert2ma` as well.

Hopefully this fixes numpy#3145, numpy#2572.
@tomyun
Copy link
tomyun commented Jun 5, 2013

Well, a5a3457 forces empty/ones/zeros_like() to copy mask when returned for MaskedArray. _convert2ma class has been slightly modified to distinguish those functions. I'd appreciate any comment on this approach.

@njsmith
Copy link
Member
njsmith commented Jun 5, 2013

Please submit a PR - that's the standard interface we use for reviewing
patches. And thanks!
On 5 Jun 2013 08:41, "Kyungdahm Yun" notifications@github.com wrote:

Well, a5a3457 a5a3457 forces
empty/ones/zeros_like() to copy mask when returned for MaskedArray.
_convert2ma class has been slightly modified to distinguish those
functions. I'd appreciate any comment on this approach.


Reply to this email directly or view it on GitHubhttps://github.com//issues/3145#issuecomment-18960235
.

@jjhelmus
Copy link
Contributor

This issue is also reported in #2572 and has been fixed in PR #5203. I believe both this issues can be closed.

@njsmith
Copy link
Member
njsmith commented Oct 19, 2015

@jjhelmus: Thanks!

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

No branches or pull requests

4 participants
0