-
Notifications
You must be signed in to change notification settings - Fork 653
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
Replaced numpy.testing.assert_almost_equal to numpy.testing.assert_allclose #4438
Changes from 2 commits
cb0391c
911c808
a90f4b5
bee7297
4353a09
9096aee
5273bec
b4000f0
6bd2496
72b26fa
2bdf930
2cd7535
4c5c472
d649ea4
2a9e77b
6ef4f2c
15cb23f
09162ba
b466982
e1f2f4e
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 |
---|---|---|
|
@@ -167,7 +167,8 @@ def test_subselection_alignto(self, universe, reference, subselection, expectati | |
|
||
with expectation: | ||
rmsd = align.alignto(universe, reference, subselection=subselection) | ||
assert_almost_equal(rmsd[1], 0.0, decimal=9) | ||
# assert_almost_equal(rmsd[1], 0.0, decimal=9) | ||
assert_allclose(rmsd[1], 0.0, rtol=0, atol=1.5*1e-9) | ||
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. I think we can remove the commented old version |
||
|
||
def test_no_atom_masses(self, universe): | ||
#if no masses are present | ||
|
@@ -199,43 +200,49 @@ def test_rmsd(self, universe, reference): | |
first_frame = bb.positions | ||
universe.trajectory[-1] | ||
last_frame = bb.positions | ||
assert_almost_equal(rms.rmsd(first_frame, first_frame), 0.0, 5, | ||
err_msg="error: rmsd(X,X) should be 0") | ||
|
||
assert_allclose(rms.rmsd(first_frame, first_frame), 0.0, rtol=0, atol=1.5*1e-5, | ||
err_msg="error: rmsd(X,X) should be 0") | ||
|
||
|
||
# rmsd(A,B) = rmsd(B,A) should be exact but spurious failures in the | ||
# 9th decimal have been observed (see Issue 57 comment #1) so we relax | ||
# the test to 6 decimals. | ||
rmsd = rms.rmsd(first_frame, last_frame, superposition=True) | ||
assert_almost_equal( | ||
rms.rmsd(last_frame, first_frame, superposition=True), rmsd, 6, | ||
|
||
assert_allclose( | ||
rms.rmsd(last_frame, first_frame, superposition=True), rmsd, rtol=0, atol=1.5*1e-6, | ||
err_msg="error: rmsd() is not symmetric") | ||
assert_almost_equal(rmsd, 6.820321761927005, 5, | ||
err_msg="RMSD calculation between 1st and last AdK frame gave wrong answer") | ||
|
||
assert_allclose(rmsd, 6.820321761927005, rtol=0, atol=1.5*(1e-5), | ||
err_msg="RMSD calculation between 1st and last AdK frame gave wrong answer") | ||
|
||
# test masses as weights | ||
last_atoms_weight = universe.atoms.masses | ||
A = universe.trajectory[0] | ||
B = reference.trajectory[-1] | ||
rmsd = align.alignto(universe, reference, weights='mass') | ||
rmsd_sup_weight = rms.rmsd(A, B, weights=last_atoms_weight, center=True, | ||
superposition=True) | ||
assert_almost_equal(rmsd[1], rmsd_sup_weight, 6) | ||
superposition=True) | ||
assert_allclose(rmsd[1], rmsd_sup_weight, rtol=0, atol=1.5*(1e-6)) | ||
|
||
def test_rmsd_custom_mass_weights(self, universe, reference): | ||
last_atoms_weight = universe.atoms.masses | ||
A = universe.trajectory[0] | ||
B = reference.trajectory[-1] | ||
rmsd = align.alignto(universe, reference, | ||
weights=reference.atoms.masses) | ||
weights=reference.atoms.masses) | ||
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. some of the whitespace changes don't seem useful? |
||
rmsd_sup_weight = rms.rmsd(A, B, weights=last_atoms_weight, center=True, | ||
superposition=True) | ||
assert_almost_equal(rmsd[1], rmsd_sup_weight, 6) | ||
superposition=True) | ||
assert_allclose(rmsd[1], rmsd_sup_weight, rtol=0, atol=1.5*(1e-6)) | ||
|
||
def test_rmsd_custom_weights(self, universe, reference): | ||
weights = np.zeros(universe.atoms.n_atoms) | ||
ca = universe.select_atoms('name CA') | ||
weights[ca.indices] = 1 | ||
rmsd = align.alignto(universe, reference, select='name CA') | ||
rmsd_weights = align.alignto(universe, reference, weights=weights) | ||
assert_almost_equal(rmsd[1], rmsd_weights[1], 6) | ||
assert_allclose(rmsd[1], rmsd_weights[1], rtol=0, atol=1.5*(1e-6)) | ||
|
||
def test_AlignTraj_outfile_default(self, universe, reference, tmpdir): | ||
with tmpdir.as_cwd(): | ||
|
@@ -285,8 +292,8 @@ def test_AlignTraj(self, universe, reference, tmpdir): | |
x = align.AlignTraj(universe, reference, filename=outfile).run() | ||
fitted = mda.Universe(PSF, outfile) | ||
|
||
assert_almost_equal(x.results.rmsd[0], 6.9290, decimal=3) | ||
assert_almost_equal(x.results.rmsd[-1], 5.2797e-07, decimal=3) | ||
assert_allclose(x.results.rmsd[0], 6.9290, rtol=0, atol=1.5*(1e-3)) | ||
assert_allclose(x.results.rmsd[-1], 5.2797e-07, rtol=0, atol=1.5*(1e-3)) | ||
|
||
# RMSD against the reference frame | ||
# calculated on Mac OS X x86 with MDA 0.7.2 r689 | ||
|
@@ -299,13 +306,13 @@ def test_AlignTraj_weighted(self, universe, reference, tmpdir): | |
x = align.AlignTraj(universe, reference, | ||
filename=outfile, weights='mass').run() | ||
fitted = mda.Universe(PSF, outfile) | ||
assert_almost_equal(x.results.rmsd[0], 0, decimal=3) | ||
assert_almost_equal(x.results.rmsd[-1], 6.9033, decimal=3) | ||
assert_allclose(x.results.rmsd[0], 0, rtol=0, atol=1.5*(1e-3)) | ||
assert_allclose(x.results.rmsd[-1], 6.9033, rtol=0, atol=1.5*(1e-3)) | ||
|
||
self._assert_rmsd(reference, fitted, 0, 0.0, | ||
weights=universe.atoms.masses) | ||
weights=universe.atoms.masses) | ||
self._assert_rmsd(reference, fitted, -1, 6.929083032629219, | ||
weights=universe.atoms.masses) | ||
weights=universe.atoms.masses) | ||
|
||
def test_AlignTraj_custom_weights(self, universe, reference, tmpdir): | ||
weights = np.zeros(universe.atoms.n_atoms) | ||
|
@@ -327,13 +334,13 @@ def test_AlignTraj_custom_mass_weights(self, universe, reference, tmpdir): | |
filename=outfile, | ||
weights=reference.atoms.masses).run() | ||
fitted = mda.Universe(PSF, outfile) | ||
assert_almost_equal(x.results.rmsd[0], 0, decimal=3) | ||
assert_almost_equal(x.results.rmsd[-1], 6.9033, decimal=3) | ||
assert_allclose(x.results.rmsd[0], 0, rtol=0, atol=1.5*(1e-3)) | ||
assert_allclose(x.results.rmsd[-1], 6.9033, rtol=0, atol=1.5*(1e-3)) | ||
|
||
self._assert_rmsd(reference, fitted, 0, 0.0, | ||
weights=universe.atoms.masses) | ||
weights=universe.atoms.masses) | ||
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. undo indent |
||
self._assert_rmsd(reference, fitted, -1, 6.929083032629219, | ||
weights=universe.atoms.masses) | ||
weights=universe.atoms.masses) | ||
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. undo indent |
||
|
||
def test_AlignTraj_partial_fit(self, universe, reference, tmpdir): | ||
outfile = str(tmpdir.join('align_test.dcd')) | ||
|
@@ -348,20 +355,21 @@ def test_AlignTraj_in_memory(self, universe, reference, tmpdir): | |
x = align.AlignTraj(universe, reference, filename=outfile, | ||
in_memory=True).run() | ||
assert x.filename is None | ||
assert_almost_equal(x.results.rmsd[0], 6.9290, decimal=3) | ||
assert_almost_equal(x.results.rmsd[-1], 5.2797e-07, decimal=3) | ||
assert_allclose(x.results.rmsd[0], 6.9290, rtol=0, atol=1.5*(1e-3)) | ||
assert_allclose(x.results.rmsd[-1], 5.2797e-07, rtol=0, atol=1.5*(1e-3)) | ||
|
||
# check in memory trajectory | ||
self._assert_rmsd(reference, universe, 0, 6.929083044751061) | ||
self._assert_rmsd(reference, universe, -1, 0.0) | ||
|
||
def _assert_rmsd(self, reference, fitted, frame, desired, weights=None): | ||
fitted.trajectory[frame] | ||
rmsd = rms.rmsd(reference.atoms.positions, fitted.atoms.positions, | ||
superposition=True) | ||
assert_almost_equal(rmsd, desired, decimal=5, | ||
err_msg="frame {0:d} of fit does not have " | ||
"expected RMSD".format(frame)) | ||
fitted.trajectory[frame] | ||
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. You still have unwanted whitespace changes here and elsewhere, please re-read the entire diff line-by-line and make sure those changes are not present (since I'm spending time doing the same). |
||
rmsd = rms.rmsd(reference.atoms.positions, fitted.atoms.positions, | ||
superposition=True) | ||
np.testing.assert_allclose(rmsd, desired, rtol=0, atol=1.5*(1e-5), | ||
err_msg="frame {0:d} of fit does not have " | ||
"expected RMSD".format(frame)) | ||
|
||
|
||
def test_alignto_checks_selections(self, universe, reference): | ||
"""Testing that alignto() fails if selections do not | ||
|
@@ -388,7 +396,6 @@ def test_alignto_partial_universe(self, universe, reference): | |
u_bound = mda.Universe(ALIGN_BOUND) | ||
u_free = mda.Universe(ALIGN_UNBOUND) | ||
selection = 'segid B' | ||
|
||
segB_bound = u_bound.select_atoms(selection) | ||
segB_free = u_free.select_atoms(selection) | ||
segB_free.translate(segB_bound.centroid() - segB_free.centroid()) | ||
|
@@ -398,6 +405,7 @@ def test_alignto_partial_universe(self, universe, reference): | |
decimal=3) | ||
|
||
|
||
|
||
def _get_aligned_average_positions(ref_files, ref, select="all", **kwargs): | ||
u = mda.Universe(*ref_files, in_memory=True) | ||
prealigner = align.AlignTraj(u, ref, select=select, **kwargs).run() | ||
|
@@ -485,15 +493,19 @@ def test_average_structure_ref_frame(self, universe): | |
universe.trajectory[0] | ||
ref, rmsd = _get_aligned_average_positions(self.ref_files, u) | ||
avg = align.AverageStructure(universe, ref_frame=ref_frame).run() | ||
assert_almost_equal(avg.results.universe.atoms.positions, ref, | ||
decimal=4) | ||
assert_almost_equal(avg.results.rmsd, rmsd) | ||
assert_allclose(avg.results.universe.atoms.positions, ref, | ||
rtol=0, atol=1.5*(1e-4), | ||
err_msg="assert_allclose failed for positions") | ||
assert_allclose(avg.results.rmsd, rmsd, | ||
rtol=0, atol=1.5*(1e-4), | ||
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. Don't you want 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. Yeah, that's a mistake. I will correct this. |
||
err_msg="assert_allclose failed for rmsd") | ||
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. fwiw, I don't think these custom messages are worth it in this context, but that may just be based on the style of the projects I usually maintain |
||
|
||
def test_average_structure_in_memory(self, universe): | ||
avg = align.AverageStructure(universe, in_memory=True).run() | ||
reference_coordinates = universe.trajectory.timeseries().mean(axis=1) | ||
assert_almost_equal(avg.results.universe.atoms.positions, | ||
reference_coordinates, decimal=4) | ||
assert_allclose(avg.results.universe.atoms.positions, | ||
reference_coordinates, rtol=0, atol=1.5*(1e-4), | ||
err_msg="assert_allclose failed for positions") | ||
assert avg.filename is None | ||
|
||
|
||
|
@@ -603,4 +615,4 @@ def test_alignto_reorder_atomgroups(): | |
mobile = u.atoms[:4] | ||
ref = u.atoms[[3, 2, 1, 0]] | ||
rmsd = align.alignto(mobile, ref, select='bynum 1-4') | ||
assert_allclose(rmsd, (0.0, 0.0)) | ||
assert_allclose(rmsd, (0.0, 0.0)) |
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.
Write floats as
1.5e-4
and not as1.5*(1e-4)
.This applies to all floats with powers of ten throughout.