-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
MAINT: Warn users when calling np.ma.MaskedArray.partition function. #8669
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
Conversation
I feel like we could Also, it feels like this should touch |
@eric-wieser I played around a bit and one could make it "partially" working by placing half of the masked items at the beginning of the axis and the other half of the masked items at the end of the axis and partition everything in between. However this approach still gives wrong results if there's an uneven amount of masked items. So there is no approach that does an exact partition in the general case which is probably why that method hasn't been implemented yet.
You're right! Should I go forward and fix the failing test and put a Warning in |
Yeah, adding the warning seems like a good interim solution. Perhaps it should mention
This also doesn't really work for |
@juliantaylor Comment? |
numpy/ma/core.py
Outdated
@@ -5640,6 +5640,12 @@ def ptp(self, axis=None, out=None, fill_value=None): | |||
np.subtract(out, min_value, out=out, casting='unsafe') | |||
return out | |||
|
|||
def partition(self, *args, **kwargs): | |||
warnings.warn("Warning: partition will ignore masked items " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"ignore the mask" is different to "ignore masked items". The former is what happens, the latter is what the user expects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
would it make more sense to ma np.median masked array aware and just call ma.median? it should have the same feature set by now. |
@juliantaylor: Can we just make |
@juliantaylor @eric-wieser That
I don't think that workaround wouldn't be very helpful when the Warning arises from a |
numpy/ma/tests/test_extras.py
Outdated
@@ -1039,14 +1039,14 @@ def test_empty(self): | |||
|
|||
# axis 0 and 1 | |||
b = np.ma.masked_array(np.array([], dtype=float, ndmin=2)) | |||
assert_equal(np.median(a, axis=0), b) | |||
assert_equal(np.median(a, axis=1), b) | |||
assert_equal(np.ma.median(a, axis=0), b) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@juliantaylor These np.median
calls were introduced in ff4758f. Was it np.median
instead of np.ma.median
on purpose?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these tests are mostly copy pastes from median tests, likely forgot to update these. Should be ma.median.
ok, I'm really stuck here. The tests with Could someone advise me how to proceed? The Error:
|
hm they should behave the same. Probably another bug not found due to the copy paste error in the test. |
Is there some easy way to restart the tests (or one test) here? I think #8705 should've fixed the failing test. |
@MSeifert04: You'll need to rebase on master to actually use those changes |
…tion. Using the np.median function on MaskedArrays uses the not-overriden partition method of a plain np.ndarray without error or warning. (#7330) This PR overrides the partition method on MaskedArrays but simply to throw a Warning. This will make users aware that something ignores the mask without breaking backwards-compatibility. This also applies to the argpartition method (even if it's not called by np.median).
Oh, I thought that it should pick up changes even without rebase. I've rebased the branch and am hoping for green tests. |
@MSeifert04: I get the feeling the test is done in isolation, rather than merged with master, but I could be wrong. |
def argpartition(self, *args, **kwargs): | ||
warnings.warn("Warning: 'argpartition' will ignore the 'mask' " | ||
"of the {}.".format(self.__class__.__name__), | ||
stacklevel=2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, why level 2?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, this is consistent with everything else
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that was simply copied from another warning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Python docs seemed to suggest that stacklevel=2
is what you should use in a warning helper function. But yeah, this is better for consistency
Thanks @MSeifert04 |
Using the
np.median
function onMaskedArray
s uses the not-overridenpartition
method of a plainnp.ndarray
without error or warning. (#7330)This PR overrides the
partition
method onMaskedArray
s but simply tothrow a Warning. This will make users aware that something ignores the
mask without breaking backwards-compatibility.