[Numpy-svn] r4973 - in trunk/numpy/ma: . tests

numpy-svn at scipy.org numpy-svn at scipy.org
Mon Apr 7 14:12:16 EDT 2008


Author: peridot
Date: 2008-04-07 13:12:09 -0500 (Mon, 07 Apr 2008)
New Revision: 4973

Modified:
   trunk/numpy/ma/core.py
   trunk/numpy/ma/tests/test_core.py
Log:
Fix maskedarray's std and var of complex arrays, with test. Add test for ddof.


Modified: trunk/numpy/ma/core.py
===================================================================
--- trunk/numpy/ma/core.py	2008-04-07 14:42:50 UTC (rev 4972)
+++ trunk/numpy/ma/core.py	2008-04-07 18:12:09 UTC (rev 4973)
@@ -68,7 +68,7 @@
 import numpy.core.fromnumeric  as fromnumeric
 import numpy.core.numeric as numeric
 import numpy.core.numerictypes as ntypes
-from numpy import bool_, dtype, typecodes, amax, amin, ndarray
+from numpy import bool_, dtype, typecodes, amax, amin, ndarray, iscomplexobj
 from numpy import expand_dims as n_expand_dims
 from numpy import array as narray
 import warnings
@@ -2180,7 +2180,10 @@
         else:
             cnt = self.count(axis=axis)-ddof
             danom = self.anom(axis=axis, dtype=dtype)
-            danom *= danom
+            if iscomplexobj(self):
+                danom = umath.absolute(danom)**2
+            else:
+                danom *= danom
             dvar = narray(danom.sum(axis) / cnt).view(type(self))
             if axis is not None:
                 dvar._mask = mask_or(self._mask.all(axis), (cnt==1))

Modified: trunk/numpy/ma/tests/test_core.py
===================================================================
--- trunk/numpy/ma/tests/test_core.py	2008-04-07 14:42:50 UTC (rev 4972)
+++ trunk/numpy/ma/tests/test_core.py	2008-04-07 18:12:09 UTC (rev 4973)
@@ -1006,6 +1006,10 @@
         (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_almost_equal(mX.std(axis=None,ddof=1),
+                            mX.compressed().std(ddof=1))
+        assert_almost_equal(mX.var(axis=None,ddof=1),
+                            mX.compressed().var(ddof=1))
         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))
@@ -1453,6 +1457,56 @@
         assert_equal(b.shape, a.shape)
         assert_equal(b.fill_value, a.fill_value)
 
+class TestArrayMethodsComplex(NumpyTestCase):
+    "Test class for miscellaneous MaskedArrays methods."
+    def setUp(self):
+        "Base data definition."
+        x = numpy.array([ 8.375j,  7.545j,  8.828j,  8.5j  ,  1.757j,  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.479j,
+                      7.189j,  9.645,  5.395,  4.961,  9.894,  2.893,
+                      7.357,  9.828,  6.272,  3.758,  6.693,  0.993j])
+        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 test_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))
+        assert_almost_equal(mX.var(axis=None,ddof=2),mX.compressed().var(ddof=2))
+        assert_almost_equal(mX.std(axis=None,ddof=2),mX.compressed().std(ddof=2))
+        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())
+
 #..............................................................................
 
 class TestMiscFunctions(NumpyTestCase):




More information about the Numpy-svn mailing list