Skip to content
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

Fix np.testing.assert_almost_equal usage #630

Merged
merged 5 commits into from
Feb 15, 2022
Merged

Conversation

mcwitt
Copy link
Collaborator

@mcwitt mcwitt commented Feb 14, 2022

np.testing.assert_almost_equal expects a number of decimals to check as its 3rd argument, but we instead pass rtol in tests for du_dl and du_dp in GradientTest.compare_forces.

It appears that decimal between 0 and 1 is interpreted as 0 rather than raising an error. For example, the following succeeds:

np.testing.assert_almost_equal(0.1, 0.2, 1e-4)

To do

  • Investigate cases where we needed to modify tests to pass

Third argument to np.testing.assert_almost_equal should be integer number of decimals
@@ -443,9 +443,9 @@ def compare_forces(
if compute_du_dx:
self.assert_equal_vectors(np.array(ref_du_dx), np.array(test_du_dx), rtol)
if compute_du_dl:
np.testing.assert_almost_equal(ref_du_dl, test_du_dl, rtol)
np.testing.assert_allclose(ref_du_dl, test_du_dl, rtol=rtol)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add an atol too?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want a rtol at all? Based on what is here: https://numpy.org/doc/stable/reference/generated/numpy.allclose.html it seems like rtol might be more flexible than we want? IE if the values are say 1,000,000 a relative tolerance of 1e-4 is 100, when we probably want to say that the values are always different by 0.001?

Copy link
Collaborator Author

@mcwitt mcwitt Feb 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want a rtol at all?

Using atol instead makes the most sense to me for fixed-fixed comparisons, but also seems appropriate for fixed-floating like we have here. Maybe requiring both atol and rtol would make sense for fixed-floating?

tests/test_nonbonded.py Outdated Show resolved Hide resolved
@proteneer proteneer added the bug Something isn't working label Feb 14, 2022
@mcwitt mcwitt force-pushed the fix-almost-equal-test branch from 33d8477 to b1fae89 Compare February 14, 2022 22:01
if compute_du_dp:
np.testing.assert_almost_equal(ref_du_dp, test_du_dp, rtol)
np.testing.assert_allclose(ref_du_dp, test_du_dp, rtol=rtol, atol=atol)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: isclose(a, b, rtol=rtol) != isclose(b, a, rtol=rtol) in general -- the order of ref_ and test_ should probably be flipped in a few places

Copy link
Collaborator Author

@mcwitt mcwitt Feb 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this occurs in other files, and to not block the bug fix, I will address these all together in an upcoming PR. Created #635 to track

@mcwitt mcwitt marked this pull request as ready for review February 15, 2022 00:44
Copy link
Owner

@proteneer proteneer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@mcwitt mcwitt merged commit e9f2034 into master Feb 15, 2022
@mcwitt mcwitt deleted the fix-almost-equal-test branch February 15, 2022 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cr_scipy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants