MCPcopy Create free account
hub / github.com/numpy/numpy / assert_array_compare

Function assert_array_compare

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

Source from the content-addressed store, hash-verified

640
641@np._no_nep50_warning()
642def assert_array_compare(comparison, x, y, err_msg='', verbose=True, header='',
643 precision=6, equal_nan=True, equal_inf=True,
644 *, strict=False):
645 __tracebackhide__ = True # Hide traceback for py.test
646 from numpy.core import (array2string, isnan, inf, bool_, errstate,
647 all, max, object_)
648
649 x = np.asanyarray(x)
650 y = np.asanyarray(y)
651
652 # original array for output formatting
653 ox, oy = x, y
654
655 def isnumber(x):
656 return x.dtype.char in '?bhilqpBHILQPefdgFDG'
657
658 def istime(x):
659 return x.dtype.char in "Mm"
660
661 def func_assert_same_pos(x, y, func=isnan, hasval='nan'):
662 """Handling nan/inf.
663
664 Combine results of running func on x and y, checking that they are True
665 at the same locations.
666
667 """
668 __tracebackhide__ = True # Hide traceback for py.test
669
670 x_id = func(x)
671 y_id = func(y)
672 # We include work-arounds here to handle three types of slightly
673 # pathological ndarray subclasses:
674 # (1) all() on `masked` array scalars can return masked arrays, so we
675 # use != True
676 # (2) __eq__ on some ndarray subclasses returns Python booleans
677 # instead of element-wise comparisons, so we cast to bool_() and
678 # use isinstance(..., bool) checks
679 # (3) subclasses with bare-bones __array_function__ implementations may
680 # not implement np.all(), so favor using the .all() method
681 # We are not committed to supporting such subclasses, but it's nice to
682 # support them if possible.
683 if bool_(x_id == y_id).all() != True:
684 msg = build_err_msg([x, y],
685 err_msg + '\nx and y %s location mismatch:'
686 % (hasval), verbose=verbose, header=header,
687 names=('x', 'y'), precision=precision)
688 raise AssertionError(msg)
689 # If there is a scalar, then here we know the array has the same
690 # flag as it everywhere, so we should return the scalar flag.
691 if isinstance(x_id, bool) or x_id.ndim == 0:
692 return bool_(x_id)
693 elif isinstance(y_id, bool) or y_id.ndim == 0:
694 return bool_(y_id)
695 else:
696 return y_id
697
698 try:
699 if strict:

Callers 5

test_zero_paddingFunction · 0.90
assert_array_equalFunction · 0.70
assert_array_lessFunction · 0.70
assert_allcloseFunction · 0.70

Calls 14

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
istimeFunction · 0.85
absFunction · 0.85
ravelMethod · 0.45
allMethod · 0.45

Tested by 1

test_zero_paddingFunction · 0.72