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

numpy-svn@scip... numpy-svn@scip...
Mon Jun 16 12:29:36 CDT 2008


Author: pierregm
Date: 2008-06-16 12:29:28 -0500 (Mon, 16 Jun 2008)
New Revision: 5284

Modified:
   trunk/numpy/ma/core.py
   trunk/numpy/ma/tests/test_core.py
Log:
core.MaskedArray.__new__
* Force a mask to be created from a list of masked arrays when mask=nomask and keep_mask=True

Modified: trunk/numpy/ma/core.py
===================================================================
--- trunk/numpy/ma/core.py	2008-06-14 06:06:13 UTC (rev 5283)
+++ trunk/numpy/ma/core.py	2008-06-16 17:29:28 UTC (rev 5284)
@@ -1225,11 +1225,19 @@
                 # With full version
                 else:
                     _data._mask = np.zeros(_data.shape, dtype=mdtype)
-            if copy:
-                _data._mask = _data._mask.copy()
-                _data._sharedmask = False
+            # Check whether we missed something
+            elif isinstance(data, (tuple,list)):
+                mask = np.array([getmaskarray(m) for m in data], dtype=mdtype)
+                # Force shrinking of the mask if needed (and possible)
+                if (mdtype == MaskType) and mask.any():
+                    _data._mask = mask
+                    _data._sharedmask = False
             else:
-                _data._sharedmask = True
+                if copy:
+                    _data._mask = _data._mask.copy()
+                    _data._sharedmask = False
+                else:
+                    _data._sharedmask = True
         # Case 2. : With a mask in input ........
         else:
             # Read the mask with the current mdtype

Modified: trunk/numpy/ma/tests/test_core.py
===================================================================
--- trunk/numpy/ma/tests/test_core.py	2008-06-14 06:06:13 UTC (rev 5283)
+++ trunk/numpy/ma/tests/test_core.py	2008-06-16 17:29:28 UTC (rev 5284)
@@ -167,6 +167,18 @@
         dma_3 = MaskedArray(dma_1, mask=[1,0,0,0]*6)
         fail_if_equal(dma_3.mask, dma_1.mask)
 
+    def test_creation_with_list_of_maskedarrays(self):
+        "Tests creaating a masked array from alist of masked arrays."
+        x = array(np.arange(5), mask=[1,0,0,0,0])
+        data = array((x,x[::-1]))
+        assert_equal(data, [[0,1,2,3,4],[4,3,2,1,0]])
+        assert_equal(data._mask, [[1,0,0,0,0],[0,0,0,0,1]])
+        #
+        x.mask = nomask
+        data = array((x,x[::-1]))
+        assert_equal(data, [[0,1,2,3,4],[4,3,2,1,0]])
+        assert(data.mask is nomask)
+
     def test_asarray(self):
         (x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
         xm.fill_value = -9999



More information about the Numpy-svn mailing list