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

numpy-svn at scipy.org numpy-svn at scipy.org
Wed May 28 22:31:36 EDT 2008


Author: pierregm
Date: 2008-05-28 21:31:28 -0500 (Wed, 28 May 2008)
New Revision: 5244

Modified:
   trunk/numpy/ma/core.py
   trunk/numpy/ma/extras.py
   trunk/numpy/ma/mrecords.py
   trunk/numpy/ma/tests/test_mrecords.py
Log:
mrecords : Make sure a field shares its mask with the whole array
mrecords : IMPORTANT : the mask of a field is no longer set to nomask when it's full of False, which simplifies masking specific fields.
extras   : Reorganized personal comments

Modified: trunk/numpy/ma/core.py
===================================================================
--- trunk/numpy/ma/core.py	2008-05-26 22:21:13 UTC (rev 5243)
+++ trunk/numpy/ma/core.py	2008-05-29 02:31:28 UTC (rev 5244)
@@ -1633,7 +1633,7 @@
                     else:
                         return str(self._data)
                 # convert to object array to make filled work
-#CHECK: the two lines below seem more robust than the self._data.astype
+#!!!: the two lines below seem more robust than the self._data.astype
 #                res = numeric.empty(self._data.shape, object_)
 #                numeric.putmask(res,~m,self._data)
                 res = self._data.astype("|O8")
@@ -2032,7 +2032,7 @@
         indicated `axis1` and `axis2`.
 
         """
-        # TODO: What are we doing with `out`?
+        #!!!: implement out + test!
         m = self._mask
         if m is nomask:
             result = super(MaskedArray, self).trace(offset=offset, axis1=axis1,
@@ -2207,7 +2207,7 @@
 
         """
         if self._mask is nomask:
-            # TODO: Do we keep super, or var _data and take a view ?
+            #???: Do we keep super, or var _data and take a view ?
             return super(MaskedArray, self).var(axis=axis, dtype=dtype,
                                                 ddof=ddof)
         else:
@@ -2676,7 +2676,7 @@
     for convenience.  And backwards compatibility...
 
     """
-    #TODO: we should try to put 'order' somwehere
+    #!!!: we should try to put 'order' somwehere
     return MaskedArray(data, mask=mask, dtype=dtype, copy=copy, subok=subok,
                        keep_mask=keep_mask, hard_mask=hard_mask,
                        fill_value=fill_value, ndmin=ndmin, shrink=shrink)
@@ -3186,7 +3186,7 @@
 
 def choose (indices, t, out=None, mode='raise'):
     "Return array shaped like indices with elements chosen from t"
-    #TODO: implement options `out` and `mode`, if possible.
+    #!!!: implement options `out` and `mode`, if possible + test.
     def fmask (x):
         "Returns the filled array, or True if masked."
         if x is masked:

Modified: trunk/numpy/ma/extras.py
===================================================================
--- trunk/numpy/ma/extras.py	2008-05-26 22:21:13 UTC (rev 5243)
+++ trunk/numpy/ma/extras.py	2008-05-29 02:31:28 UTC (rev 5244)
@@ -567,7 +567,7 @@
         The first argument is not conjugated.
 
     """
-    #TODO: Works only with 2D arrays. There should be a way to get it to run with higher dimension
+    #!!!: Works only with 2D arrays. There should be a way to get it to run with higher dimension
     if strict and (a.ndim == 2) and (b.ndim == 2):
         a = mask_rows(a)
         b = mask_cols(b)

Modified: trunk/numpy/ma/mrecords.py
===================================================================
--- trunk/numpy/ma/mrecords.py	2008-05-26 22:21:13 UTC (rev 5243)
+++ trunk/numpy/ma/mrecords.py	2008-05-29 02:31:28 UTC (rev 5244)
@@ -6,11 +6,11 @@
 
 :author: Pierre Gerard-Marchant
 """
-#TODO: We should make sure that no field is called '_mask','mask','_fieldmask',
-#TODO: ...or whatever restricted keywords.
-#TODO: An idea would be to no bother in the first place, and then rename the
-#TODO: invalid fields with a trailing underscore...
-#TODO: Maybe we could just overload the parser function ?
+#!!!: * We should make sure that no field is called '_mask','mask','_fieldmask',
+#!!!:   or whatever restricted keywords.
+#!!!:   An idea would be to no bother in the first place, and then rename the
+#!!!:   invalid fields with a trailing underscore...
+#!!!:   Maybe we could just overload the parser function ?
 
 
 __author__ = "Pierre GF Gerard-Marchant"
@@ -51,9 +51,6 @@
     formats = ''
     for obj in data:
         obj = np.asarray(obj)
-#        if not isinstance(obj, ndarray):
-##        if not isinstance(obj, ndarray):
-#            raise ValueError, "item in the array list must be an ndarray."
         formats += _typestr[obj.dtype.type]
         if issubclass(obj.dtype.type, ntypes.flexible):
             formats += `obj.itemsize`
@@ -124,7 +121,6 @@
         self = recarray.__new__(cls, shape, dtype=dtype, buf=buf, offset=offset,
                                 strides=strides, formats=formats,
                                 byteorder=byteorder, aligned=aligned,)
-#        self = self.view(cls)
         #
         mdtype = [(k,'|b1') for (k,_) in self.dtype.descr]
         if mask is nomask or not np.size(mask):
@@ -331,11 +327,13 @@
         _data = self._data
         # We want a field ........
         if isinstance(indx, basestring):
+            #NB: Make sure _sharedmask is True to propagate back to _fieldmask
+            #NB: Don't use _set_mask, there are some copies being made that break propagation
+            #NB: Don't force the mask to nomask, that wrecks easy masking
             obj = _data[indx].view(MaskedArray)
-            obj._set_mask(_fieldmask[indx])
-            # Force to nomask if the mask is empty
-            if not obj._mask.any():
-                obj._mask = nomask
+#            obj._set_mask(_fieldmask[indx])
+            obj._mask = _fieldmask[indx]
+            obj._sharedmask = True
             # Force to masked if the mask is True
             if not obj.ndim and obj._mask:
                 return masked
@@ -780,4 +778,3 @@
     newdata._fieldmask = newmask
     return newdata
 
-###############################################################################

Modified: trunk/numpy/ma/tests/test_mrecords.py
===================================================================
--- trunk/numpy/ma/tests/test_mrecords.py	2008-05-26 22:21:13 UTC (rev 5243)
+++ trunk/numpy/ma/tests/test_mrecords.py	2008-05-29 02:31:28 UTC (rev 5244)
@@ -132,6 +132,20 @@
         assert_equal(rdata.num, val)
         assert_equal(rdata.num.mask, [1,0,0])
 
+    def test_set_fields_mask(self):
+        "Tests setting the mask of a field."
+        base = self.base.copy()
+        # This one has already a mask....
+        mbase = base.view(mrecarray)
+        mbase['a'][-2] = masked
+        assert_equal(mbase.a, [1,2,3,4,5])
+        assert_equal(mbase.a._mask, [0,1,0,1,1])
+        # This one has not yet
+        mbase = fromarrays([np.arange(5), np.random.rand(5)],
+                           dtype=[('a',int),('b',float)])
+        mbase['a'][-2] = masked
+        assert_equal(mbase.a, [0,1,2,3,4])
+        assert_equal(mbase.a._mask, [0,0,0,1,0])
     #
     def test_set_mask(self):
         base = self.base.copy()
@@ -231,7 +245,8 @@
         mbase.soften_mask()
         assert(not mbase._hardmask)
         mbase._mask = nomask
-        assert(mbase['b']._mask is nomask)
+        # So, the mask of a field is no longer set to nomask...
+        assert(ma.make_mask(mbase['b']._mask) is nomask)
         assert_equal(mbase['a']._mask,mbase['b']._mask)
     #
     def test_pickling(self):
@@ -365,7 +380,7 @@
         f.write(fcontent)
         f.close()
         mrectxt = fromtextfile(fname,delimitor=',',varnames='ABCDEFG')
-        os.unlink(fname)
+        os.remove(fname)
         #
         assert(isinstance(mrectxt, MaskedRecords))
         assert_equal(mrectxt.F, [1,1,1,1])




More information about the Numpy-svn mailing list