Exemplo n.º 1
0
    def test_testPut2(self):
        # Test of put
        d = arange(5)
        x = array(d, mask=[0, 0, 0, 0, 0])
        z = array([10, 40], mask=[1, 0])
        assert_(x[2] is not masked)
        assert_(x[3] is not masked)
        x[2:4] = z
        assert_(x[2] is masked)
        assert_(x[3] is not masked)
        assert_(eq(x, [0, 1, 10, 40, 4]))

        d = arange(5)
        x = array(d, mask=[0, 0, 0, 0, 0])
        y = x[2:4]
        z = array([10, 40], mask=[1, 0])
        assert_(x[2] is not masked)
        assert_(x[3] is not masked)
        y[:] = z
        assert_(y[0] is masked)
        assert_(y[1] is not masked)
        assert_(eq(y, [10, 40]))
        assert_(x[2] is masked)
        assert_(x[3] is not masked)
        assert_(eq(x, [0, 1, 10, 40, 4]))
Exemplo n.º 2
0
    def test_matching_named_fields(self):
        # Test combination of arrays w/ matching field names
        (_, x, _, z) = self.data
        zz = np.array([('a', 10., 100.), ('b', 20., 200.), ('c', 30., 300.)],
                      dtype=[('A', '|S3'), ('B', float), ('C', float)])
        test = stack_arrays((z, zz))
        control = ma.array([('A', 1, -1), ('B', 2, -1), ('a', 10., 100.),
                            ('b', 20., 200.), ('c', 30., 300.)],
                           dtype=[('A', '|S3'), ('B', float), ('C', float)],
                           mask=[(0, 0, 1), (0, 0, 1), (0, 0, 0), (0, 0, 0),
                                 (0, 0, 0)])
        assert_equal(test, control)
        assert_equal(test.mask, control.mask)

        test = stack_arrays((z, zz, x))
        ndtype = [('A', '|S3'), ('B', float), ('C', float), ('f3', int)]
        control = ma.array([('A', 1, -1, -1), ('B', 2, -1, -1),
                            ('a', 10., 100., -1), ('b', 20., 200., -1),
                            ('c', 30., 300., -1), (-1, -1, -1, 1),
                            (-1, -1, -1, 2)],
                           dtype=ndtype,
                           mask=[(0, 0, 1, 1), (0, 0, 1, 1), (0, 0, 0, 1),
                                 (0, 0, 0, 1), (0, 0, 0, 1), (1, 1, 1, 0),
                                 (1, 1, 1, 0)])
        assert_equal(test, control)
        assert_equal(test.mask, control.mask)
Exemplo n.º 3
0
    def test_testAverage2(self):
        # More tests of average.
        w1 = [0, 1, 1, 1, 1, 0]
        w2 = [[0, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 1]]
        x = arange(6)
        assert_(allclose(average(x, axis=0), 2.5))
        assert_(allclose(average(x, axis=0, weights=w1), 2.5))
        y = array([arange(6), 2.0 * arange(6)])
        assert_(allclose(average(y, None),
                                 np.add.reduce(np.arange(6)) * 3. / 12.))
        assert_(allclose(average(y, axis=0), np.arange(6) * 3. / 2.))
        assert_(allclose(average(y, axis=1),
                                 [average(x, axis=0), average(x, axis=0)*2.0]))
        assert_(allclose(average(y, None, weights=w2), 20. / 6.))
        assert_(allclose(average(y, axis=0, weights=w2),
                                 [0., 1., 2., 3., 4., 10.]))
        assert_(allclose(average(y, axis=1),
                                 [average(x, axis=0), average(x, axis=0)*2.0]))
        m1 = zeros(6)
        m2 = [0, 0, 1, 1, 0, 0]
        m3 = [[0, 0, 1, 1, 0, 0], [0, 1, 1, 1, 1, 0]]
        m4 = ones(6)
        m5 = [0, 1, 1, 1, 1, 1]
        assert_(allclose(average(masked_array(x, m1), axis=0), 2.5))
        assert_(allclose(average(masked_array(x, m2), axis=0), 2.5))
        assert_(average(masked_array(x, m4), axis=0) is masked)
        assert_equal(average(masked_array(x, m5), axis=0), 0.0)
        assert_equal(count(average(masked_array(x, m4), axis=0)), 0)
        z = masked_array(y, m3)
        assert_(allclose(average(z, None), 20. / 6.))
        assert_(allclose(average(z, axis=0),
                                 [0., 1., 99., 99., 4.0, 7.5]))
        assert_(allclose(average(z, axis=1), [2.5, 5.0]))
        assert_(allclose(average(z, axis=0, weights=w2),
                                 [0., 1., 99., 99., 4.0, 10.0]))

        a = arange(6)
        b = arange(6) * 3
        r1, w1 = average([[a, b], [b, a]], axis=1, returned=1)
        assert_equal(shape(r1), shape(w1))
        assert_equal(r1.shape, w1.shape)
        r2, w2 = average(ones((2, 2, 3)), axis=0, weights=[3, 1], returned=1)
        assert_equal(shape(w2), shape(r2))
        r2, w2 = average(ones((2, 2, 3)), returned=1)
        assert_equal(shape(w2), shape(r2))
        r2, w2 = average(ones((2, 2, 3)), weights=ones((2, 2, 3)), returned=1)
        assert_(shape(w2) == shape(r2))
        a2d = array([[1, 2], [0, 4]], float)
        a2dm = masked_array(a2d, [[0, 0], [1, 0]])
        a2da = average(a2d, axis=0)
        assert_(eq(a2da, [0.5, 3.0]))
        a2dma = average(a2dm, axis=0)
        assert_(eq(a2dma, [1.0, 3.0]))
        a2dma = average(a2dm, axis=None)
        assert_(eq(a2dma, 7. / 3.))
        a2dma = average(a2dm, axis=1)
        assert_(eq(a2dma, [1.5, 4.0]))
Exemplo n.º 4
0
 def test_masked_flexible(self):
     # Test recursive_fill_fields on masked flexible-array
     a = ma.array([(1, 10.), (2, 20.)],
                  mask=[(0, 1), (1, 0)],
                  dtype=[('A', int), ('B', float)])
     b = ma.zeros((3, ), dtype=a.dtype)
     test = recursive_fill_fields(a, b)
     control = ma.array([(1, 10.), (2, 20.), (0, 0.)],
                        mask=[(0, 1), (1, 0), (0, 0)],
                        dtype=[('A', int), ('B', float)])
     assert_equal(test, control)
Exemplo n.º 5
0
 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))
Exemplo n.º 6
0
 def test_checktitles(self):
     # Test using titles in the field names
     adtype = [(('a', 'A'), int), (('b', 'B'), bool), (('c', 'C'), float)]
     a = ma.array([(1, 2, 3)], mask=[(0, 1, 0)], dtype=adtype)
     bdtype = [(('a', 'A'), int), (('b', 'B'), bool), (('c', 'C'), float)]
     b = ma.array([(4, 5, 6)], dtype=bdtype)
     test = stack_arrays((a, b))
     control = ma.array([(1, 2, 3), (4, 5, 6)],
                        mask=[(0, 1, 0), (0, 0, 0)],
                        dtype=bdtype)
     assert_equal(test, control)
     assert_equal(test.mask, control.mask)
Exemplo n.º 7
0
    def test_tolist(self):
        # Test tolist.
        _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int)
        _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float)
        _c = ma.array(['one', 'two', 'three'], mask=[1, 0, 0], dtype='|S8')
        ddtype = [('a', int), ('b', float), ('c', '|S8')]
        mrec = fromarrays([_a, _b, _c],
                          dtype=ddtype,
                          fill_value=(99999, 99999., 'N/A'))

        assert_equal(mrec.tolist(), [(1, 1.1, None), (2, 2.2, b'two'),
                                     (None, None, b'three')])
Exemplo n.º 8
0
 def test_fromarrays(self):
     _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int)
     _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float)
     _c = ma.array(['one', 'two', 'three'], mask=[0, 0, 1], dtype='|S8')
     (mrec, nrec, _) = self.data
     for (f, l) in zip(('a', 'b', 'c'), (_a, _b, _c)):
         assert_equal(getattr(mrec, f)._mask, l._mask)
     # One record only
     _x = ma.array(
         [1, 1.1, 'one'],
         mask=[1, 0, 0],
     )
     assert_equal_records(fromarrays(_x, dtype=mrec.dtype), mrec[0])
