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

numpy-svn@scip... numpy-svn@scip...
Mon Aug 9 14:57:33 CDT 2010


Author: pierregm
Date: 2010-08-09 14:57:33 -0500 (Mon, 09 Aug 2010)
New Revision: 8613

Modified:
   trunk/numpy/ma/extras.py
   trunk/numpy/ma/tests/test_extras.py
Log:
ma.extras
* fixed the documentation of (flat)notmasked_xxx


Modified: trunk/numpy/ma/extras.py
===================================================================
--- trunk/numpy/ma/extras.py	2010-08-09 19:27:14 UTC (rev 8612)
+++ trunk/numpy/ma/extras.py	2010-08-09 19:57:33 UTC (rev 8613)
@@ -1522,7 +1522,8 @@
 
     See Also
     --------
-    flatnotmasked_contiguous, notmasked_contiguous, notmasked_edges
+    flatnotmasked_contiguous, notmasked_contiguous, notmasked_edges, 
+    clump_masked, clump_unmasked
 
     Notes
     -----
@@ -1530,24 +1531,26 @@
 
     Examples
     --------
-    >>> a = np.arange(10)
+    >>> a = np.ma.arange(10)
+    >>> flatnotmasked_edges(a)
+    [0,-1]
+
     >>> mask = (a < 3) | (a > 8) | (a == 5)
-
-    >>> ma = np.ma.array(a, mask=m)
-    >>> np.array(ma[~ma.mask])
+    >>> a[mask] = np.ma.masked
+    >>> np.array(a[~a.mask])
     array([3, 4, 6, 7, 8])
 
-    >>> flatnotmasked_edges(ma)
+    >>> flatnotmasked_edges(a)
     array([3, 8])
 
-    >>> ma = np.ma.array(a, mask=np.ones_like(a))
+    >>> a[:] = np.ma.masked
     >>> print flatnotmasked_edges(ma)
     None
 
     """
     m = getmask(a)
     if m is nomask or not np.any(m):
-        return [0, -1]
+        return np.array([0, a.size - 1])
     unmasked = np.flatnonzero(~m)
     if len(unmasked) > 0:
         return unmasked[[0, -1]]
@@ -1580,7 +1583,8 @@
 
     See Also
     --------
-    flatnotmasked_contiguous, flatnotmasked_edges, notmasked_contiguous
+    flatnotmasked_contiguous, flatnotmasked_edges, notmasked_contiguous,
+    clump_masked, clump_unmasked
 
     Examples
     --------
@@ -1588,8 +1592,8 @@
     >>> m = np.zeros_like(a)
     >>> m[1:, 1:] = 1
 
-    >>> ma = np.ma.array(a, mask=m)
-    >>> np.array(ma[~ma.mask])
+    >>> am = np.ma.array(a, mask=m)
+    >>> np.array(am[~am.mask])
     array([0, 1, 2, 3, 6])
 
     >>> np.ma.extras.notmasked_edges(ma)
@@ -1621,7 +1625,8 @@
 
     See Also
     --------
-    flatnotmasked_edges, notmasked_contiguous, notmasked_edges
+    flatnotmasked_edges, notmasked_contiguous, notmasked_edges,
+    clump_masked, clump_unmasked
 
     Notes
     -----
@@ -1629,16 +1634,19 @@
 
     Examples
     --------
-    >>> a = np.arange(10)
+    >>> a = np.ma.arange(10)
+    >>> np.ma.extras.flatnotmasked_contiguous(a)
+    slice(0, 10, None)
+    
     >>> mask = (a < 3) | (a > 8) | (a == 5)
-    >>> ma = np.ma.array(a, mask=mask)
-    >>> np.array(ma[~ma.mask])
+    >>> a[mask] = np.ma.masked
+    >>> np.array(a[~a.mask])
     array([3, 4, 6, 7, 8])
 
-    >>> np.ma.extras.flatnotmasked_contiguous(ma)
-    [slice(3, 4, None), slice(6, 8, None)]
-    >>> ma = np.ma.array(a, mask=np.ones_like(a))
-    >>> print np.ma.extras.flatnotmasked_edges(ma)
+    >>> np.ma.extras.flatnotmasked_contiguous(a)
+    [slice(3, 5, None), slice(6, 9, None)]
+    >>> a[:] = np.ma.masked
+    >>> print np.ma.extras.flatnotmasked_edges(a)
     None
 
     """
@@ -1652,7 +1660,7 @@
         if not k:
             result.append(slice(i, i + n))
         i += n
-    return result
+    return result or None
 
 def notmasked_contiguous(a, axis=None):
     """
@@ -1674,7 +1682,8 @@
 
     See Also
     --------
-    flatnotmasked_edges, flatnotmasked_contiguous, notmasked_edges
+    flatnotmasked_edges, flatnotmasked_contiguous, notmasked_edges,
+    clump_masked, clump_unmasked
 
     Notes
     -----
@@ -1691,7 +1700,7 @@
     array([0, 1, 2, 3, 6])
 
     >>> np.ma.extras.notmasked_contiguous(ma)
-    [slice(0, 3, None), slice(6, 6, None)]
+    [slice(0, 4, None), slice(6, 7, None)]
 
     """
     a = asarray(a)
@@ -1792,7 +1801,7 @@
     >>> a = np.ma.masked_array(np.arange(10))
     >>> a[[0, 1, 2, 6, 8, 9]] = np.ma.masked
     >>> np.ma.extras.clump_masked(a)
-    [slice(0, 3, None), slice(6, 7, None), slice(8, None, None)]
+    [slice(0, 3, None), slice(6, 7, None), slice(8, 10, None)]
 
     """
     mask = ma.getmask(a)

Modified: trunk/numpy/ma/tests/test_extras.py
===================================================================
--- trunk/numpy/ma/tests/test_extras.py	2010-08-09 19:27:14 UTC (rev 8612)
+++ trunk/numpy/ma/tests/test_extras.py	2010-08-09 19:57:33 UTC (rev 8613)
@@ -88,6 +88,20 @@
         control = [slice(3, 6), slice(7, 8), ]
         assert_equal(test, control)
 
+    def test_flatnotmasked_contiguous(self):
+        "Test flatnotmasked_contiguous"
+        a = arange(10)
+        # No mask
+        test = flatnotmasked_contiguous(a)
+        assert_equal(test, slice(0, a.size))
+        # Some mask
+        a[(a < 3) | (a > 8) | (a == 5)] = masked
+        test = flatnotmasked_contiguous(a)
+        assert_equal(test, [slice(3, 5), slice(6, 9)])
+        #
+        a[:] = masked
+        test = flatnotmasked_contiguous(a)
+        assert_equal(test, [])
 
 
 class TestAverage(TestCase):
@@ -261,9 +275,9 @@
                                [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))
+        assert_equal(tmp[-1], slice(23, 24, None))
+        assert_equal(tmp[-2], slice(16, 22, None))
+        assert_equal(tmp[-3], slice(0, 4, None))
         #
         tmp = notmasked_contiguous(a, 0)
         self.assertTrue(len(tmp[-1]) == 1)
@@ -272,10 +286,10 @@
         self.assertTrue(len(tmp[0]) == 2)
         #
         tmp = notmasked_contiguous(a, 1)
-        assert_equal(tmp[0][-1], slice(0, 3, None))
+        assert_equal(tmp[0][-1], slice(0, 4, None))
         self.assertTrue(tmp[1] is None)
-        assert_equal(tmp[2][-1], slice(7, 7, None))
-        assert_equal(tmp[2][-2], slice(0, 5, None))
+        assert_equal(tmp[2][-1], slice(7, 8, None))
+        assert_equal(tmp[2][-2], slice(0, 6, None))
 
 
 



More information about the Numpy-svn mailing list