summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpierregm <pierregm@localhost>2008-12-05 20:40:44 +0000
committerpierregm <pierregm@localhost>2008-12-05 20:40:44 +0000
commit57c9ad3baa68a11aa8cfd272e4ccbb9002526cf1 (patch)
treeb0ef0042ecf148b476f4e790bde879f9128c838b
parent5c62843546fe4e386ecc1de4c8fab50ff8d07df1 (diff)
downloadnumpy-57c9ad3baa68a11aa8cfd272e4ccbb9002526cf1.tar.gz
* 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
-rw-r--r--numpy/ma/core.py38
-rw-r--r--numpy/ma/tests/test_core.py13
2 files changed, 33 insertions, 18 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py
index 23f6bc037..a3cda941d 100644
--- a/numpy/ma/core.py
+++ b/numpy/ma/core.py
@@ -19,7 +19,7 @@ Improvements suggested by Reggie Dugard (reggie_AT_merfinllc_DOT_com)
__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 @@ def get_object_signature(obj):
class MAError(Exception):
"Class for MA related errors."
pass
+class MaskError(MAError):
+ "Class for mask related errors."
+ pass
#####--------------------------------------------------------------------------
@@ -1478,7 +1481,7 @@ class MaskedArray(ndarray):
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 @@ class MaskedArray(ndarray):
"""
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 @@ masked_%(name)s(data = %(data)s,
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 @@ masked_%(name)s(data = %(data)s,
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 @@ masked_%(name)s(data = %(data)s,
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 @@ masked_%(name)s(data = %(data)s,
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 @@ def power(a, b, third=None):
"""
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)
diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py
index 2128fa5ea..f9f46e563 100644
--- a/numpy/ma/tests/test_core.py
+++ b/numpy/ma/tests/test_core.py
@@ -693,7 +693,7 @@ class TestMaskedArrayArithmetic(TestCase):
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 @@ class TestMaskedArrayArithmetic(TestCase):
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 @@ class TestMaskedArrayAttributes(TestCase):
a[1] = 1
assert_equal(a._mask, zeros(10))
+ def _wtv(self):
+ int(np.nan)
#------------------------------------------------------------------------------