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

numpy-svn@scip... numpy-svn@scip...
Mon Apr 7 09:42:56 CDT 2008


Author: pierregm
Date: 2008-04-07 09:42:50 -0500 (Mon, 07 Apr 2008)
New Revision: 4972

Modified:
   trunk/numpy/ma/core.py
   trunk/numpy/ma/extras.py
   trunk/numpy/ma/mstats.py
   trunk/numpy/ma/tests/test_core.py
   trunk/numpy/ma/tests/test_mstats.py
   trunk/numpy/ma/tests/test_old_ma.py
Log:
core: 
compressed    : make sure that the result is always an array.
count         : make sure that a regular ndarray is returned.
added the get_fill_value function

extras:
added the expand_dims function
deleted varu/stdu (redundant with the ddof=1 parameter)

Modified: trunk/numpy/ma/core.py
===================================================================
--- trunk/numpy/ma/core.py	2008-04-07 02:59:18 UTC (rev 4971)
+++ trunk/numpy/ma/core.py	2008-04-07 14:42:50 UTC (rev 4972)
@@ -1796,7 +1796,7 @@
         if axis is None:
             return (n1-n2)
         else:
-            return masked_array(n1 - n2)
+            return narray(n1 - n2)
     #............................................
     flatten = _arraymethod('flatten')
     #
@@ -2168,7 +2168,7 @@
         -----
         The value returned is by default a biased estimate of the 
         true variance, since the mean is computed by dividing by N-ddof.
-        For the (more standard) unbiased estimate, use ddof=1 or call varu.
+        For the (more standard) unbiased estimate, use ddof=1 or.
         Note that for complex numbers the absolute value is taken before 
         squaring, so that the result is always real and nonnegative.
 
@@ -2209,10 +2209,9 @@
         -----
         The value returned is by default a biased estimate of the 
         true standard deviation, since the mean is computed by dividing 
-        by N-ddof.  For the more standard unbiased estimate, use ddof=1 
-        or call stdu. Note that for complex numbers the absolute value
-        is taken before squaring, so that the result is always real
-        and nonnegative.
+        by N-ddof.  For the more standard unbiased estimate, use ddof=1. 
+        Note that for complex numbers the absolute value is taken before 
+        squaring, so that the result is always real and nonnegative.
         """
         dvar = self.var(axis,dtype,ddof=ddof)
         if axis is not None or dvar is not masked:
@@ -2899,7 +2898,7 @@
 def compressed(x):
     """Return a 1-D array of all the non-masked data."""
     if getmask(x) is nomask:
-        return x
+        return numpy.asanyarray(x)
     else:
         return x.compressed()
 

Modified: trunk/numpy/ma/extras.py
===================================================================
--- trunk/numpy/ma/extras.py	2008-04-07 02:59:18 UTC (rev 4971)
+++ trunk/numpy/ma/extras.py	2008-04-07 14:42:50 UTC (rev 4972)
@@ -12,11 +12,18 @@
 __date__     = '$Date: 2007-10-29 17:18:13 +0200 (Mon, 29 Oct 2007) $'
 
 __all__ = ['apply_along_axis', 'atleast_1d', 'atleast_2d', 'atleast_3d', 
-           'average', 'vstack', 'hstack', 'dstack', 'row_stack', 'column_stack',
-           'compress_rowcols', 'compress_rows', 'compress_cols', 'count_masked',
-           'dot', 'hsplit', 'mask_rowcols','mask_rows','mask_cols','masked_all',
-           'masked_all_like', 'mediff1d', 'median', 'mr_', 'notmasked_edges',
-           'notmasked_contiguous', 'stdu', 'varu',
+           'average', 
+           'column_stack','compress_cols','compress_rowcols', 'compress_rows',
+           'count_masked',
+           'dot','dstack',
+           'expand_dims',
+           'flatnotmasked_contiguous','flatnotmasked_edges', 
+           'hsplit','hstack', 
+           'mask_cols','mask_rowcols','mask_rows','masked_all','masked_all_like',
+           'median','mediff1d','mr_', 
+           'notmasked_contiguous','notmasked_edges', 
+           'row_stack', 
+           'vstack',
            ]
 
 from itertools import groupby
@@ -82,76 +89,7 @@
                      mask=numeric.ones(arr.shape, bool_))
     return a
 
-#####--------------------------------------------------------------------------
-#---- --- New methods ---
-#####--------------------------------------------------------------------------
-def varu(a, axis=None, dtype=None):
-    """Return an unbiased estimate of the variance.
-    i.e. var = sum((x - x.mean())**2)/(size(x,axis)-1)
 
