Add fixes to make `numpy.approx` array-scalar comparisons work with older numpy versions
This commit is contained in:
parent
161d4e5fe4
commit
97f9a8bfdf
|
@ -31,9 +31,9 @@ class ApproxBase(object):
|
||||||
or sequences of numbers.
|
or sequences of numbers.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Tell numpy to use our `__eq__` operator instead of its when left side in a numpy array but right side is
|
# Tell numpy to use our `__eq__` operator instead of its
|
||||||
# an instance of ApproxBase
|
|
||||||
__array_ufunc__ = None
|
__array_ufunc__ = None
|
||||||
|
__array_priority__ = 100
|
||||||
|
|
||||||
def __init__(self, expected, rel=None, abs=None, nan_ok=False):
|
def __init__(self, expected, rel=None, abs=None, nan_ok=False):
|
||||||
self.expected = expected
|
self.expected = expected
|
||||||
|
@ -73,9 +73,6 @@ class ApproxNumpy(ApproxBase):
|
||||||
Perform approximate comparisons for numpy arrays.
|
Perform approximate comparisons for numpy arrays.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Tell numpy to use our `__eq__` operator instead of its.
|
|
||||||
__array_priority__ = 100
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
# It might be nice to rewrite this function to account for the
|
# It might be nice to rewrite this function to account for the
|
||||||
# shape of the array...
|
# shape of the array...
|
||||||
|
@ -109,13 +106,13 @@ class ApproxNumpy(ApproxBase):
|
||||||
|
|
||||||
if np.isscalar(self.expected):
|
if np.isscalar(self.expected):
|
||||||
for i in np.ndindex(actual.shape):
|
for i in np.ndindex(actual.shape):
|
||||||
yield actual[i], self.expected
|
yield np.asscalar(actual[i]), self.expected
|
||||||
elif np.isscalar(actual):
|
elif np.isscalar(actual):
|
||||||
for i in np.ndindex(self.expected.shape):
|
for i in np.ndindex(self.expected.shape):
|
||||||
yield actual, self.expected[i]
|
yield actual, np.asscalar(self.expected[i])
|
||||||
else:
|
else:
|
||||||
for i in np.ndindex(self.expected.shape):
|
for i in np.ndindex(self.expected.shape):
|
||||||
yield actual[i], self.expected[i]
|
yield np.asscalar(actual[i]), np.asscalar(self.expected[i])
|
||||||
|
|
||||||
|
|
||||||
class ApproxMapping(ApproxBase):
|
class ApproxMapping(ApproxBase):
|
||||||
|
@ -145,9 +142,6 @@ class ApproxSequence(ApproxBase):
|
||||||
Perform approximate comparisons for sequences of numbers.
|
Perform approximate comparisons for sequences of numbers.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Tell numpy to use our `__eq__` operator instead of its.
|
|
||||||
__array_priority__ = 100
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
seq_type = type(self.expected)
|
seq_type = type(self.expected)
|
||||||
if seq_type not in (tuple, list, set):
|
if seq_type not in (tuple, list, set):
|
||||||
|
|
Loading…
Reference in New Issue