Exemplo n.º 9
0
 def test_testScalarArithmetic(self):
     xm = array(0, mask=1)
     #TODO FIXME: Find out what the following raises a warning in r8247
     with np.errstate(divide='ignore'):
         assert_((1 / array(0)).mask)
     assert_((1 + xm).mask)
     assert_((-xm).mask)
     assert_((-xm).mask)
     assert_(maximum(xm, xm).mask)
     assert_(minimum(xm, xm).mask)
     assert_(xm.filled().dtype is xm._data.dtype)
     x = array(0, mask=0)
     assert_(x.filled() == x._data)
     assert_equal(str(xm), str(masked_print_option))
Exemplo n.º 10
0
 def setup(self):
     x = np.array([1., 1., 1., -2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.])
     y = np.array([5., 0., 3., 2., -1., -4., 0., -10., 10., 1., 0., 3.])
     a10 = 10.
     m1 = [1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
     m2 = [0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1]
     xm = array(x, mask=m1)
     ym = array(y, mask=m2)
     z = np.array([-.5, 0., .5, .8])
     zm = array(z, mask=[0, 1, 0, 0])
     xf = np.where(m1, 1e+20, x)
     s = x.shape
     xm.set_fill_value(1e+20)
     self.d = (x, y, a10, m1, m2, xm, ym, z, zm, xf, s)
Exemplo n.º 11
0
    def test_testCopySize(self):
        # Tests of some subtle points of copying and sizing.
        n = [0, 0, 1, 0, 0]
        m = make_mask(n)
        m2 = make_mask(m)
        assert_(m is m2)
        m3 = make_mask(m, copy=1)
        assert_(m is not m3)

        x1 = np.arange(5)
        y1 = array(x1, mask=m)
        assert_(y1._data is not x1)
        assert_(allequal(x1, y1._data))
        assert_(y1.mask is m)

        y1a = array(y1, copy=0)
        # For copy=False, one might expect that the array would just
        # passed on, i.e., that it would be "is" instead of "==".
        # See gh-4043 for discussion.
        assert_(y1a._mask.__array_interface__ ==
                y1._mask.__array_interface__)

        y2 = array(x1, mask=m3, copy=0)
        assert_(y2.mask is m3)
        assert_(y2[2] is masked)
        y2[2] = 9
        assert_(y2[2] is not masked)
        assert_(y2.mask is m3)
        assert_(allequal(y2.mask, 0))

        y2a = array(x1, mask=m, copy=1)
        assert_(y2a.mask is not m)
        assert_(y2a[2] is masked)
        y2a[2] = 9
        assert_(y2a[2] is not masked)
        assert_(y2a.mask is not m)
        assert_(allequal(y2a.mask, 0))

        y3 = array(x1 * 1.0, mask=m)
        assert_(filled(y3).dtype is (x1 * 1.0).dtype)

        x4 = arange(4)
        x4[2] = masked
        y4 = resize(x4, (8,))
        assert_(eq(concatenate([x4, x4]), y4))
        assert_(eq(getmask(y4), [0, 0, 1, 0, 0, 0, 1, 0]))
        y5 = repeat(x4, (2, 2, 2, 2), axis=0)
        assert_(eq(y5, [0, 0, 1, 1, 2, 2, 3, 3]))
        y6 = repeat(x4, 2, axis=0)
        assert_(eq(y5, y6))
Exemplo n.º 12
0
 def test_xtestCount(self):
     # Test count
     ott = array([0., 1., 2., 3.], mask=[1, 0, 0, 0])
     assert_(count(ott).dtype.type is np.intp)
     assert_equal(3, count(ott))
     assert_equal(1, count(1))
     assert_(eq(0, array(1, mask=[1])))
     ott = ott.reshape((2, 2))
     assert_(count(ott).dtype.type is np.intp)
     assert_(isinstance(count(ott, 0), np.ndarray))
     assert_(count(ott).dtype.type is np.intp)
     assert_(eq(3, count(ott)))
     assert_(getmask(count(ott, 0)) is nomask)
     assert_(eq([1, 2], count(ott, 0)))
Exemplo n.º 13
0
 def test_filled(self):
     # Test filling the array
     _a = ma.array([1, 2, 3], mask=[0, 0, 1], dtype=int)
     _b = ma.array([1.1, 2.2, 3.3], mask=[0, 0, 1], dtype=float)
     _c = ma.array(['one', 'two', 'three'], mask=[0, 0, 1], dtype='|S8')
     ddtype = [('a', int), ('b', float), ('c', '|S8')]
     mrec = fromarrays([_a, _b, _c],
                       dtype=ddtype,
                       fill_value=(99999, 99999., 'N/A'))
     mrecfilled = mrec.filled()
     assert_equal(mrecfilled['a'], np.array((1, 2, 99999), dtype=int))
     assert_equal(mrecfilled['b'], np.array((1.1, 2.2, 99999.),
                                            dtype=float))
     assert_equal(mrecfilled['c'],
                  np.array(('one', 'two', 'N/A'), dtype='|S8'))
Exemplo n.º 14
0
    def test_set_fields(self):
        # Tests setting fields.
        base = self.base.copy()
        mbase = base.view(mrecarray)
        mbase = mbase.copy()
        mbase.fill_value = (999999, 1e20, 'N/A')
        # Change the data, the mask should be conserved
        mbase.a._data[:] = 5
        assert_equal(mbase['a']._data, [5, 5, 5, 5, 5])
        assert_equal(mbase['a']._mask, [0, 1, 0, 0, 1])
        # Change the elements, and the mask will follow
        mbase.a = 1
        assert_equal(mbase['a']._data, [1] * 5)
        assert_equal(ma.getmaskarray(mbase['a']), [0] * 5)
        # Use to be _mask, now it's recordmask
        assert_equal(mbase.recordmask, [False] * 5)
        assert_equal(
            mbase._mask.tolist(),
            np.array([(0, 0, 0), (0, 1, 1), (0, 0, 0), (0, 0, 0), (0, 1, 1)],
                     dtype=bool))
        # Set a field to mask ........................
        mbase.c = masked
        # Use to be mask, and now it's still mask !
        assert_equal(mbase.c.mask, [1] * 5)
        assert_equal(mbase.c.recordmask, [1] * 5)
        assert_equal(ma.getmaskarray(mbase['c']), [1] * 5)
        assert_equal(ma.getdata(mbase['c']), [b'N/A'] * 5)
        assert_equal(
            mbase._mask.tolist(),
            np.array([(0, 0, 1), (0, 1, 1), (0, 0, 1), (0, 0, 1), (0, 1, 1)],
                     dtype=bool))
        # Set fields by slices .......................
        mbase = base.view(mrecarray).copy()
        mbase.a[3:] = 5
        assert_equal(mbase.a, [1, 2, 3, 5, 5])
        assert_equal(mbase.a._mask, [0, 1, 0, 0, 0])
        mbase.b[3:] = masked
        assert_equal(mbase.b, base['b'])
        assert_equal(mbase.b._mask, [0, 1, 0, 1, 1])
        # Set fields globally..........................
        ndtype = [('alpha', '|S1'), ('num', int)]
        data = ma.array([('a', 1), ('b', 2), ('c', 3)], dtype=ndtype)
        rdata = data.view(MaskedRecords)
        val = ma.array([10, 20, 30], mask=[1, 0, 0])

        rdata['num'] = val
        assert_equal(rdata.num, val)
        assert_equal(rdata.num.mask, [1, 0, 0])
Exemplo n.º 15
0
def addfield(mrecord, newfield, newfieldname=None):
    """Adds a new field to the masked record array

    Uses `newfield` as data and `newfieldname` as name. If `newfieldname`
    is None, the new field name is set to 'fi', where `i` is the number of
    existing fields.

    """
    _data = mrecord._data
    _mask = mrecord._mask
    if newfieldname is None or newfieldname in reserved_fields:
        newfieldname = 'f%i' % len(_data.dtype)
    newfield = ma.array(newfield)
    # Get the new data.
    # Create a new empty recarray
    newdtype = np.dtype(_data.dtype.descr + [(newfieldname, newfield.dtype)])
    newdata = recarray(_data.shape, newdtype)
    # Add the existing field
    [newdata.setfield(_data.getfield(*f), *f)
         for f in _data.dtype.fields.values()]
    # Add the new field
    newdata.setfield(newfield._data, *newdata.dtype.fields[newfieldname])
    newdata = newdata.view(MaskedRecords)
    # Get the new mask
    # Create a new empty recarray
    newmdtype = np.dtype([(n, bool_) for n in newdtype.names])
    newmask = recarray(_data.shape, newmdtype)
    # Add the old masks
    [newmask.setfield(_mask.getfield(*f), *f)
         for f in _mask.dtype.fields.values()]
    # Add the mask of the new field
    newmask.setfield(getmaskarray(newfield),
                     *newmask.dtype.fields[newfieldname])
    newdata._mask = newmask
    return newdata
Exemplo n.º 16
0
 def test_testArrayMethods(self):
     a = array([1, 3, 2])
     assert_(eq(a.any(), a._data.any()))
     assert_(eq(a.all(), a._data.all()))
     assert_(eq(a.argmax(), a._data.argmax()))
     assert_(eq(a.argmin(), a._data.argmin()))
     assert_(eq(a.choose(0, 1, 2, 3, 4),
                        a._data.choose(0, 1, 2, 3, 4)))
     assert_(eq(a.compress([1, 0, 1]), a._data.compress([1, 0, 1])))
     assert_(eq(a.conj(), a._data.conj()))
     assert_(eq(a.conjugate(), a._data.conjugate()))
     m = array([[1, 2], [3, 4]])
     assert_(eq(m.diagonal(), m._data.diagonal()))
     assert_(eq(a.sum(), a._data.sum()))
     assert_(eq(a.take([1, 2]), a._data.take([1, 2])))
     assert_(eq(m.transpose(), m._data.transpose()))
Exemplo n.º 17
0
 def test_addfield(self):
     # Tests addfield
     (mrec, nrec, ddtype) = self.data
     (d, m) = ([100, 200, 300], [1, 0, 0])
     mrec = addfield(mrec, ma.array(d, mask=m))
     assert_equal(mrec.f3, d)
     assert_equal(mrec.f3._mask, m)
Exemplo n.º 18
0
    def test_find_duplicates(self):
        # Test find_duplicates
        a = ma.array([(2, (2., 'B')), (1, (2., 'B')), (2, (2., 'B')),
                      (1, (1., 'B')), (2, (2., 'B')), (2, (2., 'C'))],
                     mask=[(0, (0, 0)), (0, (0, 0)), (0, (0, 0)), (0, (0, 0)),
                           (1, (0, 0)), (0, (1, 0))],
                     dtype=[('A', int), ('B', [('BA', float), ('BB', '|S1')])])

        test = find_duplicates(a, ignoremask=False, return_index=True)
        control = [0, 2]
        assert_equal(sorted(test[-1]), control)
        assert_equal(test[0], a[test[-1]])

        test = find_duplicates(a, key='A', return_index=True)
        control = [0, 1, 2, 3, 5]
        assert_equal(sorted(test[-1]), control)
        assert_equal(test[0], a[test[-1]])

        test = find_duplicates(a, key='B', return_index=True)
        control = [0, 1, 2, 4]
        assert_equal(sorted(test[-1]), control)
        assert_equal(test[0], a[test[-1]])

        test = find_duplicates(a, key='BA', return_index=True)
        control = [0, 1, 2, 4]
        assert_equal(sorted(test[-1]), control)
        assert_equal(test[0], a[test[-1]])

        test = find_duplicates(a, key='BB', return_index=True)
        control = [0, 1, 2, 3, 4]
        assert_equal(sorted(test[-1]), control)
        assert_equal(test[0], a[test[-1]])
Exemplo n.º 19
0
 def test_w_singlefield(self):
     # Test single field
     test = merge_arrays(
         (np.array([1, 2]).view([('a', int)]), np.array([10., 20., 30.])), )
     control = ma.array([(1, 10.), (2, 20.), (-1, 30.)],
                        mask=[(0, 0), (0, 0), (1, 0)],
                        dtype=[('a', int), ('f1', float)])
     assert_equal(test, control)
Exemplo n.º 20
0
 def test_testAverage1(self):
     # Test of average.
     ott = array([0., 1., 2., 3.], mask=[1, 0, 0, 0])
     assert_(eq(2.0, average(ott, axis=0)))
     assert_(eq(2.0, average(ott, weights=[1., 1., 2., 1.])))
     result, wts = average(ott, weights=[1., 1., 2., 1.], returned=1)
     assert_(eq(2.0, result))
     assert_(wts == 4.0)
     ott[:] = masked
     assert_(average(ott, axis=0) is masked)
     ott = array([0., 1., 2., 3.], mask=[1, 0, 0, 0])
     ott = ott.reshape(2, 2)
     ott[:, 1] = masked
     assert_(eq(average(ott, axis=0), [2.0, 0.0]))
     assert_(average(ott, axis=1)[0] is masked)
     assert_(eq([2., 0.], average(ott, axis=0)))
     result, wts = average(ott, axis=0, returned=1)
     assert_(eq(wts, [1., 0.]))
Exemplo n.º 21
0
 def test_autoconversion(self):
     # Tests autoconversion
     adtype = [('A', int), ('B', bool), ('C', float)]
     a = ma.array([(1, 2, 3)], mask=[(0, 1, 0)], dtype=adtype)
     bdtype = [('A', int), ('B', float), ('C', float)]
     b = ma.array([(4, 5, 6)], dtype=bdtype)
     control = ma.array([(1, 2, 3), (4, 5, 6)],
                        mask=[(0, 1, 0), (0, 0, 0)],
                        dtype=bdtype)
     test = stack_arrays((a, b), autoconvert=True)
     assert_equal(test, control)
     assert_equal(test.mask, control.mask)
     try:
         test = stack_arrays((a, b), autoconvert=False)
     except TypeError:
         pass
     else:
         raise AssertionError
Exemplo n.º 22
0
 def test_testCI(self):
     # Test of conversions and indexing
     x1 = np.array([1, 2, 4, 3])
     x2 = array(x1, mask=[1, 0, 0, 0])
     x3 = array(x1, mask=[0, 1, 0, 1])
     x4 = array(x1)
     # test conversion to strings
     str(x2)  # raises?
     repr(x2)  # raises?
     assert_(eq(np.sort(x1), sort(x2, fill_value=0)))
     # tests of indexing
     assert_(type(x2[1]) is type(x1[1]))
     assert_(x1[1] == x2[1])
     assert_(x2[0] is masked)
     assert_(eq(x1[2], x2[2]))
     assert_(eq(x1[2:5], x2[2:5]))
     assert_(eq(x1[:], x2[:]))
     assert_(eq(x1[1:], x3[1:]))
     x1[2] = 9
     x2[2] = 9
     assert_(eq(x1, x2))
     x1[1:3] = 99
     x2[1:3] = 99
     assert_(eq(x1, x2))
     x2[1] = masked
     assert_(eq(x1, x2))
     x2[1:3] = masked
     assert_(eq(x1, x2))
     x2[:] = x1
     x2[1] = masked
     assert_(allequal(getmask(x2), array([0, 1, 0, 0])))
     x3[:] = masked_array([1, 2, 3, 4], [0, 1, 1, 0])
     assert_(allequal(getmask(x3), array([0, 1, 1, 0])))
     x4[:] = masked_array([1, 2, 3, 4], [0, 1, 1, 0])
     assert_(allequal(getmask(x4), array([0, 1, 1, 0])))
     assert_(allequal(x4, array([1, 2, 3, 4])))
     x1 = np.arange(5) * 1.0
     x2 = masked_values(x1, 3.0)
     assert_(eq(x1, x2))
     assert_(allequal(array([0, 0, 0, 1, 0], MaskType), x2.mask))
     assert_(eq(3.0, x2.fill_value))
     x1 = array([1, 'hello', 2, 3], object)
     x2 = np.array([1, 'hello', 2, 3], object)
     s1 = x1[1]
     s2 = x2[1]
     assert_equal(type(s2), str)
     assert_equal(type(s1), str)
     assert_equal(s1, s2)
     assert_(x1[1:1].shape == (0,))
Exemplo n.º 23
0
 def test_testToPython(self):
     assert_equal(1, int(array(1)))
     assert_equal(1.0, float(array(1)))
     assert_equal(1, int(array([[[1]]])))
     assert_equal(1.0, float(array([[1]])))
     assert_raises(TypeError, float, array([1, 1]))
     assert_raises(ValueError, bool, array([0, 1]))
     assert_raises(ValueError, bool, array([0, 0], mask=[0, 1]))
Exemplo n.º 24
0
 def test_append_on_flex(self):
     # Test append_fields on flexible type arrays
     z = self.data[-1]
     test = append_fields(z, 'C', data=[10, 20, 30])
     control = ma.array(
         [('A', 1., 10), ('B', 2., 20), (-1, -1., 30)],
         mask=[(0, 0, 0), (0, 0, 0), (1, 1, 0)],
         dtype=[('A', '|S3'), ('B', float), ('C', int)],
     )
     assert_equal(test, control)
Exemplo n.º 25
0
 def test_append_double(self):
     # Test simple case
     (_, x, _, _) = self.data
     test = append_fields(x, ('A', 'B'), data=[[10, 20, 30], [100, 200]])
     control = ma.array(
         [(1, 10, 100), (2, 20, 200), (-1, 30, -1)],
         mask=[(0, 0, 0), (0, 0, 0), (1, 0, 1)],
         dtype=[('f0', int), ('A', int), ('B', int)],
     )
     assert_equal(test, control)
Exemplo n.º 26
0
 def test_append_single(self):
     # Test simple case
     (_, x, _, _) = self.data
     test = append_fields(x, 'A', data=[10, 20, 30])
     control = ma.array(
         [(1, 10), (2, 20), (-1, 30)],
         mask=[(0, 0), (0, 0), (1, 0)],
         dtype=[('f0', int), ('A', int)],
     )
     assert_equal(test, control)
Exemplo n.º 27
0
 def test_testAddSumProd(self):
     # Test add, sum, product.
     (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
     assert_(eq(np.add.reduce(x), add.reduce(x)))
     assert_(eq(np.add.accumulate(x), add.accumulate(x)))
     assert_(eq(4, sum(array(4), axis=0)))
     assert_(eq(4, sum(array(4), axis=0)))
     assert_(eq(np.sum(x, axis=0), sum(x, axis=0)))
     assert_(eq(np.sum(filled(xm, 0), axis=0), sum(xm, axis=0)))
     assert_(eq(np.sum(x, 0), sum(x, 0)))
     assert_(eq(np.product(x, axis=0), product(x, axis=0)))
     assert_(eq(np.product(x, 0), product(x, 0)))
     assert_(eq(np.product(filled(xm, 1), axis=0),
                        product(xm, axis=0)))
     if len(s) > 1:
         assert_(eq(np.concatenate((x, y), 1),
                            concatenate((xm, ym), 1)))
         assert_(eq(np.add.reduce(x, 1), add.reduce(x, 1)))
         assert_(eq(np.sum(x, 1), sum(x, 1)))
         assert_(eq(np.product(x, 1), product(x, 1)))
Exemplo n.º 28
0
 def test_append_on_nested(self):
     # Test append_fields on nested fields
     w = self.data[0]
     test = append_fields(w, 'C', data=[10, 20, 30])
     control = ma.array(
         [(1, (2, 3.0), 10), (4, (5, 6.0), 20), (-1, (-1, -1.), 30)],
         mask=[(0, (0, 0), 0), (0, (0, 0), 0), (1, (1, 1), 0)],
         dtype=[('a', int), ('b', [('ba', float), ('bb', int)]),
                ('C', int)],
     )
     assert_equal(test, control)
Exemplo n.º 29
0
    def test_testPut(self):
        # Test of put
        d = arange(5)
        n = [0, 0, 0, 1, 1]
        m = make_mask(n)
        m2 = m.copy()
        x = array(d, mask=m)
        assert_(x[3] is masked)
        assert_(x[4] is masked)
        x[[1, 4]] = [10, 40]
        assert_(x.mask is m)
        assert_(x[3] is masked)
        assert_(x[4] is not masked)
        assert_(eq(x, [0, 10, 2, -1, 40]))

        x = array(d, mask=m2, copy=True)
        x.put([0, 1, 2], [-1, 100, 200])
        assert_(x.mask is not m2)
        assert_(x[3] is masked)
        assert_(x[4] is masked)
        assert_(eq(x, [-1, 100, 200, 0, 0]))
Exemplo n.º 30
0
    def setup(self):
        x = np.array([8.375, 7.545, 8.828, 8.5, 1.757, 5.928,
                      8.43, 7.78, 9.865, 5.878, 8.979, 4.732,
                      3.012, 6.022, 5.095, 3.116, 5.238, 3.957,
                      6.04, 9.63, 7.712, 3.382, 4.489, 6.479,
                      7.189, 9.645, 5.395, 4.961, 9.894, 2.893,
                      7.357, 9.828, 6.272, 3.758, 6.693, 0.993])
        X = x.reshape(6, 6)
        XX = x.reshape(3, 2, 2, 3)

        m = np.array([0, 1, 0, 1, 0, 0,
                      1, 0, 1, 1, 0, 1,
                      0, 0, 0, 1, 0, 1,
                      0, 0, 0, 1, 1, 1,
                      1, 0, 0, 1, 0, 0,
                      0, 0, 1, 0, 1, 0])
        mx = array(data=x, mask=m)
        mX = array(data=X, mask=m.reshape(X.shape))
        mXX = array(data=XX, mask=m.reshape(XX.shape))

        self.d = (x, X, XX, m, mx, mX, mXX)