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

numpy-svn@scip... numpy-svn@scip...
Sat Feb 7 03:19:14 CST 2009


Author: pierregm
Date: 2009-02-07 03:19:12 -0600 (Sat, 07 Feb 2009)
New Revision: 6349

Modified:
   trunk/numpy/ma/core.py
   trunk/numpy/ma/tests/test_core.py
Log:
MaskedArray.resize : systematically raise a TypeError exception, as a masked array never owns its data
MaskedIterator : fixed to allow .flat on masked matrices

Modified: trunk/numpy/ma/core.py
===================================================================
--- trunk/numpy/ma/core.py	2009-02-06 14:38:57 UTC (rev 6348)
+++ trunk/numpy/ma/core.py	2009-02-07 09:19:12 UTC (rev 6349)
@@ -1478,9 +1478,8 @@
     "Define an interator."
     def __init__(self, ma):
         self.ma = ma
-        self.ma1d = ma.ravel()
-        self.ma_iter = np.asarray(ma).flat
-
+        self.dataiter = ma._data.flat
+        #
         if ma._mask is nomask:
             self.maskiter = None
         else:
@@ -1490,15 +1489,23 @@
         return self
 
     def __getitem__(self, indx):
-        return self.ma1d.__getitem__(indx)
+        result = self.dataiter.__getitem__(indx).view(type(self.ma))
+        if self.maskiter is not None:
+            _mask = self.maskiter.__getitem__(indx)
+            _mask.shape = result.shape
+            result._mask = _mask
+        return result
 
     ### This won't work is ravel makes a copy
     def __setitem__(self, index, value):
-        self.ma1d[index] = value
+        self.dataiter[index] = getdata(value)
+        if self.maskiter is not None:
+            self.maskiter[index] = getmaskarray(value)
+#        self.ma1d[index] = value
 
     def next(self):
         "Returns the next element of the iterator."
-        d = self.ma_iter.next()
+        d = self.dataiter.next()
         if self.maskiter is not None and self.maskiter.next():
             d = masked
         return d
@@ -2707,25 +2714,24 @@
         return result
     #
     def resize(self, newshape, refcheck=True, order=False):
-        """Attempt to modify the size and the shape of the array in place.
+        """
+    Change shape and size of array in-place.
 
-        The array must own its own memory and not be referenced by
-        other arrays.
-
-        Returns
-        -------
-        None.
-
         """
-        try:
-            self._data.resize(newshape, refcheck, order)
-            if self.mask is not nomask:
-                self._mask.resize(newshape, refcheck, order)
-        except ValueError:
-            raise ValueError("Cannot resize an array that has been referenced "
-                             "or is referencing another array in this way.\n"
-                             "Use the resize function.")
-        return None
+        # Note : the 'order' keyword looks broken, let's just drop it
+#        try:
+#            ndarray.resize(self, newshape, refcheck=refcheck)
+#            if self.mask is not nomask:
+#                self._mask.resize(newshape, refcheck=refcheck)
+#        except ValueError:
+#            raise ValueError("Cannot resize an array that has been referenced "
+#                             "or is referencing another array in this way.\n"
+#                             "Use the numpy.ma.resize function.")
+#        return None
+        errmsg = "A masked array does not own its data "\
+                 "and therefore cannot be resized.\n" \
+                 "Use the numpy.ma.resize function instead."
+        raise ValueError(errmsg)
     #
     def put(self, indices, values, mode='raise'):
         """

Modified: trunk/numpy/ma/tests/test_core.py
===================================================================
--- trunk/numpy/ma/tests/test_core.py	2009-02-06 14:38:57 UTC (rev 6348)
+++ trunk/numpy/ma/tests/test_core.py	2009-02-07 09:19:12 UTC (rev 6349)
@@ -1122,6 +1122,17 @@
         a[1] = 1
         assert_equal(a._mask, zeros(10))
 
+    def test_flat(self):
+        "Test flat on masked_matrices"
+        test = ma.array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
+        test.flat = ma.array([3, 2, 1], mask=[1, 0, 0])
+        control = ma.array(np.matrix([[3, 2, 1]]), mask=[1, 0, 0])
+        assert_equal(test, control)
+        #
+        test = ma.array(np.matrix([[1, 2, 3]]), mask=[0, 0, 1])
+        testflat = test.flat
+        testflat[:] = testflat[[2, 1, 0]]
+        assert_equal(test, control)
 
 #------------------------------------------------------------------------------
 



More information about the Numpy-svn mailing list