Skip to content

BUG: Look up methods on MaskedArray in _frommethod#8665

Merged
ahaldane merged 2 commits intonumpy:masterfrom
eric-wieser:better-ma-method-lookup
Feb 23, 2017
Merged

BUG: Look up methods on MaskedArray in _frommethod#8665
ahaldane merged 2 commits intonumpy:masterfrom
eric-wieser:better-ma-method-lookup

Conversation

@eric-wieser
Copy link
Member

@eric-wieser eric-wieser commented Feb 22, 2017

Fixes #8019

Squashed version of #8060, with the addition of the suggestions I made there in the comments

@MSeifert04
Copy link
Contributor

It would be great if #8019 is finally adressed. :) Thanks for working on it.

@eric-wieser
Copy link
Member Author

@MSeifert04: I'm actually targeting this because this can remove a lot of special-casing in np.ma functions.

@eric-wieser eric-wieser requested a review from ahaldane February 22, 2017 10:29
# 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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why look up the method on the class rather than just getattr(marr, method_name, None)?

Copy link
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

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
Copy link
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

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!

Copy link
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Just to be sure: I think for a bug fix you're current approach is preferable!

Copy link
Member Author

@eric-wieser eric-wieser Feb 22, 2017

Choose a reason for hiding this comment

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

@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

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, makes sense.

if not isinstance(x, MaskedArray):
x = asanyarray(x)
return x.compressed()
return asanyarray(x).compressed()
Copy link
Contributor

Choose a reason for hiding this comment

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

I think with your other change it is now safe (and shorter though a bit slower) to just do

compressed = _frommethod('compressed')

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree. Until just now, I thought that this was only for methods that exist in np, but it is not

Copy link
Member Author

Choose a reason for hiding this comment

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

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

Copy link
Member Author

Choose a reason for hiding this comment

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

@mhvk: actually, can I bump that method cleanup to another PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, better in another PR. As is, this is a bug fix, but that would be maintenance.

Copy link
Member Author

Choose a reason for hiding this comment

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

Happy for me to leave this line in then, or do you want me to remove it?

Copy link
Contributor

Choose a reason for hiding this comment

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

In another PR is slightly better, but really this is making work, so whatever you feel like!

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd rather this just be merged as is, to be honest

Copy link
Contributor

Choose a reason for hiding this comment

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

No problem.

@mhvk
Copy link
Contributor

mhvk commented Feb 22, 2017

Looks good!

@mhvk mhvk added this to the 1.12.1 release milestone Feb 22, 2017
@mhvk
Copy link
Contributor

mhvk commented Feb 22, 2017

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.

@eric-wieser
Copy link
Member Author

@mhvk: tests have passed, but travis isn't pinging github correctly

Copy link
Member

@ahaldane ahaldane left a comment

Choose a reason for hiding this comment

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

It checks out to me. We can merge once the testing system is happy.

@ahaldane ahaldane closed this Feb 23, 2017
@ahaldane ahaldane reopened this Feb 23, 2017
@ahaldane
Copy link
Member

let's see if that gets the tests to register... if not I think it's OK merge anyway since the tests do pass.

@ahaldane
Copy link
Member

OK, merging. Thanks @eric-wieser !

@ahaldane ahaldane merged commit 081a865 into numpy:master Feb 23, 2017
@charris
Copy link
Member

charris commented Mar 4, 2017

@eric-wieser @mhvk Was it intended to backport this?

@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Mar 4, 2017
@mhvk
Copy link
Contributor

mhvk commented Mar 4, 2017

@charris - yes, this was meant to be backported to 1.12.1; I'll try to do the backport-candidate labelling in the future.

@charris
Copy link
Member

charris commented Mar 4, 2017

No, don't. I'll just cherry-pick it into #8739.

@charris
Copy link
Member

charris commented Mar 4, 2017

Oops, I thought you were volunteering to do the backport...

@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Mar 4, 2017
@charris charris removed this from the 1.12.1 release milestone Mar 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants