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

scipy-svn@scip... scipy-svn@scip...
Wed Mar 21 16:28:09 CDT 2007


Author: pierregm
Date: 2007-03-21 16:28:05 -0500 (Wed, 21 Mar 2007)
New Revision: 2861

Added:
   trunk/Lib/sandbox/maskedarray/alternative_versions/test_mrecarray.py
Modified:
   trunk/Lib/sandbox/maskedarray/core.py
   trunk/Lib/sandbox/maskedarray/mrecords.py
   trunk/Lib/sandbox/maskedarray/tests/test_subclassing.py
   trunk/Lib/sandbox/maskedarray/testutils.py
Log:
fixed a pb w/ mask propagation
force the update of __dict__ w/ the __dict__ of data
moved test_mrecarray to 'alternative_versions'

Added: trunk/Lib/sandbox/maskedarray/alternative_versions/test_mrecarray.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/alternative_versions/test_mrecarray.py	2007-03-20 19:31:18 UTC (rev 2860)
+++ trunk/Lib/sandbox/maskedarray/alternative_versions/test_mrecarray.py	2007-03-21 21:28:05 UTC (rev 2861)
@@ -0,0 +1,128 @@
+# pylint: disable-msg=W0611, W0612, W0511,R0201
+"""Tests suite for mrecarray.
+
+:author: Pierre Gerard-Marchant
+:contact: pierregm_at_uga_dot_edu
+:version: $Id: test_mrecarray.py 68 2007-01-10 17:46:04Z backtopop $
+"""
+__author__ = "Pierre GF Gerard-Marchant ($Author: backtopop $)"
+__version__ = '1.0'
+__revision__ = "$Revision: 68 $"
+__date__     = '$Date: 2007-01-10 12:46:04 -0500 (Wed, 10 Jan 2007) $'
+
+import types
+
+import numpy as N
+import numpy.core.fromnumeric  as fromnumeric
+from numpy.testing import NumpyTest, NumpyTestCase
+from numpy.testing.utils import build_err_msg
+
+import maskedarray.testutils
+from maskedarray.testutils import *
+
+import maskedarray.core as MA
+import maskedarray.mrecords
+from maskedarray.mrecords import mrecarray, fromarrays, fromtextfile, fromrecords
+
+
+#..............................................................................
+class test_mrecarray(NumpyTestCase):
+    "Base test class for MaskedArrays."
+    def __init__(self, *args, **kwds):
+        NumpyTestCase.__init__(self, *args, **kwds)
+        self.setup()
+        
+    def setup(self):       
+        "Generic setup" 
+        d = N.arange(5)
+        m = MA.make_mask([1,0,0,1,1])
+        base_d = N.r_[d,d[::-1]].reshape(2,-1).T
+        base_m = N.r_[[m, m[::-1]]].T
+        base = MA.array(base_d, mask=base_m)    
+        mrec = fromarrays(base.T,)
+        self.data = [d, m, mrec]
+        
+    def test_get(self):
+        "Tests fields retrieval"
+        [d, m, mrec] = self.data
+        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, mrec['f0'])
+        
+    def test_set(self):
+        "Tests setting fields/attributes."
+        [d, m, mrec] = self.data
+        mrec.f0_data = 5
+        assert_equal(mrec['f0_data'], [5,5,5,5,5])
+        mrec.f0 = 1
+        assert_equal(mrec['f0_data'], [1]*5)
+        assert_equal(mrec['f0_mask'], [0]*5)
+        mrec.f1 = MA.masked
+        assert_equal(mrec.f1.mask, [1]*5)
+        assert_equal(mrec['f1_mask'], [1]*5)
+        mrec._mask = MA.masked
+        assert_equal(mrec['f1_mask'], [1]*5)
+        assert_equal(mrec['f0_mask'],mrec['f1_mask'])
+        mrec._mask = MA.nomask
+        assert_equal(mrec['f1_mask'], [0]*5)
+        assert_equal(mrec['f0_mask'],mrec['f1_mask'])    
+        
+    def test_hardmask(self):
+        "Test hardmask"
+        [d, m, mrec] = self.data
+        print mrec._mask
+        mrec.harden_mask()
+        assert(mrec._hardmask)
+        mrec._mask = nomask
+        assert_equal(mrec._mask, N.r_[[m,m[::-1]]].all(0))
+        mrec.soften_mask()
+        assert(not mrec._hardmask)
+        mrec._mask = nomask
+        assert_equal(mrec['f1_mask'], [0]*5)
+        assert_equal(mrec['f0_mask'],mrec['f1_mask'])      
+        
+    def test_fromtextfile(self):        
+        "Tests reading from a text file."
+        fcontent = """#
+'One (S)','Two (I)','Three (F)','Four (M)','Five (-)','Six (C)'
+'strings',1,1.0,'mixed column',,1
+'with embedded "double quotes"',2,2.0,1.0,,1
+'strings',3,3.0E5,3,,1
+'strings',4,-1e-10,,,1
+"""    
+        import os
+        from datetime import datetime
+        fname = 'tmp%s' % datetime.now().strftime("%y%m%d%H%M%S%s")
+        f = open(fname, 'w')
+        f.write(fcontent)
+        f.close()
+        mrectxt = fromtextfile(fname,delimitor=',',varnames='ABCDEFG')        
+        os.unlink(fname)
+        #
+        assert(isinstance(mrectxt, mrecarray))
+        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_fromrecords(self):
+        "Test from recarray."
+        [d, m, mrec] = self.data 
+        nrec = N.core.records.fromarrays(N.r_[[d,d[::-1]]])
+        mrecfr = fromrecords(nrec.tolist())
+        assert_equal(mrecfr.f0, mrec.f0)
+        assert_equal(mrecfr.dtype, mrec.dtype)
+        #....................
+        mrecfr = fromrecords(nrec)
+        assert_equal(mrecfr.f0, mrec.f0)
+        assert_equal(mrecfr.dtype, mrec.dtype)
+        #....................
+        tmp = mrec[::-1] #.tolist()
+        mrecfr = fromrecords(tmp)
+        assert_equal(mrecfr.f0, mrec.f0[::-1])
+        
+###############################################################################
+#------------------------------------------------------------------------------
+if __name__ == "__main__":
+    NumpyTest().run()        
\ No newline at end of file

