Exemple #1
0
 def test_testMasked(self):
     # Test of masked element
     xx = arange(6)
     xx[1] = masked
     assert_(str(masked) == '--')
     assert_(xx[1] is masked)
     assert_equal(filled(xx[1], 0), 0)
Exemple #2
0
 def test_testTakeTransposeInnerOuter(self):
     # Test of take, transpose, inner, outer products
     x = arange(24)
     y = np.arange(24)
     x[5:6] = masked
     x = x.reshape(2, 3, 4)
     y = y.reshape(2, 3, 4)
     assert_(eq(np.transpose(y, (2, 0, 1)), transpose(x, (2, 0, 1))))
     assert_(eq(np.take(y, (2, 0, 1), 1), take(x, (2, 0, 1), 1)))
     assert_(eq(np.inner(filled(x, 0), filled(y, 0)),
                inner(x, y)))
     assert_(eq(np.outer(filled(x, 0), filled(y, 0)),
                outer(x, y)))
     y = array(['abc', 1, 'def', 2, 3], object)
     y[2] = masked
     t = take(y, [0, 3, 4])
     assert_(t[0] == 'abc')
     assert_(t[1] == 2)
     assert_(t[2] == 3)
Exemple #3
0
    def __setattr__(self, attr, val):
        """
        Sets the attribute attr to the value val.

        """
        # Should we call __setmask__ first ?
        if attr in ['mask', 'fieldmask']:
            self.__setmask__(val)
            return
        # Create a shortcut (so that we don't have to call getattr all the time)
        _localdict = object.__getattribute__(self, '__dict__')
        # Check whether we're creating a new field
        newattr = attr not in _localdict
        try:
            # Is attr a generic attribute ?
            ret = object.__setattr__(self, attr, val)
        except Exception:
            # Not a generic attribute: exit if it's not a valid field
            fielddict = ndarray.__getattribute__(self, 'dtype').fields or {}
            optinfo = ndarray.__getattribute__(self, '_optinfo') or {}
            if not (attr in fielddict or attr in optinfo):
                exctype, value = sys.exc_info()[:2]
                raise exctype(value)
        else:
            # Get the list of names
            fielddict = ndarray.__getattribute__(self, 'dtype').fields or {}
            # Check the attribute
            if attr not in fielddict:
                return ret
            if newattr:
                # We just added this one or this setattr worked on an
                # internal attribute.
                try:
                    object.__delattr__(self, attr)
                except Exception:
                    return ret
        # Let's try to set the field
        try:
            res = fielddict[attr][:2]
        except (TypeError, KeyError):
            raise AttributeError("record array has no attribute %s" % attr)

        if val is masked:
            _fill_value = _localdict['_fill_value']
            if _fill_value is not None:
                dval = _localdict['_fill_value'][attr]
            else:
                dval = val
            mval = True
        else:
            dval = filled(val)
            mval = getmaskarray(val)
        obj = ndarray.__getattribute__(self, '_data').setfield(dval, *res)
        _localdict['_mask'].__setitem__(attr, mval)
        return obj
Exemple #4
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)))
Exemple #5
0
 def test_testBasic1d(self):
     # Test of basic array creation and properties in 1 dimension.
     (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
     assert_(not isMaskedArray(x))
     assert_(isMaskedArray(xm))
     assert_equal(shape(xm), s)
     assert_equal(xm.shape, s)
     assert_equal(xm.dtype, x.dtype)
     assert_equal(xm.size, reduce(lambda x, y:x * y, s))
     assert_equal(count(xm), len(m1) - reduce(lambda x, y:x + y, m1))
     assert_(eq(xm, xf))
     assert_(eq(filled(xm, 1.e20), xf))
     assert_(eq(x, xm))
Exemple #6
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))
Exemple #7
0
def _fix_output(output, usemask=True, asrecarray=False):
    """
    Private function: return a recarray, a ndarray, a MaskedArray
    or a MaskedRecords depending on the input parameters
    """
    if not isinstance(output, MaskedArray):
        usemask = False
    if usemask:
        if asrecarray:
            output = output.view(MaskedRecords)
    else:
        output = ma.filled(output)
        if asrecarray:
            output = output.view(recarray)
    return output
Exemple #8
0
    def test_testBasic2d(self):
        # Test of basic array creation and properties in 2 dimensions.
        for s in [(4, 3), (6, 2)]:
            (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
            x.shape = s
            y.shape = s
            xm.shape = s
            ym.shape = s
            xf.shape = s

            assert_(not isMaskedArray(x))
            assert_(isMaskedArray(xm))
            assert_equal(shape(xm), s)
            assert_equal(xm.shape, s)
            assert_equal(xm.size, reduce(lambda x, y:x * y, s))
            assert_equal(count(xm),
                             len(m1) - reduce(lambda x, y:x + y, m1))
            assert_(eq(xm, xf))
            assert_(eq(filled(xm, 1.e20), xf))
            assert_(eq(x, xm))
            self.setup()