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

Merged
merged 6 commits into from
Jul 20, 2025

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 3 times, most recently 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.

@ngoldbaum
Copy link
Member

It's not totally clear to me: is it possible for this change to cause tests that previously passed to start failing with a numpy version that includes this change? Or similarly, for a test that used to fail to start passing? If it's the latter, was the failure that happened before actually a bug?

Or is it that this is a purely internal change that can't change the behavior of the numpy's testing utilities?

@danra
Copy link
Contributor Author

danra commented Jul 17, 2025

is it possible for this change to cause tests that previously passed to start failing with a numpy version that includes this change?

No.

Or similarly, for a test that used to fail to start passing? If it's the latter, was the failure that happened before actually a bug?

The first commit in the PR demonstrates a case of assert_array_almost_equal raising an IndexError due to the recently merged #29112. This PR fixes the bug so that assert_array_almost_equal can return a real result, whether pass or fail.

Other than that, there should be no impact on np.testing assertions pass/fail results. The change is internal in nature to avoid modifying the array shape, towards reporting indexes in #28827. Since the recently merged #29112 also reports indexes, it also needs this fix to work correctly in all cases.

@ngoldbaum
Copy link
Member

Ah, I missed that you have really detailed commit messages in your commits.

In the future I encourage you to include that information in your PR description or at least clearly note the commit messages have extra context. Normally on github I assume commit messages don't have useful information, so I tend to just look at the PR description.

Copy link
Member

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! After reading through this commit-by-commit this makes much more sense.

@mhvk would you mind looking at this one as well?

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks great! I have two nitpicky comments, which barely are worth rerunning CI for, so feel free to ignore.

I also checked that the astropy test suite passes with this PR.

else:
assert infs_mask.all()

# For details on the work-arounds employed here, see func_assert_same_pos above
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might it be worth factoring out these two functions? They seem identical except for the part that gets added to err_msg.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted robust_any_difference for the significant part of the logic

assert_allclose(a, b)

b = np.array([3.])
expected_msg = 'inf location mismatch:'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and below I would just put expected_msg inside the pytest.raises statement, as there is no reason for escaping, so it easily fits on one line. Less clutter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a bit nicer to keep it the same pattern as everywhere else in the file
Also, does a colon not require escaping in a regex? Who knows? :)

@ngoldbaum
Copy link
Member

I have two nitpicky comments, which barely are worth rerunning CI for, so feel free to ignore

You can skip subsets of the CI too, if you don't want everything to run: https://numpy.org/doc/stable/dev/development_workflow.html#commands-to-skip-continuous-integration

@danra
Copy link
Contributor Author

danra commented Jul 18, 2025

The CI failure looks real but unrelated to this change

danra added 6 commits July 18, 2025 16:30
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).
@mhvk
Copy link
Contributor

mhvk commented Jul 20, 2025

Thanks! The refactoring made this (even) nicer. Let's get it in.

@mhvk mhvk merged commit bd5cb2a into numpy:main Jul 20, 2025
76 checks passed
@danra danra deleted the testing_infs_handling branch July 21, 2025 02:51
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.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy