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

scipy-svn@scip... scipy-svn@scip...
Fri Jun 15 11:15:08 CDT 2007


Author: pierregm
Date: 2007-06-15 11:14:55 -0500 (Fri, 15 Jun 2007)
New Revision: 3107

Modified:
   trunk/Lib/sandbox/maskedarray/core.py
   trunk/Lib/sandbox/maskedarray/extras.py
   trunk/Lib/sandbox/maskedarray/mrecords.py
   trunk/Lib/sandbox/maskedarray/tests/test_core.py
   trunk/Lib/sandbox/maskedarray/tests/test_extras.py
   trunk/Lib/sandbox/maskedarray/tests/test_mrecords.py
Log:
core     : __setitem__ now calls __setmask__ when value is masked
core     : added a test in filled on m.any()
extras   : make_rowcols : make sure the mask is copied to avoid propagation
mrecords : fixed a couple of bugs

Modified: trunk/Lib/sandbox/maskedarray/core.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/core.py	2007-06-15 01:00:10 UTC (rev 3106)
+++ trunk/Lib/sandbox/maskedarray/core.py	2007-06-15 16:14:55 UTC (rev 3107)
@@ -1132,11 +1132,13 @@
 #            raise IndexError, msg
         #....
         if value is masked:
-            if self._mask is nomask:
-                self._mask = make_mask_none(self.shape)
-            else:
-                self._mask = self._mask.copy()
-            self._mask[indx] = True
+            m = self._mask
+            if m is nomask:
+                m = make_mask_none(self.shape)
+#            else:
+#                m = m.copy()
+            m[indx] = True
+            self.__setmask__(m)
             return
         #....
         dval = numeric.asarray(value).astype(self.dtype)
