summaryrefslogtreecommitdiff
path: root/numpy/ma/tests
diff options
context:
space:
mode:
authorStefan van der Walt <stefan@sun.ac.za>2007-12-15 01:15:26 +0000
committerStefan van der Walt <stefan@sun.ac.za>2007-12-15 01:15:26 +0000
commit703e8d6323b19cbfeb96772c1e35f1cd68629336 (patch)
tree34bd23200d97ff43369d7d23d37c9c08c3d3a3b4 /numpy/ma/tests
parent61f9f6d0fb169cadefe35ea2bdd783848aa771f5 (diff)
downloadnumpy-703e8d6323b19cbfeb96772c1e35f1cd68629336.tar.gz
Move ma to numpy root. Fix unit tests. Remove references to numpy.core.ma.
Diffstat (limited to 'numpy/ma/tests')
-rw-r--r--numpy/ma/tests/test_core.py1305
-rw-r--r--numpy/ma/tests/test_extras.py331
-rw-r--r--numpy/ma/tests/test_morestats.py114
-rw-r--r--numpy/ma/tests/test_mrecords.py181
-rw-r--r--numpy/ma/tests/test_mstats.py174
-rw-r--r--numpy/ma/tests/test_subclassing.py183
6 files changed, 2288 insertions, 0 deletions
diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py
new file mode 100644
index 000000000..0849be135
--- /dev/null
+++ b/numpy/ma/tests/test_core.py
@@ -0,0 +1,1305 @@
+# pylint: disable-msg=W0611, W0612, W0511,R0201
+"""Tests suite for MaskedArray & subclassing.
+
+:author: Pierre Gerard-Marchant
+:contact: pierregm_at_uga_dot_edu
+:version: $Id: test_core.py 3473 2007-10-29 15:18:13Z jarrod.millman $
+"""
+__author__ = "Pierre GF Gerard-Marchant ($Author: jarrod.millman $)"
+__version__ = '1.0'
+__revision__ = "$Revision: 3473 $"
+__date__ = '$Date: 2007-10-29 17:18:13 +0200 (Mon, 29 Oct 2007) $'
+
+import types
+
+import numpy
+import numpy.core.fromnumeric as fromnumeric
+from numpy.testing import NumpyTest, NumpyTestCase
+from numpy.testing.utils import build_err_msg
+from numpy import array as narray
+
+import numpy.ma.testutils
+from numpy.ma.testutils import *
+
+import numpy.ma.core as coremodule
+from numpy.ma.core import *
+
+pi = numpy.pi
+
+#..............................................................................
+class TestMA(NumpyTestCase):
+ "Base test class for MaskedArrays."
+ def __init__(self, *args, **kwds):
+ NumpyTestCase.__init__(self, *args, **kwds)
+ self.setUp()
+
+ def setUp (self):
+ "Base data definition."
+ x = narray([1.,1.,1.,-2., pi/2.0, 4., 5., -10., 10., 1., 2., 3.])
+ y = narray([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 = masked_array(x, mask=m1)
+ ym = masked_array(y, mask=m2)
+ z = narray([-.5, 0., .5, .8])
+ zm = masked_array(z, mask=[0,1,0,0])
+ xf = numpy.where(m1, 1.e+20, x)
+ xm.set_fill_value(1.e+20)
+ self.d = (x, y, a10, m1, m2, xm, ym, z, zm, xf)
+ #........................
+ def check_basic1d(self):
+ "Test of basic array creation and properties in 1 dimension."
+ (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
+ assert(not isMaskedArray(x))
+ assert(isMaskedArray(xm))
+ assert((xm-ym).filled(0).any())
+ fail_if_equal(xm.mask.astype(int_), ym.mask.astype(int_))
+ s = x.shape
+ assert_equal(numpy.shape(xm), s)
+ assert_equal(xm.shape, s)
+ assert_equal(xm.dtype, x.dtype)
+ assert_equal(zm.dtype, z.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_array_equal(xm, xf)
+ assert_array_equal(filled(xm, 1.e20), xf)
+ assert_array_equal(x, xm)
+ #........................
+ def check_basic2d(self):
+ "Test of basic array creation and properties in 2 dimensions."
+ (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
+ for s in [(4,3), (6,2)]:
+ 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_equal(xm, xf)
+ assert_equal(filled(xm, 1.e20), xf)
+ assert_equal(x, xm)
+ #........................
+ def check_basic_arithmetic (self):
+ "Test of basic arithmetic."
+ (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
+ a2d = array([[1,2],[0,4]])
+ a2dm = masked_array(a2d, [[0,0],[1,0]])
+ assert_equal(a2d * a2d, a2d * a2dm)
+ assert_equal(a2d + a2d, a2d + a2dm)
+ assert_equal(a2d - a2d, a2d - a2dm)
+ for s in [(12,), (4,3), (2,6)]:
+ x = x.reshape(s)
+ y = y.reshape(s)
+ xm = xm.reshape(s)
+ ym = ym.reshape(s)
+ xf = xf.reshape(s)
+ assert_equal(-x, -xm)
+ assert_equal(x + y, xm + ym)
+ assert_equal(x - y, xm - ym)
+ assert_equal(x * y, xm * ym)
+ assert_equal(x / y, xm / ym)
+ assert_equal(a10 + y, a10 + ym)
+ assert_equal(a10 - y, a10 - ym)
+ assert_equal(a10 * y, a10 * ym)
+ assert_equal(a10 / y, a10 / ym)
+ assert_equal(x + a10, xm + a10)
+ assert_equal(x - a10, xm - a10)
+ assert_equal(x * a10, xm * a10)
+ assert_equal(x / a10, xm / a10)
+ assert_equal(x**2, xm**2)
+ assert_equal(abs(x)**2.5, abs(xm) **2.5)
+ assert_equal(x**y, xm**ym)
+ assert_equal(numpy.add(x,y), add(xm, ym))
+ assert_equal(numpy.subtract(x,y), subtract(xm, ym))
+ assert_equal(numpy.multiply(x,y), multiply(xm, ym))
+ assert_equal(numpy.divide(x,y), divide(xm, ym))
+ #........................
+ def check_mixed_arithmetic(self):
+ "Tests mixed arithmetics."
+ na = narray([1])
+ ma = array([1])
+ self.failUnless(isinstance(na + ma, MaskedArray))
+ self.failUnless(isinstance(ma + na, MaskedArray))
+ #........................
+ def check_inplace_arithmetic(self):
+ """Test of inplace operations and rich comparisons"""
+ # addition
+ x = arange(10)
+ y = arange(10)
+ xm = arange(10)
+ xm[2] = masked
+ x += 1
+ assert_equal(x, y+1)
+ xm += 1
+ assert_equal(xm, y+1)
+ # subtraction
+ x = arange(10)
+ xm = arange(10)
+ xm[2] = masked
+ x -= 1
+ assert_equal(x, y-1)
+ xm -= 1
+ assert_equal(xm, y-1)
+ # multiplication
+ x = arange(10)*1.0
+ xm = arange(10)*1.0
+ xm[2] = masked
+ x *= 2.0
+ assert_equal(x, y*2)
+ xm *= 2.0
+ assert_equal(xm, y*2)
+ # division
+ x = arange(10)*2
+ xm = arange(10)*2
+ xm[2] = masked
+ x /= 2
+ assert_equal(x, y)
+ xm /= 2
+ assert_equal(xm, y)
+ # division, pt 2
+ x = arange(10)*1.0
+ xm = arange(10)*1.0
+ xm[2] = masked
+ x /= 2.0
+ assert_equal(x, y/2.0)
+ xm /= arange(10)
+ assert_equal(xm, ones((10,)))
+
+ x = arange(10).astype(float_)
+ xm = arange(10)
+ xm[2] = masked
+# id1 = id(x.raw_data())
+ id1 = x.raw_data().ctypes.data
+ x += 1.
+# assert id1 == id(x.raw_data())
+ assert (id1 == x.raw_data().ctypes.data)
+ assert_equal(x, y+1.)
+ # addition w/ array
+ x = arange(10, dtype=float_)
+ xm = arange(10, dtype=float_)
+ xm[2] = masked
+ m = xm.mask
+ a = arange(10, dtype=float_)
+ a[-1] = masked
+ x += a
+ xm += a
+ assert_equal(x,y+a)
+ assert_equal(xm,y+a)
+ assert_equal(xm.mask, mask_or(m,a.mask))
+ # subtraction w/ array
+ x = arange(10, dtype=float_)
+ xm = arange(10, dtype=float_)
+ xm[2] = masked
+ m = xm.mask
+ a = arange(10, dtype=float_)
+ a[-1] = masked
+ x -= a
+ xm -= a
+ assert_equal(x,y-a)
+ assert_equal(xm,y-a)
+ assert_equal(xm.mask, mask_or(m,a.mask))
+ # multiplication w/ array
+ x = arange(10, dtype=float_)
+ xm = arange(10, dtype=float_)
+ xm[2] = masked
+ m = xm.mask
+ a = arange(10, dtype=float_)
+ a[-1] = masked
+ x *= a
+ xm *= a
+ assert_equal(x,y*a)
+ assert_equal(xm,y*a)
+ assert_equal(xm.mask, mask_or(m,a.mask))
+ # division w/ array
+ x = arange(10, dtype=float_)
+ xm = arange(10, dtype=float_)
+ xm[2] = masked
+ m = xm.mask
+ a = arange(10, dtype=float_)
+ a[-1] = masked
+ x /= a
+ xm /= a
+ assert_equal(x,y/a)
+ assert_equal(xm,y/a)
+ assert_equal(xm.mask, mask_or(mask_or(m,a.mask), (a==0)))
+ #
+ (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
+ z = xm/ym
+ assert_equal(z._mask, [1,1,1,0,0,1,1,0,0,0,1,1])
+ assert_equal(z._data, [0.2,1.,1./3.,-1.,-pi/2.,-1.,5.,1.,1.,1.,2.,1.])
+ xm = xm.copy()
+ xm /= ym
+ assert_equal(xm._mask, [1,1,1,0,0,1,1,0,0,0,1,1])
+ assert_equal(xm._data, [1/5.,1.,1./3.,-1.,-pi/2.,-1.,5.,1.,1.,1.,2.,1.])
+
+
+ #..........................
+ def check_scalararithmetic(self):
+ "Tests some scalar arithmetics on MaskedArrays."
+ xm = array(0, mask=1)
+ 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_equal(x.filled().ctypes.data, x.ctypes.data)
+ assert_equal(str(xm), str(masked_print_option))
+ #.........................
+ def check_basic_ufuncs (self):
+ "Test various functions such as sin, cos."
+ (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
+ assert_equal(numpy.cos(x), cos(xm))
+ assert_equal(numpy.cosh(x), cosh(xm))
+ assert_equal(numpy.sin(x), sin(xm))
+ assert_equal(numpy.sinh(x), sinh(xm))
+ assert_equal(numpy.tan(x), tan(xm))
+ assert_equal(numpy.tanh(x), tanh(xm))
+ assert_equal(numpy.sqrt(abs(x)), sqrt(xm))
+ assert_equal(numpy.log(abs(x)), log(xm))
+ assert_equal(numpy.log10(abs(x)), log10(xm))
+ assert_equal(numpy.exp(x), exp(xm))
+ assert_equal(numpy.arcsin(z), arcsin(zm))
+ assert_equal(numpy.arccos(z), arccos(zm))
+ assert_equal(numpy.arctan(z), arctan(zm))
+ assert_equal(numpy.arctan2(x, y), arctan2(xm, ym))
+ assert_equal(numpy.absolute(x), absolute(xm))
+ assert_equal(numpy.equal(x,y), equal(xm, ym))
+ assert_equal(numpy.not_equal(x,y), not_equal(xm, ym))
+ assert_equal(numpy.less(x,y), less(xm, ym))
+ assert_equal(numpy.greater(x,y), greater(xm, ym))
+ assert_equal(numpy.less_equal(x,y), less_equal(xm, ym))
+ assert_equal(numpy.greater_equal(x,y), greater_equal(xm, ym))
+ assert_equal(numpy.conjugate(x), conjugate(xm))
+ #........................
+ def check_count_func (self):
+ "Tests count"
+ ott = array([0.,1.,2.,3.], mask=[1,0,0,0])
+ assert( isinstance(count(ott), int))
+ assert_equal(3, count(ott))
+ assert_equal(1, count(1))
+ assert_equal(0, array(1,mask=[1]))
+ ott = ott.reshape((2,2))
+ assert isMaskedArray(count(ott,0))
+ assert isinstance(count(ott), types.IntType)
+ assert_equal(3, count(ott))
+ assert getmask(count(ott,0)) is nomask
+ assert_equal([1,2],count(ott,0))
+ #........................
+ def check_minmax_func (self):
+ "Tests minimum and maximum."
+ (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
+ xr = numpy.ravel(x) #max doesn't work if shaped
+ xmr = ravel(xm)
+ assert_equal(max(xr), maximum(xmr)) #true because of careful selection of data
+ assert_equal(min(xr), minimum(xmr)) #true because of careful selection of data
+ #
+ assert_equal(minimum([1,2,3],[4,0,9]), [1,0,3])
+ assert_equal(maximum([1,2,3],[4,0,9]), [4,2,9])
+ x = arange(5)
+ y = arange(5) - 2
+ x[3] = masked
+ y[0] = masked
+ assert_equal(minimum(x,y), where(less(x,y), x, y))
+ assert_equal(maximum(x,y), where(greater(x,y), x, y))
+ assert minimum(x) == 0
+ assert maximum(x) == 4
+ #
+ x = arange(4).reshape(2,2)
+ x[-1,-1] = masked
+ assert_equal(maximum(x), 2)
+
+ def check_minmax_methods(self):
+ "Additional tests on max/min"
+ (_, _, _, _, _, xm, _, _, _, _) = self.d
+ xm.shape = (xm.size,)
+ assert_equal(xm.max(), 10)
+ assert(xm[0].max() is masked)
+ assert(xm[0].max(0) is masked)
+ assert(xm[0].max(-1) is masked)
+ assert_equal(xm.min(), -10.)
+ assert(xm[0].min() is masked)
+ assert(xm[0].min(0) is masked)
+ assert(xm[0].min(-1) is masked)
+ assert_equal(xm.ptp(), 20.)
+ assert(xm[0].ptp() is masked)
+ assert(xm[0].ptp(0) is masked)
+ assert(xm[0].ptp(-1) is masked)
+ #........................
+ def check_addsumprod (self):
+ "Tests add, sum, product."
+ (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
+ assert_equal(numpy.add.reduce(x), add.reduce(x))
+ assert_equal(numpy.add.accumulate(x), add.accumulate(x))
+ assert_equal(4, sum(array(4),axis=0))
+ assert_equal(4, sum(array(4), axis=0))
+ assert_equal(numpy.sum(x,axis=0), sum(x,axis=0))
+ assert_equal(numpy.sum(filled(xm,0),axis=0), sum(xm,axis=0))
+ assert_equal(numpy.sum(x,0), sum(x,0))
+ assert_equal(numpy.product(x,axis=0), product(x,axis=0))
+ assert_equal(numpy.product(x,0), product(x,0))
+ assert_equal(numpy.product(filled(xm,1),axis=0), product(xm,axis=0))
+ s = (3,4)
+ x.shape = y.shape = xm.shape = ym.shape = s
+ if len(s) > 1:
+ assert_equal(numpy.concatenate((x,y),1), concatenate((xm,ym),1))
+ assert_equal(numpy.add.reduce(x,1), add.reduce(x,1))
+ assert_equal(numpy.sum(x,1), sum(x,1))
+ assert_equal(numpy.product(x,1), product(x,1))
+ #.........................
+ def check_concat(self):
+ "Tests concatenations."
+ (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
+ # basic concatenation
+ assert_equal(numpy.concatenate((x,y)), concatenate((xm,ym)))
+ assert_equal(numpy.concatenate((x,y)), concatenate((x,y)))
+ assert_equal(numpy.concatenate((x,y)), concatenate((xm,y)))
+ assert_equal(numpy.concatenate((x,y,x)), concatenate((x,ym,x)))
+ # Concatenation along an axis
+ s = (3,4)
+ x.shape = y.shape = xm.shape = ym.shape = s
+ assert_equal(xm.mask, numpy.reshape(m1, s))
+ assert_equal(ym.mask, numpy.reshape(m2, s))
+ xmym = concatenate((xm,ym),1)
+ assert_equal(numpy.concatenate((x,y),1), xmym)
+ assert_equal(numpy.concatenate((xm.mask,ym.mask),1), xmym._mask)
+ #........................
+ def check_indexing(self):
+ "Tests conversions and indexing"
+ x1 = numpy.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
+ junk, garbage = str(x2), repr(x2)
+ assert_equal(numpy.sort(x1),sort(x2,endwith=False))
+ # tests of indexing
+ assert type(x2[1]) is type(x1[1])
+ assert x1[1] == x2[1]
+ assert x2[0] is masked
+ assert_equal(x1[2],x2[2])
+ assert_equal(x1[2:5],x2[2:5])
+ assert_equal(x1[:],x2[:])
+ assert_equal(x1[1:], x3[1:])
+ x1[2] = 9
+ x2[2] = 9
+ assert_equal(x1,x2)
+ x1[1:3] = 99
+ x2[1:3] = 99
+ assert_equal(x1,x2)
+ x2[1] = masked
+ assert_equal(x1,x2)
+ x2[1:3] = masked
+ assert_equal(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 = numpy.arange(5)*1.0
+ x2 = masked_values(x1, 3.0)
+ assert_equal(x1,x2)
+ assert allequal(array([0,0,0,1,0],MaskType), x2.mask)
+#FIXME: Well, eh, fill_value is now a property assert_equal(3.0, x2.fill_value())
+ assert_equal(3.0, x2.fill_value)
+ x1 = array([1,'hello',2,3],object)
+ x2 = numpy.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,)
+ #........................
+ def check_copy(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 = numpy.arange(5)
+ y1 = array(x1, mask=m)
+ #assert( y1._data is x1)
+ assert_equal(y1._data.__array_interface__, x1.__array_interface__)
+ assert( allequal(x1,y1.raw_data()))
+ #assert( y1.mask is m)
+ assert_equal(y1._mask.__array_interface__, m.__array_interface__)
+
+ y1a = array(y1)
+ #assert( y1a.raw_data() is y1.raw_data())
+ assert( y1a._data.__array_interface__ == y1._data.__array_interface__)
+ assert( y1a.mask is y1.mask)
+
+ y2 = array(x1, mask=m)
+ #assert( y2.raw_data() is x1)
+ assert (y2._data.__array_interface__ == x1.__array_interface__)
+ #assert( y2.mask is m)
+ assert (y2._mask.__array_interface__ == m.__array_interface__)
+ assert( y2[2] is masked)
+ y2[2] = 9
+ assert( y2[2] is not masked)
+ #assert( y2.mask is not m)
+ assert (y2._mask.__array_interface__ != m.__array_interface__)
+ assert( allequal(y2.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_equal(concatenate([x4,x4]), y4)
+ assert_equal(getmask(y4),[0,0,1,0,0,0,1,0])
+ y5 = repeat(x4, (2,2,2,2), axis=0)
+ assert_equal(y5, [0,0,1,1,2,2,3,3])
+ y6 = repeat(x4, 2, axis=0)
+ assert_equal(y5, y6)
+ y7 = x4.repeat((2,2,2,2), axis=0)
+ assert_equal(y5,y7)
+ y8 = x4.repeat(2,0)
+ assert_equal(y5,y8)
+
+ y9 = x4.copy()
+ assert_equal(y9._data, x4._data)
+ assert_equal(y9._mask, x4._mask)
+ #
+ x = masked_array([1,2,3], mask=[0,1,0])
+ # Copy is False by default
+ y = masked_array(x)
+# assert_equal(id(y._data), id(x._data))
+# assert_equal(id(y._mask), id(x._mask))
+ assert_equal(y._data.ctypes.data, x._data.ctypes.data)
+ assert_equal(y._mask.ctypes.data, x._mask.ctypes.data)
+ y = masked_array(x, copy=True)
+# assert_not_equal(id(y._data), id(x._data))
+# assert_not_equal(id(y._mask), id(x._mask))
+ assert_not_equal(y._data.ctypes.data, x._data.ctypes.data)
+ assert_not_equal(y._mask.ctypes.data, x._mask.ctypes.data)
+ #........................
+ def check_where(self):
+ "Test the where function"
+ (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
+ d = where(xm>2,xm,-9)
+ assert_equal(d, [-9.,-9.,-9.,-9., -9., 4., -9., -9., 10., -9., -9., 3.])
+ assert_equal(d._mask, xm._mask)
+ d = where(xm>2,-9,ym)
+ assert_equal(d, [5.,0.,3., 2., -1.,-9.,-9., -10., -9., 1., 0., -9.])
+ assert_equal(d._mask, [1,0,1,0,0,0,1,0,0,0,0,0])
+ d = where(xm>2, xm, masked)
+ assert_equal(d, [-9.,-9.,-9.,-9., -9., 4., -9., -9., 10., -9., -9., 3.])
+ tmp = xm._mask.copy()
+ tmp[(xm<=2).filled(True)] = True
+ assert_equal(d._mask, tmp)
+ #
+ ixm = xm.astype(int_)
+ d = where(ixm>2, ixm, masked)
+ assert_equal(d, [-9,-9,-9,-9, -9, 4, -9, -9, 10, -9, -9, 3])
+ assert_equal(d.dtype, ixm.dtype)
+ #
+ x = arange(10)
+ x[3] = masked
+ c = x >= 8
+ z = where(c , x, masked)
+ assert z.dtype is x.dtype
+ assert z[3] is masked
+ assert z[4] is masked
+ assert z[7] is masked
+ assert z[8] is not masked
+ assert z[9] is not masked
+ assert_equal(x,z)
+ #
+ z = where(c , masked, x)
+ assert z.dtype is x.dtype
+ assert z[3] is masked
+ assert z[4] is not masked
+ assert z[7] is not masked
+ assert z[8] is masked
+ assert z[9] is masked
+
+ #........................
+ def check_oddfeatures_1(self):
+ "Test of other odd features"
+ x = arange(20)
+ x = x.reshape(4,5)
+ x.flat[5] = 12
+ assert x[1,0] == 12
+ z = x + 10j * x
+ assert_equal(z.real, x)
+ assert_equal(z.imag, 10*x)
+ assert_equal((z*conjugate(z)).real, 101*x*x)
+ z.imag[...] = 0.0
+
+ x = arange(10)
+ x[3] = masked
+ assert str(x[3]) == str(masked)
+ c = x >= 8
+ assert count(where(c,masked,masked)) == 0
+ assert shape(where(c,masked,masked)) == c.shape
+ #
+ z = masked_where(c, x)
+ assert z.dtype is x.dtype
+ assert z[3] is masked
+ assert z[4] is not masked
+ assert z[7] is not masked
+ assert z[8] is masked
+ assert z[9] is masked
+ assert_equal(x,z)
+ #
+ #........................
+ def check_oddfeatures_2(self):
+ "Tests some more features."
+ x = array([1.,2.,3.,4.,5.])
+ c = array([1,1,1,0,0])
+ x[2] = masked
+ z = where(c, x, -x)
+ assert_equal(z, [1.,2.,0., -4., -5])
+ c[0] = masked
+ z = where(c, x, -x)
+ assert_equal(z, [1.,2.,0., -4., -5])
+ assert z[0] is masked
+ assert z[1] is not masked
+ assert z[2] is masked
+ #
+ x = arange(6)
+ x[5] = masked
+ y = arange(6)*10
+ y[2] = masked
+ c = array([1,1,1,0,0,0], mask=[1,0,0,0,0,0])
+ cm = c.filled(1)
+ z = where(c,x,y)
+ zm = where(cm,x,y)
+ assert_equal(z, zm)
+ assert getmask(zm) is nomask
+ assert_equal(zm, [0,1,2,30,40,50])
+ z = where(c, masked, 1)
+ assert_equal(z, [99,99,99,1,1,1])
+ z = where(c, 1, masked)
+ assert_equal(z, [99, 1, 1, 99, 99, 99])
+ #........................
+ def check_oddfeatures_3(self):
+ """Tests some generic features."""
+ atest = ones((10,10,10), dtype=float_)
+ btest = zeros(atest.shape, MaskType)
+ ctest = masked_where(btest,atest)
+ assert_equal(atest,ctest)
+ #........................
+ def check_maskingfunctions(self):
+ "Tests masking functions."
+ x = array([1.,2.,3.,4.,5.])
+ x[2] = masked
+ assert_equal(masked_where(greater(x, 2), x), masked_greater(x,2))
+ assert_equal(masked_where(greater_equal(x, 2), x), masked_greater_equal(x,2))
+ assert_equal(masked_where(less(x, 2), x), masked_less(x,2))
+ assert_equal(masked_where(less_equal(x, 2), x), masked_less_equal(x,2))
+ assert_equal(masked_where(not_equal(x, 2), x), masked_not_equal(x,2))
+ assert_equal(masked_where(equal(x, 2), x), masked_equal(x,2))
+ assert_equal(masked_where(not_equal(x,2), x), masked_not_equal(x,2))
+ assert_equal(masked_inside(range(5), 1, 3), [0, 199, 199, 199, 4])
+ assert_equal(masked_outside(range(5), 1, 3),[199,1,2,3,199])
+ assert_equal(masked_inside(array(range(5), mask=[1,0,0,0,0]), 1, 3).mask, [1,1,1,1,0])
+ assert_equal(masked_outside(array(range(5), mask=[0,1,0,0,0]), 1, 3).mask, [1,1,0,0,1])
+ assert_equal(masked_equal(array(range(5), mask=[1,0,0,0,0]), 2).mask, [1,0,1,0,0])
+ assert_equal(masked_not_equal(array([2,2,1,2,1], mask=[1,0,0,0,0]), 2).mask, [1,0,1,0,1])
+ assert_equal(masked_where([1,1,0,0,0], [1,2,3,4,5]), [99,99,3,4,5])
+ #........................
+ def check_TakeTransposeInnerOuter(self):
+ "Test of take, transpose, inner, outer products"
+ x = arange(24)
+ y = numpy.arange(24)
+ x[5:6] = masked
+ x = x.reshape(2,3,4)
+ y = y.reshape(2,3,4)
+ assert_equal(numpy.transpose(y,(2,0,1)), transpose(x,(2,0,1)))
+ assert_equal(numpy.take(y, (2,0,1), 1), take(x, (2,0,1), 1))
+ assert_equal(numpy.inner(filled(x,0),filled(y,0)),
+ inner(x, y))
+ assert_equal(numpy.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
+ #.......................
+ def check_maskedelement(self):
+ "Test of masked element"
+ x = arange(6)
+ x[1] = masked
+ assert(str(masked) == '--')
+ assert(x[1] is masked)
+ assert_equal(filled(x[1], 0), 0)
+ # don't know why these should raise an exception...
+ #self.failUnlessRaises(Exception, lambda x,y: x+y, masked, masked)
+ #self.failUnlessRaises(Exception, lambda x,y: x+y, masked, 2)
+ #self.failUnlessRaises(Exception, lambda x,y: x+y, masked, xx)
+ #self.failUnlessRaises(Exception, lambda x,y: x+y, xx, masked)
+ #........................
+ def check_scalar(self):
+ "Checks masking a scalar"
+ x = masked_array(0)
+ assert_equal(str(x), '0')
+ x = masked_array(0,mask=True)
+ assert_equal(str(x), str(masked_print_option))
+ x = masked_array(0, mask=False)
+ assert_equal(str(x), '0')
+ #........................
+ def check_usingmasked(self):
+ "Checks that there's no collapsing to masked"
+ x = masked_array([1,2])
+ y = x * masked
+ assert_equal(y.shape, x.shape)
+ assert_equal(y._mask, [True, True])
+ y = x[0] * masked
+ assert y is masked
+ y = x + masked
+ assert_equal(y.shape, x.shape)
+ assert_equal(y._mask, [True, True])
+
+ #........................
+ def check_topython(self):
+ "Tests some communication issues with Python."
+ 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]])))
+ self.failUnlessRaises(ValueError, float, array([1,1]))
+ assert numpy.isnan(float(array([1],mask=[1])))
+#TODO: Check how bool works...
+#TODO: self.failUnless(bool(array([0,1])))
+#TODO: self.failUnless(bool(array([0,0],mask=[0,1])))
+#TODO: self.failIf(bool(array([0,0])))
+#TODO: self.failIf(bool(array([0,0],mask=[0,0])))
+ #........................
+ def check_arraymethods(self):
+ "Tests some MaskedArray methods."
+ a = array([1,3,2])
+ b = array([1,3,2], mask=[1,0,1])
+ assert_equal(a.any(), a.data.any())
+ assert_equal(a.all(), a.data.all())
+ assert_equal(a.argmax(), a.data.argmax())
+ assert_equal(a.argmin(), a.data.argmin())
+ assert_equal(a.choose(0,1,2,3,4), a.data.choose(0,1,2,3,4))
+ assert_equal(a.compress([1,0,1]), a.data.compress([1,0,1]))
+ assert_equal(a.conj(), a.data.conj())
+ assert_equal(a.conjugate(), a.data.conjugate())
+ #
+ m = array([[1,2],[3,4]])
+ assert_equal(m.diagonal(), m.data.diagonal())
+ assert_equal(a.sum(), a.data.sum())
+ assert_equal(a.take([1,2]), a.data.take([1,2]))
+ assert_equal(m.transpose(), m.data.transpose())
+ #........................
+ def check_basicattributes(self):
+ "Tests some basic array attributes."
+ a = array([1,3,2])
+ b = array([1,3,2], mask=[1,0,1])
+ assert_equal(a.ndim, 1)
+ assert_equal(b.ndim, 1)
+ assert_equal(a.size, 3)
+ assert_equal(b.size, 3)
+ assert_equal(a.shape, (3,))
+ assert_equal(b.shape, (3,))
+ #........................
+ def check_single_element_subscript(self):
+ "Tests single element subscripts of Maskedarrays."
+ a = array([1,3,2])
+ b = array([1,3,2], mask=[1,0,1])
+ assert_equal(a[0].shape, ())
+ assert_equal(b[0].shape, ())
+ assert_equal(b[1].shape, ())
+ #........................
+ def check_maskcreation(self):
+ "Tests how masks are initialized at the creation of Maskedarrays."
+ data = arange(24, dtype=float_)
+ data[[3,6,15]] = masked
+ dma_1 = MaskedArray(data)
+ assert_equal(dma_1.mask, data.mask)
+ dma_2 = MaskedArray(dma_1)
+ assert_equal(dma_2.mask, dma_1.mask)
+ dma_3 = MaskedArray(dma_1, mask=[1,0,0,0]*6)
+ fail_if_equal(dma_3.mask, dma_1.mask)
+
+ def check_pickling(self):
+ "Tests pickling"
+ import cPickle
+ a = arange(10)
+ a[::3] = masked
+ a.fill_value = 999
+ a_pickled = cPickle.loads(a.dumps())
+ assert_equal(a_pickled._mask, a._mask)
+ assert_equal(a_pickled._data, a._data)
+ assert_equal(a_pickled.fill_value, 999)
+ #
+ a = array(numpy.matrix(range(10)), mask=[1,0,1,0,0]*2)
+ a_pickled = cPickle.loads(a.dumps())
+ assert_equal(a_pickled._mask, a._mask)
+ assert_equal(a_pickled, a)
+ assert(isinstance(a_pickled._data,numpy.matrix))
+ #
+ def check_fillvalue(self):
+ "Check that we don't lose the fill_value"
+ data = masked_array([1,2,3],fill_value=-999)
+ series = data[[0,2,1]]
+ assert_equal(series._fill_value, data._fill_value)
+ #
+ def check_asarray(self):
+ (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
+ xmm = asarray(xm)
+ assert_equal(xmm._data, xm._data)
+ assert_equal(xmm._mask, xm._mask)
+ #
+ def check_fix_invalid(self):
+ "Checks fix_invalid."
+ data = masked_array(numpy.sqrt([-1., 0., 1.]), mask=[0,0,1])
+ data_fixed = fix_invalid(data)
+ assert_equal(data_fixed._data, [data.fill_value, 0., 1.])
+ assert_equal(data_fixed._mask, [1., 0., 1.])
+ #
+ def check_imag_real(self):
+ xx = array([1+10j,20+2j], mask=[1,0])
+ assert_equal(xx.imag,[10,2])
+ assert_equal(xx.imag.filled(), [1e+20,2])
+ assert_equal(xx.imag.dtype, xx._data.imag.dtype)
+ assert_equal(xx.real,[1,20])
+ assert_equal(xx.real.filled(), [1e+20,20])
+ assert_equal(xx.real.dtype, xx._data.real.dtype)
+
+#...............................................................................
+
+class TestUfuncs(NumpyTestCase):
+ "Test class for the application of ufuncs on MaskedArrays."
+ def setUp(self):
+ "Base data definition."
+ self.d = (array([1.0, 0, -1, pi/2]*2, mask=[0,1]+[0]*6),
+ array([1.0, 0, -1, pi/2]*2, mask=[1,0]+[0]*6),)
+
+ def check_testUfuncRegression(self):
+ "Tests new ufuncs on MaskedArrays."
+ for f in ['sqrt', 'log', 'log10', 'exp', 'conjugate',
+ 'sin', 'cos', 'tan',
+ 'arcsin', 'arccos', 'arctan',
+ 'sinh', 'cosh', 'tanh',
+ 'arcsinh',
+ 'arccosh',
+ 'arctanh',
+ 'absolute', 'fabs', 'negative',
+ # 'nonzero', 'around',
+ 'floor', 'ceil',
+ # 'sometrue', 'alltrue',
+ 'logical_not',
+ 'add', 'subtract', 'multiply',
+ 'divide', 'true_divide', 'floor_divide',
+ 'remainder', 'fmod', 'hypot', 'arctan2',
+ 'equal', 'not_equal', 'less_equal', 'greater_equal',
+ 'less', 'greater',
+ 'logical_and', 'logical_or', 'logical_xor',
+ ]:
+ #print f
+ try:
+ uf = getattr(umath, f)
+ except AttributeError:
+ uf = getattr(fromnumeric, f)
+ mf = getattr(coremodule, f)
+ args = self.d[:uf.nin]
+ ur = uf(*args)
+ mr = mf(*args)
+ assert_equal(ur.filled(0), mr.filled(0), f)
+ assert_mask_equal(ur.mask, mr.mask)
+ #........................
+ def test_reduce(self):
+ "Tests reduce on MaskedArrays."
+ a = self.d[0]
+ assert(not alltrue(a,axis=0))
+ assert(sometrue(a,axis=0))
+ assert_equal(sum(a[:3],axis=0), 0)
+ assert_equal(product(a,axis=0), 0)
+ assert_equal(add.reduce(a), pi)
+ #........................
+ def test_minmax(self):
+ "Tests extrema on MaskedArrays."
+ a = arange(1,13).reshape(3,4)
+ amask = masked_where(a < 5,a)
+ assert_equal(amask.max(), a.max())
+ assert_equal(amask.min(), 5)
+ assert_equal(amask.max(0), a.max(0))
+ assert_equal(amask.min(0), [5,6,7,8])
+ assert(amask.max(1)[0].mask)
+ assert(amask.min(1)[0].mask)
+
+#...............................................................................
+
+class TestArrayMethods(NumpyTestCase):
+ "Test class for miscellaneous MaskedArrays methods."
+ def setUp(self):
+ "Base data definition."
+ x = numpy.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 = numpy.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))
+
+ m2 = numpy.array([1, 1, 0, 1, 0, 0,
+ 1, 1, 1, 1, 0, 1,
+ 0, 0, 1, 1, 0, 1,
+ 0, 0, 0, 1, 1, 1,
+ 1, 0, 0, 1, 1, 0,
+ 0, 0, 1, 0, 1, 1])
+ m2x = array(data=x,mask=m2)
+ m2X = array(data=X,mask=m2.reshape(X.shape))
+ m2XX = array(data=XX,mask=m2.reshape(XX.shape))
+ self.d = (x,X,XX,m,mx,mX,mXX,m2x,m2X,m2XX)
+
+ #------------------------------------------------------
+ def check_trace(self):
+ "Tests trace on MaskedArrays."
+ (x,X,XX,m,mx,mX,mXX,m2x,m2X,m2XX) = self.d
+ mXdiag = mX.diagonal()
+ assert_equal(mX.trace(), mX.diagonal().compressed().sum())
+ assert_almost_equal(mX.trace(),
+ X.trace() - sum(mXdiag.mask*X.diagonal(),axis=0))
+
+ def check_clip(self):
+ "Tests clip on MaskedArrays."
+ (x,X,XX,m,mx,mX,mXX,m2x,m2X,m2XX) = self.d
+ clipped = mx.clip(2,8)
+ assert_equal(clipped.mask,mx.mask)
+ assert_equal(clipped.data,x.clip(2,8))
+ assert_equal(clipped.data,mx.data.clip(2,8))
+
+ def check_ptp(self):
+ "Tests ptp on MaskedArrays."
+ (x,X,XX,m,mx,mX,mXX,m2x,m2X,m2XX) = self.d
+ (n,m) = X.shape
+ assert_equal(mx.ptp(),mx.compressed().ptp())
+ rows = numpy.zeros(n,numpy.float_)
+ cols = numpy.zeros(m,numpy.float_)
+ for k in range(m):
+ cols[k] = mX[:,k].compressed().ptp()
+ for k in range(n):
+ rows[k] = mX[k].compressed().ptp()
+ assert_equal(mX.ptp(0),cols)
+ assert_equal(mX.ptp(1),rows)
+
+ def check_swapaxes(self):
+ "Tests swapaxes on MaskedArrays."
+ (x,X,XX,m,mx,mX,mXX,m2x,m2X,m2XX) = self.d
+ mXswapped = mX.swapaxes(0,1)
+ assert_equal(mXswapped[-1],mX[:,-1])
+ mXXswapped = mXX.swapaxes(0,2)
+ assert_equal(mXXswapped.shape,(2,2,3,3))
+
+ def check_cumsumprod(self):
+ "Tests cumsum & cumprod on MaskedArrays."
+ (x,X,XX,m,mx,mX,mXX,m2x,m2X,m2XX) = self.d
+ mXcp = mX.cumsum(0)
+ assert_equal(mXcp.data,mX.filled(0).cumsum(0))
+ mXcp = mX.cumsum(1)
+ assert_equal(mXcp.data,mX.filled(0).cumsum(1))
+ #
+ mXcp = mX.cumprod(0)
+ assert_equal(mXcp.data,mX.filled(1).cumprod(0))
+ mXcp = mX.cumprod(1)
+ assert_equal(mXcp.data,mX.filled(1).cumprod(1))
+
+ def check_varstd(self):
+ "Tests var & std on MaskedArrays."
+ (x,X,XX,m,mx,mX,mXX,m2x,m2X,m2XX) = self.d
+ assert_almost_equal(mX.var(axis=None),mX.compressed().var())
+ assert_almost_equal(mX.std(axis=None),mX.compressed().std())
+ assert_equal(mXX.var(axis=3).shape,XX.var(axis=3).shape)
+ assert_equal(mX.var().shape,X.var().shape)
+ (mXvar0,mXvar1) = (mX.var(axis=0), mX.var(axis=1))
+ for k in range(6):
+ assert_almost_equal(mXvar1[k],mX[k].compressed().var())
+ assert_almost_equal(mXvar0[k],mX[:,k].compressed().var())
+ assert_almost_equal(numpy.sqrt(mXvar0[k]), mX[:,k].compressed().std())
+
+ def check_argmin(self):
+ "Tests argmin & argmax on MaskedArrays."
+ (x,X,XX,m,mx,mX,mXX,m2x,m2X,m2XX) = self.d
+ #
+ assert_equal(mx.argmin(),35)
+ assert_equal(mX.argmin(),35)
+ assert_equal(m2x.argmin(),4)
+ assert_equal(m2X.argmin(),4)
+ assert_equal(mx.argmax(),28)
+ assert_equal(mX.argmax(),28)
+ assert_equal(m2x.argmax(),31)
+ assert_equal(m2X.argmax(),31)
+ #
+ assert_equal(mX.argmin(0), [2,2,2,5,0,5])
+ assert_equal(m2X.argmin(0), [2,2,4,5,0,4])
+ assert_equal(mX.argmax(0), [0,5,0,5,4,0])
+ assert_equal(m2X.argmax(0), [5,5,0,5,1,0])
+ #
+ assert_equal(mX.argmin(1), [4,1,0,0,5,5,])
+ assert_equal(m2X.argmin(1), [4,4,0,0,5,3])
+ assert_equal(mX.argmax(1), [2,4,1,1,4,1])
+ assert_equal(m2X.argmax(1), [2,4,1,1,1,1])
+
+ def check_put(self):
+ "Tests put."
+ d = arange(5)
+ n = [0,0,0,1,1]
+ m = make_mask(n)
+ x = array(d, mask = m)
+ assert( x[3] is masked)
+ assert( x[4] is masked)
+ x[[1,4]] = [10,40]
+# assert( x.mask is not m)
+ assert( x[3] is masked)
+ assert( x[4] is not masked)
+ assert_equal(x, [0,10,2,-1,40])
+ #
+ x = masked_array(arange(10), mask=[1,0,0,0,0]*2)
+ i = [0,2,4,6]
+ x.put(i, [6,4,2,0])
+ assert_equal(x, asarray([6,1,4,3,2,5,0,7,8,9,]))
+ assert_equal(x.mask, [0,0,0,0,0,1,0,0,0,0])
+ x.put(i, masked_array([0,2,4,6],[1,0,1,0]))
+ assert_array_equal(x, [0,1,2,3,4,5,6,7,8,9,])
+ assert_equal(x.mask, [1,0,0,0,1,1,0,0,0,0])
+ #
+ x = masked_array(arange(10), mask=[1,0,0,0,0]*2)
+ put(x, i, [6,4,2,0])
+ assert_equal(x, asarray([6,1,4,3,2,5,0,7,8,9,]))
+ assert_equal(x.mask, [0,0,0,0,0,1,0,0,0,0])
+ put(x, i, masked_array([0,2,4,6],[1,0,1,0]))
+ assert_array_equal(x, [0,1,2,3,4,5,6,7,8,9,])
+ assert_equal(x.mask, [1,0,0,0,1,1,0,0,0,0])
+
+ def check_put_hardmask(self):
+ "Tests put on hardmask"
+ d = arange(5)
+ n = [0,0,0,1,1]
+ m = make_mask(n)
+ xh = array(d+1, mask = m, hard_mask=True, copy=True)
+ xh.put([4,2,0,1,3],[1,2,3,4,5])
+ assert_equal(xh._data, [3,4,2,4,5])
+
+ def check_take(self):
+ "Tests take"
+ x = masked_array([10,20,30,40],[0,1,0,1])
+ assert_equal(x.take([0,0,3]), masked_array([10, 10, 40], [0,0,1]) )
+ assert_equal(x.take([0,0,3]), x[[0,0,3]])
+ assert_equal(x.take([[0,1],[0,1]]),
+ masked_array([[10,20],[10,20]], [[0,1],[0,1]]) )
+ #
+ x = array([[10,20,30],[40,50,60]], mask=[[0,0,1],[1,0,0,]])
+ assert_equal(x.take([0,2], axis=1),
+ array([[10,30],[40,60]], mask=[[0,1],[1,0]]))
+ assert_equal(take(x, [0,2], axis=1),
+ array([[10,30],[40,60]], mask=[[0,1],[1,0]]))
+ #........................
+ def check_anyall(self):
+ """Checks the any/all methods/functions."""
+ x = numpy.array([[ 0.13, 0.26, 0.90],
+ [ 0.28, 0.33, 0.63],
+ [ 0.31, 0.87, 0.70]])
+ m = numpy.array([[ True, False, False],
+ [False, False, False],
+ [True, True, False]], dtype=numpy.bool_)
+ mx = masked_array(x, mask=m)
+ xbig = numpy.array([[False, False, True],
+ [False, False, True],
+ [False, True, True]], dtype=numpy.bool_)
+ mxbig = (mx > 0.5)
+ mxsmall = (mx < 0.5)
+ #
+ assert (mxbig.all()==False)
+ assert (mxbig.any()==True)
+ assert_equal(mxbig.all(0),[False, False, True])
+ assert_equal(mxbig.all(1), [False, False, True])
+ assert_equal(mxbig.any(0),[False, False, True])
+ assert_equal(mxbig.any(1), [True, True, True])
+ #
+ assert (mxsmall.all()==False)
+ assert (mxsmall.any()==True)
+ assert_equal(mxsmall.all(0), [True, True, False])
+ assert_equal(mxsmall.all(1), [False, False, False])
+ assert_equal(mxsmall.any(0), [True, True, False])
+ assert_equal(mxsmall.any(1), [True, True, False])
+ #
+ X = numpy.matrix(x)
+ mX = masked_array(X, mask=m)
+ mXbig = (mX > 0.5)
+ mXsmall = (mX < 0.5)
+ #
+ assert (mXbig.all()==False)
+ assert (mXbig.any()==True)
+ assert_equal(mXbig.all(0), numpy.matrix([False, False, True]))
+ assert_equal(mXbig.all(1), numpy.matrix([False, False, True]).T)
+ assert_equal(mXbig.any(0), numpy.matrix([False, False, True]))
+ assert_equal(mXbig.any(1), numpy.matrix([ True, True, True]).T)
+ #
+ assert (mXsmall.all()==False)
+ assert (mXsmall.any()==True)
+ assert_equal(mXsmall.all(0), numpy.matrix([True, True, False]))
+ assert_equal(mXsmall.all(1), numpy.matrix([False, False, False]).T)
+ assert_equal(mXsmall.any(0), numpy.matrix([True, True, False]))
+ assert_equal(mXsmall.any(1), numpy.matrix([True, True, False]).T)
+
+ def check_keepmask(self):
+ "Tests the keep mask flag"
+ x = masked_array([1,2,3], mask=[1,0,0])
+ mx = masked_array(x)
+ assert_equal(mx.mask, x.mask)
+ mx = masked_array(x, mask=[0,1,0], keep_mask=False)
+ assert_equal(mx.mask, [0,1,0])
+ mx = masked_array(x, mask=[0,1,0], keep_mask=True)
+ assert_equal(mx.mask, [1,1,0])
+ # We default to true
+ mx = masked_array(x, mask=[0,1,0])
+ assert_equal(mx.mask, [1,1,0])
+
+ def check_hardmask(self):
+ "Test hard_mask"
+ d = arange(5)
+ n = [0,0,0,1,1]
+ m = make_mask(n)
+ xh = array(d, mask = m, hard_mask=True)
+ # We need to copy, to avoid updating d in xh!
+ xs = array(d, mask = m, hard_mask=False, copy=True)
+ xh[[1,4]] = [10,40]
+ xs[[1,4]] = [10,40]
+ assert_equal(xh._data, [0,10,2,3,4])
+ assert_equal(xs._data, [0,10,2,3,40])
+ #assert_equal(xh.mask.ctypes.data, m.ctypes.data)
+ assert_equal(xs.mask, [0,0,0,1,0])
+ assert(xh._hardmask)
+ assert(not xs._hardmask)
+ xh[1:4] = [10,20,30]
+ xs[1:4] = [10,20,30]
+ assert_equal(xh._data, [0,10,20,3,4])
+ assert_equal(xs._data, [0,10,20,30,40])
+ #assert_equal(xh.mask.ctypes.data, m.ctypes.data)
+ assert_equal(xs.mask, nomask)
+ xh[0] = masked
+ xs[0] = masked
+ assert_equal(xh.mask, [1,0,0,1,1])
+ assert_equal(xs.mask, [1,0,0,0,0])
+ xh[:] = 1
+ xs[:] = 1
+ assert_equal(xh._data, [0,1,1,3,4])
+ assert_equal(xs._data, [1,1,1,1,1])
+ assert_equal(xh.mask, [1,0,0,1,1])
+ assert_equal(xs.mask, nomask)
+ # Switch to soft mask
+ xh.soften_mask()
+ xh[:] = arange(5)
+ assert_equal(xh._data, [0,1,2,3,4])
+ assert_equal(xh.mask, nomask)
+ # Switch back to hard mask
+ xh.harden_mask()
+ xh[xh<3] = masked
+ assert_equal(xh._data, [0,1,2,3,4])
+ assert_equal(xh._mask, [1,1,1,0,0])
+ xh[filled(xh>1,False)] = 5
+ assert_equal(xh._data, [0,1,2,5,5])
+ assert_equal(xh._mask, [1,1,1,0,0])
+ #
+ xh = array([[1,2],[3,4]], mask = [[1,0],[0,0]], hard_mask=True)
+ xh[0] = 0
+ assert_equal(xh._data, [[1,0],[3,4]])
+ assert_equal(xh._mask, [[1,0],[0,0]])
+ xh[-1,-1] = 5
+ assert_equal(xh._data, [[1,0],[3,5]])
+ assert_equal(xh._mask, [[1,0],[0,0]])
+ xh[filled(xh<5,False)] = 2
+ assert_equal(xh._data, [[1,2],[2,5]])
+ assert_equal(xh._mask, [[1,0],[0,0]])
+ #
+ "Another test of hardmask"
+ d = arange(5)
+ n = [0,0,0,1,1]
+ m = make_mask(n)
+ xh = array(d, mask = m, hard_mask=True)
+ xh[4:5] = 999
+ #assert_equal(xh.mask.ctypes.data, m.ctypes.data)
+ xh[0:1] = 999
+ assert_equal(xh._data,[999,1,2,3,4])
+
+ def check_smallmask(self):
+ "Checks the behaviour of _smallmask"
+ a = arange(10)
+ a[1] = masked
+ a[1] = 1
+ assert_equal(a._mask, nomask)
+ a = arange(10)
+ a._smallmask = False
+ a[1] = masked
+ a[1] = 1
+ assert_equal(a._mask, zeros(10))
+
+
+ def check_sort(self):
+ "Test sort"
+ x = array([1,4,2,3],mask=[0,1,0,0],dtype=numpy.uint8)
+ #
+ sortedx = sort(x)
+ assert_equal(sortedx._data,[1,2,3,4])
+ assert_equal(sortedx._mask,[0,0,0,1])
+ #
+ sortedx = sort(x, endwith=False)
+ assert_equal(sortedx._data, [4,1,2,3])
+ assert_equal(sortedx._mask, [1,0,0,0])
+ #
+ x.sort()
+ assert_equal(x._data,[1,2,3,4])
+ assert_equal(x._mask,[0,0,0,1])
+ #
+ x = array([1,4,2,3],mask=[0,1,0,0],dtype=numpy.uint8)
+ x.sort(endwith=False)
+ assert_equal(x._data, [4,1,2,3])
+ assert_equal(x._mask, [1,0,0,0])
+ #
+ x = [1,4,2,3]
+ sortedx = sort(x)
+ assert(not isinstance(sorted, MaskedArray))
+ #
+ x = array([0,1,-1,-2,2], mask=nomask, dtype=numpy.int8)
+ sortedx = sort(x, endwith=False)
+ assert_equal(sortedx._data, [-2,-1,0,1,2])
+ x = array([0,1,-1,-2,2], mask=[0,1,0,0,1], dtype=numpy.int8)
+ sortedx = sort(x, endwith=False)
+ assert_equal(sortedx._data, [1,2,-2,-1,0])
+ assert_equal(sortedx._mask, [1,1,0,0,0])
+
+ def check_sort_2d(self):
+ "Check sort of 2D array."
+ # 2D array w/o mask
+ a = masked_array([[8,4,1],[2,0,9]])
+ a.sort(0)
+ assert_equal(a, [[2,0,1],[8,4,9]])
+ a = masked_array([[8,4,1],[2,0,9]])
+ a.sort(1)
+ assert_equal(a, [[1,4,8],[0,2,9]])
+ # 2D array w/mask
+ a = masked_array([[8,4,1],[2,0,9]], mask=[[1,0,0],[0,0,1]])
+ a.sort(0)
+ assert_equal(a, [[2,0,1],[8,4,9]])
+ assert_equal(a._mask, [[0,0,0],[1,0,1]])
+ a = masked_array([[8,4,1],[2,0,9]], mask=[[1,0,0],[0,0,1]])
+ a.sort(1)
+ assert_equal(a, [[1,4,8],[0,2,9]])
+ assert_equal(a._mask, [[0,0,1],[0,0,1]])
+ # 3D
+ a = masked_array([[[7, 8, 9],[4, 5, 6],[1, 2, 3]],
+ [[1, 2, 3],[7, 8, 9],[4, 5, 6]],
+ [[7, 8, 9],[1, 2, 3],[4, 5, 6]],
+ [[4, 5, 6],[1, 2, 3],[7, 8, 9]]])
+ a[a%4==0] = masked
+ am = a.copy()
+ an = a.filled(99)
+ am.sort(0)
+ an.sort(0)
+ assert_equal(am, an)
+ am = a.copy()
+ an = a.filled(99)
+ am.sort(1)
+ an.sort(1)
+ assert_equal(am, an)
+ am = a.copy()
+ an = a.filled(99)
+ am.sort(2)
+ an.sort(2)
+ assert_equal(am, an)
+
+
+ def check_ravel(self):
+ "Tests ravel"
+ a = array([[1,2,3,4,5]], mask=[[0,1,0,0,0]])
+ aravel = a.ravel()
+ assert_equal(a._mask.shape, a.shape)
+ a = array([0,0], mask=[1,1])
+ aravel = a.ravel()
+ assert_equal(a._mask.shape, a.shape)
+ a = array(numpy.matrix([1,2,3,4,5]), mask=[[0,1,0,0,0]])
+ aravel = a.ravel()
+ assert_equal(a.shape,(1,5))
+ assert_equal(a._mask.shape, a.shape)
+ # Checs that small_mask is preserved
+ a = array([1,2,3,4],mask=[0,0,0,0],shrink=False)
+ assert_equal(a.ravel()._mask, [0,0,0,0])
+
+ def check_reshape(self):
+ "Tests reshape"
+ x = arange(4)
+ x[0] = masked
+ y = x.reshape(2,2)
+ assert_equal(y.shape, (2,2,))
+ assert_equal(y._mask.shape, (2,2,))
+ assert_equal(x.shape, (4,))
+ assert_equal(x._mask.shape, (4,))
+
+ def check_compressed(self):
+ "Tests compressed"
+ a = array([1,2,3,4],mask=[0,0,0,0])
+ b = a.compressed()
+ assert_equal(b, a)
+ assert_equal(b._mask, nomask)
+ a[0] = masked
+ b = a.compressed()
+ assert_equal(b._data, [2,3,4])
+ assert_equal(b._mask, nomask)
+
+ def check_tolist(self):
+ "Tests to list"
+ x = array(numpy.arange(12))
+ x[[1,-2]] = masked
+ xlist = x.tolist()
+ assert(xlist[1] is None)
+ assert(xlist[-2] is None)
+ #
+ x.shape = (3,4)
+ xlist = x.tolist()
+ #
+ assert_equal(xlist[0],[0,None,2,3])
+ assert_equal(xlist[1],[4,5,6,7])
+ assert_equal(xlist[2],[8,9,None,11])
+
+ def check_squeeze(self):
+ "Check squeeze"
+ data = masked_array([[1,2,3]])
+ assert_equal(data.squeeze(), [1,2,3])
+ data = masked_array([[1,2,3]], mask=[[1,1,1]])
+ assert_equal(data.squeeze(), [1,2,3])
+ assert_equal(data.squeeze()._mask, [1,1,1])
+ data = masked_array([[1]], mask=True)
+ assert(data.squeeze() is masked)
+
+#..............................................................................
+
+###############################################################################
+#------------------------------------------------------------------------------
+if __name__ == "__main__":
+ NumpyTest().run()
diff --git a/numpy/ma/tests/test_extras.py b/numpy/ma/tests/test_extras.py
new file mode 100644
index 000000000..5a52aeeee
--- /dev/null
+++ b/numpy/ma/tests/test_extras.py
@@ -0,0 +1,331 @@
+# pylint: disable-msg=W0611, W0612, W0511
+"""Tests suite for MaskedArray.
+Adapted from the original test_ma by Pierre Gerard-Marchant
+
+:author: Pierre Gerard-Marchant
+:contact: pierregm_at_uga_dot_edu
+:version: $Id: test_extras.py 3473 2007-10-29 15:18:13Z jarrod.millman $
+"""
+__author__ = "Pierre GF Gerard-Marchant ($Author: jarrod.millman $)"
+__version__ = '1.0'
+__revision__ = "$Revision: 3473 $"
+__date__ = '$Date: 2007-10-29 17:18:13 +0200 (Mon, 29 Oct 2007) $'
+
+import numpy as N
+from numpy.testing import NumpyTest, NumpyTestCase
+from numpy.testing.utils import build_err_msg
+
+import numpy.ma.testutils
+from numpy.ma.testutils import *
+
+import numpy.ma.core
+from numpy.ma.core import *
+import numpy.ma.extras
+from numpy.ma.extras import *
+
+class TestAverage(NumpyTestCase):
+ "Several tests of average. Why so many ? Good point..."
+ def check_testAverage1(self):
+ "Test of average."
+ ott = array([0.,1.,2.,3.], mask=[1,0,0,0])
+ assert_equal(2.0, average(ott,axis=0))
+ assert_equal(2.0, average(ott, weights=[1., 1., 2., 1.]))
+ result, wts = average(ott, weights=[1.,1.,2.,1.], returned=1)
+ assert_equal(2.0, result)
+ assert(wts == 4.0)
+ ott[:] = masked
+ assert_equal(average(ott,axis=0).mask, [True])
+ ott = array([0.,1.,2.,3.], mask=[1,0,0,0])
+ ott = ott.reshape(2,2)
+ ott[:,1] = masked
+ assert_equal(average(ott,axis=0), [2.0, 0.0])
+ assert_equal(average(ott,axis=1).mask[0], [True])
+ assert_equal([2.,0.], average(ott, axis=0))
+ result, wts = average(ott, axis=0, returned=1)
+ assert_equal(wts, [1., 0.])
+
+ def check_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, dtype=float_)
+ assert_equal(average(x, axis=0), 2.5)
+ assert_equal(average(x, axis=0, weights=w1), 2.5)
+ y = array([arange(6, dtype=float_), 2.0*arange(6)])
+ assert_equal(average(y, None), N.add.reduce(N.arange(6))*3./12.)
+ assert_equal(average(y, axis=0), N.arange(6) * 3./2.)
+ assert_equal(average(y, axis=1), [average(x,axis=0), average(x,axis=0) * 2.0])
+ assert_equal(average(y, None, weights=w2), 20./6.)
+ assert_equal(average(y, axis=0, weights=w2), [0.,1.,2.,3.,4.,10.])
+ assert_equal(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_equal(average(masked_array(x, m1),axis=0), 2.5)
+ assert_equal(average(masked_array(x, m2),axis=0), 2.5)
+ assert_equal(average(masked_array(x, m4),axis=0).mask, [True])
+ 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_equal(average(z, None), 20./6.)
+ assert_equal(average(z, axis=0), [0.,1.,99.,99.,4.0, 7.5])
+ assert_equal(average(z, axis=1), [2.5, 5.0])
+ assert_equal(average(z,axis=0, weights=w2), [0.,1., 99., 99., 4.0, 10.0])
+
+ def check_testAverage3(self):
+ "Yet more tests of average!"
+ 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_equal(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_equal(a2da, [0.5, 3.0])
+ a2dma = average(a2dm, axis=0)
+ assert_equal(a2dma, [1.0, 3.0])
+ a2dma = average(a2dm, axis=None)
+ assert_equal(a2dma, 7./3.)
+ a2dma = average(a2dm, axis=1)
+ assert_equal(a2dma, [1.5, 4.0])
+
+class TestConcatenator(NumpyTestCase):
+ "Tests for mr_, the equivalent of r_ for masked arrays."
+ def check_1d(self):
+ "Tests mr_ on 1D arrays."
+ assert_array_equal(mr_[1,2,3,4,5,6],array([1,2,3,4,5,6]))
+ b = ones(5)
+ m = [1,0,0,0,0]
+ d = masked_array(b,mask=m)
+ c = mr_[d,0,0,d]
+ assert(isinstance(c,MaskedArray) or isinstance(c,core.MaskedArray))
+ assert_array_equal(c,[1,1,1,1,1,0,0,1,1,1,1,1])
+ assert_array_equal(c.mask, mr_[m,0,0,m])
+
+ def check_2d(self):
+ "Tests mr_ on 2D arrays."
+ a_1 = rand(5,5)
+ a_2 = rand(5,5)
+ m_1 = N.round_(rand(5,5),0)
+ m_2 = N.round_(rand(5,5),0)
+ b_1 = masked_array(a_1,mask=m_1)
+ b_2 = masked_array(a_2,mask=m_2)
+ d = mr_['1',b_1,b_2] # append columns
+ assert(d.shape == (5,10))
+ assert_array_equal(d[:,:5],b_1)
+ assert_array_equal(d[:,5:],b_2)
+ assert_array_equal(d.mask, N.r_['1',m_1,m_2])
+ d = mr_[b_1,b_2]
+ assert(d.shape == (10,5))
+ assert_array_equal(d[:5,:],b_1)
+ assert_array_equal(d[5:,:],b_2)
+ assert_array_equal(d.mask, N.r_[m_1,m_2])
+
+class TestNotMasked(NumpyTestCase):
+ "Tests notmasked_edges and notmasked_contiguous."
+ def check_edges(self):
+ "Tests unmasked_edges"
+ a = masked_array(N.arange(24).reshape(3,8),
+ mask=[[0,0,0,0,1,1,1,0],
+ [1,1,1,1,1,1,1,1],
+ [0,0,0,0,0,0,1,0],])
+ #
+ assert_equal(notmasked_edges(a, None), [0,23])
+ #
+ tmp = notmasked_edges(a, 0)
+ assert_equal(tmp[0], (array([0,0,0,0,2,2,0]), array([0,1,2,3,4,5,7])))
+ assert_equal(tmp[1], (array([2,2,2,2,2,2,2]), array([0,1,2,3,4,5,7])))
+ #
+ tmp = notmasked_edges(a, 1)
+ assert_equal(tmp[0], (array([0,2,]), array([0,0])))
+ assert_equal(tmp[1], (array([0,2,]), array([7,7])))
+
+ def check_contiguous(self):
+ "Tests notmasked_contiguous"
+ a = masked_array(N.arange(24).reshape(3,8),
+ mask=[[0,0,0,0,1,1,1,1],
+ [1,1,1,1,1,1,1,1],
+ [0,0,0,0,0,0,1,0],])
+ tmp = notmasked_contiguous(a, None)
+ assert_equal(tmp[-1], slice(23,23,None))
+ assert_equal(tmp[-2], slice(16,21,None))
+ assert_equal(tmp[-3], slice(0,3,None))
+ #
+ tmp = notmasked_contiguous(a, 0)
+ assert(len(tmp[-1]) == 1)
+ assert(tmp[-2] is None)
+ assert_equal(tmp[-3],tmp[-1])
+ assert(len(tmp[0]) == 2)
+ #
+ tmp = notmasked_contiguous(a, 1)
+ assert_equal(tmp[0][-1], slice(0,3,None))
+ assert(tmp[1] is None)
+ assert_equal(tmp[2][-1], slice(7,7,None))
+ assert_equal(tmp[2][-2], slice(0,5,None))
+
+class Test2DFunctions(NumpyTestCase):
+ "Tests 2D functions"
+ def check_compress2d(self):
+ "Tests compress2d"
+ x = array(N.arange(9).reshape(3,3), mask=[[1,0,0],[0,0,0],[0,0,0]])
+ assert_equal(compress_rowcols(x), [[4,5],[7,8]] )
+ assert_equal(compress_rowcols(x,0), [[3,4,5],[6,7,8]] )
+ assert_equal(compress_rowcols(x,1), [[1,2],[4,5],[7,8]] )
+ x = array(x._data, mask=[[0,0,0],[0,1,0],[0,0,0]])
+ assert_equal(compress_rowcols(x), [[0,2],[6,8]] )
+ assert_equal(compress_rowcols(x,0), [[0,1,2],[6,7,8]] )
+ assert_equal(compress_rowcols(x,1), [[0,2],[3,5],[6,8]] )
+ x = array(x._data, mask=[[1,0,0],[0,1,0],[0,0,0]])
+ assert_equal(compress_rowcols(x), [[8]] )
+ assert_equal(compress_rowcols(x,0), [[6,7,8]] )
+ assert_equal(compress_rowcols(x,1,), [[2],[5],[8]] )
+ x = array(x._data, mask=[[1,0,0],[0,1,0],[0,0,1]])
+ assert_equal(compress_rowcols(x).size, 0 )
+ assert_equal(compress_rowcols(x,0).size, 0 )
+ assert_equal(compress_rowcols(x,1).size, 0 )
+ #
+ def check_mask_rowcols(self):
+ "Tests mask_rowcols."
+ x = array(N.arange(9).reshape(3,3), mask=[[1,0,0],[0,0,0],[0,0,0]])
+ assert_equal(mask_rowcols(x).mask, [[1,1,1],[1,0,0],[1,0,0]] )
+ assert_equal(mask_rowcols(x,0).mask, [[1,1,1],[0,0,0],[0,0,0]] )
+ assert_equal(mask_rowcols(x,1).mask, [[1,0,0],[1,0,0],[1,0,0]] )
+ x = array(x._data, mask=[[0,0,0],[0,1,0],[0,0,0]])
+ assert_equal(mask_rowcols(x).mask, [[0,1,0],[1,1,1],[0,1,0]] )
+ assert_equal(mask_rowcols(x,0).mask, [[0,0,0],[1,1,1],[0,0,0]] )
+ assert_equal(mask_rowcols(x,1).mask, [[0,1,0],[0,1,0],[0,1,0]] )
+ x = array(x._data, mask=[[1,0,0],[0,1,0],[0,0,0]])
+ assert_equal(mask_rowcols(x).mask, [[1,1,1],[1,1,1],[1,1,0]] )
+ assert_equal(mask_rowcols(x,0).mask, [[1,1,1],[1,1,1],[0,0,0]] )
+ assert_equal(mask_rowcols(x,1,).mask, [[1,1,0],[1,1,0],[1,1,0]] )
+ x = array(x._data, mask=[[1,0,0],[0,1,0],[0,0,1]])
+ assert(mask_rowcols(x).all())
+ assert(mask_rowcols(x,0).all())
+ assert(mask_rowcols(x,1).all())
+ #
+ def test_dot(self):
+ "Tests dot product"
+ n = N.arange(1,7)
+ #
+ m = [1,0,0,0,0,0]
+ a = masked_array(n, mask=m).reshape(2,3)
+ b = masked_array(n, mask=m).reshape(3,2)
+ c = dot(a,b,True)
+ assert_equal(c.mask, [[1,1],[1,0]])
+ c = dot(b,a,True)
+ assert_equal(c.mask, [[1,1,1],[1,0,0],[1,0,0]])
+ c = dot(a,b,False)
+ assert_equal(c, N.dot(a.filled(0), b.filled(0)))
+ c = dot(b,a,False)
+ assert_equal(c, N.dot(b.filled(0), a.filled(0)))
+ #
+ m = [0,0,0,0,0,1]
+ a = masked_array(n, mask=m).reshape(2,3)
+ b = masked_array(n, mask=m).reshape(3,2)
+ c = dot(a,b,True)
+ assert_equal(c.mask,[[0,1],[1,1]])
+ c = dot(b,a,True)
+ assert_equal(c.mask, [[0,0,1],[0,0,1],[1,1,1]])
+ c = dot(a,b,False)
+ assert_equal(c, N.dot(a.filled(0), b.filled(0)))
+ assert_equal(c, dot(a,b))
+ c = dot(b,a,False)
+ assert_equal(c, N.dot(b.filled(0), a.filled(0)))
+ #
+ m = [0,0,0,0,0,0]
+ a = masked_array(n, mask=m).reshape(2,3)
+ b = masked_array(n, mask=m).reshape(3,2)
+ c = dot(a,b)
+ assert_equal(c.mask,nomask)
+ c = dot(b,a)
+ assert_equal(c.mask,nomask)
+ #
+ a = masked_array(n, mask=[1,0,0,0,0,0]).reshape(2,3)
+ b = masked_array(n, mask=[0,0,0,0,0,0]).reshape(3,2)
+ c = dot(a,b,True)
+ assert_equal(c.mask,[[1,1],[0,0]])
+ c = dot(a,b,False)
+ assert_equal(c, N.dot(a.filled(0),b.filled(0)))
+ c = dot(b,a,True)
+ assert_equal(c.mask,[[1,0,0],[1,0,0],[1,0,0]])
+ c = dot(b,a,False)
+ assert_equal(c, N.dot(b.filled(0),a.filled(0)))
+ #
+ a = masked_array(n, mask=[0,0,0,0,0,1]).reshape(2,3)
+ b = masked_array(n, mask=[0,0,0,0,0,0]).reshape(3,2)
+ c = dot(a,b,True)
+ assert_equal(c.mask,[[0,0],[1,1]])
+ c = dot(a,b)
+ assert_equal(c, N.dot(a.filled(0),b.filled(0)))
+ c = dot(b,a,True)
+ assert_equal(c.mask,[[0,0,1],[0,0,1],[0,0,1]])
+ c = dot(b,a,False)
+ assert_equal(c, N.dot(b.filled(0), a.filled(0)))
+ #
+ a = masked_array(n, mask=[0,0,0,0,0,1]).reshape(2,3)
+ b = masked_array(n, mask=[0,0,1,0,0,0]).reshape(3,2)
+ c = dot(a,b,True)
+ assert_equal(c.mask,[[1,0],[1,1]])
+ c = dot(a,b,False)
+ assert_equal(c, N.dot(a.filled(0),b.filled(0)))
+ c = dot(b,a,True)
+ assert_equal(c.mask,[[0,0,1],[1,1,1],[0,0,1]])
+ c = dot(b,a,False)
+ assert_equal(c, N.dot(b.filled(0),a.filled(0)))
+
+ def test_mediff1d(self):
+ "Tests mediff1d"
+ x = masked_array(N.arange(5), mask=[1,0,0,0,1])
+ difx_d = (x._data[1:]-x._data[:-1])
+ difx_m = (x._mask[1:]-x._mask[:-1])
+ dx = mediff1d(x)
+ assert_equal(dx._data, difx_d)
+ assert_equal(dx._mask, difx_m)
+ #
+ dx = mediff1d(x, to_begin=masked)
+ assert_equal(dx._data, N.r_[0,difx_d])
+ assert_equal(dx._mask, N.r_[1,difx_m])
+ dx = mediff1d(x, to_begin=[1,2,3])
+ assert_equal(dx._data, N.r_[[1,2,3],difx_d])
+ assert_equal(dx._mask, N.r_[[0,0,0],difx_m])
+ #
+ dx = mediff1d(x, to_end=masked)
+ assert_equal(dx._data, N.r_[difx_d,0])
+ assert_equal(dx._mask, N.r_[difx_m,1])
+ dx = mediff1d(x, to_end=[1,2,3])
+ assert_equal(dx._data, N.r_[difx_d,[1,2,3]])
+ assert_equal(dx._mask, N.r_[difx_m,[0,0,0]])
+ #
+ dx = mediff1d(x, to_end=masked, to_begin=masked)
+ assert_equal(dx._data, N.r_[0,difx_d,0])
+ assert_equal(dx._mask, N.r_[1,difx_m,1])
+ dx = mediff1d(x, to_end=[1,2,3], to_begin=masked)
+ assert_equal(dx._data, N.r_[0,difx_d,[1,2,3]])
+ assert_equal(dx._mask, N.r_[1,difx_m,[0,0,0]])
+ #
+ dx = mediff1d(x._data, to_end=masked, to_begin=masked)
+ assert_equal(dx._data, N.r_[0,difx_d,0])
+ assert_equal(dx._mask, N.r_[1,0,0,0,0,1])
+
+class TestApplyAlongAxis(NumpyTestCase):
+ "Tests 2D functions"
+ def check_3d(self):
+ a = arange(12.).reshape(2,2,3)
+ def myfunc(b):
+ return b[1]
+ xa = apply_along_axis(myfunc,2,a)
+ assert_equal(xa,[[1,4],[7,10]])
+
+###############################################################################
+#------------------------------------------------------------------------------
+if __name__ == "__main__":
+ NumpyTest().run()
diff --git a/numpy/ma/tests/test_morestats.py b/numpy/ma/tests/test_morestats.py
new file mode 100644
index 000000000..933e974da
--- /dev/null
+++ b/numpy/ma/tests/test_morestats.py
@@ -0,0 +1,114 @@
+# pylint: disable-msg=W0611, W0612, W0511,R0201
+"""Tests suite for maskedArray statistics.
+
+:author: Pierre Gerard-Marchant
+:contact: pierregm_at_uga_dot_edu
+:version: $Id: test_morestats.py 317 2007-10-04 19:31:14Z backtopop $
+"""
+__author__ = "Pierre GF Gerard-Marchant ($Author: backtopop $)"
+__version__ = '1.0'
+__revision__ = "$Revision: 317 $"
+__date__ = '$Date: 2007-10-04 15:31:14 -0400 (Thu, 04 Oct 2007) $'
+
+import numpy
+
+import numpy.ma
+from numpy.ma import masked, masked_array
+
+import numpy.ma.mstats
+from numpy.ma.mstats import *
+import numpy.ma.morestats
+from numpy.ma.morestats import *
+
+import numpy.ma.testutils
+from numpy.ma.testutils import *
+
+
+class TestMisc(NumpyTestCase):
+ #
+ def __init__(self, *args, **kwargs):
+ NumpyTestCase.__init__(self, *args, **kwargs)
+ #
+ def test_mjci(self):
+ "Tests the Marits-Jarrett estimator"
+ data = masked_array([ 77, 87, 88,114,151,210,219,246,253,262,
+ 296,299,306,376,428,515,666,1310,2611])
+ assert_almost_equal(mjci(data),[55.76819,45.84028,198.8788],5)
+ #
+ def test_trimmedmeanci(self):
+ "Tests the confidence intervals of the trimmed mean."
+ data = masked_array([545,555,558,572,575,576,578,580,
+ 594,605,635,651,653,661,666])
+ assert_almost_equal(trimmed_mean(data,0.2), 596.2, 1)
+ assert_equal(numpy.round(trimmed_mean_ci(data,0.2),1), [561.8, 630.6])
+
+#..............................................................................
+class TestRanking(NumpyTestCase):
+ #
+ def __init__(self, *args, **kwargs):
+ NumpyTestCase.__init__(self, *args, **kwargs)
+ #
+ def test_ranking(self):
+ x = masked_array([0,1,1,1,2,3,4,5,5,6,])
+ assert_almost_equal(rank_data(x),[1,3,3,3,5,6,7,8.5,8.5,10])
+ x[[3,4]] = masked
+ assert_almost_equal(rank_data(x),[1,2.5,2.5,0,0,4,5,6.5,6.5,8])
+ assert_almost_equal(rank_data(x,use_missing=True),
+ [1,2.5,2.5,4.5,4.5,4,5,6.5,6.5,8])
+ x = masked_array([0,1,5,1,2,4,3,5,1,6,])
+ assert_almost_equal(rank_data(x),[1,3,8.5,3,5,7,6,8.5,3,10])
+ x = masked_array([[0,1,1,1,2], [3,4,5,5,6,]])
+ assert_almost_equal(rank_data(x),[[1,3,3,3,5],[6,7,8.5,8.5,10]])
+ assert_almost_equal(rank_data(x,axis=1),[[1,3,3,3,5],[1,2,3.5,3.5,5]])
+ assert_almost_equal(rank_data(x,axis=0),[[1,1,1,1,1],[2,2,2,2,2,]])
+
+#..............................................................................
+class TestQuantiles(NumpyTestCase):
+ #
+ def __init__(self, *args, **kwargs):
+ NumpyTestCase.__init__(self, *args, **kwargs)
+ #
+ def test_hdquantiles(self):
+ data = [0.706560797,0.727229578,0.990399276,0.927065621,0.158953014,
+ 0.887764025,0.239407086,0.349638551,0.972791145,0.149789972,
+ 0.936947700,0.132359948,0.046041972,0.641675031,0.945530547,
+ 0.224218684,0.771450991,0.820257774,0.336458052,0.589113496,
+ 0.509736129,0.696838829,0.491323573,0.622767425,0.775189248,
+ 0.641461450,0.118455200,0.773029450,0.319280007,0.752229111,
+ 0.047841438,0.466295911,0.583850781,0.840581845,0.550086491,
+ 0.466470062,0.504765074,0.226855960,0.362641207,0.891620942,
+ 0.127898691,0.490094097,0.044882048,0.041441695,0.317976349,
+ 0.504135618,0.567353033,0.434617473,0.636243375,0.231803616,
+ 0.230154113,0.160011327,0.819464108,0.854706985,0.438809221,
+ 0.487427267,0.786907310,0.408367937,0.405534192,0.250444460,
+ 0.995309248,0.144389588,0.739947527,0.953543606,0.680051621,
+ 0.388382017,0.863530727,0.006514031,0.118007779,0.924024803,
+ 0.384236354,0.893687694,0.626534881,0.473051932,0.750134705,
+ 0.241843555,0.432947602,0.689538104,0.136934797,0.150206859,
+ 0.474335206,0.907775349,0.525869295,0.189184225,0.854284286,
+ 0.831089744,0.251637345,0.587038213,0.254475554,0.237781276,
+ 0.827928620,0.480283781,0.594514455,0.213641488,0.024194386,
+ 0.536668589,0.699497811,0.892804071,0.093835427,0.731107772]
+ #
+ assert_almost_equal(hdquantiles(data,[0., 1.]),
+ [0.006514031, 0.995309248])
+ hdq = hdquantiles(data,[0.25, 0.5, 0.75])
+ assert_almost_equal(hdq, [0.253210762, 0.512847491, 0.762232442,])
+ hdq = hdquantiles_sd(data,[0.25, 0.5, 0.75])
+ assert_almost_equal(hdq, [0.03786954, 0.03805389, 0.03800152,], 4)
+ #
+ data = numpy.array(data).reshape(10,10)
+ hdq = hdquantiles(data,[0.25,0.5,0.75],axis=0)
+ assert_almost_equal(hdq[:,0], hdquantiles(data[:,0],[0.25,0.5,0.75]))
+ assert_almost_equal(hdq[:,-1], hdquantiles(data[:,-1],[0.25,0.5,0.75]))
+ hdq = hdquantiles(data,[0.25,0.5,0.75],axis=0,var=True)
+ assert_almost_equal(hdq[...,0],
+ hdquantiles(data[:,0],[0.25,0.5,0.75],var=True))
+ assert_almost_equal(hdq[...,-1],
+ hdquantiles(data[:,-1],[0.25,0.5,0.75], var=True))
+
+
+###############################################################################
+#------------------------------------------------------------------------------
+if __name__ == "__main__":
+ NumpyTest().run()
diff --git a/numpy/ma/tests/test_mrecords.py b/numpy/ma/tests/test_mrecords.py
new file mode 100644
index 000000000..1d7d5a966
--- /dev/null
+++ b/numpy/ma/tests/test_mrecords.py
@@ -0,0 +1,181 @@
+# pylint: disable-msg=W0611, W0612, W0511,R0201
+"""Tests suite for mrecarray.
+
+:author: Pierre Gerard-Marchant
+:contact: pierregm_at_uga_dot_edu
+:version: $Id: test_mrecords.py 3473 2007-10-29 15:18:13Z jarrod.millman $
+"""
+__author__ = "Pierre GF Gerard-Marchant ($Author: jarrod.millman $)"
+__version__ = '1.0'
+__revision__ = "$Revision: 3473 $"
+__date__ = '$Date: 2007-10-29 17:18:13 +0200 (Mon, 29 Oct 2007) $'
+
+import types
+
+import numpy as N
+import numpy.core.fromnumeric as fromnumeric
+from numpy.testing import NumpyTest, NumpyTestCase
+from numpy.testing.utils import build_err_msg
+
+import numpy.ma.testutils
+from numpy.ma.testutils import *
+
+import numpy.ma
+from numpy.ma import masked_array, masked, nomask
+
+#import numpy.ma.mrecords
+#from numpy.ma.mrecords import mrecarray, fromarrays, fromtextfile, fromrecords
+
+import numpy.ma.mrecords
+from numpy.ma.mrecords import MaskedRecords, \
+ fromarrays, fromtextfile, fromrecords, addfield
+
+#..............................................................................
+class TestMRecords(NumpyTestCase):
+ "Base test class for MaskedArrays."
+ def __init__(self, *args, **kwds):
+ NumpyTestCase.__init__(self, *args, **kwds)
+ self.setup()
+
+ def setup(self):
+ "Generic setup"
+ d = N.arange(5)
+ m = numpy.ma.make_mask([1,0,0,1,1])
+ base_d = N.r_[d,d[::-1]].reshape(2,-1).T
+ base_m = N.r_[[m, m[::-1]]].T
+ base = masked_array(base_d, mask=base_m)
+ mrecord = fromarrays(base.T, dtype=[('a',N.float_),('b',N.float_)])
+ self.data = [d, m, mrecord]
+
+ def test_get(self):
+ "Tests fields retrieval"
+ [d, m, mrec] = self.data
+ mrec = mrec.copy()
+ assert_equal(mrec.a, masked_array(d,mask=m))
+ assert_equal(mrec.b, masked_array(d[::-1],mask=m[::-1]))
+ assert((mrec._fieldmask == N.core.records.fromarrays([m, m[::-1]], dtype=mrec._fieldmask.dtype)).all())
+ assert_equal(mrec._mask, N.r_[[m,m[::-1]]].all(0))
+ assert_equal(mrec.a[1], mrec[1].a)
+ #
+ assert(isinstance(mrec[:2], MaskedRecords))
+ assert_equal(mrec[:2]['a'], d[:2])
+
+ def test_set(self):
+ "Tests setting fields/attributes."
+ [d, m, mrecord] = self.data
+ mrecord.a._data[:] = 5
+ assert_equal(mrecord['a']._data, [5,5,5,5,5])
+ mrecord.a = 1
+ assert_equal(mrecord['a']._data, [1]*5)
+ assert_equal(getmaskarray(mrecord['a']), [0]*5)
+ mrecord.b = masked
+ assert_equal(mrecord.b.mask, [1]*5)
+ assert_equal(getmaskarray(mrecord['b']), [1]*5)
+ mrecord._mask = masked
+ assert_equal(getmaskarray(mrecord['b']), [1]*5)
+ assert_equal(mrecord['a']._mask, mrecord['b']._mask)
+ mrecord._mask = nomask
+ assert_equal(getmaskarray(mrecord['b']), [0]*5)
+ assert_equal(mrecord['a']._mask, mrecord['b']._mask)
+ #
+ def test_setfields(self):
+ "Tests setting fields."
+ [d, m, mrecord] = self.data
+ mrecord.a[3:] = 5
+ assert_equal(mrecord.a, [0,1,2,5,5])
+ assert_equal(mrecord.a._mask, [1,0,0,0,0])
+ #
+ mrecord.b[3:] = masked
+ assert_equal(mrecord.b, [4,3,2,1,0])
+ assert_equal(mrecord.b._mask, [1,1,0,1,1])
+
+ def test_setslices(self):
+ "Tests setting slices."
+ [d, m, mrec] = self.data
+ mrec[:2] = 5
+ assert_equal(mrec.a._data, [5,5,2,3,4])
+ assert_equal(mrec.b._data, [5,5,2,1,0])
+ assert_equal(mrec.a._mask, [0,0,0,1,1])
+ assert_equal(mrec.b._mask, [0,0,0,0,1])
+ #
+ mrec[:2] = masked
+ assert_equal(mrec._mask, [1,1,0,0,1])
+ mrec[-2] = masked
+ assert_equal(mrec._mask, [1,1,0,1,1])
+ #
+ def test_setslices_hardmask(self):
+ "Tests setting slices w/ hardmask."
+ [d, m, mrec] = self.data
+ mrec.harden_mask()
+ mrec[-2:] = 5
+ assert_equal(mrec.a._data, [0,1,2,3,4])
+ assert_equal(mrec.b._data, [4,3,2,5,0])
+ assert_equal(mrec.a._mask, [1,0,0,1,1])
+ assert_equal(mrec.b._mask, [1,1,0,0,1])
+
+ def test_hardmask(self):
+ "Test hardmask"
+ [d, m, mrec] = self.data
+ mrec = mrec.copy()
+ mrec.harden_mask()
+ assert(mrec._hardmask)
+ mrec._mask = nomask
+ assert_equal(mrec._mask, N.r_[[m,m[::-1]]].all(0))
+ mrec.soften_mask()
+ assert(not mrec._hardmask)
+ mrec._mask = nomask
+ assert(mrec['b']._mask is nomask)
+ assert_equal(mrec['a']._mask,mrec['b']._mask)
+
+ def test_fromrecords(self):
+ "Test from recarray."
+ [d, m, mrec] = self.data
+ nrec = N.core.records.fromarrays(N.r_[[d,d[::-1]]],
+ dtype=[('a',N.float_),('b',N.float_)])
+ #....................
+ mrecfr = fromrecords(nrec)
+ assert_equal(mrecfr.a, mrec.a)
+ assert_equal(mrecfr.dtype, mrec.dtype)
+ #....................
+ tmp = mrec[::-1] #.tolist()
+ mrecfr = fromrecords(tmp)
+ assert_equal(mrecfr.a, mrec.a[::-1])
+ #....................
+ mrecfr = fromrecords(nrec.tolist(), names=nrec.dtype.names)
+ assert_equal(mrecfr.a, mrec.a)
+ assert_equal(mrecfr.dtype, mrec.dtype)
+
+ 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
+"""
+ import os
+ from datetime import datetime
+ fname = 'tmp%s' % datetime.now().strftime("%y%m%d%H%M%S%s")
+ f = open(fname, 'w')
+ f.write(fcontent)
+ f.close()
+ mrectxt = fromtextfile(fname,delimitor=',',varnames='ABCDEFG')
+ os.unlink(fname)
+ #
+ 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_addfield(self):
+ "Tests addfield"
+ [d, m, mrec] = self.data
+ mrec = addfield(mrec, masked_array(d+10, mask=m[::-1]))
+ assert_equal(mrec.f2, d+10)
+ assert_equal(mrec.f2._mask, m[::-1])
+
+###############################################################################
+#------------------------------------------------------------------------------
+if __name__ == "__main__":
+ NumpyTest().run()
diff --git a/numpy/ma/tests/test_mstats.py b/numpy/ma/tests/test_mstats.py
new file mode 100644
index 000000000..e4657a58f
--- /dev/null
+++ b/numpy/ma/tests/test_mstats.py
@@ -0,0 +1,174 @@
+# pylint: disable-msg=W0611, W0612, W0511,R0201
+"""Tests suite for maskedArray statistics.
+
+:author: Pierre Gerard-Marchant
+:contact: pierregm_at_uga_dot_edu
+:version: $Id: test_mstats.py 3473 2007-10-29 15:18:13Z jarrod.millman $
+"""
+__author__ = "Pierre GF Gerard-Marchant ($Author: jarrod.millman $)"
+__version__ = '1.0'
+__revision__ = "$Revision: 3473 $"
+__date__ = '$Date: 2007-10-29 17:18:13 +0200 (Mon, 29 Oct 2007) $'
+
+import numpy
+
+import numpy.ma
+from numpy.ma import masked, masked_array
+
+import numpy.ma.testutils
+from numpy.ma.testutils import *
+
+from numpy.ma.mstats import *
+
+#..............................................................................
+class TestQuantiles(NumpyTestCase):
+ "Base test class for MaskedArrays."
+ def __init__(self, *args, **kwds):
+ NumpyTestCase.__init__(self, *args, **kwds)
+ self.a = numpy.ma.arange(1,101)
+ #
+ def test_1d_nomask(self):
+ "Test quantiles 1D - w/o mask."
+ a = self.a
+ assert_almost_equal(mquantiles(a, alphap=1., betap=1.),
+ [25.75, 50.5, 75.25])
+ assert_almost_equal(mquantiles(a, alphap=0, betap=1.),
+ [25., 50., 75.])
+ assert_almost_equal(mquantiles(a, alphap=0.5, betap=0.5),
+ [25.5, 50.5, 75.5])
+ assert_almost_equal(mquantiles(a, alphap=0., betap=0.),
+ [25.25, 50.5, 75.75])
+ assert_almost_equal(mquantiles(a, alphap=1./3, betap=1./3),
+ [25.41666667, 50.5, 75.5833333])
+ assert_almost_equal(mquantiles(a, alphap=3./8, betap=3./8),
+ [25.4375, 50.5, 75.5625])
+ assert_almost_equal(mquantiles(a), [25.45, 50.5, 75.55])#
+ #
+ def test_1d_mask(self):
+ "Test quantiles 1D - w/ mask."
+ a = self.a
+ a[1::2] = masked
+ assert_almost_equal(mquantiles(a, alphap=1., betap=1.),
+ [25.5, 50.0, 74.5])
+ assert_almost_equal(mquantiles(a, alphap=0, betap=1.),
+ [24., 49., 74.])
+ assert_almost_equal(mquantiles(a, alphap=0.5, betap=0.5),
+ [25., 50., 75.])
+ assert_almost_equal(mquantiles(a, alphap=0., betap=0.),
+ [24.5, 50.0, 75.5])
+ assert_almost_equal(mquantiles(a, alphap=1./3, betap=1./3),
+ [24.833333, 50.0, 75.166666])
+ assert_almost_equal(mquantiles(a, alphap=3./8, betap=3./8),
+ [24.875, 50., 75.125])
+ assert_almost_equal(mquantiles(a), [24.9, 50., 75.1])
+ #
+ def test_2d_nomask(self):
+ "Test quantiles 2D - w/o mask."
+ a = self.a
+ b = numpy.ma.resize(a, (100,100))
+ assert_almost_equal(mquantiles(b), [25.45, 50.5, 75.55])
+ assert_almost_equal(mquantiles(b, axis=0), numpy.ma.resize(a,(3,100)))
+ assert_almost_equal(mquantiles(b, axis=1),
+ numpy.ma.resize([25.45, 50.5, 75.55], (100,3)))
+ #
+ def test_2d_mask(self):
+ "Test quantiles 2D - w/ mask."
+ a = self.a
+ a[1::2] = masked
+ b = numpy.ma.resize(a, (100,100))
+ assert_almost_equal(mquantiles(b), [25., 50., 75.])
+ assert_almost_equal(mquantiles(b, axis=0), numpy.ma.resize(a,(3,100)))
+ assert_almost_equal(mquantiles(b, axis=1),
+ numpy.ma.resize([24.9, 50., 75.1], (100,3)))
+
+class TestMedian(NumpyTestCase):
+ def __init__(self, *args, **kwds):
+ NumpyTestCase.__init__(self, *args, **kwds)
+
+ def test_2d(self):
+ "Tests median w/ 2D"
+ (n,p) = (101,30)
+ x = masked_array(numpy.linspace(-1.,1.,n),)
+ x[:10] = x[-10:] = masked
+ z = masked_array(numpy.empty((n,p), dtype=numpy.float_))
+ z[:,0] = x[:]
+ idx = numpy.arange(len(x))
+ for i in range(1,p):
+ numpy.random.shuffle(idx)
+ z[:,i] = x[idx]
+ assert_equal(mmedian(z[:,0]), 0)
+ assert_equal(mmedian(z), numpy.zeros((p,)))
+
+ def test_3d(self):
+ "Tests median w/ 3D"
+ x = numpy.ma.arange(24).reshape(3,4,2)
+ x[x%3==0] = masked
+ assert_equal(mmedian(x,0), [[12,9],[6,15],[12,9],[18,15]])
+ x.shape = (4,3,2)
+ assert_equal(mmedian(x,0),[[99,10],[11,99],[13,14]])
+ x = numpy.ma.arange(24).reshape(4,3,2)
+ x[x%5==0] = masked
+ assert_equal(mmedian(x,0), [[12,10],[8,9],[16,17]])
+
+#..............................................................................
+class TestTrimming(NumpyTestCase):
+ #
+ def __init__(self, *args, **kwds):
+ NumpyTestCase.__init__(self, *args, **kwds)
+ #
+ def test_trim(self):
+ "Tests trimming."
+ x = numpy.ma.arange(100)
+ assert_equal(trim_both(x).count(), 60)
+ assert_equal(trim_tail(x,tail='r').count(), 80)
+ x[50:70] = masked
+ trimx = trim_both(x)
+ assert_equal(trimx.count(), 48)
+ assert_equal(trimx._mask, [1]*16 + [0]*34 + [1]*20 + [0]*14 + [1]*16)
+ x._mask = nomask
+ x.shape = (10,10)
+ assert_equal(trim_both(x).count(), 60)
+ assert_equal(trim_tail(x).count(), 80)
+ #
+ def test_trimmedmean(self):
+ "Tests the trimmed mean."
+ data = masked_array([ 77, 87, 88,114,151,210,219,246,253,262,
+ 296,299,306,376,428,515,666,1310,2611])
+ assert_almost_equal(trimmed_mean(data,0.1), 343, 0)
+ assert_almost_equal(trimmed_mean(data,0.2), 283, 0)
+ #
+ def test_trimmed_stde(self):
+ "Tests the trimmed mean standard error."
+ data = masked_array([ 77, 87, 88,114,151,210,219,246,253,262,
+ 296,299,306,376,428,515,666,1310,2611])
+ assert_almost_equal(trimmed_stde(data,0.2), 56.1, 1)
+ #
+ def test_winsorization(self):
+ "Tests the Winsorization of the data."
+ data = masked_array([ 77, 87, 88,114,151,210,219,246,253,262,
+ 296,299,306,376,428,515,666,1310,2611])
+ assert_almost_equal(winsorize(data).varu(), 21551.4, 1)
+ data[5] = masked
+ winsorized = winsorize(data)
+ assert_equal(winsorized.mask, data.mask)
+#..............................................................................
+
+class TestMisc(NumpyTestCase):
+ def __init__(self, *args, **kwds):
+ NumpyTestCase.__init__(self, *args, **kwds)
+
+ def check_cov(self):
+ "Tests the cov function."
+ x = masked_array([[1,2,3],[4,5,6]], mask=[[1,0,0],[0,0,0]])
+ c = cov(x[0])
+ assert_equal(c, (x[0].anom()**2).sum())
+ c = cov(x[1])
+ assert_equal(c, (x[1].anom()**2).sum()/2.)
+ c = cov(x)
+ assert_equal(c[1,0], (x[0].anom()*x[1].anom()).sum())
+
+
+###############################################################################
+#------------------------------------------------------------------------------
+if __name__ == "__main__":
+ NumpyTest().run()
diff --git a/numpy/ma/tests/test_subclassing.py b/numpy/ma/tests/test_subclassing.py
new file mode 100644
index 000000000..331ef887d
--- /dev/null
+++ b/numpy/ma/tests/test_subclassing.py
@@ -0,0 +1,183 @@
+# pylint: disable-msg=W0611, W0612, W0511,R0201
+"""Tests suite for MaskedArray & subclassing.
+
+:author: Pierre Gerard-Marchant
+:contact: pierregm_at_uga_dot_edu
+:version: $Id: test_subclassing.py 3473 2007-10-29 15:18:13Z jarrod.millman $
+"""
+__author__ = "Pierre GF Gerard-Marchant ($Author: jarrod.millman $)"
+__version__ = '1.0'
+__revision__ = "$Revision: 3473 $"
+__date__ = '$Date: 2007-10-29 17:18:13 +0200 (Mon, 29 Oct 2007) $'
+
+import numpy as N
+import numpy.core.numeric as numeric
+
+from numpy.testing import NumpyTest, NumpyTestCase
+
+import numpy.ma.testutils
+from numpy.ma.testutils import *
+
+import numpy.ma.core as coremodule
+from numpy.ma.core import *
+
+
+class SubArray(N.ndarray):
+ """Defines a generic N.ndarray subclass, that stores some metadata
+ in the dictionary `info`."""
+ def __new__(cls,arr,info={}):
+ x = N.asanyarray(arr).view(cls)
+ x.info = info
+ return x
+ def __array_finalize__(self, obj):
+ self.info = getattr(obj,'info',{})
+ return
+ def __add__(self, other):
+ result = N.ndarray.__add__(self, other)
+ result.info.update({'added':result.info.pop('added',0)+1})
+ return result
+subarray = SubArray
+
+class MSubArray(SubArray,MaskedArray):
+ def __new__(cls, data, info=None, mask=nomask):
+ subarr = SubArray(data, info)
+ _data = MaskedArray.__new__(cls, data=subarr, mask=mask)
+ _data.info = subarr.info
+ return _data
+ def __array_finalize__(self,obj):
+ MaskedArray.__array_finalize__(self,obj)
+ SubArray.__array_finalize__(self, obj)
+ return
+ def _get_series(self):
+ _view = self.view(MaskedArray)
+ _view._sharedmask = False
+ return _view
+ _series = property(fget=_get_series)
+msubarray = MSubArray
+
+class MMatrix(MaskedArray, N.matrix,):
+ def __new__(cls, data, mask=nomask):
+ mat = N.matrix(data)
+ _data = MaskedArray.__new__(cls, data=mat, mask=mask)
+ return _data
+ def __array_finalize__(self,obj):
+ N.matrix.__array_finalize__(self, obj)
+ MaskedArray.__array_finalize__(self,obj)
+ return
+ def _get_series(self):
+ _view = self.view(MaskedArray)
+ _view._sharedmask = False
+ return _view
+ _series = property(fget=_get_series)
+mmatrix = MMatrix
+
+
+
+class TestSubclassing(NumpyTestCase):
+ """Test suite for masked subclasses of ndarray."""
+
+ def check_data_subclassing(self):
+ "Tests whether the subclass is kept."
+ x = N.arange(5)
+ m = [0,0,1,0,0]
+ xsub = SubArray(x)
+ xmsub = masked_array(xsub, mask=m)
+ assert isinstance(xmsub, MaskedArray)
+ assert_equal(xmsub._data, xsub)
+ assert isinstance(xmsub._data, SubArray)
+
+ def check_maskedarray_subclassing(self):
+ "Tests subclassing MaskedArray"
+ x = N.arange(5)
+ mx = mmatrix(x,mask=[0,1,0,0,0])
+ assert isinstance(mx._data, N.matrix)
+ "Tests masked_unary_operation"
+ assert isinstance(add(mx,mx), mmatrix)
+ assert isinstance(add(mx,x), mmatrix)
+ assert_equal(add(mx,x), mx+x)
+ assert isinstance(add(mx,mx)._data, N.matrix)
+ assert isinstance(add.outer(mx,mx), mmatrix)
+ "Tests masked_binary_operation"
+ assert isinstance(hypot(mx,mx), mmatrix)
+ assert isinstance(hypot(mx,x), mmatrix)
+
+ def check_attributepropagation(self):
+ x = array(arange(5), mask=[0]+[1]*4)
+ my = masked_array(subarray(x))
+ ym = msubarray(x)
+ #
+ z = (my+1)
+ assert isinstance(z,MaskedArray)
+ assert not isinstance(z, MSubArray)
+ assert isinstance(z._data, SubArray)
+ assert_equal(z._data.info, {})
+ #
+ z = (ym+1)
+ assert isinstance(z, MaskedArray)
+ assert isinstance(z, MSubArray)
+ assert isinstance(z._data, SubArray)
+ assert z._data.info['added'] > 0
+ #
+ ym._set_mask([1,0,0,0,1])
+ assert_equal(ym._mask, [1,0,0,0,1])
+ ym._series._set_mask([0,0,0,0,1])
+ assert_equal(ym._mask, [0,0,0,0,1])
+ #
+ xsub = subarray(x, info={'name':'x'})
+ mxsub = masked_array(xsub)
+ assert hasattr(mxsub, 'info')
+ assert_equal(mxsub.info, xsub.info)
+
+ def check_subclasspreservation(self):
+ "Checks that masked_array(...,subok=True) preserves the class."
+ x = N.arange(5)
+ m = [0,0,1,0,0]
+ xinfo = [(i,j) for (i,j) in zip(x,m)]
+ xsub = MSubArray(x, mask=m, info={'xsub':xinfo})
+ #
+ mxsub = masked_array(xsub, subok=False)
+ assert not isinstance(mxsub, MSubArray)
+ assert isinstance(mxsub, MaskedArray)
+ assert_equal(mxsub._mask, m)
+ #
+ mxsub = asarray(xsub)
+ assert not isinstance(mxsub, MSubArray)
+ assert isinstance(mxsub, MaskedArray)
+ assert_equal(mxsub._mask, m)
+ #
+ mxsub = masked_array(xsub, subok=True)
+ assert isinstance(mxsub, MSubArray)
+ assert_equal(mxsub.info, xsub.info)
+ assert_equal(mxsub._mask, xsub._mask)
+ #
+ mxsub = asanyarray(xsub)
+ assert isinstance(mxsub, MSubArray)
+ assert_equal(mxsub.info, xsub.info)
+ assert_equal(mxsub._mask, m)
+
+
+################################################################################
+if __name__ == '__main__':
+ NumpyTest().run()
+ #
+ if 0:
+ x = array(arange(5), mask=[0]+[1]*4)
+ my = masked_array(subarray(x))
+ ym = msubarray(x)
+ #
+ z = (my+1)
+ assert isinstance(z,MaskedArray)
+ assert not isinstance(z, MSubArray)
+ assert isinstance(z._data, SubArray)
+ assert_equal(z._data.info, {})
+ #
+ z = (ym+1)
+ assert isinstance(z, MaskedArray)
+ assert isinstance(z, MSubArray)
+ assert isinstance(z._data, SubArray)
+ assert z._data.info['added'] > 0
+ #
+ ym._set_mask([1,0,0,0,1])
+ assert_equal(ym._mask, [1,0,0,0,1])
+ ym._series._set_mask([0,0,0,0,1])
+ assert_equal(ym._mask, [0,0,0,0,1])