MCPcopy
hub / github.com/numpy/numpy / assert_array_compare

Function assert_array_compare

numpy/testing/_private/utils.py:738–996  ·  view source on GitHub ↗
(comparison, x, y, err_msg='', verbose=True, header='',
                         precision=6, equal_nan=True, equal_inf=True,
                         *, strict=False, names=('ACTUAL', 'DESIRED'))

Source from the content-addressed store, hash-verified

736
737
738def assert_array_compare(comparison, x, y, err_msg='', verbose=True, header='',
739 precision=6, equal_nan=True, equal_inf=True,
740 *, strict=False, names=('ACTUAL', 'DESIRED')):
741 __tracebackhide__ = True # Hide traceback for py.test
742 from numpy._core import all, array2string, errstate, inf, isnan, max, object_
743
744 x = np.asanyarray(x)
745 y = np.asanyarray(y)
746
747 # original array for output formatting
748 ox, oy = x, y
749
750 def isnumber(x):
751 return type(x.dtype)._is_numeric
752
753 def istime(x):
754 return x.dtype.char in "Mm"
755
756 def isvstring(x):
757 return x.dtype.char == "T"
758
759 def robust_any_difference(x, y):
760 # We include work-arounds here to handle three types of slightly
761 # pathological ndarray subclasses:
762 # (1) all() on fully masked arrays returns np.ma.masked, so we use != True
763 # (np.ma.masked != True evaluates as np.ma.masked, which is falsy).
764 # (2) __eq__ on some ndarray subclasses returns Python booleans
765 # instead of element-wise comparisons, so we cast to np.bool() in
766 # that case (or in case __eq__ returns some other value with no
767 # all() method).
768 # (3) subclasses with bare-bones __array_function__ implementations may
769 # not implement np.all(), so favor using the .all() method
770 # We are not committed to supporting cases (2) and (3), but it's nice to
771 # support them if possible.
772 result = x == y
773 if not hasattr(result, "all") or not callable(result.all):
774 result = np.bool(result)
775 return result.all() != True
776
777 def func_assert_same_pos(x, y, func=isnan, hasval='nan'):
778 """Handling nan/inf.
779
780 Combine results of running func on x and y, checking that they are True
781 at the same locations.
782
783 """
784 __tracebackhide__ = True # Hide traceback for py.test
785
786 x_id = func(x)
787 y_id = func(y)
788 if robust_any_difference(x_id, y_id):
789 msg = build_err_msg(
790 [x, y],
791 err_msg + f'\n{hasval} location mismatch:',
792 verbose=verbose, header=header,
793 names=names,
794 precision=precision)
795 raise AssertionError(msg)

Callers 7

test_zero_paddingFunction · 0.90
assert_arr_partitionedFunction · 0.90
assert_partitionedMethod · 0.90
assert_array_equalFunction · 0.70
assert_array_lessFunction · 0.70
assert_allcloseFunction · 0.70

Calls 15

arrayFunction · 0.90
errstateClass · 0.90
maxFunction · 0.90
array2stringFunction · 0.90
allFunction · 0.90
build_err_msgFunction · 0.85
isnumberFunction · 0.85
func_assert_same_posFunction · 0.85
assert_same_inf_valuesFunction · 0.85
istimeFunction · 0.85
isvstringFunction · 0.85
joinMethod · 0.80

Tested by 3

test_zero_paddingFunction · 0.72
assert_arr_partitionedFunction · 0.72
assert_partitionedMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…