From 4b95b70eba7a721f1f4f4b7f7988ba06eda2bf89 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Thu, 15 Aug 2019 11:00:04 +0200 Subject: ENH: Improve mismatch message of np.testing.assert_array_equal (#14203) The original message included "Mismatch: 33.3%". It's not obvious what this percentage means. This commit changes the text to "Mismatched elements: 1 / 3 (33.3%)". --- numpy/testing/tests/test_utils.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'numpy/testing/tests/test_utils.py') diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py index bf60772d3..4f1b46d4f 100644 --- a/numpy/testing/tests/test_utils.py +++ b/numpy/testing/tests/test_utils.py @@ -520,7 +520,7 @@ class TestAlmostEqual(_GenericTest): with pytest.raises(AssertionError) as exc_info: self._assert_func(x, y, decimal=12) msgs = str(exc_info.value).split('\n') - assert_equal(msgs[3], 'Mismatch: 100%') + assert_equal(msgs[3], 'Mismatched elements: 3 / 3 (100%)') assert_equal(msgs[4], 'Max absolute difference: 1.e-05') assert_equal(msgs[5], 'Max relative difference: 3.33328889e-06') assert_equal( @@ -536,7 +536,7 @@ class TestAlmostEqual(_GenericTest): with pytest.raises(AssertionError) as exc_info: self._assert_func(x, y) msgs = str(exc_info.value).split('\n') - assert_equal(msgs[3], 'Mismatch: 33.3%') + assert_equal(msgs[3], 'Mismatched elements: 1 / 3 (33.3%)') assert_equal(msgs[4], 'Max absolute difference: 1.e-05') assert_equal(msgs[5], 'Max relative difference: 3.33328889e-06') assert_equal(msgs[6], ' x: array([1. , 2. , 3.00003])') @@ -548,7 +548,7 @@ class TestAlmostEqual(_GenericTest): with pytest.raises(AssertionError) as exc_info: self._assert_func(x, y) msgs = str(exc_info.value).split('\n') - assert_equal(msgs[3], 'Mismatch: 50%') + assert_equal(msgs[3], 'Mismatched elements: 1 / 2 (50%)') assert_equal(msgs[4], 'Max absolute difference: 1.') assert_equal(msgs[5], 'Max relative difference: 1.') assert_equal(msgs[6], ' x: array([inf, 0.])') @@ -560,7 +560,7 @@ class TestAlmostEqual(_GenericTest): with pytest.raises(AssertionError) as exc_info: self._assert_func(x, y) msgs = str(exc_info.value).split('\n') - assert_equal(msgs[3], 'Mismatch: 100%') + assert_equal(msgs[3], 'Mismatched elements: 2 / 2 (100%)') assert_equal(msgs[4], 'Max absolute difference: 2') assert_equal(msgs[5], 'Max relative difference: inf') @@ -855,7 +855,8 @@ class TestAssertAllclose(object): with pytest.raises(AssertionError) as exc_info: assert_allclose(a, b) msg = str(exc_info.value) - assert_('Mismatch: 25%\nMax absolute difference: 1\n' + assert_('Mismatched elements: 1 / 4 (25%)\n' + 'Max absolute difference: 1\n' 'Max relative difference: 0.5' in msg) def test_equal_nan(self): -- cgit v1.2.1 From 3dccd841476d26b5807156b904b55c04c6a3d370 Mon Sep 17 00:00:00 2001 From: CakeWithSteak <37267737+CakeWithSteak@users.noreply.github.com> Date: Thu, 5 Sep 2019 16:20:13 +0200 Subject: BUG: Fixed maximum relative error reporting in assert_allclose (gh-13802) Fixed maximum relative error reporting in assert_allclose: In cases where the two arrays have zeros at the same positions it will no longer report nan as the max relative error --- numpy/testing/tests/test_utils.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'numpy/testing/tests/test_utils.py') diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py index 4f1b46d4f..688bedc16 100644 --- a/numpy/testing/tests/test_utils.py +++ b/numpy/testing/tests/test_utils.py @@ -881,6 +881,15 @@ class TestAssertAllclose(object): assert_array_less(a, b) assert_allclose(a, b) + def test_report_max_relative_error(self): + a = np.array([0, 1]) + b = np.array([0, 2]) + + with pytest.raises(AssertionError) as exc_info: + assert_allclose(a, b) + msg = str(exc_info.value) + assert_('Max relative difference: 0.5' in msg) + class TestArrayAlmostEqualNulp(object): -- cgit v1.2.1 From df85cd664b36c34f314e23f629d1ad53458468f7 Mon Sep 17 00:00:00 2001 From: Maxwell Aladago Date: Fri, 6 Sep 2019 17:10:38 -0400 Subject: adding tests --- numpy/testing/tests/test_utils.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'numpy/testing/tests/test_utils.py') diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py index 688bedc16..c1e9fd237 100644 --- a/numpy/testing/tests/test_utils.py +++ b/numpy/testing/tests/test_utils.py @@ -564,6 +564,16 @@ class TestAlmostEqual(_GenericTest): assert_equal(msgs[4], 'Max absolute difference: 2') assert_equal(msgs[5], 'Max relative difference: inf') + # check the error message when x is scalar + x = 2 + y = np.ones(20) + with pytest.raises(AssertionError) as exc_info: + self._assert_func(x, y) + msgs = str(exc_info.value).split('\n') + assert_equal(msgs[3], 'Mismatched elements: 20 / 20 (100%)') + assert_equal(msgs[4], 'Max absolute difference: 1.') + assert_equal(msgs[5], 'Max relative difference: 1.') + 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 -- cgit v1.2.1 From 3e75bec894ad5ef231ed912fc9b943d3a4fc327f Mon Sep 17 00:00:00 2001 From: Maxwell Aladago Date: Sun, 8 Sep 2019 11:32:58 -0400 Subject: a separate test for scalars base on review comments --- numpy/testing/tests/test_utils.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'numpy/testing/tests/test_utils.py') diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py index c1e9fd237..44f93a693 100644 --- a/numpy/testing/tests/test_utils.py +++ b/numpy/testing/tests/test_utils.py @@ -564,7 +564,8 @@ class TestAlmostEqual(_GenericTest): assert_equal(msgs[4], 'Max absolute difference: 2') assert_equal(msgs[5], 'Max relative difference: inf') - # check the error message when x is scalar + def test_error_message_2(self): + """Check the message is formatted correctly when either x or y is a scalar.""" x = 2 y = np.ones(20) with pytest.raises(AssertionError) as exc_info: @@ -574,6 +575,15 @@ class TestAlmostEqual(_GenericTest): assert_equal(msgs[4], 'Max absolute difference: 1.') assert_equal(msgs[5], 'Max relative difference: 1.') + y = 2 + x = np.ones(20) + with pytest.raises(AssertionError) as exc_info: + self._assert_func(x, y) + msgs = str(exc_info.value).split('\n') + assert_equal(msgs[3], 'Mismatched elements: 20 / 20 (100%)') + assert_equal(msgs[4], 'Max absolute difference: 1.') + assert_equal(msgs[5], 'Max relative difference: 0.5') + 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 -- cgit v1.2.1