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

numpy-svn@scip... numpy-svn@scip...
Sun Aug 3 18:45:52 CDT 2008


Author: pierregm
Date: 2008-08-03 18:45:49 -0500 (Sun, 03 Aug 2008)
New Revision: 5602

Modified:
   trunk/numpy/ma/core.py
   trunk/numpy/ma/tests/test_core.py
Log:
core:
* renamed _basedict to _optinfo
* prevented the back propagation of _optinfo in _update_from : the __dict__ is copied only if the underlying object is not a MaskedArray
* fixed getmaskarray for flexible dtype
* fixed __setitem__ when filling fields (the mask might still be nomask)
* _arraymethod now stores the name of the method in both self._name and self.__name__

Modified: trunk/numpy/ma/core.py
===================================================================
--- trunk/numpy/ma/core.py	2008-08-03 11:11:24 UTC (rev 5601)
+++ trunk/numpy/ma/core.py	2008-08-03 23:45:49 UTC (rev 5602)
@@ -79,6 +79,8 @@
 
 np.seterr(all='ignore')
 
+
+
 def doc_note(note):
     return "\nNotes\n-----\n%s" % note
 
@@ -765,7 +767,7 @@
     """
     mask = getmask(arr)
     if mask is nomask:
-        mask = make_mask_none(np.shape(arr), getdata(arr).dtype.names)
+        mask = make_mask_none(np.shape(arr), getdata(arr).dtype)
     return mask
 
 def is_mask(m):
@@ -1102,7 +1104,7 @@
 
     """
     def __init__(self, funcname, onmask=True):
-        self._name = funcname
+        self._name = self.__name__ = funcname
         self._onmask = onmask
         self.obj = None
         self.__doc__ = self.getdoc()
@@ -1226,7 +1228,7 @@
         # Process data............
         _data = np.array(data, dtype=dtype, copy=copy, subok=True, ndmin=ndmin)
         _baseclass = getattr(data, '_baseclass', type(_data))
-        _basedict = getattr(data, '_basedict', getattr(data, '__dict__', {}))
+        _optinfo = {}
         # Careful, cls might not always be MaskedArray...
         if not isinstance(data, cls) or not subok:
             _data = _data.view(cls)
@@ -1312,7 +1314,7 @@
         # Process extra options ..
         _data._hardmask = hard_mask
         _data._baseclass = _baseclass
-        _data._basedict = _basedict
+        _data._optinfo = _data._basedict = _optinfo
         return _data
     #
     def _update_from(self, obj):
@@ -1323,16 +1325,20 @@
         else:
             _baseclass = ndarray
         # We need to copy the _basedict to avoid backward propagation
-        _basedict = {}
-        _basedict.update(getattr(obj, '_basedict', getattr(obj, '__dict__',{})))
+        _optinfo = {}
+        _optinfo.update(getattr(obj, '_optinfo', {}))
+        _optinfo.update(getattr(obj, '_basedict',{}))
+        if not isinstance(obj, MaskedArray):
+            _optinfo.update(getattr(obj, '__dict__', {}))
         _dict = dict(_fill_value=getattr(obj, '_fill_value', None),
                      _hardmask=getattr(obj, '_hardmask', False),
                      _sharedmask=getattr(obj, '_sharedmask', False),
                      _isfield=getattr(obj, '_isfield', False),
                      _baseclass=getattr(obj,'_baseclass', _baseclass),
-                     _basedict=_basedict,)
+                     _optinfo=_optinfo,
+                     _basedict=_optinfo)
         self.__dict__.update(_dict)
-        self.__dict__.update(_basedict)
+        self.__dict__.update(_optinfo)
         return
     #........................
     def __array_finalize__(self,obj):
@@ -1461,6 +1467,8 @@
 #            raise IndexError, msg
         if isinstance(indx, basestring):
             ndarray.__setitem__(self._data, indx, value)
+            if self._mask is nomask:
+                self._mask = make_mask_none(self.shape, self.dtype)
             ndarray.__setitem__(self._mask, indx, getmask(value))
             return
         #........................................

Modified: trunk/numpy/ma/tests/test_core.py
===================================================================
--- trunk/numpy/ma/tests/test_core.py	2008-08-03 11:11:24 UTC (rev 5601)
+++ trunk/numpy/ma/tests/test_core.py	2008-08-03 23:45:49 UTC (rev 5602)
@@ -437,14 +437,14 @@
                      np.array([(1, '1', 1.)], dtype=flexi.dtype))
 
 
-    def test_basedict_propagation(self):
-        "Checks that basedict isn't back-propagated"
+    def test_optinfo_propagation(self):
+        "Checks that _optinfo dictionary isn't back-propagated"
         x = array([1,2,3,], dtype=float)
-        x._basedict['info'] = '???'
+        x._optinfo['info'] = '???'
         y = x.copy()
-        assert_equal(y._basedict['info'],'???')
-        y._basedict['info'] = '!!!'
-        assert_equal(x._basedict['info'], '???')
+        assert_equal(y._optinfo['info'],'???')
+        y._optinfo['info'] = '!!!'
+        assert_equal(x._optinfo['info'], '???')
 
 #------------------------------------------------------------------------------
 
@@ -918,7 +918,7 @@
         # We had a tailored comment to make sure special attributes are properly
         # dealt with
         a = array(['3', '4', '5'])
-        a._basedict.update({'comment':"updated!"})
+        a._optinfo.update({'comment':"updated!"})
         #
         b = array(a, dtype=int)
         assert_equal(b._data, [3,4,5])
@@ -931,7 +931,7 @@
         b = a.astype(int)
         assert_equal(b._data, [3,4,5])
         assert_equal(b.fill_value, default_fill_value(0))
-        assert_equal(b._basedict['comment'], "updated!")
+        assert_equal(b._optinfo['comment'], "updated!")
         #
         b = a.astype([('a','|S3')])
         assert_equal(b['a']._data, a._data)
@@ -2268,7 +2268,20 @@
         for n in ('a','b','c'):
             assert_equal(base[n].mask, [1,1,0,0,1])
             assert_equal(base[n]._data, base._data[n])
+    #
+    def test_getmaskarray(self):
+        "Test getmaskarray on flexible dtype"
+        ndtype = [('a', int), ('b', float)]
+        test = empty(3, dtype=ndtype)
+        assert_equal(getmaskarray(test),
+                     np.array([(0, 0) , (0, 0), (0, 0)],
+                              dtype=[('a', '|b1'), ('b', '|b1')]))
+        test[:] = masked
+        assert_equal(getmaskarray(test),
+                     np.array([(1, 1) , (1, 1), (1, 1)],
+                              dtype=[('a', '|b1'), ('b', '|b1')]))
 
+
 ###############################################################################
 #------------------------------------------------------------------------------
 if __name__ == "__main__":



More information about the Numpy-svn mailing list