Example #1
0
    def test_pearsonr(self):
        # Tests some computations of Pearson's r
        x = ma.arange(10)
        with warnings.catch_warnings():
            # The tests in this context are edge cases, with perfect
            # correlation or anticorrelation, or totally masked data.
            # None of these should trigger a RuntimeWarning.
            warnings.simplefilter("error", RuntimeWarning)

            assert_almost_equal(mstats.pearsonr(x, x)[0], 1.0)
            assert_almost_equal(mstats.pearsonr(x, x[::-1])[0], -1.0)

            x = ma.array(x, mask=True)
            pr = mstats.pearsonr(x, x)
            assert_(pr[0] is masked)
            assert_(pr[1] is masked)

        x1 = ma.array([-1.0, 0.0, 1.0])
        y1 = ma.array([0, 0, 3])
        r, p = mstats.pearsonr(x1, y1)
        assert_almost_equal(r, np.sqrt(3)/2)
        assert_almost_equal(p, 1.0/3)

        # (x2, y2) have the same unmasked data as (x1, y1).
        mask = [False, False, False, True]
        x2 = ma.array([-1.0, 0.0, 1.0, 99.0], mask=mask)
        y2 = ma.array([0, 0, 3, -1], mask=mask)
        r, p = mstats.pearsonr(x2, y2)
        assert_almost_equal(r, np.sqrt(3)/2)
        assert_almost_equal(p, 1.0/3)
Example #2
0
 def test_setxor1d(self):
     # Test setxor1d
     a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
     b = array([1, 2, 3, 4, 5, -1], mask=[0, 0, 0, 0, 0, 1])
     test = setxor1d(a, b)
     assert_equal(test, array([3, 4, 7]))
     #
     a = array([1, 2, 5, 7, -1], mask=[0, 0, 0, 0, 1])
     b = [1, 2, 3, 4, 5]
     test = setxor1d(a, b)
     assert_equal(test, array([3, 4, 7, -1], mask=[0, 0, 0, 1]))
     #
     a = array([1, 2, 3])
     b = array([6, 5, 4])
     test = setxor1d(a, b)
     assert_(isinstance(test, MaskedArray))
     assert_equal(test, [1, 2, 3, 4, 5, 6])
     #
     a = array([1, 8, 2, 3], mask=[0, 1, 0, 0])
     b = array([6, 5, 4, 8], mask=[0, 0, 0, 1])
     test = setxor1d(a, b)
     assert_(isinstance(test, MaskedArray))
     assert_equal(test, [1, 2, 3, 4, 5, 6])
     #
     assert_array_equal([], setxor1d([], []))
 def test_pickling_subbaseclass(self):
     # Test pickling w/ a subclass of ndarray
     a = masked_array(np.matrix(list(range(10))), mask=[1, 0, 1, 0, 0] * 2)
     a_pickled = pickle.loads(a.dumps())
     assert_equal(a_pickled._mask, a._mask)
     assert_equal(a_pickled, a)
     assert_(isinstance(a_pickled._data, np.matrix))
Example #4
0
 def test_masked_binary_operations2(self):
     # Tests domained_masked_binary_operation
     (x, mx) = self.data
     xmx = masked_array(mx.data.__array__(), mask=mx.mask)
     assert_(isinstance(divide(mx, mx), MMatrix))
     assert_(isinstance(divide(mx, x), MMatrix))
     assert_equal(divide(mx, mx), divide(xmx, xmx))
Example #5
0
 def test_view_simple_dtype(self):
     (mrec, a, b, arr) = self.data
     ntype = (float, 2)
     test = mrec.view(ntype)
     assert_(isinstance(test, ma.MaskedArray))
     assert_equal(test, np.array(list(zip(a, b)), dtype=float))
     assert_(test[3, 1] is ma.masked)
    def test_drop_fields(self):
        # Test drop_fields
        a = np.array([(1, (2, 3.0)), (4, (5, 6.0))],
                     dtype=[('a', int), ('b', [('ba', float), ('bb', int)])])

        # A basic field
        test = drop_fields(a, 'a')
        control = np.array([((2, 3.0),), ((5, 6.0),)],
                           dtype=[('b', [('ba', float), ('bb', int)])])
        assert_equal(test, control)

        # Another basic field (but nesting two fields)
        test = drop_fields(a, 'b')
        control = np.array([(1,), (4,)], dtype=[('a', int)])
        assert_equal(test, control)

        # A nested sub-field
        test = drop_fields(a, ['ba', ])
        control = np.array([(1, (3.0,)), (4, (6.0,))],
                           dtype=[('a', int), ('b', [('bb', int)])])
        assert_equal(test, control)

        # All the nested sub-field from a field: zap that field
        test = drop_fields(a, ['ba', 'bb'])
        control = np.array([(1,), (4,)], dtype=[('a', int)])
        assert_equal(test, control)

        test = drop_fields(a, ['a', 'b'])
        assert_(test is None)
Example #7
0
 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)
Example #8
0
 def test_1D_float96(self):
     a = ma.array((1,2,3,4), mask=(0,0,0,1))
     actual_dt = mstats.hmean(a, dtype=np.float96)
     desired_dt = np.asarray(3. / (1./1 + 1./2 + 1./3),
                             dtype=np.float96)
     assert_almost_equal(actual_dt, desired_dt, decimal=14)
     assert_(actual_dt.dtype == desired_dt.dtype)
Example #9
0
 def test_compressed(self):
     a = masked_array(np.matrix([1, 2, 3, 4]), mask=[0, 0, 0, 0])
     b = a.compressed()
     assert_equal(b, a)
     assert_(isinstance(b, np.matrix))
     a[0, 0] = masked
     b = a.compressed()
     assert_equal(b, [[2, 3, 4]])
Example #10
0
 def test_pickling_subbaseclass(self):
     # Test pickling w/ a subclass of ndarray
     a = masked_array(np.matrix(list(range(10))), mask=[1, 0, 1, 0, 0] * 2)
     for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
         a_pickled = pickle.loads(pickle.dumps(a, protocol=proto))
         assert_equal(a_pickled._mask, a._mask)
         assert_equal(a_pickled, a)
         assert_(isinstance(a_pickled._data, np.matrix))
Example #11
0
 def test_view(self):
     # Test view w/ flexible dtype
     iterator = list(zip(np.arange(10), np.random.rand(10)))
     data = np.array(iterator)
     a = masked_array(iterator, dtype=[('a', float), ('b', float)])
     a.mask[0] = (1, 0)
     test = a.view((float, 2), np.matrix)
     assert_equal(test, data)
     assert_(isinstance(test, np.matrix))
     assert_(not isinstance(test, MaskedArray))
 def test_pearsonr(self):
     "Tests some computations of Pearson's r"
     x = ma.arange(10)
     assert_almost_equal(mstats.pearsonr(x,x)[0], 1.0)
     assert_almost_equal(mstats.pearsonr(x,x[::-1])[0], -1.0)
     #
     x = ma.array(x, mask=True)
     pr = mstats.pearsonr(x,x)
     assert_(pr[0] is masked)
     assert_(pr[1] is masked)
Example #13
0
 def test_byview(self):
     # Test creation by view
     base = self.base
     mbase = base.view(mrecarray)
     assert_equal(mbase.recordmask, base.recordmask)
     assert_equal_records(mbase._mask, base._mask)
     assert_(isinstance(mbase._data, recarray))
     assert_equal_records(mbase._data, base._data.view(recarray))
     for field in ('a', 'b', 'c'):
         assert_equal(base[field], mbase[field])
     assert_equal_records(mbase.view(mrecarray), mbase)
 def test_wmasked_arrays(self):
     # Test merge_arrays masked arrays
     (_, x, _, _) = self.data
     mx = ma.array([1, 2, 3], mask=[1, 0, 0])
     test = merge_arrays((x, mx), usemask=True)
     control = ma.array([(1, 1), (2, 2), (-1, 3)],
                        mask=[(0, 1), (0, 0), (1, 0)],
                        dtype=[('f0', int), ('f1', int)])
     assert_equal(test, control)
     test = merge_arrays((x, mx), usemask=True, asrecarray=True)
     assert_equal(test, control)
     assert_(isinstance(test, MaskedRecords))
Example #15
0
    def test_pearsonr(self):
        "Tests some computations of Pearson's r"
        x = ma.arange(10)
        olderr = np.seterr(all='ignore')
        try:
            assert_almost_equal(mstats.pearsonr(x,x)[0], 1.0)
            assert_almost_equal(mstats.pearsonr(x,x[::-1])[0], -1.0)

            x = ma.array(x, mask=True)
            pr = mstats.pearsonr(x,x)
        finally:
            np.seterr(**olderr)
        assert_(pr[0] is masked)
        assert_(pr[1] is masked)
def assert_mask_equivalent(m1, m2, err_msg=''):
    '''
    Asserts the equality of two masks.

    This is an amended version of ``np.ma.testutils.assert_mask_equal`` to
    allow for one test array to have no mask and the other to have a mask array
    where each element is ``False``. We want to handle this situation as the
    two arrays are functionally equivalent.
    '''
    if m1 is np.ma.nomask or not np.any(m1):
        assert_(m2 is np.ma.nomask or not np.any(m2), msg=err_msg)
    if m2 is np.ma.nomask or not np.any(m2):
        assert_(m1 is np.ma.nomask or not np.any(m1), msg=err_msg)
    assert_array_equal(m1, m2, err_msg=err_msg)
Example #17
0
    def test_allany_onmatrices(self):
        x = np.array([[0.13, 0.26, 0.90],
                      [0.28, 0.33, 0.63],
                      [0.31, 0.87, 0.70]])
        X = np.matrix(x)
        m = np.array([[True, False, False],
                      [False, False, False],
                      [True, True, False]], dtype=np.bool_)
        mX = masked_array(X, mask=m)
        mXbig = (mX > 0.5)
        mXsmall = (mX < 0.5)

        assert_(not mXbig.all())
        assert_(mXbig.any())
        assert_equal(mXbig.all(0), np.matrix([False, False, True]))
        assert_equal(mXbig.all(1), np.matrix([False, False, True]).T)
        assert_equal(mXbig.any(0), np.matrix([False, False, True]))
        assert_equal(mXbig.any(1), np.matrix([True, True, True]).T)

        assert_(not mXsmall.all())
        assert_(mXsmall.any())
        assert_equal(mXsmall.all(0), np.matrix([True, True, False]))
        assert_equal(mXsmall.all(1), np.matrix([False, False, False]).T)
        assert_equal(mXsmall.any(0), np.matrix([True, True, False]))
        assert_equal(mXsmall.any(1), np.matrix([True, True, False]).T)
Example #18
0
 def test_dot_returns_maskedarray(self):
     # See gh-6611
     a = np.eye(3)
     b = array(a)
     assert_(type(dot(a, a)) is MaskedArray)
     assert_(type(dot(a, b)) is MaskedArray)
     assert_(type(dot(b, a)) is MaskedArray)
     assert_(type(dot(b, b)) is MaskedArray)
Example #19
0
    def test_1D(self):
        a = (1,2,3,4)
        actual = mstats.gmean(a)
        desired = np.power(1*2*3*4,1./4.)
        assert_almost_equal(actual, desired, decimal=14)

        desired1 = mstats.gmean(a,axis=-1)
        assert_almost_equal(actual, desired1, decimal=14)
        assert_(not isinstance(desired1, ma.MaskedArray))

        a = ma.array((1,2,3,4),mask=(0,0,0,1))
        actual = mstats.gmean(a)
        desired = np.power(1*2*3,1./3.)
        assert_almost_equal(actual, desired,decimal=14)

        desired1 = mstats.gmean(a,axis=-1)
        assert_almost_equal(actual, desired1, decimal=14)
Example #20
0
    def test_fromtextfile(self):
        # Tests reading from a text file.
        fcontent = (
"""#
'One (S)','Two (I)','Three (F)','Four (M)','Five (-)','Six (C)'
'strings',1,1.0,'mixed column',,1
'with embedded "double quotes"',2,2.0,1.0,,1
'strings',3,3.0E5,3,,1
'strings',4,-1e-10,,,1
""")
        with temppath() as path:
            with open(path, 'w') as f:
                f.write(fcontent)
            mrectxt = fromtextfile(path, delimitor=',', varnames='ABCDEFG')
        assert_(isinstance(mrectxt, MaskedRecords))
        assert_equal(mrectxt.F, [1, 1, 1, 1])
        assert_equal(mrectxt.E._mask, [1, 1, 1, 1])
        assert_equal(mrectxt.C, [1, 2, 3.e+5, -1e-10])
