8000 BUG: Fix numpy.ma.median. by charris · Pull Request #8016 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fix numpy.ma.median. #8016

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

Merged
merged 2 commits into from
Sep 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions numpy/ma/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -708,34 +708,37 @@ def _median(a, axis=None, out=None, overwrite_input=False):
asorted = a
else:
asorted = sort(a, axis=axis)

if axis is None:
axis = 0
elif axis < 0:
axis += a.ndim
axis += asorted.ndim

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(out=out)

counts = asorted.shape[axis] - (asorted.mask).sum(axis=axis)
counts = count(asorted, axis=axis)
h = counts // 2

# create indexing mesh grid for all but reduced axis
axes_grid = [np.arange(x) for i, x in enumerate(asorted.shape)
if i != axis]
ind = np.meshgrid(*axes_grid, sparse=True, indexing='ij')

# insert indices of low and high median
ind.insert(axis, h - 1)
low = asorted[tuple(ind)]
low._sharedmask = False
ind[axis] = h
high = asorted[tuple(ind)]

# duplicate high if odd number of elements so mean does nothing
odd = counts % 2 == 1
if asorted.ndim == 1:
if odd:
low = high
else:
low[odd] = high[odd]
if asorted.ndim > 1:
np.copyto(low, high, where=odd)
elif odd:
low = high

if np.issubdtype(asorted.dtype, np.inexact):
# avoid inf / x = masked
Expand Down
32 changes: 32 additions & 0 deletions numpy/ma/tests/test_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from __future__ import division, absolute_import, print_function

import warnings
import itertools

import numpy as np
from numpy.testing import (
Expand Down Expand Up @@ -684,6 +685,37 @@ def test_docstring_examples(self):
assert_equal(ma_x.shape, (2,), "shape mismatch")
assert_(type(ma_x) is MaskedArray)

def test_axis_argument_errors(self):
msg = "mask = %s, ndim = %s, axis = %s, overwrite_input = %s"
for ndmin in range(5):
for mask in [False, True]:
x = array(1, ndmin=ndmin, mask=mask)

# Valid axis values should not raise exception
args = itertools.product(range(-ndmin, ndmin), [False, True])
for axis, over in args:
try:
np.ma.median(x, axis=axis, overwrite_input=over)
except:
raise AssertionError(msg % (mask, ndmin, axis, over))

# Invalid axis values should raise exception
args = itertools.product([-(ndmin + 1), ndmin], [False, True])
for axis, over in args:
try:
np.ma.median(x, axis=axis, overwrite_input=over)
except IndexError:
pass
else:
raise AssertionError(msg % (mask, ndmin, axis, over))

def test_masked_0d(self):
# Check values
x = array(1, mask=False)
assert_equal(np.ma.median(x), 1)
x = array(1, mask=True)
assert_equal(np.ma.median(x), np.ma.masked)

def test_masked_1d(self):
x = array(np.arange(5), mask=True)
assert_equal(np.ma.median(x), np.ma.masked)
Expand Down
0