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

numpy-svn@scip... numpy-svn@scip...
Thu Sep 11 14:54:35 CDT 2008


Author: pierregm
Date: 2008-09-11 14:54:31 -0500 (Thu, 11 Sep 2008)
New Revision: 5803

Modified:
   trunk/numpy/ma/core.py
   trunk/numpy/ma/tests/test_core.py
Log:
* fixed view for MaskedArrays w/ flexible dtype

Modified: trunk/numpy/ma/core.py
===================================================================
--- trunk/numpy/ma/core.py	2008-09-10 07:56:09 UTC (rev 5802)
+++ trunk/numpy/ma/core.py	2008-09-11 19:54:31 UTC (rev 5803)
@@ -1257,9 +1257,9 @@
             copy = True
         # Careful, cls might not always be MaskedArray...
         if not isinstance(data, cls) or not subok:
-            _data = _data.view(cls)
+            _data = ndarray.view(_data, cls)
         else:
-            _data = _data.view(type(data))
+            _data = ndarray.view(_data, type(data))
         # Backwards compatibility w/ numpy.core.ma .......
         if hasattr(data,'_mask') and not isinstance(data, ndarray):
             _data._mask = data._mask
@@ -1374,7 +1374,15 @@
         """
         # Get main attributes .........
         self._update_from(obj)
-        self._mask = getattr(obj, '_mask', nomask)
+        if isinstance(obj, ndarray):
+            odtype = obj.dtype
+            if odtype.names:
+                _mask = getattr(obj, '_mask', make_mask_none(obj.shape, odtype))
+            else:
+                _mask = getattr(obj, '_mask', nomask)
+        else:
+            _mask = nomask
+        self._mask = _mask
         # Finalize the mask ...........
         if self._mask is not nomask:
             self._mask.shape = self.shape
@@ -1425,6 +1433,24 @@
         #....
         return result
     #.............................................
+    def view(self, dtype=None, type=None):
+        if dtype is not None:
+            if type is None:
+                args = (dtype,)
+            else:
+                args = (dtype, type)
+        elif type is None:
+            args = ()
+        else:
+            args = (type,)
+        output = ndarray.view(self, *args)
+        if (getattr(output,'_mask', nomask) is not nomask):
+            mdtype = make_mask_descr(output.dtype)
+            output._mask = self._mask.view(mdtype, ndarray)
+            output._mask.shape = output.shape
+        return output
+    view.__doc__ = ndarray.view.__doc__
+    #.............................................
     def astype(self, newtype):
         """Returns a copy of the array cast to newtype."""
         newtype = np.dtype(newtype)
@@ -1701,7 +1727,7 @@
         underlying data.
 
         """
-        return self.view(self._baseclass)
+        return ndarray.view(self, self._baseclass)
     _data = property(fget=_get_data)
     data = property(fget=_get_data)
 

Modified: trunk/numpy/ma/tests/test_core.py
===================================================================
--- trunk/numpy/ma/tests/test_core.py	2008-09-10 07:56:09 UTC (rev 5802)
+++ trunk/numpy/ma/tests/test_core.py	2008-09-11 19:54:31 UTC (rev 5803)
@@ -2342,6 +2342,34 @@
         assert_equal(getmaskarray(test),
                      np.array([(1, 1) , (1, 1), (1, 1)],
                               dtype=[('a', '|b1'), ('b', '|b1')]))
+    #
+    def test_view(self):
+        "Test view w/ flexible dtype"
+        iterator = zip(np.arange(10), np.random.rand(10))
+        data = np.array(iterator)
+        a = array(iterator, dtype=[('a',float),('b',float)])
+        a.mask[0] = (1,0)
+        controlmask = np.array([1]+19*[0], dtype=bool)
+        #
+        test = a.view(float)
+        assert_equal(test, data.ravel())
+        assert_equal(test.mask, controlmask)
+        #
+        test = a.view((float,2))
+        assert_equal(test, data)
+        assert_equal(test.mask, controlmask.reshape(-1,2))
+        #
+        test = a.view([('A',float),('B',float)])
+        assert_equal(test.mask.dtype.names, ('A', 'B'))
+        assert_equal(test['A'], a['a'])
+        assert_equal(test['B'], a['b'])
+        #
+        test = a.view(np.ndarray)
+        assert_equal(test, a._data)
+        #
+        test = a.view((float,2), np.matrix)
+        assert_equal(test, data)
+        assert(isinstance(test, np.matrix))
 
 
 ###############################################################################



More information about the Numpy-svn mailing list