BUG: Look up methods on MaskedArray in _frommethod#8665
BUG: Look up methods on MaskedArray in _frommethod#8665ahaldane merged 2 commits intonumpy:masterfrom
Conversation
|
It would be great if #8019 is finally adressed. :) Thanks for working on it. |
|
@MSeifert04: I'm actually targeting this because this can remove a lot of special-casing in |
| # Still here ? OK, let's call the corresponding np function | ||
| method = getattr(np, method_name) | ||
| return method(a, *args, **params) | ||
| method = getattr(type(marr), method_name, None) |
There was a problem hiding this comment.
Why look up the method on the class rather than just getattr(marr, method_name, None)?
There was a problem hiding this comment.
Seems tidier I guess (and closed to how dunder lookup works), and makes it work on masked record arrays.
You've actually made me notice a bug here, in that reversed should only apply to the method call. Either way, that bug was here all along, and can wait for another PR.
There was a problem hiding this comment.
It does look really weird to me to look it up on the class! But of course if one looks up on marr, one cannot pass marr as first argument below. If I were to write this, I'd probably do:
method = getattr(marr, 'method_name', None)
if method is not None:
return method(*args, **params)
else:
return getattr(np, method_name)(marr, *args, **params)
Anyway, no big deal either way.
| <class 'numpy.ma.core.MaskedArray'> | ||
|
|
||
| """ | ||
| # workaround for #8666, to preserve identity. Ideally the bottom line |
There was a problem hiding this comment.
One could change MaskedArray.__new__, though perhaps calling a class constructor and not getting back a new instance might also be surprising... Perhaps best might have been to have had a logical distinction between the np.ma.MaskedArray class and a np.ma.masked_array/np.ma.array function...
Overall, this is probably the best solution.
There was a problem hiding this comment.
One could change
MaskedArray.__new__
This is the correct approach, and already how ndarray.__new__ behaves. Now take a look at MaskedArray.__new__, and tell me how easy that changes looks. That's why I'm calling this a workaround!
There was a problem hiding this comment.
I think one would add right after the first line:
if _data is data and (type(_data) is cls or subok and isinstance(_data, cls)):
return _data
There was a problem hiding this comment.
Just to be sure: I think for a bug fix you're current approach is preferable!
There was a problem hiding this comment.
@mhkv: Not enough. I think you also need to check that all of the other arguments are their default value, or more difficultly, compatible values
| if not isinstance(x, MaskedArray): | ||
| x = asanyarray(x) | ||
| return x.compressed() | ||
| return asanyarray(x).compressed() |
There was a problem hiding this comment.
I think with your other change it is now safe (and shorter though a bit slower) to just do
compressed = _frommethod('compressed')
There was a problem hiding this comment.
I agree. Until just now, I thought that this was only for methods that exist in np, but it is not
There was a problem hiding this comment.
I'll add another commit for this that fixes a bunch of other methods as well, that were out of scope for the original PR
There was a problem hiding this comment.
@mhvk: actually, can I bump that method cleanup to another PR?
There was a problem hiding this comment.
Yes, better in another PR. As is, this is a bug fix, but that would be maintenance.
There was a problem hiding this comment.
Happy for me to leave this line in then, or do you want me to remove it?
There was a problem hiding this comment.
In another PR is slightly better, but really this is making work, so whatever you feel like!
There was a problem hiding this comment.
I'd rather this just be merged as is, to be honest
|
Looks good! |
|
This one should go in 12.1, I think, since it was a specific, somewhat long-standing issue that got solved. Since this was reviewed before, I'm quite happy to merge once the tests pass. |
|
@mhvk: tests have passed, but travis isn't pinging github correctly |
ahaldane
left a comment
There was a problem hiding this comment.
It checks out to me. We can merge once the testing system is happy.
|
let's see if that gets the tests to register... if not I think it's OK merge anyway since the tests do pass. |
|
OK, merging. Thanks @eric-wieser ! |
|
@eric-wieser @mhvk Was it intended to backport this? |
|
@charris - yes, this was meant to be backported to 1.12.1; I'll try to do the |
|
No, don't. I'll just cherry-pick it into #8739. |
|
Oops, I thought you were volunteering to do the backport... |
Fixes #8019
Squashed version of #8060, with the addition of the suggestions I made there in the comments