def assert_arrays_almost_equal(a, b, threshold, dtype=False):
'''
Check if two arrays have the same shape and contents that differ
by abs(a - b) <= threshold for all elements.
If threshold is None, do an absolute comparison rather than a relative
comparison.
'''
if threshold is None:
return assert_arrays_equal(a, b, dtype=dtype)
assert isinstance(a, np.ndarray), "a is a %s" % type(a)
assert isinstance(b, np.ndarray), "b is a %s" % type(b)
assert a.shape == b.shape, "%s != %s" % (a, b)
#assert a.dtype == b.dtype, "%s and %b not same dtype %s %s" % (a, b,
# a.dtype,
# b.dtype)
if a.dtype.kind in ['f', 'c', 'i']:
assert (abs(a - b) < threshold).all(), \
"abs(%s - %s) max(|a - b|) = %s threshold:%s" % \
(a, b, (abs(a - b)).max(), threshold)
if dtype:
assert a.dtype == b.dtype, \
"%s and %s not same dtype %s and %s" % (a, b, a.dtype, b.dtype)
评论列表
文章目录