def test_chisquare_masked_arrays():
    # The other tests were taken from the tests for stats.chisquare, so
    # they don't test the function with masked arrays.  Here masked arrays
    # are tested.
    obs = np.array([[8, 8, 16, 32, -1], [-1, -1, 3, 4, 5]]).T
    mask = np.array([[0, 0, 0, 0, 1], [1, 1, 0, 0, 0]]).T
    mobs = ma.masked_array(obs, mask)
    expected_chisq = np.array([24.0, 0.5])

    chisq, p = mstats.chisquare(mobs)
    assert_array_equal(chisq, expected_chisq)
    assert_array_almost_equal(p, stats.chisqprob(expected_chisq, mobs.count(axis=0) - 1))

    chisq, p = mstats.chisquare(mobs.T, axis=1)
    assert_array_equal(chisq, expected_chisq)
    assert_array_almost_equal(p, stats.chisqprob(expected_chisq, mobs.T.count(axis=1) - 1))

    # When axis=None, the two values should have type np.float64.
    chisq, p = mstats.chisquare([1,2,3], axis=None)
    assert_(isinstance(chisq, np.float64))
    assert_(isinstance(p, np.float64))
    assert_equal(chisq, 1.0)
    assert_almost_equal(p, stats.chisqprob(1.0, 2))
Example #22
0
 def test_view_flexible_type(self):
     (mrec, a, b, arr) = self.data
     alttype = [('A', float), ('B', float)]
     test = mrec.view(alttype)
     assert_(isinstance(test, MaskedRecords))
     assert_equal_records(test, arr.view(alttype))
     assert_(test['B'][3] is masked)
     assert_equal(test.dtype, np.dtype(alttype))
     assert_(test._fill_value is None)
Example #23
0
 def test_view_flexible_type(self):
     (mrec, a, b, arr) = self.data
     alttype = [('A', np.float), ('B', np.float)]
     test = mrec.view(alttype)
     assert_(isinstance(test, MaskedRecords))
     assert_equal_records(test, arr.view(alttype))
     assert_(test['B'][3] is masked)
     assert_equal(test.dtype, np.dtype(alttype))
     assert_(test._fill_value is None)
 def test_flat(self):
     # Test that flat can return items even for matrices [#4585, #4615]
     # test simple access
     test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
     assert_equal(test.flat[1], 2)
     assert_equal(test.flat[2], masked)
     assert_(np.all(test.flat[0:2] == test[0, 0:2]))
     # Test flat on masked_matrices
     test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
     test.flat = masked_array([3, 2, 1], mask=[1, 0, 0])
     control = masked_array(np.matrix([[3, 2, 1]]), mask=[1, 0, 0])
     assert_equal(test, control)
     # Test setting
     test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
     testflat = test.flat
     testflat[:] = testflat[[2, 1, 0]]
     assert_equal(test, control)
     testflat[0] = 9
     # test that matrices keep the correct shape (#4615)
     a = masked_array(np.matrix(np.eye(2)), mask=0)
     b = a.flat
     b01 = b[:2]
     assert_equal(b01.data, np.array([[1., 0.]]))
     assert_equal(b01.mask, np.array([[False, False]]))
Example #25
0
 def test_flat(self):
     # Test that flat can return items even for matrices [#4585, #4615]
     # test simple access
     test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
     assert_equal(test.flat[1], 2)
     assert_equal(test.flat[2], masked)
     assert_(np.all(test.flat[0:2] == test[0, 0:2]))
     # Test flat on masked_matrices
     test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
     test.flat = masked_array([3, 2, 1], mask=[1, 0, 0])
     control = masked_array(np.matrix([[3, 2, 1]]), mask=[1, 0, 0])
     assert_equal(test, control)
     # Test setting
     test = masked_array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
     testflat = test.flat
     testflat[:] = testflat[[2, 1, 0]]
     assert_equal(test, control)
     testflat[0] = 9
     # test that matrices keep the correct shape (#4615)
     a = masked_array(np.matrix(np.eye(2)), mask=0)
     b = a.flat
     b01 = b[:2]
     assert_equal(b01.data, np.array([[1., 0.]]))
     assert_equal(b01.mask, np.array([[False, False]]))
