[Scipy-svn] r2572 - in trunk/Lib/sandbox/maskedarray: . tests

scipy-svn at scipy.org scipy-svn at scipy.org
Tue Jan 16 01:11:50 CST 2007


Author: pierregm
Date: 2007-01-16 01:11:41 -0600 (Tue, 16 Jan 2007)
New Revision: 2572

Modified:
   trunk/Lib/sandbox/maskedarray/CHANGELOG
   trunk/Lib/sandbox/maskedarray/extras.py
   trunk/Lib/sandbox/maskedarray/tests/test_extras.py
Log:


Modified: trunk/Lib/sandbox/maskedarray/CHANGELOG
===================================================================
--- trunk/Lib/sandbox/maskedarray/CHANGELOG	2007-01-16 05:28:28 UTC (rev 2571)
+++ trunk/Lib/sandbox/maskedarray/CHANGELOG	2007-01-16 07:11:41 UTC (rev 2572)
@@ -1,4 +1,5 @@
-#2007-01-15 : mrecords: Add the addfield method
+#2007-01-16 : extras  : new function : `mediff1d`
+#2007-01-15 : mrecords: new method: `addfield`
 #           : core    : Force the mask of a masked array `x` to be copied w/...
 #                     ...masked_array(x,copy=True,mask=nomask,keep_mask=true)
 #2007-01-14 : mrecords: Slices are now properly supported

Modified: trunk/Lib/sandbox/maskedarray/extras.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/extras.py	2007-01-16 05:28:28 UTC (rev 2571)
+++ trunk/Lib/sandbox/maskedarray/extras.py	2007-01-16 07:11:41 UTC (rev 2572)
@@ -11,15 +11,15 @@
 __revision__ = "$Revision$"
 __date__     = '$Date$'
 
-__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',
-           'mask_rowcols', 'mask_rows', 'mask_cols', 'masked_all', 
-               'masked_all_like', 'mr_',
-           'notmasked_edges', 'notmasked_contiguous',
-           'stdu', 'varu',
+__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', 
+'mask_rowcols','mask_rows','mask_cols','masked_all','masked_all_like', 
+'mediff1d', 'mr_',
+'notmasked_edges','notmasked_contiguous',
+'stdu', 'varu',
            ]
 
 from itertools import groupby
@@ -66,7 +66,7 @@
     """Returns an empty masked array of the same shape and dtype as the array `a`,
     where all the data are masked."""
     a = masked_array(numeric.empty_like(arr),
-                     mask=numeric.ones(shape, bool_))
+                     mask=numeric.ones(arr.shape, bool_))
     return a
 
 #####--------------------------------------------------------------------------
@@ -446,6 +446,53 @@
     m = ~numpy.dot(am,bm)
     return masked_array(d, mask=m)
 
+#...............................................................................
+def mediff1d(array, to_end=None, to_begin=None):
+    """Array difference with prefixed and/or appended value."""
+    a = MA.masked_array(array, copy=True)
+    if a.ndim > 1:
+        a.reshape((a.size,))
+    (d, m, n) = (a._data, a._mask, a.size-1)
+    dd = d[1:]-d[:-1]
+    if m is nomask:
+        dm = nomask
+    else:
+        dm = m[1:]-m[:-1]
+    #
+    if to_end is not None:
+        to_end = MA.asarray(to_end)
+        nend = to_end.size
+        if to_begin is not None:
+            to_begin = MA.asarray(to_begin)
+            nbegin = to_begin.size
+            r_data = N.empty((n+nend+nbegin,), dtype=a.dtype)
+            r_mask = N.zeros((n+nend+nbegin,), dtype=bool_)
+            r_data[:nbegin] = to_begin._data
+            r_mask[:nbegin] = to_begin._mask
+            r_data[nbegin:-nend] = dd
+            r_mask[nbegin:-nend] = dm
+        else:
+            r_data = N.empty((n+nend,), dtype=a.dtype)
+            r_mask = N.zeros((n+nend,), dtype=bool_)
+            r_data[:-nend] = dd
+            r_mask[:-nend] = dm
+        r_data[-nend:] = to_end._data
+        r_mask[-nend:] = to_end._mask
+    #
+    elif to_begin is not None:
+        to_begin = MA.asarray(to_begin)
+        nbegin = to_begin.size
+        r_data = N.empty((n+nbegin,), dtype=a.dtype)
+        r_mask = N.zeros((n+nbegin,), dtype=bool_)
+        r_data[:nbegin] = to_begin._data
+        r_mask[:nbegin] = to_begin._mask
+        r_data[nbegin:] = dd
+        r_mask[nbegin:] = dm
+    #
+    else:
+        r_data = dd
+        r_mask = dm
+    return masked_array(r_data, mask=r_mask)
 
 #####--------------------------------------------------------------------------
 #---- --- Concatenation helpers ---

Modified: trunk/Lib/sandbox/maskedarray/tests/test_extras.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/tests/test_extras.py	2007-01-16 05:28:28 UTC (rev 2571)
+++ trunk/Lib/sandbox/maskedarray/tests/test_extras.py	2007-01-16 07:11:41 UTC (rev 2572)
@@ -261,6 +261,40 @@
         assert_equal(c.mask,[[1,0],[1,1]])
         c = dot(b,a)
         assert_equal(c.mask,[[0,0,1],[1,1,1],[0,0,1]])
+        
+    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])
 
 ###############################################################################
 #------------------------------------------------------------------------------



More information about the Scipy-svn mailing list