-    Parameters
-    ----------
-        axis : int, optional
-            Axis along which to perform the operation.
-            If None, applies to a flattened version of the array.
-        dtype : {dtype}, optional
-            Datatype for the intermediary computation. If not given,
-            the current dtype is used instead.
-
-    Notes
-    -----
-        The value returned is an unbiased estimate of the true variance.
-        For the (less standard) biased estimate, use var.
-
-    """
-    a = asarray(a)
-    cnt = a.count(axis=axis)
-    anom = a.anom(axis=axis, dtype=dtype)
-    anom *= anom
-    dvar = anom.sum(axis) / (cnt-1)
-    if axis is None:
-        return dvar
-    dvar.__setmask__(mask_or(a._mask.all(axis), (cnt==1)))
-    return dvar
-#    return a.__class__(dvar,
-#                          mask=mask_or(a._mask.all(axis), (cnt==1)),
-#                          fill_value=a._fill_value)
-
-def stdu(a, axis=None, dtype=None):
-    """Return an unbiased estimate of the standard deviation.  The
-    standard deviation is the square root of the average of the
-    squared deviations from the mean, i.e. stdu = sqrt(varu(x)).
-
-    Parameters
-    ----------
-        axis : int, optional
-            Axis along which to perform the operation.
-            If None, applies to a flattened version of the array.
-        dtype : dtype, optional
-            Datatype for the intermediary computation.
-            If not given, the current dtype is used instead.
-
-    Notes
-    -----
-        The value returned is an unbiased estimate of the true
-        standard deviation.  For the biased estimate,
-        use std.
-
-    """
-    a = asarray(a)
-    dvar = a.varu(axis,dtype)
-    if axis is None:
-        if dvar is masked:
-            return masked
-        else:
-            # Should we use umath.sqrt instead ?
-            return sqrt(dvar)
-    return sqrt(dvar)
-
-
-MaskedArray.stdu = stdu
-MaskedArray.varu = varu
-
 #####--------------------------------------------------------------------------
 #---- --- Standard functions ---
 #####--------------------------------------------------------------------------
@@ -199,6 +137,17 @@
 
 hsplit = _fromnxfunction('hsplit')
 
+def expand_dims(a, axis):
+    """Expands the shape of a by including newaxis before axis.
+    """
+    if not isinstance(a, MaskedArray):
+        return numpy.expand_dims(a,axis)
+    elif getmask(a) is nomask:
+        return numpy.expand_dims(a,axis).view(MaskedArray)
+    m = getmaskarray(a)
+    return masked_array(numpy.expand_dims(a,axis),
+                        mask=numpy.expand_dims(m,axis))
+
 #####--------------------------------------------------------------------------
 #----
 #####--------------------------------------------------------------------------
@@ -875,3 +824,6 @@
     return result
 
 ################################################################################
+testmathworks = fix_invalid([1.165 , 0.6268, 0.0751, 0.3516, -0.6965, 
+                                        numpy.nan])
+expand_dims(testmathworks.mean(0),0)
\ No newline at end of file

Modified: trunk/numpy/ma/mstats.py
===================================================================
--- trunk/numpy/ma/mstats.py	2008-04-07 02:59:18 UTC (rev 4971)
+++ trunk/numpy/ma/mstats.py	2008-04-07 14:42:50 UTC (rev 4972)
@@ -201,7 +201,7 @@
         "Returns the standard error of the trimmed mean for a 1D input data."
         winsorized = winsorize(data)
         nsize = winsorized.count()
-        winstd = winsorized.stdu()
+        winstd = winsorized.std(ddof=1)
         return winstd / ((1-2*trim) * numpy.sqrt(nsize))
     #........................
     data = masked_array(data, copy=False, subok=True)

Modified: trunk/numpy/ma/tests/test_core.py
===================================================================
--- trunk/numpy/ma/tests/test_core.py	2008-04-07 02:59:18 UTC (rev 4971)
+++ trunk/numpy/ma/tests/test_core.py	2008-04-07 14:42:50 UTC (rev 4972)
@@ -292,7 +292,7 @@
         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,0), ndarray)
         assert isinstance(count(ott), types.IntType)
         assert_equal(3, count(ott))
         assert getmask(count(ott,0)) is nomask

Modified: trunk/numpy/ma/tests/test_mstats.py
===================================================================
--- trunk/numpy/ma/tests/test_mstats.py	2008-04-07 02:59:18 UTC (rev 4971)
+++ trunk/numpy/ma/tests/test_mstats.py	2008-04-07 14:42:50 UTC (rev 4972)
@@ -148,7 +148,7 @@
         "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)
+        assert_almost_equal(winsorize(data).var(ddof=1), 21551.4, 1)
         data[5] = masked
         winsorized = winsorize(data)
         assert_equal(winsorized.mask, data.mask)

Modified: trunk/numpy/ma/tests/test_old_ma.py
===================================================================
--- trunk/numpy/ma/tests/test_old_ma.py	2008-04-07 02:59:18 UTC (rev 4971)
+++ trunk/numpy/ma/tests/test_old_ma.py	2008-04-07 14:42:50 UTC (rev 4972)
@@ -156,7 +156,7 @@
         self.assertEqual(1, count(1))
         self.failUnless (eq(0, array(1,mask=[1])))
         ott=ott.reshape((2,2))
-        assert isMaskedArray(count(ott,0))
+        assert isinstance(count(ott,0),numpy.ndarray)
         assert isinstance(count(ott), types.IntType)
         self.failUnless (eq(3, count(ott)))
         assert getmask(count(ott,0)) is nomask



More information about the Numpy-svn mailing list