Modified: trunk/Lib/sandbox/maskedarray/core.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/core.py	2007-03-20 19:31:18 UTC (rev 2860)
+++ trunk/Lib/sandbox/maskedarray/core.py	2007-03-21 21:28:05 UTC (rev 2861)
@@ -990,22 +990,25 @@
                           DeprecationWarning)
             small_mask = flag
         # Process data............
-        _data = numeric.array(data, dtype=dtype, copy=copy, subok=True)
-        _baseclass = getattr(_data, '_baseclass', type(_data))
-        if not isinstance(data, MaskedArray) or not subok:
+        _data = numeric.array(data, dtype=dtype, copy=copy, subok=subok)
+        _baseclass = getattr(data, '_baseclass', type(_data))
+        _basedict = getattr(data, '_basedict', getattr(data, '__dict__', None))
+        if not isinstance(data, MaskedArray): 
             _data = _data.view(cls)
-        # Process mask ...........
-        # Backwards compat
+        elif not subok:
+            _data = _data.view(cls)
+        else:
+            _data = _data.view(type(data))
+        # Backwards compat .......
         if hasattr(data,'_mask') and not isinstance(data, ndarray):
             _data._mask = data._mask
-            _data._sharedmask = True
+            _sharedmask = True
+        # Process mask ...........
         if mask is nomask:
-            if _data._mask is not nomask:
-                if copy:
-                    _data._mask = data._mask.copy()
-                    _data._sharedmask = False
-                if not keep_mask:
-                    _data._mask = nomask
+            if not keep_mask:
+                _data._mask = nomask
+            if copy:
+                _data._mask = _data._mask.copy()
         else:
             mask = numeric.array(mask, dtype=MaskType, copy=copy)
             if mask.shape != _data.shape:
@@ -1018,21 +1021,27 @@
                     msg = "Mask and data not compatible: data size is %i, "+\
                           "mask size is %i."
                     raise MAError, msg % (nd, nm)
-            if _data._mask is nomask or not keep_mask:
+            if _data._mask is nomask:
                 _data._mask = mask
                 _data._sharedmask = True
             else:
-                _data._mask = _data._mask.copy()
-                _data._mask.__ior__(mask) 
+                # Make a copy of the mask to avoid propagation
                 _data._sharedmask = False
-        # Process extra options ..
-        # Update fille_value
+                if not keep_mask:
+                    _data._mask = mask
+                else:
+                    _data._mask = umath.logical_or(mask, _data._mask) 
+                    
+                    
+        # Update fill_value.......
         _data._fill_value = getattr(data, '_fill_value', fill_value)
         if _data._fill_value is None:
             _data._fill_value = default_fill_value(_data)
+        # Process extra options ..
         _data._hardmask = hard_mask
         _data._smallmask = small_mask
         _data._baseclass = _baseclass
+        _data._basedict = _basedict
         return _data
     #........................
     def __array_finalize__(self,obj):
@@ -1048,6 +1057,10 @@
         self._sharedmask = True
         self._baseclass = getattr(obj, '_baseclass', type(obj))
         self._fill_value = getattr(obj, '_fill_value', None)
+        # Update special attributes ...
+        self._basedict = getattr(obj, '_basedict', getattr(obj, '__dict__', None))
+        if self._basedict is not None:
+            self.__dict__.update(self._basedict)
         return
     #..................................
     def __array_wrap__(self, obj, context=None):
@@ -1179,7 +1192,7 @@
                 self._mask.flat = newmask
         if self._mask.shape:
             self._mask = numeric.reshape(self._mask, self.shape)
-    _setmask = __setmask__
+    _set_mask = __setmask__
     
     def _get_mask(self):
         """Returns the current mask."""

Modified: trunk/Lib/sandbox/maskedarray/mrecords.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/mrecords.py	2007-03-20 19:31:18 UTC (rev 2860)
+++ trunk/Lib/sandbox/maskedarray/mrecords.py	2007-03-21 21:28:05 UTC (rev 2861)
@@ -226,7 +226,7 @@
         # We want a field ........
         if isinstance(indx, str):           
             obj = _data[indx].view(MaskedArray)
-            obj._setmask(_localdict['_fieldmask'][indx])
+            obj._set_mask(_localdict['_fieldmask'][indx])
             return obj
         # We want some elements ..
         obj = ndarray.__getitem__(self, indx).view(type(self))

Modified: trunk/Lib/sandbox/maskedarray/tests/test_subclassing.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/tests/test_subclassing.py	2007-03-20 19:31:18 UTC (rev 2860)
+++ trunk/Lib/sandbox/maskedarray/tests/test_subclassing.py	2007-03-21 21:28:05 UTC (rev 2861)
@@ -114,10 +114,15 @@
         assert isinstance(z._data, SubArray)
         assert z._data.info['added'] > 0
         #
-        ym._setmask([1,0,0,0,1])
+        ym._set_mask([1,0,0,0,1])
         assert_equal(ym._mask, [1,0,0,0,1])
-        ym._series._setmask([0,0,0,0,1])
+        ym._series._set_mask([0,0,0,0,1])
         assert_equal(ym._mask, [0,0,0,0,1])
+        #
+        xsub = subarray(x, info={'name':'x'})
+        mxsub = masked_array(xsub)
+        assert hasattr(mxsub, 'info')
+        assert_equal(mxsub.info, xsub.info)
     
     def check_subclasspreservation(self):
         "Checks that masked_array(...,subok=True) preserves the class."
@@ -126,13 +131,13 @@
         xinfo = [(i,j) for (i,j) in zip(x,m)]
         xsub = MSubArray(x, mask=m, info={'xsub':xinfo})
         #
+        mxsub = masked_array(xsub, subok=False)
+        assert not isinstance(mxsub, MSubArray)
+        assert isinstance(mxsub, MaskedArray)
+        #
         mxsub = masked_array(xsub, subok=True)
         assert isinstance(mxsub, MSubArray)
         assert_equal(mxsub.info, xsub.info)
-        #
-        mxsub = masked_array(xsub, subok=False)
-        assert not isinstance(mxsub, MSubArray)
-        assert isinstance(mxsub, MaskedArray)
         
         
 ################################################################################

Modified: trunk/Lib/sandbox/maskedarray/testutils.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/testutils.py	2007-03-20 19:31:18 UTC (rev 2860)
+++ trunk/Lib/sandbox/maskedarray/testutils.py	2007-03-21 21:28:05 UTC (rev 2861)
@@ -178,7 +178,7 @@
     number of decimals."""
     def compare(x, y):
         "Returns the result of the loose comparison between x and y)."
-        return approx(x,y)
+        return approx(x,y, rtol=10.**-decimal)
     assert_array_compare(compare, x, y, err_msg=err_msg, 
                          header='Arrays are not almost equal')
 #............................



More information about the Scipy-svn mailing list