[Scipy-svn] r3308 - in trunk/scipy/sandbox/maskedarray: . tests

scipy-svn at scipy.org scipy-svn at scipy.org
Fri Sep 7 20:37:12 EDT 2007


Author: pierregm
Date: 2007-09-07 19:37:09 -0500 (Fri, 07 Sep 2007)
New Revision: 3308

Modified:
   trunk/scipy/sandbox/maskedarray/core.py
   trunk/scipy/sandbox/maskedarray/morestats.py
   trunk/scipy/sandbox/maskedarray/tests/test_core.py
Log:
core.arraymethods : force to return masked when the result has no dimension but the mask is (an array of) True
morestats.hdquantiles : prevents using apply_along_axis on 1D data
morestats : introduction of hdmedian (as a shortcut to hdquantiles)

Modified: trunk/scipy/sandbox/maskedarray/core.py
===================================================================
--- trunk/scipy/sandbox/maskedarray/core.py	2007-09-07 19:55:57 UTC (rev 3307)
+++ trunk/scipy/sandbox/maskedarray/core.py	2007-09-08 00:37:09 UTC (rev 3308)
@@ -932,6 +932,9 @@
                 result._mask = mask
             elif mask is not nomask:
                 result.__setmask__(getattr(mask, methodname)(*args, **params))
+        else:
+            if mask.ndim and mask.all():
+                return masked
         return result
 #..........................................................
 
@@ -2745,3 +2748,14 @@
         data = masked_array([1,2,3],fill_value=-999)
         series = data[[0,2,1]]
         assert_equal(series._fill_value, data._fill_value)
+        
+    if 1:
+        "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)
+        

Modified: trunk/scipy/sandbox/maskedarray/morestats.py
===================================================================
--- trunk/scipy/sandbox/maskedarray/morestats.py	2007-09-07 19:55:57 UTC (rev 3307)
+++ trunk/scipy/sandbox/maskedarray/morestats.py	2007-09-08 00:37:09 UTC (rev 3308)
@@ -29,7 +29,7 @@
 from scipy.stats.distributions import norm, beta, t, binom
 from scipy.stats.morestats import find_repeats
 
-__all__ = ['hdquantiles', 'hdquantiles_sd',
+__all__ = ['hdquantiles', 'hdmedian', 'hdquantiles_sd',
            'trimmed_mean_ci', 'mjci', 'rank_data']
 
 
@@ -57,9 +57,10 @@
     The function is restricted to 2D arrays.
     """
     def _hd_1D(data,prob,var):
-        "Computes the HD quantiles for a 1D array."
+        "Computes the HD quantiles for a 1D array. Returns nan for invalid data."
         xsorted = numpy.squeeze(numpy.sort(data.compressed().view(ndarray)))
-        n = len(xsorted)
+        # Don't use length here, in case we have a numpy scalar
+        n = xsorted.size
         #.........
         hd = empty((2,len(prob)), float_)
         if n < 2:
@@ -88,7 +89,7 @@
     data = masked_array(data, copy=False, dtype=float_)
     p = numpy.array(prob, copy=False, ndmin=1)
     # Computes quantiles along axis (or globally)
-    if (axis is None): 
+    if (axis is None) or (data.ndim == 1): 
         result = _hd_1D(data, p, var)
     else:
         assert data.ndim <= 2, "Array should be 2D at most !"
@@ -97,6 +98,22 @@
     return masked_array(result, mask=numpy.isnan(result))
     
 #..............................................................................
+def hdmedian(data, axis=-1, var=False):
+    """Returns the Harrell-Davis estimate of the median along the given axis.
+    
+:Inputs:
+    data: ndarray
+        Data array.    
+    axis : integer *[None]*
+        Axis along which to compute the quantiles. If None, use a flattened array.
+    var : boolean *[False]*
+        Whether to return the variance of the estimate.
+    """
+    result = hdquantiles(data,[0.5], axis=axis, var=var)
+    return result.squeeze()
+
+    
+#..............................................................................
 def hdquantiles_sd(data, prob=list([.25,.5,.75]), axis=None):
     """Computes the standard error of the Harrell-Davis quantile estimates by jackknife.
     

Modified: trunk/scipy/sandbox/maskedarray/tests/test_core.py
===================================================================
--- trunk/scipy/sandbox/maskedarray/tests/test_core.py	2007-09-07 19:55:57 UTC (rev 3307)
+++ trunk/scipy/sandbox/maskedarray/tests/test_core.py	2007-09-08 00:37:09 UTC (rev 3308)
@@ -1245,6 +1245,16 @@
         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)
+        
 #..............................................................................
 
 ###############################################################################




More information about the Scipy-svn mailing list