-
-
Notifications
You must be signed in to change notification settings - Fork 11k
ENH: Improve error message in numpy.testing.assert_array_compare #29112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…, showing the position of differing elements.
Thanks, looks like a nice idea on first thought! Could you show before/after outputs? Also at least the documentation test failures (circleci) are very much real here. |
I like where this is going, but I think care needs to go into edge cases so we don't unnecessarily add noise. If all the values are different in two 1000 element arrays, it's not particularly useful to print the first five indices, for example. Maybe there should be a cutoff where the index display starts showing up? Also for this to be mergeable you're going to need to get CI to pass and you're going to need to add a release note describing the change. Try to get the tests, docs build, doctests, and linter to pass locally before pushing here to run CI. |
… improved error message.
I hope to have addressed all the edge cases now and have also adapted the tests and docs to the new error message. |
@@ -563,6 +563,8 @@ def assert_almost_equal(actual, desired, decimal=7, err_msg='', verbose=True): | |||
Arrays are not almost equal to 9 decimals | |||
<BLANKLINE> | |||
Mismatched elements: 1 / 2 (50%) | |||
Failure at indices (only showing the first 5 failures): | |||
[1]: 2.3333333333333 (ACTUAL), 2.33333334 (DESIRED) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a small nitpick, but I think you should indent the points here by at least one space (I suppose that is what the below array prints use).
Might suggest rephrasing a bit, maybe
First {len(positions)} mismatches are at the position/index:
[]:
(could consider making it a header position: ACTUAL, DESIRED
, but dunno that it is better)
Not sure if e.g. @ngoldbaum had some thoughts, so might want to wait before we iterate multiple times on the message...
Code looks good to me, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, these are some good points. I'll take them into account in the next push, after waiting for more comments.
Improve the error message in numpy.testing.assert_array_compare to show the position of differing elements.
Currently, if there is an assertion error in functions like
numpy.testing.assert_allclose
, both arrays are printed. Typically the arrays are so large that they are truncated before being printed, making it hard to spot the differences. I suggest to print the indices of the differing elements as well as the corresponding values. To avoid excessive output, the number of printed out differences is limited to five.