def assert_rel_equal(a1, a2, decimals, err_msg='', verbose=True):
# We have nan checks in here because occasionally we have fields that get
# weighted without non-zero weights. I'm looking at you, particle fields!
if isinstance(a1, np.ndarray):
assert(a1.size == a2.size)
# Mask out NaNs
assert((np.isnan(a1) == np.isnan(a2)).all())
a1[np.isnan(a1)] = 1.0
a2[np.isnan(a2)] = 1.0
# Mask out 0
ind1 = np.array(np.abs(a1) < np.finfo(a1.dtype).eps)
ind2 = np.array(np.abs(a2) < np.finfo(a2.dtype).eps)
assert((ind1 == ind2).all())
a1[ind1] = 1.0
a2[ind2] = 1.0
elif np.any(np.isnan(a1)) and np.any(np.isnan(a2)):
return True
if not isinstance(a1, np.ndarray) and a1 == a2 == 0.0:
# NANS!
a1 = a2 = 1.0
return assert_almost_equal(np.array(a1)/np.array(a2), 1.0, decimals, err_msg=err_msg,
verbose=verbose)
评论列表
文章目录