8000 BUG: fix percentage reporting when testing.assert_allclose fails. by pp-mo · Pull Request #5025 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: fix percentage reporting when testing.assert_allclose fails. #5025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 36 additions & 26 deletions numpy/core/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -2153,6 +2153,41 @@ def identity(n, dtype=None):
from numpy import eye
return eye(n, dtype=dtype)

def _allclose_points(a, b, rtol=1.e-5, atol=1.e-8):
"""
This is the point-wise inner calculation of 'allclose', which is subtly
different from 'isclose'. Provided as a comparison routine for use in
testing.assert_allclose.
See those routines for further details.

"""
x = array(a, copy=False, ndmin=1)
y = array(b, copy=False, ndmin=1)

# make sure y is an inexact type to avoid abs(MIN_INT); will cause
# casting of x later.
dtype = multiarray.result_type(y, 1.)
y = array(y, dtype=dtype, copy=False)

xinf = isinf(x)
yinf = isinf(y)
if any(xinf) or any(yinf):
# Check that x and y have inf's only in the same positions
if not all(xinf == yinf):
return False
# Check that sign of inf's in x and y is the same
if not all(x[xinf] == y[xinf]):
return False

x = x[~xinf]
y = y[~xinf]

# ignore invalid fpe's
with errstate(invalid='ignore'):
r = less_equal(abs(x - y), atol + rtol * abs(y))

return r

def allclose(a, b, rtol=1.e-5, atol=1.e-8):
"""
Returns True if two arrays are element-wise equal within a tolerance.
Expand Down Expand Up @@ -2208,32 +2243,7 @@ def allclose(a, b, rtol=1.e-5, atol=1.e-8):
False

"""
x = array(a, copy=False, ndmin=1)
y = array(b, copy=False, ndmin=1)

# make sure y is an inexact type to avoid abs(MIN_INT); will cause
# casting of x later.
dtype = multiarray.result_type(y, 1.)
y = array(y, dtype=dtype, copy=False)

xinf = isinf(x)
yinf = isinf(y)
if any(xinf) or any(yinf):
# Check that x and y have inf's only in the same positions
if not all(xinf == yinf):
return False
# Check that sign of inf's in x and y is the same
if not all(x[xinf] == y[xinf]):
return False

x = x[~xinf]
y = y[~xinf]

# ignore invalid fpe's
with errstate(invalid='ignore'):
r = all(less_equal(abs(x - y), atol + rtol * abs(y)))

return r
return all(_allclose_points(a, b, rtol=rtol, atol=atol))

def isclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False):
"""
Expand Down
9 changes: 9 additions & 0 deletions numpy/testing/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,15 @@ def test_min_int(self):
# Should not raise:
assert_allclose(a, a)

def test_report_fail_percentage(self):
a = np.array([1, 1, 1, 1])
b = np.array([1, 1, 1, 2])
try:
assert_allclose(a, b)
msg = ''
except AssertionError as exc:
msg = exc.args[0]
self.assertTrue("mismatch 25.0%" in msg)

class TestArrayAlmostEqualNulp(unittest.TestCase):
@dec.knownfailureif(True, "Github issue #347")
Expand Down
2 changes: 1 addition & 1 deletion numpy/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ def assert_allclose(actual, desired, rtol=1e-7, atol=0,
"""
import numpy as np
def compare(x, y):
return np.allclose(x, y, rtol=rtol, atol=atol)
return np.core.numeric._allclose_points(x, y, rtol=rtol, atol=atol)

actual, desired = np.asanyarray(actual), np.asanyarray(desired)
header = 'Not equal to tolerance rtol=%g, atol=%g' % (rtol, atol)
Expand Down
0