summaryrefslogtreecommitdiff
path: root/numpy/testing/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/testing/utils.py')
-rw-r--r--numpy/testing/utils.py27
1 files changed, 25 insertions, 2 deletions
diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py
index 4f4c3496c..654d55738 100644
--- a/numpy/testing/utils.py
+++ b/numpy/testing/utils.py
@@ -186,9 +186,14 @@ def assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=True):
def assert_array_compare(comparison, x, y, err_msg='', verbose=True,
header=''):
- from numpy.core import asarray
+ from numpy.core import asarray, isnan, any
+ from numpy import isreal, iscomplex
x = asarray(x)
y = asarray(y)
+
+ def isnumber(x):
+ return x.dtype.char in '?bhilqpBHILQPfdgFDG'
+
try:
cond = (x.shape==() or y.shape==()) or x.shape == y.shape
if not cond:
@@ -199,7 +204,25 @@ def assert_array_compare(comparison, x, y, err_msg='', verbose=True,
verbose=verbose, header=header,
names=('x', 'y'))
assert cond, msg
- val = comparison(x,y)
+
+ if (isnumber(x) and isnumber(y)) and (any(isnan(x)) or any(isnan(y))):
+ # Handling nan: we first check that x and y have the nan at the
+ # same locations, and then we mask the nan and do the comparison as
+ # usual.
+ xnanid = isnan(x)
+ ynanid = isnan(y)
+ try:
+ assert_array_equal(xnanid, ynanid)
+ except AssertionError:
+ msg = build_err_msg([x, y],
+ err_msg
+ + '\n(x and y nan location mismatch %s, '
+ + '%s mismatch)' % (xnanid, ynanid),
+ verbose=verbose, header=header,
+ names=('x', 'y'))
+ val = comparison(x[~xnanid], y[~ynanid])
+ else:
+ val = comparison(x,y)
if isinstance(val, bool):
cond = val
reduced = [0]