Repository navigation
ENH: add "max difference" messages to np.testing.assert_array_equal - #12591
Conversation
…_equal
Example behavior:
>>> x = np.array([1, 2, 3])
>>> y = np.array([1, 2, 3.0001])
>>> np.testing.assert_allclose(x, y)
AssertionError:
Not equal to tolerance rtol=1e-07, atol=0
(mismatch 33.333333333333336%, maximum difference 0.00010000000000021103)
x: array([1, 2, 3])
y: array([1. , 2. , 3.0001])
Motivation: when writing numerical algorithms, I frequently find myself
experimenting to pick the right value of `atol` and `rtol` for
`np.testing.assert_allclose()`. If I make the tolerance too generous, I risk
missing regressions in accuracy, so I usually try to pick the smallest values
for which tests pass. This change immediately reveals appropriate values to
use for these parameters, so I don't need to guess and check.
|
I like this idea |
eric-wieser
left a comment
There was a problem hiding this comment.
Generally looks good, but I don't understand the existing tests that you tweaked
|
Nice little enhancement; I've definitely done the same. |
|
Note that this definitely needs more extensive tests. I'll remove "WIP" from the title when that's done. |
a138ffd to
baed612
Compare
|
Per the mailing list discussion, I've added relative error as well. Here's what it looks like currently: |
|
It's not entirely clear to me what the right description for these error levels is. Currently I have "max abs error" and "max rel error", which is a compromise between explicitness and succinctness (i.e., fitting on one line). Other possible choices:
I think we're at about the maximum description length for fitting cleanly on one line within parentheses with "max abs error". If we want a longer or non-abbreviate description, we should figure out how to make this look good with line breaks, e.g., something like: |
|
|
||
| with errstate(invalid='ignore', divide='ignore'): | ||
| # ignore errors for non-numeric types | ||
| with contextlib.suppress(TypeError): |
There was a problem hiding this comment.
Do I get a prize for using the first Python 3 only feature in NumPy? :)
There was a problem hiding this comment.
Why not use a 2.7 feature here too, and combine both withs into a single with?
There was a problem hiding this comment.
I could, but then I need an ugly explicit continuation character \, e.g.,
with errstate(invalid='ignore', divide='ignore'), \
contextlib.suppress(TypeError):
# ignore errors for non-numeric types|
I would do your suggested Your version split over multiple lines is probably better, though (would still replace "error" with "difference"). |
|
OK, so perhaps: |
|
Last suggestion looks great to me! |
|
|
||
| Array comparison assertions include maximum differneces | ||
| ------------------------------------------------------- | ||
|
|
There was a problem hiding this comment.
nit: remove this blank line, which probably also makes git behave better in future merges.
| Arrays are not almost equal to 5 decimals | ||
| x and y nan location mismatch: | ||
| x: array([1. , 2.33333, nan]) | ||
| y: array([1. , 2.33339, 5. ]) |
There was a problem hiding this comment.
@tylerjereddy, are these now being run through refguide? If so, why was the old error type not causing a failure?
There was a problem hiding this comment.
refguide is opt-in so if @shoyer thinks we should be running the checker on these docstrings then:
diff --git a/tools/refguide_check.py b/tools/refguide_check.py
index 3d885e3..fec715f 100644
--- a/tools/refguide_check.py
+++ b/tools/refguide_check.py
@@ -76,6 +76,7 @@ PUBLIC_SUBMODULES = [
'polynomial',
'matrixlib',
'random',
+ 'testing',
]Testing locally (python runtests.py -g --refguide-check >& refguide_results.txt), this causes various failures, some for the docstrings touched here, but also for other testing/_private/utils.py docstrings with things like suppress_warnings is not defined.
I wonder if there's an extra layer of complexity given that we are testing the testing utilities' docstrings. Normally refguide failures will produce an expected and got set of values, but many of the problems here seem to complain about the desired assertion actually happening (something not being caught when it should be maybe).
My initial bet is that this is the design issue observed with refguide here:
def _do_check(self, want, got):
# This should be done exactly as written to correctly handle all of
# numpy-comparable objects, strings, and heterogeneous tuples
try:
if want == got:
return True
except Exception:
pass
return np.allclose(want, got, atol=self.atol, rtol=self.rtol)In particular, I suppose we'd want a way to check if allclose and / or similar are the actual function under test in the docstring and then perform a meta-assessment from there. I find this slightly mind-bending, but maybe obvious to other devs.
That said, is this something for a separate issue / wish-list? Or maybe simpler than I'm thinking here.
There was a problem hiding this comment.
I was able to successfully enable refguide checking on np.testing with a few docstring fixes. It appears the trick is add to include lines for the trraceback at the start of each error message:
Traceback (most recent call last):
...
Otherwise doctests don't recognize the exception.
| ' x: array([1.00000000001, 2.00000000002, 3.00003 ])') | ||
| assert_equal( | ||
| msgs[7], | ||
| ' y: array([1.00000000002, 2.00000000003, 3.00004 ])') |
There was a problem hiding this comment.
I think this should have at the end a
else:
assert_(False)Or perhaps better as:
with pytest.raises(AssertionError) as exc_info:
self._assert_func(x, y, decimal=12)
e = exc_info.value
# rest as you have itSame in all the cases below - the test is not actually checking that the error happens at all.
|
Needs rebase and (partial?) squashing. |
|
I'll squash the whole thing when we merge it. (I like to avoid rebasing
while under review so change history is preserved)
…On Tue, Dec 25, 2018 at 3:36 PM Charles Harris ***@***.***> wrote:
Needs rebase and (partial?) squashing.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#12591 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABKS1nKy0QbZzDBFWdw9RfSDdPwvneuRks5u8qh2gaJpZM4ZZjV5>
.
|
|
I think it is about to get merged. |
|
Needs rebase. |
|
Test failure unrelated to this PR. LGTM. |
tylerjereddy
left a comment
There was a problem hiding this comment.
Great that refguide has been activated for the docstrings touched here now. For exceptions, it looks like the "actual" and "desired" values (and probably also the error message itself) aren't checked.
I found one obvious typo that refguide misses as a result, but otherwise a net improvement.
| AssertionError: | ||
| Arrays are not almost equal to 10 decimals | ||
| ACTUAL: 2.3333333333333 | ||
| DESIRED: 2.2.33333334 |
There was a problem hiding this comment.
The desired value looks wrong with two decimals--probably a typo. I think you're right that refguide is now working as intended for the modules you've touched, but it will only be sensitive to the "type" of exception object, and apparently not to much beyond that.
I think @mattip recently observed this too. Maybe this is ok, but maybe our wish-list may include a bit more stringency than just checking the category of the error.
There was a problem hiding this comment.
Oops, fixed. We should definitely disable IGNORE_EXECPTION_DETAIL in refguide, but let's save that for another PR.
|
Whoever merges this, please squash as it will make a backport much easier. |
|
I'm going to merge this (via squashing) when tests pass. |
…umpy#12591) Example behavior: >>> x = np.array([1, 2, 3]) >>> y = np.array([1, 2, 3.0001]) >>> np.testing.assert_allclose(x, y) AssertionError: Not equal to tolerance rtol=1e-07, atol=0 Mismatch: 33.3% Max absolute difference: 0.0001 Max relative difference: 3.33322223e-05 x: array([1, 2, 3]) y: array([1. , 2. , 3.0001]) Motivation: when writing numerical algorithms, I frequently find myself experimenting to pick the right value of `atol` and `rtol` for `np.testing.assert_allclose()`. If I make the tolerance too generous, I risk missing regressions in accuracy, so I usually try to pick the smallest values for which tests pass. This change immediately reveals appropriate values to use for these parameters, so I don't need to guess and check.
Example behavior:
Motivation: when writing numerical algorithms, I frequently find myself experimenting to pick the right value of
atolandrtolfornp.testing.assert_allclose(). If I make the tolerance too generous, I risk missing regressions in accuracy, so I usually try to pick the smallest values for which tests pass. This change immediately reveals appropriate values to use for these parameters, so I don't need to guess and check.If others like this idea, I will run it by the mailing list.