summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarten van Kerkwijk <mhvk@astro.utoronto.ca>2017-01-11 10:36:21 -0500
committerMarten van Kerkwijk <mhvk@astro.utoronto.ca>2017-01-11 12:23:35 -0500
commit960ec0a4eab51ccc33e6a030d870f93300656df4 (patch)
tree2618102c9152641e1c3f58eac3dafc957418977e
parent124c3d809d5b2cca79957299ed126efd1300dffe (diff)
downloadpython-numpy-960ec0a4eab51ccc33e6a030d870f93300656df4.tar.gz
python-numpy-960ec0a4eab51ccc33e6a030d870f93300656df4.tar.bz2
python-numpy-960ec0a4eab51ccc33e6a030d870f93300656df4.zip
Ensure inf/nan removal in assert_array_compare is matrix-safe.
-rw-r--r--numpy/testing/tests/test_utils.py28
-rw-r--r--numpy/testing/utils.py24
2 files changed, 44 insertions, 8 deletions
diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py
index a05fc3bdb..804f22b7f 100644
--- a/numpy/testing/tests/test_utils.py
+++ b/numpy/testing/tests/test_utils.py
@@ -303,6 +303,20 @@ class TestArrayAlmostEqual(_GenericTest, unittest.TestCase):
self._assert_func(b, a)
self._assert_func(b, b)
+ def test_matrix(self):
+ # Matrix slicing keeps things 2-D, while array does not necessarily.
+ # See gh-8452.
+ m1 = np.matrix([[1., 2.]])
+ m2 = np.matrix([[1., np.nan]])
+ m3 = np.matrix([[1., -np.inf]])
+ m4 = np.matrix([[np.nan, np.inf]])
+ m5 = np.matrix([[1., 2.], [np.nan, np.inf]])
+ for m in m1, m2, m3, m4, m5:
+ self._assert_func(m, m)
+ a = np.array(m)
+ self._assert_func(a, m)
+ self._assert_func(m, a)
+
def test_subclass_that_cannot_be_bool(self):
# While we cannot guarantee testing functions will always work for
# subclasses, the tests should ideally rely only on subclasses having
@@ -402,6 +416,20 @@ class TestAlmostEqual(_GenericTest, unittest.TestCase):
# remove anything that's not the array string
self.assertEqual(str(e).split('%)\n ')[1], b)
+ def test_matrix(self):
+ # Matrix slicing keeps things 2-D, while array does not necessarily.
+ # See gh-8452.
+ m1 = np.matrix([[1., 2.]])
+ m2 = np.matrix([[1., np.nan]])
+ m3 = np.matrix([[1., -np.inf]])
+ m4 = np.matrix([[np.nan, np.inf]])
+ m5 = np.matrix([[1., 2.], [np.nan, np.inf]])
+ for m in m1, m2, m3, m4, m5:
+ self._assert_func(m, m)
+ a = np.array(m)
+ self._assert_func(a, m)
+ self._assert_func(m, a)
+
def test_subclass_that_cannot_be_bool(self):
# While we cannot guarantee testing functions will always work for
# subclasses, the tests should ideally rely only on subclasses having
diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py
index a44a51c81..b959ea741 100644
--- a/numpy/testing/utils.py
+++ b/numpy/testing/utils.py
@@ -721,27 +721,35 @@ def assert_array_compare(comparison, x, y, err_msg='', verbose=True,
y.shape),
verbose=verbose, header=header,
names=('x', 'y'), precision=precision)
- if not cond:
- raise AssertionError(msg)
+ raise AssertionError(msg)
if isnumber(x) and isnumber(y):
+ has_nan = has_inf = False
if equal_nan:
x_isnan, y_isnan = isnan(x), isnan(y)
# Validate that NaNs are in the same place
- if any(x_isnan) or any(y_isnan):
+ has_nan = any(x_isnan) or any(y_isnan)
+ if has_nan:
chk_same_position(x_isnan, y_isnan, hasval='nan')
- x = x[~x_isnan]
- y = y[~y_isnan]
if equal_inf:
x_isinf, y_isinf = isinf(x), isinf(y)
# Validate that infinite values are in the same place
- if any(x_isinf) or any(y_isinf):
+ has_inf = any(x_isinf) or any(y_isinf)
+ if has_inf:
# Check +inf and -inf separately, since they are different
chk_same_position(x == +inf, y == +inf, hasval='+inf')
chk_same_position(x == -inf, y == -inf, hasval='-inf')
- x = x[~x_isinf]
- y = y[~y_isinf]
+
+ if has_nan and has_inf:
+ x = x[~(x_isnan | x_isinf)]
+ y = y[~(y_isnan | y_isinf)]
+ elif has_nan:
+ x = x[~x_isnan]
+ y = y[~y_isnan]
+ elif has_inf:
+ x = x[~x_isinf]
+ y = y[~y_isinf]
# Only do the comparison if actual values are left
if x.size == 0: