summaryrefslogtreecommitdiff
path: root/numpy/ma/tests/test_extras.py
diff options
context:
space:
mode:
authorJulian Taylor <jtaylor.debian@googlemail.com>2016-12-24 13:54:52 +0100
committerJulian Taylor <jtaylor.debian@googlemail.com>2016-12-25 15:37:35 +0100
commit6d52633c90ec6a669dae6a63fe6dac6d1a736cab (patch)
treea9e443338ceceb61e9a059073f4e7e0b2e93ac1e /numpy/ma/tests/test_extras.py
parent44e086d2129c85410a5ea13c79f3ff507a6d6453 (diff)
downloadpython-numpy-6d52633c90ec6a669dae6a63fe6dac6d1a736cab.tar.gz
python-numpy-6d52633c90ec6a669dae6a63fe6dac6d1a736cab.tar.bz2
python-numpy-6d52633c90ec6a669dae6a63fe6dac6d1a736cab.zip
TST: extend ma.median testing and fix inconsistent out return
Diffstat (limited to 'numpy/ma/tests/test_extras.py')
-rw-r--r--numpy/ma/tests/test_extras.py69
1 files changed, 59 insertions, 10 deletions
diff --git a/numpy/ma/tests/test_extras.py b/numpy/ma/tests/test_extras.py
index f11180672..fb16d92ce 100644
--- a/numpy/ma/tests/test_extras.py
+++ b/numpy/ma/tests/test_extras.py
@@ -678,10 +678,16 @@ class TestMedian(TestCase):
x = 5
assert_equal(np.ma.median(x), 5.)
assert_(type(np.ma.median(x)) is not MaskedArray)
- # Regression test for gh-8409: even number of entries.
- x = [5., 5.]
- assert_equal(np.ma.median(x), 5.)
- assert_(type(np.ma.median(x)) is not MaskedArray)
+ # integer
+ x = np.arange(9 * 8).reshape(9, 8)
+ assert_equal(np.ma.median(x, axis=0), np.median(x, axis=0))
+ assert_equal(np.ma.median(x, axis=1), np.median(x, axis=1))
+ assert_(np.ma.median(x, axis=1) is not MaskedArray)
+ # float
+ x = np.arange(9 * 8.).reshape(9, 8)
+ assert_equal(np.ma.median(x, axis=0), np.median(x, axis=0))
+ assert_equal(np.ma.median(x, axis=1), np.median(x, axis=1))
+ assert_(np.ma.median(x, axis=1) is not MaskedArray)
def test_docstring_examples(self):
"test the examples given in the docstring of ma.median"
@@ -746,6 +752,26 @@ class TestMedian(TestCase):
assert_equal(np.ma.median(x), 0.)
assert_equal(np.ma.median(x).shape, (), "shape mismatch")
assert_(type(np.ma.median(x)) is not MaskedArray)
+ # integer
+ x = array(np.arange(5), mask=[0,1,1,0,0])
+ assert_equal(np.ma.median(x), 3.)
+ assert_equal(np.ma.median(x).shape, (), "shape mismatch")
+ assert_(type(np.ma.median(x)) is not MaskedArray)
+ # float
+ x = array(np.arange(5.), mask=[0,1,1,0,0])
+ assert_equal(np.ma.median(x), 3.)
+ assert_equal(np.ma.median(x).shape, (), "shape mismatch")
+ assert_(type(np.ma.median(x)) is not MaskedArray)
+ # integer
+ x = array(np.arange(6), mask=[0,1,1,1,1,0])
+ assert_equal(np.ma.median(x), 2.5)
+ assert_equal(np.ma.median(x).shape, (), "shape mismatch")
+ assert_(type(np.ma.median(x)) is not MaskedArray)
+ # float
+ x = array(np.arange(6.), mask=[0,1,1,1,1,0])
+ assert_equal(np.ma.median(x), 2.5)
+ assert_equal(np.ma.median(x).shape, (), "shape mismatch")
+ assert_(type(np.ma.median(x)) is not MaskedArray)
def test_1d_shape_consistency(self):
assert_equal(np.ma.median(array([1,2,3],mask=[0,0,0])).shape,
@@ -795,13 +821,36 @@ class TestMedian(TestCase):
x[:3] = x[-3:] = masked
assert_equal(median(x, axis=-1), median(x, axis=1))
+ def test_out_1d(self):
+ # integer float even odd
+ for v in (30, 30., 31, 31.):
+ x = masked_array(np.arange(v))
+ x[:3] = x[-3:] = masked
+ out = masked_array(np.ones(()))
+ r = median(x, out=out)
+ if v == 30:
+ assert_equal(out, 14.5)
+ else:
+ assert_equal(out, 15.)
+ assert_(r is out)
+ assert_(type(r) is MaskedArray)
+
def test_out(self):
- x = masked_array(np.arange(30).reshape(10, 3))
- x[:3] = x[-3:] = masked
- out = masked_array(np.ones(10))
- r = median(x, axis=1, out=out)
- assert_equal(r, out)
- assert_(type(r) == MaskedArray)
+ # integer float even odd
+ for v in (40, 40., 30, 30.):
+ x = masked_array(np.arange(v).reshape(10, -1))
+ x[:3] = x[-3:] = masked
+ out = masked_array(np.ones(10))
+ r = median(x, axis=1, out=out)
+ if v == 30:
+ e = masked_array([0.]*3 + [10, 13, 16, 19] + [0.]*3,
+ mask=[True] * 3 + [False] * 4 + [True] * 3)
+ else:
+ e = masked_array([0.]*3 + [13.5, 17.5, 21.5, 25.5] + [0.]*3,
+ mask=[True]*3 + [False]*4 + [True]*3)
+ assert_equal(r, e)
+ assert_(r is out)
+ assert_(type(r) is MaskedArray)
def test_single_non_masked_value_on_axis(self):
data = [[1., 0.],