[Scipy-svn] r2557 - in trunk/Lib/sandbox/maskedarray: . tests

scipy-svn at scipy.org scipy-svn at scipy.org
Mon Jan 15 03:57:22 CST 2007


Author: pierregm
Date: 2007-01-15 03:57:19 -0600 (Mon, 15 Jan 2007)
New Revision: 2557

Modified:
   trunk/Lib/sandbox/maskedarray/CHANGELOG
   trunk/Lib/sandbox/maskedarray/mrecords.py
   trunk/Lib/sandbox/maskedarray/tests/test_mrecords.py
Log:
cf changelog

Modified: trunk/Lib/sandbox/maskedarray/CHANGELOG
===================================================================
--- trunk/Lib/sandbox/maskedarray/CHANGELOG	2007-01-15 07:44:41 UTC (rev 2556)
+++ trunk/Lib/sandbox/maskedarray/CHANGELOG	2007-01-15 09:57:19 UTC (rev 2557)
@@ -1,3 +1,5 @@
+#2007-01-15 : MRecords
+#           : - Add the addfield method
 #2007-01-14 : Mrecords
 #           : - Slices are now properly supported
 #           : - `filled` is now properly supported
@@ -6,13 +8,12 @@
 #2007-01-10 : Mrecords
 #           : - Defines a class of records that support masked arrays
 #2007-01-08 : Core:
-#           : - Forced to reset the class defaults after initialization
-#           : - Forced the MaskedArray class defaults to be reset
+#           : - Force a reset of the class defaults after initialization
 #           : - Modified __array_finallize__ to allow objects w/ _data and _mask fields to be recognized as MA
 #2007-01-04 : Core:
 #           : - Fixed a but in masked_all_like
 #2007-01-02 : Extras
-#           : - Made sure that apply_along_axis output the proper fill_value 
+#           : - Force apply_along_axis to output the proper fill_value 
 #           : Core
 #           : - Can use dtypes for the definition of default_fill_value
 #2006-12-30 : Core

Modified: trunk/Lib/sandbox/maskedarray/mrecords.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/mrecords.py	2007-01-15 07:44:41 UTC (rev 2556)
+++ trunk/Lib/sandbox/maskedarray/mrecords.py	2007-01-15 09:57:19 UTC (rev 2557)
@@ -387,10 +387,41 @@
         self._hardmask = False
     #.............................................
     def copy(self):
+        """Returns a copy of the masked record."""
         _localdict = self.__dict__
         return MaskedRecords(_localdict['_data'].copy(),
                         mask=_localdict['_fieldmask'].copy(),
                        dtype=self.dtype)
+    #.............................................
+    def addfield(self, newfield, newfieldname=None):
+        """Adds a new field to the masked record array, using `newfield` as data
+    and `newfieldname` as name. If `newfieldname` is None, the new field name is
+    set to 'fi', where `i` is the number of existing fields.
+        """
+        _localdict = self.__dict__
+        _data = _localdict['_data']
+        _mask = _localdict['_fieldmask']
+        if newfieldname is None or newfieldname in reserved_fields:
+            newfieldname = 'f%i' % len(_data.dtype)
+        newfield = MA.asarray(newfield)
+        # Get the enw data ............
+        newdtype = numeric.dtype(_data.dtype.descr + \
+                                 [(newfieldname, newfield.dtype)])
+        newdata = recarray(_data.shape, newdtype)
+        [newdata.setfield(_data.getfield(*f),*f) 
+             for f in _data.dtype.fields.values()]
+        newdata.setfield(newfield.filled(), *newdata.dtype.fields[newfieldname])
+        # Get the new mask .............
+        newmdtype = numeric.dtype([(n,N.bool_) for n in newdtype.names])
+        newmask = recarray(_data.shape, newmdtype)
+        [newmask.setfield(_mask.getfield(*f),*f)
+             for f in _mask.dtype.fields.values()]
+        newmask.setfield(getmaskarray(newfield), 
+                         *newmask.dtype.fields[newfieldname])
+        self.__dict__.update(_data=newdata,
+                             _fieldmask=newmask)
+#        return MaskedRecords(newdata, mask=newmask, dtype=newdtype)
+        
 
 
 #####---------------------------------------------------------------------------
@@ -640,28 +671,5 @@
         mrecord = fromarrays(base.T,)
         self_data = [d, m, mrecord]
         
