diff options
author | pierregm <pierregm@localhost> | 2008-06-03 23:14:16 +0000 |
---|---|---|
committer | pierregm <pierregm@localhost> | 2008-06-03 23:14:16 +0000 |
commit | 29175fcf0edde29e01f8465037f2556680f8f5d8 (patch) | |
tree | 634f0e92da0f241198a62d13cbfa44e4acab3ab6 | |
parent | 4bf8efd1f026eb40bd55099bbb57725b51b31520 (diff) | |
download | numpy-29175fcf0edde29e01f8465037f2556680f8f5d8.tar.gz |
core
* masked_values now accept a shrink argument
* fixed the divide_tolerance to numpy.finfo(float).tiny (bug #807)
* in MaskedArray.__idiv__, use np.where instead of np.putmask to mask the denominator
-rw-r--r-- | numpy/ma/core.py | 35 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 5 |
2 files changed, 27 insertions, 13 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index b93a82857..241c6dd56 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -76,7 +76,7 @@ import warnings MaskType = np.bool_ nomask = MaskType(0) -divide_tolerance = 1.e-35 +divide_tolerance = np.finfo(float).tiny np.seterr(all='ignore') def doc_note(note): @@ -926,15 +926,22 @@ def masked_outside(x, v1, v2, copy=True): return masked_where(condition, x, copy=copy) # -def masked_object(x, value, copy=True): +def masked_object(x, value, copy=True, shrink=True): """Mask the array x where the data are exactly equal to value. This function is suitable only for object arrays: for floating point, please use ``masked_values`` instead. - Notes - ----- - The mask is set to `nomask` if posible. + Parameters + ---------- + x : array-like + Array to mask + value : var + Comparison value + copy : {True, False}, optional + Whether to return a copy of x. + shrink : {True, False}, optional + Whether to collapse a mask full of False to nomask """ if isMaskedArray(x): @@ -943,10 +950,10 @@ def masked_object(x, value, copy=True): else: condition = umath.equal(np.asarray(x), value) mask = nomask - mask = mask_or(mask, make_mask(condition, shrink=True)) + mask = mask_or(mask, make_mask(condition, shrink=shrink)) return masked_array(x, mask=mask, copy=copy, fill_value=value) -def masked_values(x, value, rtol=1.e-5, atol=1.e-8, copy=True): +def masked_values(x, value, rtol=1.e-5, atol=1.e-8, copy=True, shrink=True): """Mask the array x where the data are approximately equal in value, i.e. @@ -961,12 +968,14 @@ def masked_values(x, value, rtol=1.e-5, atol=1.e-8, copy=True): Array to fill. value : float Masking value. - rtol : float + rtol : {float}, optional Tolerance parameter. - atol : float + atol : {float}, optional Tolerance parameter (1e-8). - copy : bool + copy : {True, False}, optional Whether to return a copy of x. + shrink : {True, False}, optional + Whether to collapse a mask full of False to nomask """ abs = umath.absolute @@ -977,7 +986,7 @@ def masked_values(x, value, rtol=1.e-5, atol=1.e-8, copy=True): else: condition = umath.equal(xnew, value) mask = nomask - mask = mask_or(mask, make_mask(condition, shrink=True)) + mask = mask_or(mask, make_mask(condition, shrink=shrink)) return masked_array(xnew, mask=mask, copy=copy, fill_value=value) def masked_invalid(a, copy=True): @@ -1776,8 +1785,8 @@ masked_%(name)s(data = %(data)s, new_mask = mask_or(other_mask, dom_mask) # The following 3 lines control the domain filling if dom_mask.any(): - other_data = other_data.copy() - np.putmask(other_data, dom_mask, 1) + (_, fval) = ufunc_fills[np.divide] + other_data = np.where(dom_mask, fval, other_data) ndarray.__idiv__(self._data, other_data) self._mask = mask_or(self._mask, new_mask) return self diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 0b5bb9d30..d894f4dbe 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -242,6 +242,11 @@ class TestMA(NumpyTestCase): assert_equal(xm._mask, [1,1,1,0,0,1,1,0,0,0,1,1]) assert_equal(xm._data, [1/5.,1.,1./3.,-1.,-pi/2.,-1.,5.,1.,1.,1.,2.,1.]) + def test_inplace_arithmetixx(self): + tiny = numpy.finfo(float).tiny + a = array([tiny, 1./tiny, 0.]) + assert_equal(getmaskarray(a/2), [0,0,0]) + assert_equal(getmaskarray(2/a), [1,0,1]) #.......................... def test_scalararithmetic(self): |