(comparison, x, y, err_msg='', verbose=True, header='',
precision=6, equal_nan=True, equal_inf=True,
*, strict=False, names=('ACTUAL', 'DESIRED'))
| 736 | |
| 737 | |
| 738 | def 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) |
searching dependent graphs…