@@ -1261,7 +1263,7 @@
 If `fill_value` is None, uses self.fill_value.
         """
         m = self._mask
-        if m is nomask:
+        if m is nomask or not m.any():
             return self._data
         #
         if fill_value is None:
@@ -2645,4 +2647,13 @@
         assert(a.ravel()._mask, [0,0,0,0])
         assert(a.compressed(), a)
         a[0] = masked
-        assert(a.compressed()._mask, [0,0,0])
\ No newline at end of file
+        assert(a.compressed()._mask, [0,0,0])
+    if 1:
+        x = array(0, mask=0)
+        I = x.ctypes.data
+        J = x.filled().ctypes.data
+        print (I,J)
+        x = array([0,0], mask=0)
+        (I,J) = (x.ctypes.data, x.filled().ctypes.data)
+        print (I,J)
+        
\ No newline at end of file

Modified: trunk/Lib/sandbox/maskedarray/extras.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/extras.py	2007-06-15 01:00:10 UTC (rev 3106)
+++ trunk/Lib/sandbox/maskedarray/extras.py	2007-06-15 16:14:55 UTC (rev 3107)
@@ -411,6 +411,7 @@
     if m is nomask or not m.any():
         return a
     maskedval = m.nonzero()
+    a._mask = a._mask.copy()
     if not axis:
         a[function_base.unique(maskedval[0])] = masked
     if axis in [None, 1, -1]:
@@ -648,7 +649,15 @@
 ################################################################################
 if __name__ == '__main__':
     #
+    import numpy as N
+    from maskedarray.testutils import assert_equal
     if 1:
-        x = arange(10)
-        x[0] = masked
-        print dot(x,x)
\ No newline at end of file
+        n = N.arange(1,7)
+        #
+        m = [1,0,0,0,0,0]
+        a = masked_array(n, mask=m).reshape(2,3)
+        b = masked_array(n, mask=m).reshape(3,2)
+        c = dot(a,b, True)
+        assert_equal(c.mask, [[1,1],[1,0]])
+        c = dot(a,b,False)
+        assert_equal(c, N.dot(a.filled(0), b.filled(0)))
\ No newline at end of file

Modified: trunk/Lib/sandbox/maskedarray/mrecords.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/mrecords.py	2007-06-15 01:00:10 UTC (rev 3106)
+++ trunk/Lib/sandbox/maskedarray/mrecords.py	2007-06-15 16:14:55 UTC (rev 3107)
@@ -115,7 +115,7 @@
         if names is not None:
             descr = _checknames(descr,names)
         _names = descr.names    
-        mdescr = [(t[0],'|b1') for t in descr.descr]
+        mdescr = [(n,'|b1') for n in _names]
         #
         shape = numeric.asarray(data[0]).shape
         if isinstance(shape, int):
@@ -129,7 +129,11 @@
             _fieldmask = data._fieldmask
         elif isinstance(data, recarray):
             _data = data
-            _fieldmask = mask
+            if mask is nomask:
+                _fieldmask = data.astype(mdescr)
+                _fieldmask.flat = tuple([False]*len(mdescr))
+            else:
+                _fieldmask = mask
         else:
             _data = recarray(shape, dtype=descr)
             _fieldmask = recarray(shape, dtype=mdescr)
@@ -179,7 +183,7 @@
             _data = self._data
             _mask = self._fieldmask
             obj = numeric.asarray(_data.__getattribute__(attr)).view(MaskedArray)
-            obj._mask = make_mask(_mask.__getattribute__(attr))
+            obj.__setmask__(_mask.__getattribute__(attr))
             return obj
         raise AttributeError,"No attribute '%s' !" % attr
             
@@ -232,6 +236,10 @@
         obj = ndarray.__getitem__(self, indx).view(type(self))
         obj._fieldmask = _localdict['_fieldmask'][indx]
         return obj
+    #............................................
+    def __setitem__(self, indx, value):
+        """Sets the given record to value."""
+        MaskedArray.__setitem__(self, indx, value)
         
 #    def __getslice__(self, i, j):
 #        """Returns the slice described by [i,j]."""
@@ -243,13 +251,12 @@
     def __setslice__(self, i, j, value):
         """Sets the slice described by [i,j] to `value`."""
         _localdict = self.__dict__
-        
         d = self._data
         m = _localdict['_fieldmask']
         names = self.dtype.names
         if value is masked:
             for n in names:
-                m[i:j][n] = masked
+                m[i:j][n] = True
         elif not self._hardmask:
             fval = filled(value)
             mval = getmaskarray(value)
@@ -484,7 +491,7 @@
         descr = parsed._descr
 
     try:
-        retval = numeric.array(reclist, dtype = descr)
+        retval = numeric.array(reclist, dtype = descr).view(recarray)
     except TypeError:  # list of lists instead of list of tuples
         if (shape is None or shape == 0):
             shape = len(reclist)*2
@@ -645,13 +652,27 @@
 ################################################################################
 if __name__ == '__main__':
     import numpy as N
+    from maskedarray.testutils import assert_equal
     if 1:
         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)    
-        mrecord = fromarrays(base.T,)
-        
+        mrecord = fromarrays(base.T,dtype=[('a',N.float_),('b',N.float_)])
         mrec = MaskedRecords(mrecord)
+        #
+        mrec.a[3:] = 5
+        assert_equal(mrec.a, [0,1,2,5,5])
+        assert_equal(mrec.a._mask, [1,0,0,0,0])
+        #
+        mrec.b[3:] = masked
+        assert_equal(mrec.b, [4,3,2,1,0])
+        assert_equal(mrec.b._mask, [1,1,0,1,1])
+        #
+        mrec[:2] = masked
+        assert_equal(mrec._mask, [1,1,0,0,0])
+        mrec[-1] = masked
+        assert_equal(mrec._mask, [1,1,0,0,1])
+
         
\ No newline at end of file

Modified: trunk/Lib/sandbox/maskedarray/tests/test_core.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/tests/test_core.py	2007-06-15 01:00:10 UTC (rev 3106)
+++ trunk/Lib/sandbox/maskedarray/tests/test_core.py	2007-06-15 16:14:55 UTC (rev 3107)
@@ -240,7 +240,6 @@
         assert(minimum(xm, xm).mask)
         assert(xm.filled().dtype is xm.data.dtype)
         x = array(0, mask=0)
-#        assert(x.filled() is x.data)
         assert_equal(x.filled().ctypes.data, x.ctypes.data)
         assert_equal(str(xm), str(masked_print_option))
     #.........................

Modified: trunk/Lib/sandbox/maskedarray/tests/test_extras.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/tests/test_extras.py	2007-06-15 01:00:10 UTC (rev 3106)
+++ trunk/Lib/sandbox/maskedarray/tests/test_extras.py	2007-06-15 16:14:55 UTC (rev 3107)
@@ -219,18 +219,27 @@
         m = [1,0,0,0,0,0]
         a = masked_array(n, mask=m).reshape(2,3)
         b = masked_array(n, mask=m).reshape(3,2)
-        c = dot(a,b)
+        c = dot(a,b,True)
         assert_equal(c.mask, [[1,1],[1,0]])
-        c = dot(b,a)
+        c = dot(b,a,True)
         assert_equal(c.mask, [[1,1,1],[1,0,0],[1,0,0]])
+        c = dot(a,b,False)
+        assert_equal(c, N.dot(a.filled(0), b.filled(0)))
+        c = dot(b,a,False)
+        assert_equal(c, N.dot(b.filled(0), a.filled(0)))
         #        
         m = [0,0,0,0,0,1]
         a = masked_array(n, mask=m).reshape(2,3)
         b = masked_array(n, mask=m).reshape(3,2)
-        c = dot(a,b)
+        c = dot(a,b,True)
         assert_equal(c.mask,[[0,1],[1,1]])        
-        c = dot(b,a)
+        c = dot(b,a,True)
         assert_equal(c.mask, [[0,0,1],[0,0,1],[1,1,1]])
+        c = dot(a,b,False)
+        assert_equal(c, N.dot(a.filled(0), b.filled(0)))
+        assert_equal(c, dot(a,b))
+        c = dot(b,a,False)
+        assert_equal(c, N.dot(b.filled(0), a.filled(0)))
         #        
         m = [0,0,0,0,0,0]
         a = masked_array(n, mask=m).reshape(2,3)
@@ -242,24 +251,36 @@
         #        
         a = masked_array(n, mask=[1,0,0,0,0,0]).reshape(2,3)
         b = masked_array(n, mask=[0,0,0,0,0,0]).reshape(3,2)
-        c = dot(a,b)
+        c = dot(a,b,True)
         assert_equal(c.mask,[[1,1],[0,0]])
-        c = dot(b,a)
+        c = dot(a,b,False)
+        assert_equal(c, N.dot(a.filled(0),b.filled(0)))
+        c = dot(b,a,True)
         assert_equal(c.mask,[[1,0,0],[1,0,0],[1,0,0]])
+        c = dot(b,a,False)
+        assert_equal(c, N.dot(b.filled(0),a.filled(0)))
         #        
         a = masked_array(n, mask=[0,0,0,0,0,1]).reshape(2,3)
         b = masked_array(n, mask=[0,0,0,0,0,0]).reshape(3,2)
+        c = dot(a,b,True)
+        assert_equal(c.mask,[[0,0],[1,1]])
         c = dot(a,b)
-        assert_equal(c.mask,[[0,0],[1,1]])
-        c = dot(b,a)
+        assert_equal(c, N.dot(a.filled(0),b.filled(0)))        
+        c = dot(b,a,True)
         assert_equal(c.mask,[[0,0,1],[0,0,1],[0,0,1]])
+        c = dot(b,a,False)
+        assert_equal(c, N.dot(b.filled(0), a.filled(0)))
         #        
         a = masked_array(n, mask=[0,0,0,0,0,1]).reshape(2,3)
         b = masked_array(n, mask=[0,0,1,0,0,0]).reshape(3,2)
-        c = dot(a,b)
+        c = dot(a,b,True)
         assert_equal(c.mask,[[1,0],[1,1]])
-        c = dot(b,a)
+        c = dot(a,b,False)
+        assert_equal(c, N.dot(a.filled(0),b.filled(0)))
+        c = dot(b,a,True)
         assert_equal(c.mask,[[0,0,1],[1,1,1],[0,0,1]])
+        c = dot(b,a,False)
+        assert_equal(c, N.dot(b.filled(0),a.filled(0)))
         
     def test_mediff1d(self):    
         "Tests mediff1d"    

Modified: trunk/Lib/sandbox/maskedarray/tests/test_mrecords.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/tests/test_mrecords.py	2007-06-15 01:00:10 UTC (rev 3106)
+++ trunk/Lib/sandbox/maskedarray/tests/test_mrecords.py	2007-06-15 16:14:55 UTC (rev 3107)
@@ -41,54 +41,74 @@
         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)    
-        mrecord = fromarrays(base.T,)
+        mrecord = fromarrays(base.T, dtype=[('a',N.float_),('b',N.float_)])
         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_equal(mrec.a, MA.array(d,mask=m))
+        assert_equal(mrec.b, 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_equal(mrec.a[1], mrec[1].a)
         #
         assert(isinstance(mrec[:2], MaskedRecords))
-        assert_equal(mrec[:2]['f0'], d[:2])
+        assert_equal(mrec[:2]['a'], d[:2])
         
     def test_set(self):
         "Tests setting fields/attributes."
         [d, m, mrecord] = self.data
-        mrecord.f0._data[:] = 5
-        assert_equal(mrecord['f0']._data, [5,5,5,5,5])
-        mrecord.f0 = 1
-        assert_equal(mrecord['f0']._data, [1]*5)
-        assert_equal(getmaskarray(mrecord['f0']), [0]*5)
-        mrecord.f1 = MA.masked
-        assert_equal(mrecord.f1.mask, [1]*5)
-        assert_equal(getmaskarray(mrecord['f1']), [1]*5)
+        mrecord.a._data[:] = 5
+        assert_equal(mrecord['a']._data, [5,5,5,5,5])
+        mrecord.a = 1
+        assert_equal(mrecord['a']._data, [1]*5)
+        assert_equal(getmaskarray(mrecord['a']), [0]*5)
+        mrecord.b = MA.masked
+        assert_equal(mrecord.b.mask, [1]*5)
+        assert_equal(getmaskarray(mrecord['b']), [1]*5)
         mrecord._mask = MA.masked
-        assert_equal(getmaskarray(mrecord['f1']), [1]*5)
-        assert_equal(mrecord['f0']._mask, mrecord['f1']._mask)
+        assert_equal(getmaskarray(mrecord['b']), [1]*5)
+        assert_equal(mrecord['a']._mask, mrecord['b']._mask)
         mrecord._mask = MA.nomask
-        assert_equal(getmaskarray(mrecord['f1']), [0]*5)
-        assert_equal(mrecord['f0']._mask, mrecord['f1']._mask)   
+        assert_equal(getmaskarray(mrecord['b']), [0]*5)
+        assert_equal(mrecord['a']._mask, mrecord['b']._mask)   
         #
+    def test_setfields(self):
+        "Tests setting fields."
+        [d, m, mrecord] = self.data
+        mrecord.a[3:] = 5
+        assert_equal(mrecord.a, [0,1,2,5,5])
+        assert_equal(mrecord.a._mask, [1,0,0,0,0])
+        #
+        mrecord.b[3:] = masked
+        assert_equal(mrecord.b, [4,3,2,1,0])
+        assert_equal(mrecord.b._mask, [1,1,0,1,1])
+        
     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])
+        assert_equal(mrec.a._data, [5,5,2,3,4])
+        assert_equal(mrec.b._data, [5,5,2,1,0])
+        assert_equal(mrec.a._mask, [0,0,0,1,1])
+        assert_equal(mrec.b._mask, [0,0,0,0,1])
+        #
+        mrec[:2] = masked
+        assert_equal(mrec._mask, [1,1,0,0,1])
+        mrec[-2] = masked
+        assert_equal(mrec._mask, [1,1,0,1,1])
+        #
+    def test_setslices_hardmask(self):
+        "Tests setting slices w/ hardmask."
+        [d, m, mrec] = self.data      
         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]) 
+        assert_equal(mrec.a._data, [0,1,2,3,4])
+        assert_equal(mrec.b._data, [4,3,2,5,0])
+        assert_equal(mrec.a._mask, [1,0,0,1,1])
+        assert_equal(mrec.b._mask, [1,1,0,0,1]) 
         
     def test_hardmask(self):
         "Test hardmask"
@@ -101,24 +121,26 @@
         mrec.soften_mask()
         assert(not mrec._hardmask)
         mrec._mask = nomask
-        assert(mrec['f1']._mask is nomask)
-        assert_equal(mrec['f0']._mask,mrec['f1']._mask)   
+        assert(mrec['b']._mask is nomask)
+        assert_equal(mrec['a']._mask,mrec['b']._mask)   
 
     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)
+        nrec = N.core.records.fromarrays(N.r_[[d,d[::-1]]],
+                                         dtype=[('a',N.float_),('b',N.float_)])
         #....................
         mrecfr = fromrecords(nrec)
-        assert_equal(mrecfr.f0, mrec.f0)
+        assert_equal(mrecfr.a, mrec.a)
         assert_equal(mrecfr.dtype, mrec.dtype)
         #....................
         tmp = mrec[::-1] #.tolist()
         mrecfr = fromrecords(tmp)
-        assert_equal(mrecfr.f0, mrec.f0[::-1])
+        assert_equal(mrecfr.a, mrec.a[::-1])
+        #....................        
+        mrecfr = fromrecords(nrec.tolist())
+        assert_equal(mrecfr.a, mrec.a)
+        assert_equal(mrecfr.dtype, mrec.dtype)
         
     def test_fromtextfile(self):        
         "Tests reading from a text file."



More information about the Scipy-svn mailing list