-
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 10 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,7 @@ 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_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,25 +199,31 @@ 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) | ||
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 | ||
|
@@ -227,15 +233,15 @@ def test_rmsd_custom_mass_weights(self, universe, reference): | |
weights=reference.atoms.masses) | ||
rmsd_sup_weight = rms.rmsd(A, B, weights=last_atoms_weight, center=True, | ||
superposition=True) | ||
assert_almost_equal(rmsd[1], rmsd_sup_weight, 6) | ||
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 +291,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,8 +305,8 @@ 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) | ||
|
@@ -327,13 +333,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 +354,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 +395,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 +404,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() | ||
|
@@ -438,31 +445,30 @@ def test_average_structure_deprecated_attrs(self, universe, reference): | |
def test_average_structure(self, universe, reference): | ||
ref, rmsd = _get_aligned_average_positions(self.ref_files, reference) | ||
avg = align.AverageStructure(universe, reference).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)) | ||
assert_allclose(avg.results.rmsd, rmsd, rtol=0, atol=1.5*(1e-7)) | ||
|
||
orbeckst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_average_structure_mass_weighted(self, universe, reference): | ||
ref, rmsd = _get_aligned_average_positions(self.ref_files, reference, weights='mass') | ||
avg = align.AverageStructure(universe, reference, weights='mass').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)) | ||
assert_allclose(avg.results.rmsd, rmsd, rtol=0, atol=1.5*(1e-7)) | ||
|
||
def test_average_structure_select(self, universe, reference): | ||
select = 'protein and name CA and resid 3-5' | ||
ref, rmsd = _get_aligned_average_positions(self.ref_files, reference, select=select) | ||
avg = align.AverageStructure(universe, reference, select=select).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)) | ||
assert_allclose(avg.results.rmsd, rmsd, rtol=0, atol=1.5*(1e-7)) | ||
|
||
def test_average_structure_no_ref(self, universe): | ||
ref, rmsd = _get_aligned_average_positions(self.ref_files, universe) | ||
avg = align.AverageStructure(universe).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)) | ||
assert_allclose(avg.results.rmsd, rmsd, rtol=0, atol=1.5*(1e-7)) | ||
|
||
def test_average_structure_no_msf(self, universe): | ||
avg = align.AverageStructure(universe).run() | ||
|
@@ -485,15 +491,15 @@ 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)) | ||
assert_allclose(avg.results.rmsd, rmsd, rtol=0, atol=1.5*(1e-7)) | ||
|
||
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)) | ||
assert avg.filename is None | ||
|
||
|
||
|
@@ -603,4 +609,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)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ | |
|
||
import numpy as np | ||
|
||
from numpy.testing import assert_equal, assert_almost_equal | ||
from numpy.testing import assert_equal, assert_almost_equal, assert_allclose | ||
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. remove assert_almost_equal: the whole point of the PR is to get rid of it :-) |
||
|
||
import MDAnalysis as mda | ||
from MDAnalysis.analysis import base | ||
|
@@ -194,7 +194,7 @@ def test_start_stop_step(u, run_kwargs, frames): | |
assert an.n_frames == len(frames) | ||
assert_equal(an.found_frames, frames) | ||
assert_equal(an.frames, frames, err_msg=FRAMES_ERR) | ||
assert_almost_equal(an.times, frames+1, decimal=4, err_msg=TIMES_ERR) | ||
assert_allclose(an.times, frames+1, rtol=0, atol=1.5*(1e-4), err_msg=TIMES_ERR) | ||
|
||
|
||
@pytest.mark.parametrize('run_kwargs, frames', [ | ||
|
@@ -251,7 +251,7 @@ def test_frames_times(): | |
assert an.n_frames == len(frames) | ||
assert_equal(an.found_frames, frames) | ||
assert_equal(an.frames, frames, err_msg=FRAMES_ERR) | ||
assert_almost_equal(an.times, frames*100, decimal=4, err_msg=TIMES_ERR) | ||
assert_allclose(an.times, frames*100, rtol=0, atol=1.5*(1e-4), err_msg=TIMES_ERR) | ||
|
||
|
||
def test_verbose(u): | ||
|
@@ -372,7 +372,7 @@ def test_AnalysisFromFunction_args_content(u): | |
ans = base.AnalysisFromFunction(mass_xyz, protein, another, masses) | ||
assert len(ans.args) == 3 | ||
result = np.sum(ans.run().results.timeseries) | ||
assert_almost_equal(result, -317054.67757345125, decimal=6) | ||
assert_allclose(result, -317054.67757345125, rtol=0, atol=1.5*(1e-6)) | ||
assert (ans.args[0] is protein) and (ans.args[1] is another) | ||
assert ans._trajectory is protein.universe.trajectory | ||
|
||
|
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.