Exemplo n.º 1
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)
Exemplo n.º 2
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)
     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))
Exemplo n.º 3
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))
Exemplo n.º 4
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]])
Exemplo n.º 5
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))
Exemplo n.º 6
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)
Exemplo n.º 7
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])
Exemplo n.º 8
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)
Exemplo n.º 9
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]]))
Exemplo n.º 10
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])
Exemplo n.º 11
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)
Exemplo n.º 12
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)
Exemplo n.º 13
0
 def test_maskedarray_subclassing(self):
     # Tests subclassing MaskedArray
     (x, mx) = self.data
     assert_(isinstance(mx._data, np.matrix))
Exemplo n.º 14
0
 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))
Exemplo n.º 15
0
 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))
Exemplo n.º 16
0
 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)
Exemplo n.º 17
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)