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

numpy-svn@scip... numpy-svn@scip...
Fri Dec 5 14:40:48 CST 2008


Author: pierregm
Date: 2008-12-05 14:40:44 -0600 (Fri, 05 Dec 2008)
New Revision: 6138

Modified:
   trunk/numpy/ma/core.py
   trunk/numpy/ma/tests/test_core.py
Log:
* Added MaskError
* If a bool or int ndarray is given as the explicit output of var/min/max, an exception is raised if the function should have output np.nan

Modified: trunk/numpy/ma/core.py
===================================================================
--- trunk/numpy/ma/core.py	2008-12-04 23:06:29 UTC (rev 6137)
+++ trunk/numpy/ma/core.py	2008-12-05 20:40:44 UTC (rev 6138)
@@ -19,7 +19,7 @@
 __author__ = "Pierre GF Gerard-Marchant"
 __docformat__ = "restructuredtext en"
 
-__all__ = ['MAError', 'MaskType', 'MaskedArray',
+__all__ = ['MAError', 'MaskError', 'MaskType', 'MaskedArray',
            'bool_',
            'abs', 'absolute', 'add', 'all', 'allclose', 'allequal', 'alltrue',
            'amax', 'amin', 'anom', 'anomalies', 'any', 'arange',
@@ -119,6 +119,9 @@
 class MAError(Exception):
     "Class for MA related errors."
     pass
+class MaskError(MAError):
+    "Class for mask related errors."
+    pass
 
 
 #####--------------------------------------------------------------------------
@@ -1478,7 +1481,7 @@
                 else:
                     msg = "Mask and data not compatible: data size is %i, "+\
                           "mask size is %i."
-                    raise MAError, msg % (nd, nm)
+                    raise MaskError, msg % (nd, nm)
                 copy = True
             # Set the mask to the new value
             if _data._mask is nomask:
@@ -1727,7 +1730,7 @@
 
         """
         if self is masked:
-            raise MAError, 'Cannot alter the masked element.'
+            raise MaskError, 'Cannot alter the masked element.'
         # This test is useful, but we should keep things light...
 #        if getmask(indx) is not nomask:
 #            msg = "Masked arrays must be filled before they can be used as indices!"
@@ -2311,7 +2314,7 @@
             raise TypeError("Only length-1 arrays can be converted "\
                             "to Python scalars")
         elif self._mask:
-            raise MAError, 'Cannot convert masked element to a Python int.'
+            raise MaskError, 'Cannot convert masked element to a Python int.'
         return int(self.item())
     #............................................
     def get_imag(self):
@@ -2983,6 +2986,10 @@
             if out is not None:
                 if isinstance(out, MaskedArray):
                     out.__setmask__(True)
+                elif out.dtype.kind in 'biu':
+                    errmsg = "Masked data information would be lost in one or "\
+                             "more location."
+                    raise MaskError(errmsg)
                 else:
                     out.flat = np.nan
                 return out
@@ -3316,11 +3323,11 @@
                 outmask = out._mask = make_mask_none(out.shape)
             outmask.flat = newmask
         else:
-            if out.dtype < np.dtype(float):
-                filler = -9999
-            else:
-                filler = np.nan
-            np.putmask(out, newmask, filler)
+            if out.dtype.kind in 'biu':
+                errmsg = "Masked data information would be lost in one or more"\
+                         " location."
+                raise MaskError(errmsg)
+            np.putmask(out, newmask, np.nan)
         return out
 
     def mini(self, axis=None):
@@ -3382,11 +3389,12 @@
                 outmask = out._mask = make_mask_none(out.shape)
             outmask.flat = newmask
         else:
-            if out.dtype < np.dtype(float):
-                filler = -9999
-            else:
-                filler = np.nan
-            np.putmask(out, newmask, filler)
+            
+            if out.dtype.kind in 'biu':
+                errmsg = "Masked data information would be lost in one or more"\
+                         " location."
+                raise MaskError(errmsg)
+            np.putmask(out, newmask, np.nan)
         return out
 
     def ptp(self, axis=None, out=None, fill_value=None):
@@ -3826,7 +3834,7 @@
 
     """
     if third is not None:
-        raise MAError, "3-argument power not supported."
+        raise MaskError, "3-argument power not supported."
     # Get the masks
     ma = getmask(a)
     mb = getmask(b)

Modified: trunk/numpy/ma/tests/test_core.py
===================================================================
--- trunk/numpy/ma/tests/test_core.py	2008-12-04 23:06:29 UTC (rev 6137)
+++ trunk/numpy/ma/tests/test_core.py	2008-12-05 20:40:44 UTC (rev 6138)
@@ -693,7 +693,7 @@
     def test_minmax_funcs_with_output(self):
         "Tests the min/max functions with explicit outputs"
         mask = np.random.rand(12).round()
-        xm = array(np.random.uniform(0,10,12),mask=mask)
+        xm = array(np.random.uniform(0,10,12), mask=mask)
         xm.shape = (3,4)
         for funcname in ('min', 'max'):
             # Initialize
@@ -701,11 +701,16 @@
             mafunc = getattr(numpy.ma.core, funcname)
             # Use the np version
             nout = np.empty((4,), dtype=int)
-            result = npfunc(xm,axis=0,out=nout)
+            try:
+                result = npfunc(xm, axis=0, out=nout)
+            except MaskError:
+                pass
+            nout = np.empty((4,), dtype=float)
+            result = npfunc(xm, axis=0, out=nout)
             self.failUnless(result is nout)
             # Use the ma version
             nout.fill(-999)
-            result = mafunc(xm,axis=0,out=nout)
+            result = mafunc(xm, axis=0, out=nout)
             self.failUnless(result is nout)
 
 
@@ -917,6 +922,8 @@
         a[1] = 1
         assert_equal(a._mask, zeros(10))
 
+    def _wtv(self):
+        int(np.nan)
 
 #------------------------------------------------------------------------------
 



More information about the Numpy-svn mailing list