-#    def test_get(self):
-#        "Tests fields retrieval"
-        [d, m, mrec] = self_data
-        mrec = mrec.copy()
-        assert_equal(mrec.f0, MA.array(d,mask=m))
-        assert_equal(mrec.f1, MA.array(d[::-1],mask=m[::-1]))
-        assert((mrec._fieldmask == N.core.records.fromarrays([m, m[::-1]])).all())
-        assert_equal(mrec._mask, N.r_[[m,m[::-1]]].all(0))
-        assert_equal(mrec.f0[1], mrec[1].f0)
-        #
-        assert(isinstance(mrec[:2], MaskedRecords))
-        assert_equal(mrec[:2]['f0'], d[:2])
-        #
-        mrec[:2] = 5
-        assert_equal(mrec.f0._data, [5,5,2,3,4])
-        assert_equal(mrec.f1._data, [5,5,2,1,0])
-        assert_equal(mrec.f0._mask, [0,0,0,1,1])
-        assert_equal(mrec.f1._mask, [0,0,0,0,1])
-        mrec.harden_mask()
-        mrec[-2:] = 5
-        assert_equal(mrec.f0._data, [5,5,2,3,4])
-        assert_equal(mrec.f1._data, [5,5,2,5,0])
-        assert_equal(mrec.f0._mask, [0,0,0,1,1])
-        assert_equal(mrec.f1._mask, [0,0,0,0,1])
+
         
\ No newline at end of file

Modified: trunk/Lib/sandbox/maskedarray/tests/test_mrecords.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/tests/test_mrecords.py	2007-01-15 07:44:41 UTC (rev 2556)
+++ trunk/Lib/sandbox/maskedarray/tests/test_mrecords.py	2007-01-15 09:57:19 UTC (rev 2557)
@@ -26,9 +26,9 @@
 #import maskedarray.mrecords
 ##reload(maskedarray.mrecords)
 #from maskedarray.mrecords import mrecarray, fromarrays, fromtextfile, fromrecords
-import mrecords
-reload(mrecords)
-from mrecords import MaskedRecords, fromarrays, fromtextfile, fromrecords
+import maskedarray.mrecords
+reload(maskedarray.mrecords)
+from maskedarray.mrecords import MaskedRecords, fromarrays, fromtextfile, fromrecords
 
 #..............................................................................
 class test_mrecords(NumpyTestCase):
@@ -56,6 +56,9 @@
         assert((mrec._fieldmask == N.core.records.fromarrays([m, m[::-1]])).all())
         assert_equal(mrec._mask, N.r_[[m,m[::-1]]].all(0))
         assert_equal(mrec.f0[1], mrec[1].f0)
+        #
+        assert(isinstance(mrec[:2], MaskedRecords))
+        assert_equal(mrec[:2]['f0'], d[:2])
         
     def test_set(self):
         "Tests setting fields/attributes."
@@ -73,7 +76,22 @@
         assert_equal(mrecord['f0']._mask, mrecord['f1']._mask)
         mrecord._mask = MA.nomask
         assert_equal(getmaskarray(mrecord['f1']), [0]*5)
-        assert_equal(mrecord['f0']._mask, mrecord['f1']._mask)    
+        assert_equal(mrecord['f0']._mask, mrecord['f1']._mask)   
+        #
+    def test_setslices(self):
+        "Tests setting slices."
+        [d, m, mrec] = self.data        
+        mrec[:2] = 5
+        assert_equal(mrec.f0._data, [5,5,2,3,4])
+        assert_equal(mrec.f1._data, [5,5,2,1,0])
+        assert_equal(mrec.f0._mask, [0,0,0,1,1])
+        assert_equal(mrec.f1._mask, [0,0,0,0,1])
+        mrec.harden_mask()
+        mrec[-2:] = 5
+        assert_equal(mrec.f0._data, [5,5,2,3,4])
+        assert_equal(mrec.f1._data, [5,5,2,5,0])
+        assert_equal(mrec.f0._mask, [0,0,0,1,1])
+        assert_equal(mrec.f1._mask, [0,0,0,0,1]) 
         
     def test_hardmask(self):
         "Test hardmask"
@@ -127,6 +145,13 @@
         assert_equal(mrectxt.F, [1,1,1,1])
         assert_equal(mrectxt.E._mask, [1,1,1,1])
         assert_equal(mrectxt.C, [1,2,3.e+5,-1e-10])  
+        
+    def test_addfield(self):
+        "Tests addfield"
+        [d, m, mrec] = self.data
+        mrec.addfield(masked_array(d+10, mask=m[::-1]))
+        assert_equal(mrec.f2, d+10)
+        assert_equal(mrec.f2._mask, m[::-1])    
                 
 ###############################################################################
 #------------------------------------------------------------------------------



More information about the Scipy-svn mailing list