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
Conversation
In cases where the two arrays have zeros at the same positions it will no longer report nan as the max relative error
Note to self: needs backport to 1.16. |
Dang, I just went back to the original issue. The issue is really only for exact zeros? (everything else would not map to |
Yeah oops, you're right, I thought there would be more possibilities for the result to be nan. x = np.array([np.nan,1])
y = np.array([np.nan,2])
np.testing.assert_allclose(x,y, equal_nan=False) Output:
Should I make a revert commit to revert the previous modifications, then add your changes in a separate one, or should I do it in just one commit? |
don't worry about the history. If you want you can make a local backup branch just in case I guess. |
# Filter values where the divisor would be zero | ||
nonzero = y != 0 | ||
if all(~nonzero): | ||
max_rel_error = array(inf) |
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.
OK, I guess this seems right if y
happens to be all zero. I believe the result here should be just inf
and not an array with inf
inside, max
also returns a scalar. (Although this is probably just printed, so it may not even make a difference.
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
max_rel_error
is passed to array2string
and then printed, that's why I made it an array (although I guess float_(inf)
would also work, but it makes no difference)
For the inf
branch there is already a test: testing.tests.test_utils.TestAlmostEqual.test_error_message
which tests this exact scenario of y being all zeros.
I've also written a test for the original issue TestAssertAllclose.test_report_max_relative_error
Close/reopen to rerun tests. |
There is a problem with the test. |
Pushing off to 1.17.2. |
#14203 changed the mismatch message in assert_array_compare, and that broke my test. I'll update it tomorrow when I get home. |
LGTM. @seberg care to re-review? |
max_rel_error = (error / abs(y)).max() | ||
# Filter values where the divisor would be zero | ||
nonzero = y != 0 | ||
if all(~nonzero): |
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 use nonzero.all()
(the other one may be pythons all
). 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 test TestArrayEqual.test_subclass_that_overrides_eq
the not equal operator returns a single bool - calling .all()
on it would raise an AttributeError
. You're right about all
being python's all
- 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 use any
:
nonzero = y != 0
if not any(nonzero):
max_rel_error = array(inf)
else:
max_rel_error = (error[nonzero] / abs(y[nonzero])).max()
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?
nonzero = bool_(y != 0)
if all(~nonzero):
max_rel_error = array(inf)
else:
max_rel_error = (error[nonzero] / abs(y[nonzero])).max()
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 uses np.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.
…_array_compare, fix behaviour for certain ndarray subclasses.
@seberg Look good to you? |
Yes, looks good. Thanks @CakeWithSteak. |
…gh-13802) Fixed maximum relative error reporting in assert_allclose: In cases where the two arrays have zeros at the same positions it will no longer report nan as the max relative error
…gh-13802) Fixed maximum relative error reporting in assert_allclose: In cases where the two arrays have zeros at the same positions it will no longer report nan as the max relative error
…gh-13802) Fixed maximum relative error reporting in assert_allclose: In cases where the two arrays have zeros at the same positions it will no longer report nan as the max relative error
Fixed maximum relative error reporting in
assert_allclose
.In cases where the two arrays have zeros at the same positions it will
no longer report nan as the max relative error.
Fixes #13801.