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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MNT: assert_array_compare handles all inf values
assert_array_compare now tests all inf values for matching position
and value, including complex infs.

Fixes the failing tests.
  • Loading branch information
danra committed Jul 18, 2025
commit bd58e7cc0c2b54da1dd29833bf429c0556a9ae5e
42 changes: 36 additions & 6 deletions numpy/testing/_private/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,33 @@ def func_assert_same_pos(x, y, func=isnan, hasval='nan'):
else:
return y_id

def assert_same_inf_values(x, y, infs_mask):
"""
Verify all inf values match in the two arrays
"""
__tracebackhide__ = True # Hide traceback for py.test

if not infs_mask.any():
return
if x.ndim > 0 and y.ndim > 0:
x = x[infs_mask]
y = y[infs_mask]
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

result = x == y
if not hasattr(result, "all") or not callable(result.all):
result = np.bool(result)
if result.all() != True:
msg = build_err_msg(
[x, y],
err_msg + '\ninf values mismatch:',
verbose=verbose, header=header,
names=names,
precision=precision)
raise AssertionError(msg)

try:
if strict:
cond = x.shape == y.shape and x.dtype == y.dtype
Expand All @@ -828,12 +855,15 @@ def func_assert_same_pos(x, y, func=isnan, hasval='nan'):
flagged = func_assert_same_pos(x, y, func=isnan, hasval='nan')

if equal_inf:
flagged |= func_assert_same_pos(x, y,
func=lambda xy: xy == +inf,
hasval='+inf')
flagged |= func_assert_same_pos(x, y,
func=lambda xy: xy == -inf,
hasval='-inf')
# If equal_nan=True, skip comparing nans below for equality if they are
# also infs (e.g. inf+nanj) since that would always fail.
isinf_func = lambda xy: np.logical_and(np.isinf(xy), np.invert(flagged))
infs_mask = func_assert_same_pos(
x, y,
func=isinf_func,
hasval='inf')
assert_same_inf_values(x, y, infs_mask)
flagged |= infs_mask

elif istime(x) and istime(y):
# If one is datetime64 and the other timedelta64 there is no point
Expand Down
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