Example #26
0
 def test_masked_1d(self):
     x = array(np.arange(5), mask=True)
     assert_equal(np.ma.median(x), np.ma.masked)
     assert_equal(np.ma.median(x).shape, (), "shape mismatch")
     assert_(type(np.ma.median(x)) is np.ma.core.MaskedConstant)
     x = array(np.arange(5), mask=False)
     assert_equal(np.ma.median(x), 2.)
     assert_equal(np.ma.median(x).shape, (), "shape mismatch")
     assert_(type(np.ma.median(x)) is not MaskedArray)
     x = array(np.arange(5), mask=[0,1,0,0,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)
     x = array(np.arange(5), mask=[0,1,1,1,1])
     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)
Example #27
0
 def test_masked_1d(self):
     x = array(np.arange(5), mask=True)
     assert_equal(np.ma.median(x), np.ma.masked)
     assert_equal(np.ma.median(x).shape, (), "shape mismatch")
     assert_(type(np.ma.median(x)) is np.ma.core.MaskedConstant)
     x = array(np.arange(5), mask=False)
     assert_equal(np.ma.median(x), 2.)
     assert_equal(np.ma.median(x).shape, (), "shape mismatch")
     assert_(type(np.ma.median(x)) is not MaskedArray)
     x = array(np.arange(5), mask=[0,1,0,0,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)
     x = array(np.arange(5), mask=[0,1,1,1,1])
     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)
Example #28
0
 def test_non_masked(self):
     x = np.arange(9)
     assert_equal(np.ma.median(x), 4.)
     assert_(type(np.ma.median(x)) is not MaskedArray)
     x = range(9)
     assert_equal(np.ma.median(x), 4.)
     assert_(type(np.ma.median(x)) is not MaskedArray)
     x = 5
     assert_equal(np.ma.median(x), 5.)
     assert_(type(np.ma.median(x)) is not MaskedArray)
Example #29
0
 def test_non_masked(self):
     x = np.arange(9)
     assert_equal(np.ma.median(x), 4.)
     assert_(type(np.ma.median(x)) is not MaskedArray)
     x = range(9)
     assert_equal(np.ma.median(x), 4.)
     assert_(type(np.ma.median(x)) is not MaskedArray)
     x = 5
     assert_equal(np.ma.median(x), 5.)
     assert_(type(np.ma.median(x)) is not MaskedArray)
Example #30
0
 def test_2d_waxis(self):
     # Tests median w/ 2D arrays and different axis.
     x = masked_array(np.arange(30).reshape(10, 3))
     x[:3] = x[-3:] = masked
     assert_equal(median(x), 14.5)
     assert_(type(np.ma.median(x)) is not MaskedArray)
     assert_equal(median(x, axis=0), [13.5, 14.5, 15.5])
     assert_(type(np.ma.median(x, axis=0)) is MaskedArray)
     assert_equal(median(x, axis=1), [0, 0, 0, 10, 13, 16, 19, 0, 0, 0])
     assert_(type(np.ma.median(x, axis=1)) is MaskedArray)
     assert_equal(median(x, axis=1).mask, [1, 1, 1, 0, 0, 0, 0, 1, 1, 1])
Example #31
0
 def test_2d_waxis(self):
     # Tests median w/ 2D arrays and different axis.
     x = masked_array(np.arange(30).reshape(10, 3))
     x[:3] = x[-3:] = masked
     assert_equal(median(x), 14.5)
     assert_(type(np.ma.median(x)) is not MaskedArray)
     assert_equal(median(x, axis=0), [13.5, 14.5, 15.5])
     assert_(type(np.ma.median(x, axis=0)) is MaskedArray)
     assert_equal(median(x, axis=1), [0, 0, 0, 10, 13, 16, 19, 0, 0, 0])
     assert_(type(np.ma.median(x, axis=1)) is MaskedArray)
     assert_equal(median(x, axis=1).mask, [1, 1, 1, 0, 0, 0, 0, 1, 1, 1])
Example #32
0
 def test_get(self):
     # Tests fields retrieval
     base = self.base.copy()
     mbase = base.view(mrecarray)
     # As fields..........
     for field in ("a", "b", "c"):
         assert_equal(getattr(mbase, field), mbase[field])
         assert_equal(base[field], mbase[field])
     # as elements .......
     mbase_first = mbase[0]
     assert_(isinstance(mbase_first, mrecarray))
     assert_equal(mbase_first.dtype, mbase.dtype)
     assert_equal(mbase_first.tolist(), (1, 1.1, b"one"))
     # Used to be mask, now it's recordmask
     assert_equal(mbase_first.recordmask, nomask)
     assert_equal(mbase_first._mask.item(), (False, False, False))
     assert_equal(mbase_first["a"], mbase["a"][0])
     mbase_last = mbase[-1]
     assert_(isinstance(mbase_last, mrecarray))
     assert_equal(mbase_last.dtype, mbase.dtype)
     assert_equal(mbase_last.tolist(), (None, None, None))
     # Used to be mask, now it's recordmask
     assert_equal(mbase_last.recordmask, True)
     assert_equal(mbase_last._mask.item(), (True, True, True))
     assert_equal(mbase_last["a"], mbase["a"][-1])
     assert_((mbase_last["a"] is masked))
     # as slice ..........
     mbase_sl = mbase[:2]
     assert_(isinstance(mbase_sl, mrecarray))
     assert_equal(mbase_sl.dtype, mbase.dtype)
     # Used to be mask, now it's recordmask
     assert_equal(mbase_sl.recordmask, [0, 1])
     assert_equal_records(
         mbase_sl.mask,
         np.array(
             [(False, False, False), (True, True, True)], dtype=mbase._mask.dtype
         ),
     )
     assert_equal_records(mbase_sl, base[:2].view(mrecarray))
     for field in ("a", "b", "c"):
         assert_equal(getattr(mbase_sl, field), base[:2][field])
Example #33
0
 def test_docstring_examples(self):
     "test the examples given in the docstring of ma.median"
     x = array(np.arange(8), mask=[0]*4 + [1]*4)
     assert_equal(np.ma.median(x), 1.5)
     assert_equal(np.ma.median(x).shape, (), "shape mismatch")
     assert_(type(np.ma.median(x)) is not MaskedArray)
     x = array(np.arange(10).reshape(2, 5), mask=[0]*6 + [1]*4)
     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)
     ma_x = np.ma.median(x, axis=-1, overwrite_input=True)
     assert_equal(ma_x, [2., 5.])
     assert_equal(ma_x.shape, (2,), "shape mismatch")
     assert_(type(ma_x) is MaskedArray)
Example #34
0
 def test_docstring_examples(self):
     "test the examples given in the docstring of ma.median"
     x = array(np.arange(8), mask=[0]*4 + [1]*4)
     assert_equal(np.ma.median(x), 1.5)
     assert_equal(np.ma.median(x).shape, (), "shape mismatch")
     assert_(type(np.ma.median(x)) is not MaskedArray)
     x = array(np.arange(10).reshape(2, 5), mask=[0]*6 + [1]*4)
     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)
     ma_x = np.ma.median(x, axis=-1, overwrite_input=True)
     assert_equal(ma_x, [2., 5.])
     assert_equal(ma_x.shape, (2,), "shape mismatch")
     assert_(type(ma_x) is MaskedArray)
