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

Draft
wants to merge 9 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.

danra added 9 commits July 3, 2025 22:15
The two added test cases fail with:

E       AssertionError:
E       Arrays are not equal
E
E       nan location mismatch:
E        ACTUAL: MaskedArray(3.)
E        DESIRED: array(3.)

and

E       AssertionError:
E       Arrays are not equal
E
E       nan location mismatch:
E        ACTUAL: MaskedArray(3.)
E        DESIRED: array(nan)
…#29317)

TestArrayEqual.test_masked_scalar now passes.

This case regressed since 7315145 (merged in numpy#12119) due to:
- `<masked scalar> == <scalar>` returning np.ma.masked (not a 0-dim
   masked bool array), followed by
- `np.bool(np.ma.masked)` unintentionally converting it to np._False

There are a few ways to resolve this; I went with testing the
comparison result with `isinstance(bool)` to check if a conversion to
array is necessary, which is the same approach already taken in
assert_array_compare after evaluating `comparison(x, y)`.
Confusingly, "isinstance(..., bool) checks" in the previous wording
actually incorrectly referred to the ones towards the end of the
function, which are not actually related to __eq__'s behavior but to
the possibility of `func` returning a bool.
- Use same language as elsewhere below to explain `!= True` used to
  handle np.ma.masked
- Clarify committed to support standard MaskedArrays
- Restore note lost in 7315145 comment changes about how the
  np.bool casts towards the end of the function handle np.ma.masked,
  and expand further.
The code for infs masking and comparison in
assert_array_almost_equal.compare appears no longer needed;
since it was added, similar handling was added to assert_array_compare
which runs prior to calling the comparison function.

However, removing the handling in assert_array_almost_equal.compare
triggers a test failure, because assert_array_compare doesn't handle
complex infs:

```
<...>

x = array([ 1.+0.j ,  2.+1.j , inf+0.5j,  4.+0.j ]), y = array([ 1.+0.j ,  2.+1.j , inf+0.5j,  4.+0.j ])

    def compare(x, y):
        # make sure y is an inexact type to avoid abs(MIN_INT); will cause
        # casting of x later.
        dtype = result_type(y, 1.)
        y = np.asanyarray(y, dtype)
>       z = abs(x - y)
E       RuntimeWarning: invalid value encountered in subtract

<...>

numpy/testing/_private/utils.py:1155: RuntimeWarning
================================================================================================================== short test summary info ==================================================================================================================
FAILED numpy/lib/tests/test_function_base.py::TestInterp::test_complex_interp - RuntimeWarning: invalid value encountered in subtract
```

Besides just being a nice cleanup, a specific motivation is towards
reporting max errors indices (numpy#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.
assert_array_compare now tests all inf values for matching position
and value, including complex infs.

Fixes the test failure triggered by the previous commit.
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).
@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
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