-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Description
Describe the issue:
np.testing.assert_XXX
utilities fail when comparing a masked scalar against another scalar, with a "nan location mismatch" error. The consistent behavior is to ignore any masked elements, in this case the only one, and therefore succeed in this case.
See also #11121
Reproduce the code example:
import numpy as np
a = np.ma.MaskedArray(3., mask=True)
b = np.array(3.)
np.testing.assert_array_equal(a, b)
Error message:
Traceback (most recent call last):
File "<python-input-3>", line 1, in <module>
np.testing.assert_array_equal(a,b)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^
File "/Users/dan/.cache/uv/archive-v0/8v1Ui92GFWyWYpJZOFw5S/lib/python3.13/site-packages/numpy/testing/_private/utils.py", line 1061, in assert_array_equal
assert_array_compare(operator.__eq__, actual, desired, err_msg=err_msg,
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
verbose=verbose, header='Arrays are not equal',
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
strict=strict)
^^^^^^^^^^^^^^
File "/Users/dan/.cache/uv/archive-v0/8v1Ui92GFWyWYpJZOFw5S/lib/python3.13/site-packages/numpy/testing/_private/utils.py", line 818, in assert_array_compare
flagged = func_assert_same_pos(x, y, func=isnan, hasval='nan')
File "/Users/dan/.cache/uv/archive-v0/8v1Ui92GFWyWYpJZOFw5S/lib/python3.13/site-packages/numpy/testing/_private/utils.py", line 787, in func_assert_same_pos
raise AssertionError(msg)
AssertionError:
Arrays are not equal
nan location mismatch:
ACTUAL: MaskedArray(3.)
DESIRED: array(3.)
>>>
Python and NumPy Versions:
2.3.1
3.13.3 (main, Apr 9 2025, 03:47:57) [Clang 20.1.0 ]
Runtime Environment:
[{'numpy_version': '2.3.1',
'python': '3.13.3 (main, Apr 9 2025, 03:47:57) [Clang 20.1.0 ]',
'uname': uname_result(system='Darwin', node='Dans-M2.local', release='24.4.0', version='Darwin Kernel Version 24.4.0: Fri Apr 11 18:33:39 PDT 2025; root:xnu-11417.101.15~117/RELEASE_ARM64_T6020', machine='arm64')},
{'simd_extensions': {'baseline': ['NEON', 'NEON_FP16', 'NEON_VFPV4', 'ASIMD'],
'found': ['ASIMDHP'],
'not_found': ['ASIMDFHM']}}]
Context for the issue:
Ran into this while working on #28827
- while amending assert_array_compare
's nan/inf handling to be able to completely remove the additional inf handling in assert_array_almost_equal
- which is needed to avoid assert_array_almost_equal.compare
potentially producing an output shape different from its input
- which in turn would avoid additional logic to produce the correct indices of the max abs/rel errors in the original array.