Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

polaris-3
Copy link

@polaris-3 polaris-3 commented Jun 2, 2025

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.

…, showing the position of differing elements.
@seberg
Copy link
Member

seberg commented Jun 3, 2025

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.

@polaris-3
Copy link
Author

Sure, here is an example with outputs before and after my changes.

Example code
`import numpy as np

a = np.random.rand(100, 100)
b = a.copy()
b[79, 83] = 0.5
b[24, 59] = 0.1
np.testing.assert_allclose(a,b)`

Before:
image
Note that there is no chance to see which of the two elements are different. The parts of the arrays being printed are identical.

After:
image

@charris charris changed the title ENH: more helpful error message in numpy.testing.assert_array_compare… ENH: Improve error message in numpy.testing.assert_array_compare Jun 3, 2025
@ngoldbaum ngoldbaum added the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Jun 3, 2025
@ngoldbaum
Copy link
Member

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.

@polaris-3
Copy link
Author

I hope to have addressed all the edge cases now and have also adapted the tests and docs to the new error message.
@ngoldbaum I'm not sure I fully agree. Even if all entries of two 1000 element arrays disagree, it might still be nice to see a few examples of differing elements. On the other hand, the truncated arrays are printed anyway, so this might serves this purpose already.

@@ -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)
Copy link
Member

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.

Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
01 - Enhancement 03 - Maintenance 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants