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

scipy-svn@scip... scipy-svn@scip...
Thu Mar 1 15:15:04 CST 2007


Author: pierregm
Date: 2007-03-01 15:14:59 -0600 (Thu, 01 Mar 2007)
New Revision: 2802

Modified:
   trunk/Lib/sandbox/maskedarray/core.py
   trunk/Lib/sandbox/maskedarray/tests/test_core.py
   trunk/Lib/sandbox/maskedarray/tests/test_subclassing.py
Log:
core                   : masked_array now accepts a subok argument to prevent the conversion of a  MaskedArray subclass to plain MaskedArray
tests/test_subclassing : update

Modified: trunk/Lib/sandbox/maskedarray/core.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/core.py	2007-03-01 19:04:00 UTC (rev 2801)
+++ trunk/Lib/sandbox/maskedarray/core.py	2007-03-01 21:14:59 UTC (rev 2802)
@@ -980,7 +980,7 @@
     _baseclass =  numeric.ndarray
     def __new__(cls, data=None, mask=nomask, dtype=None, copy=False, fill_value=None,
                 keep_mask=True, small_mask=True, hard_mask=False, flag=None,
-                **options):
+                subok=True, **options):
         """array(data, dtype=None, copy=True, mask=nomask, fill_value=None)
 
 If `data` is already a ndarray, its dtype becomes the default value of dtype.
@@ -992,7 +992,8 @@
         # Process data............
         _data = numeric.array(data, dtype=dtype, copy=copy, subok=True)
         _baseclass = getattr(_data, '_baseclass', type(_data))
-        _data = _data.view(cls)
+        if not isinstance(data, MaskedArray) or not subok:
+            _data = _data.view(cls)
         # Process mask ...........
         # Backwards compat
         if hasattr(data,'_mask') and not isinstance(data, ndarray):

Modified: trunk/Lib/sandbox/maskedarray/tests/test_core.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/tests/test_core.py	2007-03-01 19:04:00 UTC (rev 2801)
+++ trunk/Lib/sandbox/maskedarray/tests/test_core.py	2007-03-01 21:14:59 UTC (rev 2802)
@@ -1106,6 +1106,7 @@
         assert_equal(sortedx._mask, [1,1,0,0,0])
     
     def check_ravel(self):
+        "Tests ravel"
         a = array([[1,2,3,4,5]], mask=[[0,1,0,0,0]])
         aravel = a.ravel()
         assert_equal(a._mask.shape, a.shape)

Modified: trunk/Lib/sandbox/maskedarray/tests/test_subclassing.py
===================================================================
--- trunk/Lib/sandbox/maskedarray/tests/test_subclassing.py	2007-03-01 19:04:00 UTC (rev 2801)
+++ trunk/Lib/sandbox/maskedarray/tests/test_subclassing.py	2007-03-01 21:14:59 UTC (rev 2802)
@@ -118,20 +118,27 @@
         assert_equal(ym._mask, [1,0,0,0,1])
         ym._series._setmask([0,0,0,0,1])
         assert_equal(ym._mask, [0,0,0,0,1])
+    
+    def check_subclasspreservation(self):
+        "Checks that masked_array(...,subok=True) preserves the class."
+        x = N.arange(5)
+        m = [0,0,1,0,0]
+        xinfo = [(i,j) for (i,j) in zip(x,m)]
+        xsub = MSubArray(x, mask=m, info={'xsub':xinfo})
+        #
+        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)
         
         
 ################################################################################
 if __name__ == '__main__':
     NumpyTest().run()
-    if 1:
-        x = N.arange(5)
-        m = [0,0,1,0,0]
-        xinfo = [(i,j) for (i,j) in zip(x,m)]
-        xsub = MSubArray(x, mask=m, info={'xsub':xinfo})
-        #
-        xsub_low = less(xsub,3)
-        assert isinstance(xsub, MSubArray)
-        assert_equal(xsub_low.info, xinfo)
+
                      
 
 



More information about the Scipy-svn mailing list