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: Fix numpy.ma.median. #8016

Merged
merged 2 commits into from
Sep 6, 2016
Merged

BUG: Fix numpy.ma.median. #8016

merged 2 commits into from
Sep 6, 2016

Conversation

charris
Copy link
Member

@charris charris commented Sep 4, 2016

This fixes four bugs:

  • Use of booleans for indexing in ma.extras._median
  • Error when mask is nomask and ndim != 1 in ma.extras._median
  • Did not work for 0-d arrays.
  • No out argument for {0,1}-d arrays.

Closes #8015.

@charris charris added this to the 1.11.2 release milestone Sep 4, 2016
@charris
Copy link
Member Author

charris commented Sep 4, 2016

Needs tests, just putting up now for reference.

if asorted.mask is nomask:
counts = asorted.shape[axis]
else:
counts = asorted.shape[axis] - (asorted.mask).sum(axis=axis)
Copy link
Contributor

Choose a reason for hiding this comment

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

purely stylistic, but why the parentheses?


counts = asorted.shape[axis] - (asorted.mask).sum(axis=axis)
if asorted.mask is nomask:
counts = asorted.shape[axis]
Copy link
Contributor

Choose a reason for hiding this comment

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

This gets the wrong dimension... The following does work:

        counts = np.full(asorted.shape[:axis] + asorted.shape[axis+1:],
                         asorted.shape[axis])

Copy link
Contributor

Choose a reason for hiding this comment

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

The above also solves the astropy test problems.

Copy link
Member Author

Choose a reason for hiding this comment

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

How about the count function? I note that it also handles a tuple of axes, which median currently does not.

    def count(self, axis=None, keepdims=np._NoValue):
        """
        Count the non-masked elements of the array along the given axis.

The count function is already imported and used for the 1-d case in _median.

Copy link
Contributor

Choose a reason for hiding this comment

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

well, almost all problems... I think the remaining ones are ours, though...

@charris
Copy link
Member Author

charris commented Sep 4, 2016

OK, I've made use of the count and copyto functions. @mhvk Could you take a look? I'm still not happy with the function and would welcome further improvements.


if asorted.ndim == 1:
idx, odd = divmod(count(asorted), 2)
return asorted[idx - (not odd) : idx + 1].mean()
return asorted[idx + odd - 1 : idx + 1].mean()
Copy link
Contributor

Choose a reason for hiding this comment

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

It is a bit strange that below (https://github.com/numpy/numpy/pull/8016/files#diff-6fd4523c4f6741d8d9e9a20d14561729R745) for inexact dtype the mean is done by summing and then dividing, and here this is ignored. It is out of scope of this PR, though.

@mhvk
Copy link
Contributor

mhvk commented Sep 4, 2016

@sharris - it is indeed not the nicest bit of code, but I think within its context what you've done makes sense (I like the use of count in particular). Probably best to leave the remainder for another time...

@MSeifert04
Copy link
Contributor

Just wanted to say that with this PR the astropy-tests are fixed (at least locally). 👍

@charris charris force-pushed the fix-ma-median branch 4 times, most recently from a9dc9f4 to 07538cf Compare September 5, 2016 19:47
@charris
Copy link
Member Author

charris commented Sep 5, 2016

OK, I think this is ready to go. If anyone wants to run extras tests that would be nice.

@charris charris changed the title BUG: Fix numpy.ma.median for. BUG: Fix numpy.ma.median. Sep 5, 2016
This fixes four bugs:

- Use of booleans for indexing in ma.extras._median
- Error when mask is nomask and ndim != 1 in ma.extras._median
- Did not work for 0-d arrays.
- No out argument for {0,1}-d arrays.

Closes numpy#8015.
@mhvk
Copy link
Contributor

mhvk commented Sep 5, 2016

@charris - all looks OK to me. As @MSeifert04 noted, with this PR, the astropy tests pass again. (I assume your last push of 11 minutes ago was just for edits to the commits, not actual changes.)

@charris
Copy link
Member Author

charris commented Sep 5, 2016

@mhvk Edited a commit message and fixed the error message for one of the tests.

@charris charris merged commit 96025b9 into numpy:master Sep 6, 2016
@charris charris removed this from the 1.11.2 release milestone Sep 6, 2016
@charris charris deleted the fix-ma-median branch September 19, 2018 14:39
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.

IndexError with np.ma.median along axis on unmasked array
3 participants