[Numpy-svn] r5948 - in branches/1.2.x/numpy/ma: . tests

numpy-svn@scip... numpy-svn@scip...
Sun Oct 19 14:33:53 CDT 2008


Author: pierregm
Date: 2008-10-19 14:33:51 -0500 (Sun, 19 Oct 2008)
New Revision: 5948

Modified:
   branches/1.2.x/numpy/ma/core.py
   branches/1.2.x/numpy/ma/tests/test_core.py
Log:
* Backporting fixes from 1.3.x r5946

Modified: branches/1.2.x/numpy/ma/core.py
===================================================================
--- branches/1.2.x/numpy/ma/core.py	2008-10-19 19:33:10 UTC (rev 5947)
+++ branches/1.2.x/numpy/ma/core.py	2008-10-19 19:33:51 UTC (rev 5948)
@@ -749,11 +749,20 @@
     Each field is set to a bool.
 
     """
+    # Make sure we do have a dtype
+    if not isinstance(ndtype, np.dtype):
+        ndtype = np.dtype(ndtype)
+    # Do we have some name fields ?
     if ndtype.names:
         mdescr = [list(_) for _ in ndtype.descr]
         for m in mdescr:
             m[1] = '|b1'
-        return [tuple(_) for _ in mdescr]
+        return np.dtype([tuple(_) for _ in mdescr])
+    # Is this some kind of composite a la (np.float,2)
+    elif ndtype.subdtype:
+        mdescr = list(ndtype.subdtype)
+        mdescr[0] = np.dtype(bool)
+        return np.dtype(tuple(mdescr))
     else:
         return MaskType
 
@@ -1434,20 +1443,33 @@
         return result
     #.............................................
     def view(self, dtype=None, type=None):
-        if dtype is not None:
+        if dtype is None:
             if type is None:
-                args = (dtype,)
+                output = ndarray.view(self)
             else:
-                args = (dtype, type)
+                output = ndarray.view(self, type)
         elif type is None:
-            args = ()
+            try:
+                if issubclass(dtype, ndarray):
+                    output = ndarray.view(self, dtype)
+                    dtype = None
+                else:
+                    output = ndarray.view(self, dtype)
+            except TypeError:
+                output = ndarray.view(self, dtype)
         else:
-            args = (type,)
-        output = ndarray.view(self, *args)
+            output = ndarray.view(self, dtype, type)
+        # Should we update the mask ?
         if (getattr(output,'_mask', nomask) is not nomask):
-            mdtype = make_mask_descr(output.dtype)
+            if dtype is None:
+                dtype = output.dtype
+            mdtype = make_mask_descr(dtype)
+            
             output._mask = self._mask.view(mdtype, ndarray)
             output._mask.shape = output.shape
+        # Make sure to reset the _fill_value if needed
+        if getattr(output, '_fill_value', None):
+            output._fill_value = None
         return output
     view.__doc__ = ndarray.view.__doc__
     #.............................................
@@ -1996,7 +2018,7 @@
     #
     def __rmul__(self, other):
         "Multiply other by self, and return a new masked array."
-        return multiply(self, other)
+        return multiply(other, self)
     #
     def __div__(self, other):
         "Divide other into self, and return a new masked array."

Modified: branches/1.2.x/numpy/ma/tests/test_core.py
===================================================================
--- branches/1.2.x/numpy/ma/tests/test_core.py	2008-10-19 19:33:10 UTC (rev 5947)
+++ branches/1.2.x/numpy/ma/tests/test_core.py	2008-10-19 19:33:51 UTC (rev 5948)
@@ -2270,6 +2270,21 @@
         self.failUnless(c[0,0] is masked)
         self.failUnless(c.flags['C'])
 
+
+    def test_make_mask_descr(self):
+        "Test make_mask_descr"
+        ntype = [('a',np.float), ('b',np.float)]
+        test = make_mask_descr(ntype)
+        assert_equal(test, [('a',np.bool),('b',np.bool)])
+        #
+        ntype = (np.float, 2)
+        test = make_mask_descr(ntype)
+        assert_equal(test, (np.bool,2))
+        #
+        ntype = np.float
+        test = make_mask_descr(ntype)
+        assert_equal(test, np.dtype(np.bool))
+
 #------------------------------------------------------------------------------
 
 class TestMaskedFields(TestCase):
@@ -2361,23 +2376,15 @@
         a = array(iterator, dtype=[('a',float),('b',float)])
         a.mask[0] = (1,0)
         controlmask = np.array([1]+19*[0], dtype=bool)
-        #
+        # Transform globally to simple dtype
         test = a.view(float)
         assert_equal(test, data.ravel())
         assert_equal(test.mask, controlmask)
-        #
+        # Transform globally to dty
         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)
         self.failUnless(isinstance(test, np.matrix))
@@ -2399,6 +2406,86 @@
         assert_equal_records(a[-2]._data, a._data[-2])
         assert_equal_records(a[-2]._mask, a._mask[-2])
 
+
+class TestMaskedView(TestCase):
+    #
+    def setUp(self):
+        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)
+        self.data = (data, a, controlmask)
+    #
+    def test_view_to_nothing(self):
+        (data, a, controlmask) = self.data
+        test = a.view()
+        self.failUnless(isinstance(test, MaskedArray))
+        assert_equal(test._data, a._data)
+        assert_equal(test._mask, a._mask)
+        
+    #
+    def test_view_to_type(self):
+        (data, a, controlmask) = self.data
+        test = a.view(np.ndarray)
+        self.failUnless(not isinstance(test, MaskedArray))
+        assert_equal(test, a._data)
+        assert_equal_records(test, data.view(a.dtype).squeeze())
+    #
+    def test_view_to_simple_dtype(self):
+        (data, a, controlmask) = self.data
+        # View globally
+        test = a.view(float)
+        self.failUnless(isinstance(test, MaskedArray))
+        assert_equal(test, data.ravel())
+        assert_equal(test.mask, controlmask)
+    #
+    def test_view_to_flexible_dtype(self):
+        (data, a, controlmask) = self.data
+        #
+        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[0].view([('A',float),('B',float)])
+        self.failUnless(isinstance(test, MaskedArray))
+        assert_equal(test.mask.dtype.names, ('A', 'B'))
+        assert_equal(test['A'], a['a'][0])
+        assert_equal(test['B'], a['b'][0])
+        #
+        test = a[-1].view([('A',float),('B',float)])
+        self.failUnless(not isinstance(test, MaskedArray))
+        assert_equal(test.dtype.names, ('A', 'B'))
+        assert_equal(test['A'], a['a'][-1])
+        assert_equal(test['B'], a['b'][-1])
+        
+    #
+    def test_view_to_subdtype(self):
+        (data, a, controlmask) = self.data
+        # View globally
+        test = a.view((float,2))
+        self.failUnless(isinstance(test, MaskedArray))
+        assert_equal(test, data)
+        assert_equal(test.mask, controlmask.reshape(-1,2))
+        # View on 1 masked element
+        test = a[0].view((float,2))
+        self.failUnless(isinstance(test, MaskedArray))
+        assert_equal(test, data[0])
+        assert_equal(test.mask, (1,0))
+        # View on 1 unmasked element
+        test = a[-1].view((float,2))
+        self.failUnless(not isinstance(test, MaskedArray))
+        assert_equal(test, data[-1])
+    #
+    def test_view_to_dtype_and_type(self):
+        (data, a, controlmask) = self.data
+        #
+        test = a.view((float,2), np.matrix)
+        assert_equal(test, data)
+        self.failUnless(isinstance(test, np.matrix))
+        self.failUnless(not isinstance(test, MaskedArray))
+
 ###############################################################################
 #------------------------------------------------------------------------------
 if __name__ == "__main__":



More information about the Numpy-svn mailing list