diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2017-12-18 22:53:17 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-18 22:53:17 -0700 |
commit | 0225771c13e2cf54dc3dd1ff4d39af6a78fde9d8 (patch) | |
tree | bcccc559e08f8ac187944fb4890a5f4ea132af55 | |
parent | 0f13a2e7fa626dbde62bdda7a92fc0a6361981ba (diff) | |
parent | 4c8376223c717ff63548f7fad65975f0a88eda41 (diff) | |
download | numpy-0225771c13e2cf54dc3dd1ff4d39af6a78fde9d8.tar.gz |
Merge pull request #10232 from eric-wieser/use-isclose-in-masked_values
BUG: Don't reimplement isclose in np.ma
-rw-r--r-- | numpy/ma/core.py | 28 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 15 |
2 files changed, 27 insertions, 16 deletions
diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 77feec335..1edfba42e 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -2245,12 +2245,14 @@ def masked_values(x, value, rtol=1e-5, atol=1e-8, copy=True, shrink=True): Mask using floating point equality. Return a MaskedArray, masked where the data in array `x` are approximately - equal to `value`, i.e. where the following condition is True + equal to `value`, determined using `isclose`. The default tolerances for + `masked_values` are the same as those for `isclose`. - (abs(x - value) <= atol+rtol*abs(value)) + For integer types, exact equality is used, in the same way as + `masked_equal`. The fill_value is set to `value` and the mask is set to ``nomask`` if - possible. For integers, consider using ``masked_equal``. + possible. Parameters ---------- @@ -2258,10 +2260,8 @@ def masked_values(x, value, rtol=1e-5, atol=1e-8, copy=True, shrink=True): Array to mask. value : float Masking value. - rtol : float, optional - Tolerance parameter. - atol : float, optional - Tolerance parameter (1e-8). + rtol, atol : float, optional + Tolerance parameters passed on to `isclose` copy : bool, optional Whether to return a copy of `x`. shrink : bool, optional @@ -2309,17 +2309,13 @@ def masked_values(x, value, rtol=1e-5, atol=1e-8, copy=True, shrink=True): fill_value=999999) """ - mabs = umath.absolute xnew = filled(x, value) - if issubclass(xnew.dtype.type, np.floating): - condition = umath.less_equal( - mabs(xnew - value), atol + rtol * mabs(value)) - mask = getmask(x) + if np.issubdtype(xnew.dtype, np.floating): + mask = np.isclose(xnew, value, atol=atol, rtol=rtol) else: - condition = umath.equal(xnew, value) - mask = nomask - mask = mask_or(mask, make_mask(condition, shrink=shrink), shrink=shrink) - return masked_array(xnew, mask=mask, copy=copy, fill_value=value) + mask = umath.equal(xnew, value) + return masked_array( + xnew, mask=mask, copy=copy, fill_value=value, shrink=shrink) def masked_invalid(a, copy=True): diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 2a5a65e5c..d5622e4bb 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -4982,6 +4982,21 @@ class TestMaskedConstant(object): assert_not_equal(repr(a), 'masked') +class TestMaskedWhereAliases(object): + + # TODO: Test masked_object, masked_equal, ... + + def test_masked_values(self): + res = masked_values(np.array([-32768.0]), np.int16(-32768)) + assert_equal(res.mask, [True]) + + res = masked_values(np.inf, np.inf) + assert_equal(res.mask, True) + + res = np.ma.masked_values(np.inf, -np.inf) + assert_equal(res.mask, False) + + def test_masked_array(): a = np.ma.array([0, 1, 2, 3], mask=[0, 0, 1, 0]) assert_equal(np.argwhere(a), [[1], [3]]) |