-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: Fixed maximum relative error reporting in assert_allclose #13802
Changes from all commits
741e6b6
26ed51a
5d2aee7
08bca18
d5cad8c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -703,7 +703,7 @@ def assert_array_compare(comparison, x, y, err_msg='', verbose=True, | |
header='', precision=6, equal_nan=True, | ||
equal_inf=True): | ||
__tracebackhide__ = True # Hide traceback for py.test | ||
from numpy.core import array, array2string, isnan, inf, bool_, errstate | ||
from numpy.core import array, array2string, isnan, inf, bool_, errstate, all | ||
|
||
x = array(x, copy=False, subok=True) | ||
y = array(y, copy=False, subok=True) | ||
|
@@ -821,7 +821,12 @@ def func_assert_same_pos(x, y, func=isnan, hasval='nan'): | |
|
||
# note: this definition of relative error matches that one | ||
# used by assert_allclose (found in np.isclose) | ||
max_rel_error = (error / abs(y)).max() | ||
# Filter values where the divisor would be zero | ||
nonzero = bool_(y != 0) | ||
if all(~nonzero): | ||
max_rel_error = array(inf) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, I guess this seems right if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a test for this branch (assuming it does not exist)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
else: | ||
max_rel_error = (error[nonzero] / abs(y[nonzero])).max() | ||
if error.dtype == 'object': | ||
remarks.append('Max relative difference: ' | ||
+ str(max_rel_error)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nonzero
is guaranteed to be an array here I think, so it would be better to usenonzero.all()
(the other one may be pythonsall
). Other than that, seems all fine to me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nonzero
isn't necessarily an array, for example in testTestArrayEqual.test_subclass_that_overrides_eq
the not equal operator returns a single bool - calling.all()
on it would raise anAttributeError
. You're right aboutall
being python'sall
- I'll import the function from numpy to fix that.Also, applying operator
~
to a bool wouldn't work as intended, so I think it would be better to useany
:That would work even if the not equal operator returns a single bool, but it's kind of hard to read, so we could also invert the if statement.
Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A numpy bool woul dbe good enough and considering that indexing works below it should do. But
np.all
is fine as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, simply this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just make it
np.any
and I will be happy (or tell me that it already usesnp.any
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean
np.all
here? Yes,all
is imported at the top of the function.