Example #35
0
 def test_get(self):
     # Tests fields retrieval
     base = self.base.copy()
     mbase = base.view(mrecarray)
     # As fields..........
     for field in ('a', 'b', 'c'):
         assert_equal(getattr(mbase, field), mbase[field])
         assert_equal(base[field], mbase[field])
     # as elements .......
     mbase_first = mbase[0]
     assert_(isinstance(mbase_first, mrecarray))
     assert_equal(mbase_first.dtype, mbase.dtype)
     assert_equal(mbase_first.tolist(), (1, 1.1, asbytes('one')))
     # Used to be mask, now it's recordmask
     assert_equal(mbase_first.recordmask, nomask)
     assert_equal(mbase_first._mask.item(), (False, False, False))
     assert_equal(mbase_first['a'], mbase['a'][0])
     mbase_last = mbase[-1]
     assert_(isinstance(mbase_last, mrecarray))
     assert_equal(mbase_last.dtype, mbase.dtype)
     assert_equal(mbase_last.tolist(), (None, None, None))
     # Used to be mask, now it's recordmask
     assert_equal(mbase_last.recordmask, True)
     assert_equal(mbase_last._mask.item(), (True, True, True))
     assert_equal(mbase_last['a'], mbase['a'][-1])
     assert_((mbase_last['a'] is masked))
     # as slice ..........
     mbase_sl = mbase[:2]
     assert_(isinstance(mbase_sl, mrecarray))
     assert_equal(mbase_sl.dtype, mbase.dtype)
     # Used to be mask, now it's recordmask
     assert_equal(mbase_sl.recordmask, [0, 1])
     assert_equal_records(mbase_sl.mask,
                          np.array([(False, False, False),
                                    (True, True, True)],
                                   dtype=mbase._mask.dtype))
     assert_equal_records(mbase_sl, base[:2].view(mrecarray))
     for field in ('a', 'b', 'c'):
         assert_equal(getattr(mbase_sl, field), base[:2][field])
Example #36
0
 def test_hardmask(self):
     # Test hardmask
     base = self.base.copy()
     mbase = base.view(mrecarray)
     mbase.harden_mask()
     assert_(mbase._hardmask)
     mbase.mask = nomask
     assert_equal_records(mbase._mask, base._mask)
     mbase.soften_mask()
     assert_(not mbase._hardmask)
     mbase.mask = nomask
     # So, the mask of a field is no longer set to nomask...
     assert_equal_records(mbase._mask, ma.make_mask_none(base.shape, base.dtype))
     assert_(ma.make_mask(mbase["b"]._mask) is nomask)
     assert_equal(mbase["a"]._mask, mbase["b"]._mask)
Example #37
0
 def test_hardmask(self):
     # Test hardmask
     base = self.base.copy()
     mbase = base.view(mrecarray)
     mbase.harden_mask()
     assert_(mbase._hardmask)
     mbase.mask = nomask
     assert_equal_records(mbase._mask, base._mask)
     mbase.soften_mask()
     assert_(not mbase._hardmask)
     mbase.mask = nomask
     # So, the mask of a field is no longer set to nomask...
     assert_equal_records(mbase._mask,
                          ma.make_mask_none(base.shape, base.dtype))
     assert_(ma.make_mask(mbase['b']._mask) is nomask)
     assert_equal(mbase['a']._mask, mbase['b']._mask)
Example #38
0
    def test_allany_onmatrices(self):
        x = np.array([[0.13, 0.26, 0.90], [0.28, 0.33, 0.63],
                      [0.31, 0.87, 0.70]])
        X = npm.matrix(x)
        m = np.array(
            [[True, False, False], [False, False, False], [True, True, False]],
            dtype=np.bool_)
        mX = masked_array(X, mask=m)
        mXbig = (mX > 0.5)
        mXsmall = (mX < 0.5)

        assert_(not mXbig.all())
        assert_(mXbig.any())
        assert_equal(mXbig.all(0), npm.matrix([False, False, True]))
        assert_equal(mXbig.all(1), npm.matrix([False, False, True]).T)
        assert_equal(mXbig.any(0), npm.matrix([False, False, True]))
        assert_equal(mXbig.any(1), npm.matrix([True, True, True]).T)

        assert_(not mXsmall.all())
        assert_(mXsmall.any())
        assert_equal(mXsmall.all(0), npm.matrix([True, True, False]))
        assert_equal(mXsmall.all(1), npm.matrix([False, False, False]).T)
        assert_equal(mXsmall.any(0), npm.matrix([True, True, False]))
        assert_equal(mXsmall.any(1), npm.matrix([True, True, False]).T)
Example #39
0
 def test_empty(self):
     res1 = mstats.ttest_1samp([], 1)
     assert_(np.all(np.isnan(res1)))
Example #40
0
 def test_1D_float96(self):
     a = ma.array((1, 2, 3, 4), mask=(0, 0, 0, 1))
     actual_dt = mstats.gmean(a, dtype=np.float96)
     desired_dt = np.power(1 * 2 * 3, 1. / 3.).astype(np.float96)
     assert_almost_equal(actual_dt, desired_dt, decimal=14)
     assert_(actual_dt.dtype == desired_dt.dtype)
Example #41
0
 def test_empty(self):
     res1 = mstats.ttest_ind([], [])
     assert_(np.all(np.isnan(res1)))
Example #42
0
 def test_empty(self):
     res1 = mstats.ttest_1samp([], 1)
     assert_(np.all(np.isnan(res1)))
 def test_matrix_indexing(self):
     # Tests conversions and indexing
     x1 = np.matrix([[1, 2, 3], [4, 3, 2]])
     x2 = masked_array(x1, mask=[[1, 0, 0], [0, 1, 0]])
     x3 = masked_array(x1, mask=[[0, 1, 0], [1, 0, 0]])
     x4 = masked_array(x1)
     # test conversion to strings
     str(x2)  # raises?
     repr(x2)  # raises?
     # tests of indexing
     assert_(type(x2[1, 0]) is type(x1[1, 0]))
     assert_(x1[1, 0] == x2[1, 0])
     assert_(x2[1, 1] is masked)
     assert_equal(x1[0, 2], x2[0, 2])
     assert_equal(x1[0, 1:], x2[0, 1:])
     assert_equal(x1[:, 2], x2[:, 2])
     assert_equal(x1[:], x2[:])
     assert_equal(x1[1:], x3[1:])
     x1[0, 2] = 9
     x2[0, 2] = 9
     assert_equal(x1, x2)
     x1[0, 1:] = 99
     x2[0, 1:] = 99
     assert_equal(x1, x2)
     x2[0, 1] = masked
     assert_equal(x1, x2)
     x2[0, 1:] = masked
     assert_equal(x1, x2)
     x2[0, :] = x1[0, :]
     x2[0, 1] = masked
     assert_(allequal(getmask(x2), np.array([[0, 1, 0], [0, 1, 0]])))
     x3[1, :] = masked_array([1, 2, 3], [1, 1, 0])
     assert_(allequal(getmask(x3)[1], masked_array([1, 1, 0])))
     assert_(allequal(getmask(x3[1]), masked_array([1, 1, 0])))
     x4[1, :] = masked_array([1, 2, 3], [1, 1, 0])
     assert_(allequal(getmask(x4[1]), masked_array([1, 1, 0])))
     assert_(allequal(x4[1], masked_array([1, 2, 3])))
     x1 = np.matrix(np.arange(5) * 1.0)
     x2 = masked_values(x1, 3.0)
     assert_equal(x1, x2)
     assert_(allequal(masked_array([0, 0, 0, 1, 0], dtype=MaskType),
                      x2.mask))
     assert_equal(3.0, x2.fill_value)
 def test_masked_binary_operations(self):
     # Tests masked_binary_operation
     (x, mx) = self.data
     # Result should be a MMatrix
     assert_(isinstance(add(mx, mx), MMatrix))
     assert_(isinstance(add(mx, x), MMatrix))
     # Result should work
     assert_equal(add(mx, x), mx+x)
     assert_(isinstance(add(mx, mx)._data, np.matrix))
     assert_(isinstance(add.outer(mx, mx), MMatrix))
     assert_(isinstance(hypot(mx, mx), MMatrix))
     assert_(isinstance(hypot(mx, x), MMatrix))
 def test_masked_unary_operations(self):
     # Tests masked_unary_operation
     (x, mx) = self.data
     with np.errstate(divide='ignore'):
         assert_(isinstance(log(mx), MMatrix))
         assert_equal(log(x), np.log(x))
 def test_maskedarray_subclassing(self):
     # Tests subclassing MaskedArray
     (x, mx) = self.data
     assert_(isinstance(mx._data, np.matrix))
 def test_dot_out(self):
     a = array(np.eye(3))
     out = array(np.zeros((3, 3)))
     res = dot(a, a, out=out)
     assert_(res is out)
     assert_equal(a, res)
Example #48
0
 def test_view_by_itself(self):
     (mrec, a, b, arr) = self.data
     test = mrec.view()
     assert_(isinstance(test, MaskedRecords))
     assert_equal_records(test, mrec)
     assert_equal_records(test._mask, mrec._mask)