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

numpy-svn@scip... numpy-svn@scip...
Thu Jul 17 21:02:23 CDT 2008


Author: pierregm
Date: 2008-07-17 21:02:19 -0500 (Thu, 17 Jul 2008)
New Revision: 5449

Modified:
   trunk/numpy/ma/core.py
   trunk/numpy/ma/tests/test_core.py
Log:
* added .torecords(), to convert a masked array to a flexible array with fields '_data' and '_mask'

Modified: trunk/numpy/ma/core.py
===================================================================
--- trunk/numpy/ma/core.py	2008-07-18 00:45:23 UTC (rev 5448)
+++ trunk/numpy/ma/core.py	2008-07-18 02:02:19 UTC (rev 5449)
@@ -3087,6 +3087,31 @@
     def tofile(self, fid, sep="", format="%s"):
         raise NotImplementedError("Not implemented yet, sorry...")
 
+    def torecords(self):
+        """Transforms a masked array into a flexible-type array with two fields:
+        * the ``_data`` field stores the ``_data`` part of the array;
+        * the ``_mask`` field stores the ``_mask`` part of the array;
+        
+        Warnings
+        --------
+        A side-effect of transforming a masked array into a flexible ndarray is 
+        that metainformation (``fill_value``, ...) will be lost.
+        
+        """
+        # Get the basic dtype ....
+        ddtype = self.dtype
+        # Make sure we have a mask
+        _mask = self._mask
+        if _mask is None:
+            _mask = make_mask_none(self.shape, ddtype)
+        # And get its dtype
+        mdtype = self._mask.dtype
+        #
+        record = np.ndarray(shape=self.shape,
+                            dtype=[('_data',ddtype),('_mask',mdtype)])
+        record['_data'] = self._data
+        record['_mask'] = self._mask
+        return record
     #--------------------------------------------
     # Pickling
     def __getstate__(self):

Modified: trunk/numpy/ma/tests/test_core.py
===================================================================
--- trunk/numpy/ma/tests/test_core.py	2008-07-18 00:45:23 UTC (rev 5448)
+++ trunk/numpy/ma/tests/test_core.py	2008-07-18 02:02:19 UTC (rev 5449)
@@ -1650,6 +1650,38 @@
         assert_equal(x.tolist(), [(1,1.1,'one'),(2,2.2,'two'),(None,None,None)])
 
 
+    def test_torecords(self):
+        "Test the conversion to records"
+        data = arange(10)
+        record = data.torecords()
+        assert_equal(record['_data'], data._data)
+        assert_equal(record['_mask'], data._mask)
+        #
+        data[[0,1,2,-1]] = masked
+        record = data.torecords()
+        assert_equal(record['_data'], data._data)
+        assert_equal(record['_mask'], data._mask)
+        #
+        ndtype = [('i',int), ('s','|S3'), ('f',float)]
+        data = array([(i,s,f) for (i,s,f) in zip(np.arange(10),
+                                                 'ABCDEFGHIJKLM',
+                                                 np.random.rand(10))],
+                     dtype=ndtype)
+        data[[0,1,2,-1]] = masked
+        record = data.torecords()
+        assert_equal(record['_data'], data._data)
+        assert_equal(record['_mask'], data._mask)
+        #
+        ndtype = np.dtype("int, (2,3)float, float")
+        data = array([(i,f,ff) for (i,f,ff) in zip(np.arange(10),
+                                                   np.random.rand(10),
+                                                   np.random.rand(10))],
+                     dtype=ndtype)
+        data[[0,1,2,-1]] = masked
+        record = data.torecords()
+        assert_equal(record['_data'], data._data)
+        assert_equal(record['_mask'], data._mask)
+
 #------------------------------------------------------------------------------
 
 



More information about the Numpy-svn mailing list