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

BUG operations involving MaskedArray with output given do not return output #8416

Merged
merged 1 commit into from
Feb 1, 2017

Conversation

mhvk
Copy link
Contributor

@mhvk mhvk commented Jan 22, 2017

EDIT -- turned into a PR.

Fixes an inconsistency found in #8414:

import numpy as np
a = np.ma.MaskedArray([1., 2.])
b = np.ma.MaskedArray(1.)
c = a.sum(out=b)
c is b
# True
d = np.true_divide(c, 2., out=b)
d is b
# False

Here, d is b should be True. That it is Falses suggested __array_wrap__ is not implemented quite correctly, probably missing a special path for if self is obj -- this PR fixes that.

@mhvk
Copy link
Contributor Author

mhvk commented Jan 22, 2017

I turned this into a PR to fix the issue that ufunc(ma, out=ma) would not return ma. cc @juliantaylor and @charris who noticed this in #8414.

@@ -2988,8 +2988,11 @@ def __array_wrap__(self, obj, context=None):
Wraps the numpy array and sets the mask according to context.

"""
result = obj.view(type(self))
result._update_from(self)
if obj is self: # for in-place operations
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: probably this is better done in __array_prepare__, but given that we hopefully soon have __array_func__ to work with, I felt it not quite worth it to try to define that...

numpy/ma/core.py Outdated
result = result.copy()
if result is not self:
# MHvK: Not sure why this copy is wanted.
result = result.copy()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just spent a little bit of effort trying to figure out if this copy had any purpose, but I can't see any. It's not related to the copying of masks discussed in #5580, for example.

Want to just remove it?

Without this commit, after an operation like
```
a = np.ma.MaskedArray([1., 2., 3.])
b = np.add(a, 1., out=a)
```
one would have `b is not a`.  With this PR, it is guaranteed that
`b is a`.
@mhvk mhvk force-pushed the ma/ensure-given-output-is-returned branch from 6640387 to 7927b3c Compare February 1, 2017 19:39
@mhvk
Copy link
Contributor Author

mhvk commented Feb 1, 2017

@ahaldane - OK, I agree it made little sence so just removed the strange copy (and rebased). Hopefully, tests will still pass.

@ahaldane
Copy link
Member

ahaldane commented Feb 1, 2017

Looks good to me otherwise, merging. Thanks @mhvk

@ahaldane ahaldane merged commit a12a171 into numpy:master Feb 1, 2017
@mhvk mhvk deleted the ma/ensure-given-output-is-returned branch February 1, 2017 21:36
juliantaylor added a commit to juliantaylor/numpy that referenced this pull request Feb 6, 2017
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

Successfully merging this pull request may close these issues.

None yet

2 participants