diff options
author | David Cournapeau <cournape@gmail.com> | 2008-04-08 00:09:12 +0000 |
---|---|---|
committer | David Cournapeau <cournape@gmail.com> | 2008-04-08 00:09:12 +0000 |
commit | 04fdbe54df00293b259458e31fa8d66a6c42c188 (patch) | |
tree | 3e62d17b7429df8027a6d0ede866e4872d4591a5 /numpy/testing/utils.py | |
parent | bfc33fd7cfef5f74d73f6df0282660e26d761942 (diff) | |
download | numpy-04fdbe54df00293b259458e31fa8d66a6c42c188.tar.gz |
Handle nan in assert_array* funcs correctly. All numpy tests pass
Diffstat (limited to 'numpy/testing/utils.py')
-rw-r--r-- | numpy/testing/utils.py | 27 |
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] |