Skip to content

MNT: Cleanup infs handling in np.testing assertion utilities #29321

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 5 commits into
base: main
Choose a base branch
from

Conversation

danra
Copy link
Contributor

@danra danra commented Jul 4, 2025

Besides just being a nice cleanup, a specific motivation is towards reporting max errors indices (#28827). Without this change, assert_array_almost_equal.compare can produce an output shape different from its input, requiring additional logic to produce the correct indices in the original array.

Note: This PR is based over #29318 commits because it somewhat depends on it. It's probably better to review #29318 before this one.

@ngoldbaum
Copy link
Member

Can you elaborate a little bit more about what is broken in the current inf handling and what this PR does exactly to fix it? It's not clear to me just looking at the diff here.

@ngoldbaum
Copy link
Member

Oh, missed this:

Note: This PR is based over #29318 commits because it somewhat depends on it. It's probably better to review #29318 before this one.

I'm gonna go ahead and mark this as draft...

@ngoldbaum ngoldbaum marked this pull request as draft July 7, 2025 15:22
@danra danra force-pushed the testing_infs_handling branch 2 times, most recently from 0d4543f to ad2317f Compare July 14, 2025 05:08
danra added 5 commits July 13, 2025 22:09
Recently, numpy#29112 added showing first mismatches indices, but
assert_array_almost_equal.compare trips it up by returning a shape
different from its input, causing an IndexError:

```
    <...>
    if invalids.ndim != 0:
        if flagged.ndim > 0:
>           positions = np.argwhere(np.asarray(~flagged))[invalids]
E           IndexError: boolean index did not match indexed array along axis 0; size of axis is 3 but size of corresponding boolean axis is 2
```
(traceback shown using pytest --full-trace)
A nice cleanup (newer, similar inf handling already exists now in
assert_array_compare), and resolves the shape mismatch issue. However,
the removed logic was handling complex infs while the one isn't, causing
the new test and an existing one (TestInterp::test_complex_interp) to
now fail with RuntimeWarnings attempting to subtract the complex infs.
assert_array_compare now tests all inf values for matching position
and value, including complex infs.

Fixes the failing tests.
The behavior for real infs is the same is before.

For complex infs, demonstrates that the behavior for mismatching
values is now cleaner, showing a concise error message vs.
previously displaying nan max errors.

For complex infs with matching values, the behavior is the same as
before, accepting them as equal (although internally they would
now be filtered ahead of being passed to isclose, like real infs
already had been).
@danra danra force-pushed the testing_infs_handling branch from ad2317f to 260cddd Compare July 14, 2025 05:10
@danra danra marked this pull request as ready for review July 14, 2025 05:13
@danra
Copy link
Contributor Author

danra commented Jul 14, 2025

Rebased and ready for review. In addition to the #28827 original motivator, since the original PR #29112 was also merged, also tripping up on the shape mismatch caused by assert_array_almost_equal, and also fixed by this PR. Added a commit demonstrating the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants