diff --git a/package/AUTHORS b/package/AUTHORS index 0e1b5d77b83..f8e82065ad1 100644 --- a/package/AUTHORS +++ b/package/AUTHORS @@ -233,6 +233,8 @@ Chronological list of authors - Lawson Woods - Johannes Stöckelmaier - Jenna M. Swarthout Goddard + 2024 + - Aditya Keshari External code ------------- diff --git a/package/CHANGELOG b/package/CHANGELOG index a08062b7b71..23a72464cdd 100644 --- a/package/CHANGELOG +++ b/package/CHANGELOG @@ -15,7 +15,7 @@ The rules for this file: ------------------------------------------------------------------------------- ??/??/?? IAlibay, HeetVekariya, marinegor, lilyminium, RMeli, - ljwoods2 + ljwoods2, aditya292002 * 2.8.0 @@ -40,6 +40,10 @@ Changes * As per NEP29, the minimum version of numpy has been raised to 1.23. We have opted to pin to 1.23.2 to ensure the same minimum numpy version is used from python 3.9 to 3.11 (Issue #4401, PR #4402) + * updated tests that used assert_almost_equal(..., decimal={N}) with + equivalent assert_allclose(... rtol=0, atol=1.5e-{N}) (issue modernize + testing code #3743, PR Replaced numpy.testing.assert_almost_equal to + numpy.testing.assert_allclose #4438) Deprecations * The MDAnalysis.analysis.waterdynamics module has been deprecated in favour diff --git a/package/MDAnalysis/visualization/streamlines_3D.py b/package/MDAnalysis/visualization/streamlines_3D.py index 1ad20063c23..1f85851c16a 100644 --- a/package/MDAnalysis/visualization/streamlines_3D.py +++ b/package/MDAnalysis/visualization/streamlines_3D.py @@ -245,8 +245,8 @@ def point_in_cube(array_point_coordinates, list_cube_vertices, cube_centroid): cube_half_side_length = scipy.spatial.distance.pdist(array_cube_vertices, 'euclidean').min() / 2.0 array_cube_vertex_distances_from_centroid = scipy.spatial.distance.cdist(array_cube_vertices, cube_centroid[np.newaxis, :]) - np.testing.assert_almost_equal(array_cube_vertex_distances_from_centroid.min(), - array_cube_vertex_distances_from_centroid.max(), decimal=4, + np.testing.assert_allclose(array_cube_vertex_distances_from_centroid.min(), + array_cube_vertex_distances_from_centroid.max(), rtol=0, atol=1.5e-4, err_msg="not all cube vertex to centroid distances are the same, " "so not a true cube") absolute_delta_coords = np.absolute(np.subtract(array_point_coordinates, cube_centroid)) diff --git a/testsuite/MDAnalysisTests/analysis/test_align.py b/testsuite/MDAnalysisTests/analysis/test_align.py index e6ff3acb4cc..b7dcc478ec5 100644 --- a/testsuite/MDAnalysisTests/analysis/test_align.py +++ b/testsuite/MDAnalysisTests/analysis/test_align.py @@ -34,10 +34,8 @@ from MDAnalysisTests.datafiles import (PSF, DCD, CRD, FASTA, ALIGN_BOUND, ALIGN_UNBOUND, PDB_helix) from numpy.testing import ( - assert_almost_equal, assert_equal, assert_array_equal, - assert_array_almost_equal, assert_allclose, ) @@ -167,7 +165,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.5e-9) def test_no_atom_masses(self, universe): #if no masses are present @@ -199,17 +197,17 @@ 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.5e-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.5e-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.5e-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] @@ -217,7 +215,7 @@ def test_rmsd(self, universe, reference): 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.5e-6) def test_rmsd_custom_mass_weights(self, universe, reference): last_atoms_weight = universe.atoms.masses @@ -227,7 +225,7 @@ 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.5e-6) def test_rmsd_custom_weights(self, universe, reference): weights = np.zeros(universe.atoms.n_atoms) @@ -235,7 +233,7 @@ def test_rmsd_custom_weights(self, universe, reference): 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.5e-6) def test_AlignTraj_outfile_default(self, universe, reference, tmpdir): with tmpdir.as_cwd(): @@ -285,8 +283,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.5e-3) + assert_allclose(x.results.rmsd[-1], 5.2797e-07, rtol=0, atol=1.5e-3) # RMSD against the reference frame # calculated on Mac OS X x86 with MDA 0.7.2 r689 @@ -299,8 +297,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.5e-3) + assert_allclose(x.results.rmsd[-1], 6.9033, rtol=0, atol=1.5e-3) self._assert_rmsd(reference, fitted, 0, 0.0, weights=universe.atoms.masses) @@ -319,7 +317,7 @@ def test_AlignTraj_custom_weights(self, universe, reference, tmpdir): x_weights = align.AlignTraj(universe, reference, filename=outfile, weights=weights).run() - assert_array_almost_equal(x.results.rmsd, x_weights.results.rmsd) + assert_allclose(x.results.rmsd, x_weights.results.rmsd, rtol=0, atol=1.5e-7) def test_AlignTraj_custom_mass_weights(self, universe, reference, tmpdir): outfile = str(tmpdir.join('align_test.dcd')) @@ -327,8 +325,8 @@ 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.5e-3) + assert_allclose(x.results.rmsd[-1], 6.9033, rtol=0, atol=1.5e-3) self._assert_rmsd(reference, fitted, 0, 0.0, weights=universe.atoms.masses) @@ -348,8 +346,8 @@ 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.5e-3) + assert_allclose(x.results.rmsd[-1], 5.2797e-07, rtol=0, atol=1.5e-3) # check in memory trajectory self._assert_rmsd(reference, universe, 0, 6.929083044751061) @@ -359,7 +357,7 @@ 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, + assert_allclose(rmsd, desired, rtol = 0, atol=1.5e-5, err_msg="frame {0:d} of fit does not have " "expected RMSD".format(frame)) @@ -394,8 +392,8 @@ def test_alignto_partial_universe(self, universe, reference): segB_free.translate(segB_bound.centroid() - segB_free.centroid()) align.alignto(u_free, u_bound, select=selection) - assert_array_almost_equal(segB_bound.positions, segB_free.positions, - decimal=3) + assert_allclose(segB_bound.positions, segB_free.positions, + rtol=0, atol=1.5e-3) def _get_aligned_average_positions(ref_files, ref, select="all", **kwargs): @@ -438,31 +436,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.5e-4) + assert_allclose(avg.results.rmsd, rmsd, rtol=0, atol=1.5e-7) 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.5e-4) + assert_allclose(avg.results.rmsd, rmsd, rtol=0, atol=1.5e-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.5e-4) + assert_allclose(avg.results.rmsd, rmsd, rtol=0, atol=1.5e-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.5e-4) + assert_allclose(avg.results.rmsd, rmsd, rtol=0, atol=1.5e-7) def test_average_structure_no_msf(self, universe): avg = align.AverageStructure(universe).run() @@ -485,15 +482,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.5e-4) + assert_allclose(avg.results.rmsd, rmsd, rtol=0, atol=1.5e-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.5e-4) assert avg.filename is None diff --git a/testsuite/MDAnalysisTests/analysis/test_base.py b/testsuite/MDAnalysisTests/analysis/test_base.py index 1817cb66b35..b6ff986a8bb 100644 --- a/testsuite/MDAnalysisTests/analysis/test_base.py +++ b/testsuite/MDAnalysisTests/analysis/test_base.py @@ -27,7 +27,7 @@ import numpy as np -from numpy.testing import assert_equal, assert_almost_equal +from numpy.testing import assert_equal, assert_allclose 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.5e-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.5e-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.5e-6) assert (ans.args[0] is protein) and (ans.args[1] is another) assert ans._trajectory is protein.universe.trajectory diff --git a/testsuite/MDAnalysisTests/analysis/test_bat.py b/testsuite/MDAnalysisTests/analysis/test_bat.py index 32b0262597b..c80353baff6 100644 --- a/testsuite/MDAnalysisTests/analysis/test_bat.py +++ b/testsuite/MDAnalysisTests/analysis/test_bat.py @@ -23,7 +23,7 @@ from __future__ import absolute_import import numpy as np -from numpy.testing import assert_equal, assert_almost_equal +from numpy.testing import assert_equal, assert_allclose import pytest import copy @@ -66,35 +66,38 @@ def test_bat_number_of_frames(self, bat): def test_bat_coordinates(self, bat): test_bat = np.load(BATArray) - assert_almost_equal( + assert_allclose( bat, test_bat, - 5, + rtol=0, + atol=1.5e-5, err_msg="error: BAT coordinates should match test values") def test_bat_coordinates_single_frame(self, selected_residues): bat = BAT(selected_residues).run(start=1, stop=2).results.bat test_bat = [np.load(BATArray)[1]] - assert_almost_equal( + assert_allclose( bat, test_bat, - 5, + rtol=0, + atol=1.5e-5, err_msg="error: BAT coordinates should match test values") def test_bat_reconstruction(self, selected_residues, bat): R = BAT(selected_residues) XYZ = R.Cartesian(bat[0]) - assert_almost_equal(XYZ, selected_residues.positions, 5, + assert_allclose(XYZ, selected_residues.positions, rtol=0, atol=1.5e-5, err_msg="error: Reconstructed Cartesian coordinates " + \ "don't match original") def test_bat_IO(self, bat_npz, selected_residues, bat): R2 = BAT(selected_residues, filename=bat_npz) test_bat = R2.results.bat - assert_almost_equal( + assert_allclose( bat, test_bat, - 5, + rtol=0, + atol=1.5e-5, err_msg="error: Loaded BAT coordinates should match test values") def test_bat_nobonds(self): @@ -133,7 +136,7 @@ def test_Cartesian_does_not_modify_input(self, selected_residues, bat): R = BAT(selected_residues) pre_transformation = copy.deepcopy(bat[0]) R.Cartesian(bat[0]) - assert_almost_equal( - pre_transformation, bat[0], + assert_allclose( + pre_transformation, bat[0], rtol=0, atol=1.5e-7, err_msg="BAT.Cartesian modified input data" ) diff --git a/testsuite/MDAnalysisTests/analysis/test_contacts.py b/testsuite/MDAnalysisTests/analysis/test_contacts.py index 26c053ae32b..ad4f96caf44 100644 --- a/testsuite/MDAnalysisTests/analysis/test_contacts.py +++ b/testsuite/MDAnalysisTests/analysis/test_contacts.py @@ -27,10 +27,9 @@ from MDAnalysis.analysis.distances import distance_array from numpy.testing import ( - assert_almost_equal, assert_equal, assert_array_equal, - assert_array_almost_equal + assert_allclose, ) import numpy as np @@ -48,8 +47,8 @@ def test_soft_cut_q(): # just check some of the extremal points assert contacts.soft_cut_q([0], [0]) == .5 - assert_almost_equal(contacts.soft_cut_q([100], [0]), 0) - assert_almost_equal(contacts.soft_cut_q([-100], [0]), 1) + assert_allclose(contacts.soft_cut_q([100], [0]), 0, rtol=0, atol=1.5e-7) + assert_allclose(contacts.soft_cut_q([-100], [0]), 1, rtol=0, atol=1.5e-7) def test_soft_cut_q_folded(): @@ -67,7 +66,7 @@ def test_soft_cut_q_folded(): lambda_constant = 1.8 Q = 1 / (1 + np.exp(beta * (r - lambda_constant * r0))) - assert_almost_equal(Q.mean(), 1.0, decimal=3) + assert_allclose(Q.mean(), 1.0, rtol=0, atol=1.5e-3) def test_soft_cut_q_unfolded(): @@ -85,7 +84,7 @@ def test_soft_cut_q_unfolded(): lambda_constant = 1.8 Q = 1 / (1 + np.exp(beta * (r - lambda_constant * r0))) - assert_almost_equal(Q.mean(), 0.0, decimal=1) + assert_allclose(Q.mean(), 0.0, rtol=0, atol=1.5e-1) @pytest.mark.parametrize('r, cutoff, expected_value', [ @@ -263,7 +262,7 @@ def test_villin_folded(self): q.run() results = soft_cut(f, u, sel, sel) - assert_almost_equal(q.results.timeseries[:, 1], results[:, 1]) + assert_allclose(q.results.timeseries[:, 1], results[:, 1], rtol=0, atol=1.5e-7) def test_villin_unfolded(self): # both folded @@ -280,7 +279,7 @@ def test_villin_unfolded(self): q.run() results = soft_cut(f, u, sel, sel) - assert_almost_equal(q.results.timeseries[:, 1], results[:, 1]) + assert_allclose(q.results.timeseries[:, 1], results[:, 1], rtol=0, atol=1.5e-7) def test_hard_cut_method(self, universe): ca = self._run_Contacts(universe) @@ -305,7 +304,7 @@ def test_hard_cut_method(self, universe): 0.48543689, 0.45631068, 0.46601942, 0.47572816, 0.51456311, 0.45631068, 0.37864078, 0.42718447] assert len(ca.results.timeseries) == len(expected) - assert_array_almost_equal(ca.results.timeseries[:, 1], expected) + assert_allclose(ca.results.timeseries[:, 1], expected, rtol=0, atol=1.5e-7) def test_radius_cut_method(self, universe): acidic = universe.select_atoms(self.sel_acidic) @@ -365,7 +364,7 @@ def test_distance_box(self, pbc, expected): r = contacts.Contacts(u, select=(sel_acidic, sel_basic), refgroup=(acidic, basic), radius=6.0, pbc=pbc) r.run() - assert_array_almost_equal(r.results.timeseries[:, 1], expected) + assert_allclose(r.results.timeseries[:, 1], expected,rtol=0, atol=1.5e-7) def test_warn_deprecated_attr(self, universe): """Test for warning message emitted on using deprecated `timeseries` @@ -411,7 +410,7 @@ def test_q1q2(): 0.93097184, 0.93006358, 0.93188011, 0.93278837, 0.93006358, 0.92915531, 0.92824705, 0.92733878, 0.92643052, 0.93188011, 0.93006358, 0.9346049, 0.93188011] - assert_array_almost_equal(q1q2.results.timeseries[:, 1], q1_expected) + assert_allclose(q1q2.results.timeseries[:, 1], q1_expected, rtol=0, atol=1.5e-7) q2_expected = [0.94649446, 0.94926199, 0.95295203, 0.95110701, 0.94833948, 0.95479705, 0.94926199, 0.9501845, 0.94926199, 0.95387454, @@ -433,4 +432,4 @@ def test_q1q2(): 0.97140221, 0.97601476, 0.97693727, 0.98154982, 0.98431734, 0.97601476, 0.9797048, 0.98154982, 0.98062731, 0.98431734, 0.98616236, 0.9898524, 1.] - assert_array_almost_equal(q1q2.results.timeseries[:, 2], q2_expected) + assert_allclose(q1q2.results.timeseries[:, 2], q2_expected, rtol=0, atol=1.5e-7) diff --git a/testsuite/MDAnalysisTests/analysis/test_dihedrals.py b/testsuite/MDAnalysisTests/analysis/test_dihedrals.py index 7aeb4e9d3b9..731d5cb0397 100644 --- a/testsuite/MDAnalysisTests/analysis/test_dihedrals.py +++ b/testsuite/MDAnalysisTests/analysis/test_dihedrals.py @@ -21,7 +21,7 @@ # J. Comput. Chem. 32 (2011), 2319--2327, doi:10.1002/jcc.21787 # import numpy as np -from numpy.testing import assert_almost_equal, assert_equal +from numpy.testing import assert_equal, assert_allclose import matplotlib import pytest @@ -46,7 +46,7 @@ def test_dihedral(self, atomgroup): dihedral = Dihedral([atomgroup]).run() test_dihedral = np.load(DihedralArray) - assert_almost_equal(dihedral.results.angles, test_dihedral, 5, + assert_allclose(dihedral.results.angles, test_dihedral, rtol=0, atol=1.5e-5, err_msg="error: dihedral angles should " "match test values") @@ -54,7 +54,7 @@ def test_dihedral_single_frame(self, atomgroup): dihedral = Dihedral([atomgroup]).run(start=5, stop=6) test_dihedral = [np.load(DihedralArray)[5]] - assert_almost_equal(dihedral.results.angles, test_dihedral, 5, + assert_allclose(dihedral.results.angles, test_dihedral, rtol=0, atol=1.5e-5, err_msg="error: dihedral angles should " "match test vales") @@ -62,7 +62,7 @@ def test_atomgroup_list(self, atomgroup): dihedral = Dihedral([atomgroup, atomgroup]).run() test_dihedral = np.load(DihedralsArray) - assert_almost_equal(dihedral.results.angles, test_dihedral, 5, + assert_allclose(dihedral.results.angles, test_dihedral, rtol=0, atol=1.5e-5, err_msg="error: dihedral angles should " "match test values") @@ -91,7 +91,7 @@ def rama_ref_array(self): def test_ramachandran(self, universe, rama_ref_array): rama = Ramachandran(universe.select_atoms("protein")).run() - assert_almost_equal(rama.results.angles, rama_ref_array, 5, + assert_allclose(rama.results.angles, rama_ref_array, rtol=0, atol=1.5e-5, err_msg="error: dihedral angles should " "match test values") @@ -99,7 +99,7 @@ def test_ramachandran_single_frame(self, universe, rama_ref_array): rama = Ramachandran(universe.select_atoms("protein")).run( start=5, stop=6) - assert_almost_equal(rama.results.angles[0], rama_ref_array[5], 5, + assert_allclose(rama.results.angles[0], rama_ref_array[5], rtol=0, atol=1.5e-5, err_msg="error: dihedral angles should " "match test values") @@ -107,7 +107,7 @@ def test_ramachandran_residue_selections(self, universe): rama = Ramachandran(universe.select_atoms("resname GLY")).run() test_rama = np.load(GLYRamaArray) - assert_almost_equal(rama.results.angles, test_rama, 5, + assert_allclose(rama.results.angles, test_rama, rtol=0, atol=1.5e-5, err_msg="error: dihedral angles should " "match test values") @@ -169,14 +169,14 @@ def _test_janin(self, u, ref_array): janin = Janin(u.select_atoms("protein")).run() # Test precision lowered to account for platform differences with osx - assert_almost_equal(janin.results.angles, ref_array, 3, + assert_allclose(janin.results.angles, ref_array, rtol=0, atol=1.5e-3, err_msg="error: dihedral angles should " "match test values") def test_janin_single_frame(self, universe, janin_ref_array): janin = Janin(universe.select_atoms("protein")).run(start=5, stop=6) - assert_almost_equal(janin.results.angles[0], janin_ref_array[5], 3, + assert_allclose(janin.results.angles[0], janin_ref_array[5], rtol=0, atol=1.5e-3, err_msg="error: dihedral angles should " "match test values") @@ -184,7 +184,7 @@ def test_janin_residue_selections(self, universe): janin = Janin(universe.select_atoms("resname LYS")).run() test_janin = np.load(LYSJaninArray) - assert_almost_equal(janin.results.angles, test_janin, 3, + assert_allclose(janin.results.angles, test_janin, rtol=0, atol=1.5e-3, err_msg="error: dihedral angles should " "match test values") diff --git a/testsuite/MDAnalysisTests/analysis/test_encore.py b/testsuite/MDAnalysisTests/analysis/test_encore.py index afcd490e648..bc07c21af73 100644 --- a/testsuite/MDAnalysisTests/analysis/test_encore.py +++ b/testsuite/MDAnalysisTests/analysis/test_encore.py @@ -32,7 +32,7 @@ import platform import pytest -from numpy.testing import assert_equal, assert_almost_equal +from numpy.testing import assert_equal, assert_allclose from MDAnalysisTests.datafiles import DCD, DCD2, PSF, TPR, XTC from MDAnalysisTests import block_import @@ -153,7 +153,7 @@ def test_rmsd_matrix_with_superimposition(self, ens1): "Calculated RMSD values differ from " "the reference implementation") for i, rmsd in enumerate(reference.results.rmsd): - assert_almost_equal(conf_dist_matrix[0, i], rmsd[2], 3, err_msg) + assert_allclose(conf_dist_matrix[0, i], rmsd[2], rtol=0, atol=1.5e-3, err_msg=err_msg) def test_rmsd_matrix_with_superimposition_custom_weights(self, ens1): conf_dist_matrix = encore.confdistmatrix.conformational_distance_matrix( @@ -173,7 +173,7 @@ def test_rmsd_matrix_with_superimposition_custom_weights(self, ens1): n_jobs=1) for i in range(conf_dist_matrix_custom.size): - assert_almost_equal(conf_dist_matrix_custom[0, i], conf_dist_matrix[0, i]) + assert_allclose(conf_dist_matrix_custom[0, i], conf_dist_matrix[0, i], rtol=0, atol=1.5e-7) def test_rmsd_matrix_without_superimposition(self, ens1): selection_string = "name CA" @@ -192,7 +192,7 @@ def test_rmsd_matrix_without_superimposition(self, ens1): n_jobs=1) print(repr(confdist_matrix.as_array()[0, :])) - assert_almost_equal(confdist_matrix.as_array()[0,:], reference_rmsd, decimal=3, + assert_allclose(confdist_matrix.as_array()[0,:], reference_rmsd, rtol=0, atol=1.5e-3, err_msg="calculated RMSD values differ from reference") def test_ensemble_superimposition(self): @@ -252,7 +252,7 @@ def test_covariance_matrix(self, ens1): covariance = encore.covariance.covariance_matrix(ens1, select="name CA and resnum 1:3", estimator=encore.covariance.shrinkage_covariance_estimator) - assert_almost_equal(covariance, reference_cov, decimal=4, + assert_allclose(covariance, reference_cov, rtol=0, atol=1.5e-4, err_msg="Covariance matrix from covariance estimation not as expected") def test_covariance_matrix_with_reference(self, ens1): @@ -274,13 +274,13 @@ def test_covariance_matrix_with_reference(self, ens1): err_msg = ( "Covariance matrix from covariance estimation not as expected" ) - assert_almost_equal(covariance, reference_cov, 4, err_msg) + assert_allclose(covariance, reference_cov, rtol=0, atol=1.5e-4, err_msg=err_msg) def test_hes_to_self(self, ens1): results, details = encore.hes([ens1, ens1]) result_value = results[0, 1] expected_value = 0. - assert_almost_equal(result_value, expected_value, + assert_allclose(result_value, expected_value, rtol=0, atol=1.5e-7, err_msg="Harmonic Ensemble Similarity to itself\ not zero:{0:f}".format(result_value)) @@ -298,7 +298,7 @@ def test_hes_custom_weights(self, ens1, ens2): ens2.select_atoms('name CA').masses)) result_value = results[0, 1] result_value_custom = results_custom[0, 1] - assert_almost_equal(result_value, result_value_custom) + assert_allclose(result_value, result_value_custom, rtol=0, atol=1.5e-7) def test_hes_align(self, ens1, ens2): # This test is massively sensitive! @@ -306,7 +306,7 @@ def test_hes_align(self, ens1, ens2): results, details = encore.hes([ens1, ens2], align=True) result_value = results[0,1] expected_value = 2047.05 - assert_almost_equal(result_value, expected_value, decimal=-3, + assert_allclose(result_value, expected_value, rtol=0, atol=1.5e3, err_msg="Unexpected value for Harmonic Ensemble Similarity: {0:f}. Expected {1:f}.".format(result_value, expected_value)) def test_ces_to_self(self, ens1): @@ -315,21 +315,21 @@ def test_ces_to_self(self, ens1): clustering_method=encore.AffinityPropagationNative(preference = -3.0)) result_value = results[0,1] expected_value = 0. - assert_almost_equal(result_value, expected_value, + assert_allclose(result_value, expected_value, rtol=0, atol=1.5e-7, err_msg="ClusteringEnsemble Similarity to itself not zero: {0:f}".format(result_value)) def test_ces(self, ens1, ens2): results, details = encore.ces([ens1, ens2]) result_value = results[0,1] expected_value = 0.51 - assert_almost_equal(result_value, expected_value, decimal=2, + assert_allclose(result_value, expected_value, rtol=0, atol=1.5e-2, err_msg="Unexpected value for Cluster Ensemble Similarity: {0:f}. Expected {1:f}.".format(result_value, expected_value)) def test_dres_to_self(self, ens1): results, details = encore.dres([ens1, ens1]) result_value = results[0,1] expected_value = 0. - assert_almost_equal(result_value, expected_value, decimal=2, + assert_allclose(result_value, expected_value, rtol=0, atol=1.5e-2, err_msg="Dim. Reduction Ensemble Similarity to itself not zero: {0:f}".format(result_value)) def test_dres(self, ens1, ens2): @@ -348,14 +348,14 @@ def test_dres_without_superimposition(self, ens1, ens2): distance_matrix = distance_matrix) result_value = results[0,1] expected_value = 0.68 - assert_almost_equal(result_value, expected_value, decimal=1, + assert_allclose(result_value, expected_value, rtol=0, atol=1.5e-1, err_msg="Unexpected value for Dim. reduction Ensemble Similarity: {0:f}. Expected {1:f}.".format(result_value, expected_value)) def test_ces_convergence(self, ens1): expected_values = [0.3443593, 0.1941854, 0.06857104, 0.] results = encore.ces_convergence(ens1, 5) for i,ev in enumerate(expected_values): - assert_almost_equal(ev, results[i], decimal=2, + assert_allclose(ev, results[i], rtol=0, atol=1.5e-2, err_msg="Unexpected value for Clustering Ensemble similarity in convergence estimation") def test_dres_convergence(self, ens1): @@ -365,7 +365,7 @@ def test_dres_convergence(self, ens1): expected_values = [0.3, 0.] results = encore.dres_convergence(ens1, 10) try: - assert_almost_equal(results[:,0], expected_values, decimal=1) + assert_allclose(results[:,0], expected_values, rtol=0, atol=1.5e-1) except AssertionError: # Random test failure is very rare, but repeating the failed test # just once would only assert that the test passes with 50% @@ -376,7 +376,7 @@ def test_dres_convergence(self, ens1): category=RuntimeWarning) for i in range(10): results = encore.dres_convergence(ens1, 10) - assert_almost_equal(results[:,0], expected_values, decimal=1, + assert_allclose(results[:,0], expected_values, rtol=0, atol=1.5e-1, err_msg="Unexpected value for Dim. " "reduction Ensemble similarity in " "convergence estimation") @@ -396,8 +396,8 @@ def test_hes_error_estimation(self, ens1): "Unexpected standard deviation for bootstrapped samples in" " Harmonic Ensemble similarity" ) - assert_almost_equal(average, expected_average, -2, err_msg) - assert_almost_equal(stdev, expected_stdev, -2, error_msg) + assert_allclose(average, expected_average, rtol=0, atol=1.5e2, err_msg=err_msg) + assert_allclose(stdev, expected_stdev, rtol=0, atol=1.5e2, err_msg=error_msg) def test_ces_error_estimation(self, ens1): expected_average = 0.03 @@ -410,10 +410,10 @@ def test_ces_error_estimation(self, ens1): average = averages[0,1] stdev = stdevs[0,1] - assert_almost_equal(average, expected_average, decimal=1, + assert_allclose(average, expected_average, rtol=0, atol=1.5e-1, err_msg="Unexpected average value for bootstrapped samples in Clustering Ensemble similarity") - assert_almost_equal(stdev, expected_stdev, decimal=0, - err_msg="Unexpected standard daviation for bootstrapped samples in Clustering Ensemble similarity") + assert_allclose(stdev, expected_stdev, rtol=0, atol=1.5, + err_msg="Unexpected standard deviation for bootstrapped samples in Clustering Ensemble similarity") def test_ces_error_estimation_ensemble_bootstrap(self, ens1): # Error estimation using a method that does not take a distance @@ -434,20 +434,22 @@ def test_ces_error_estimation_ensemble_bootstrap(self, ens1): err_msg = ( "Unexpected average value for bootstrapped samples in" " Clustering Ensemble similarity") - assert_almost_equal( + assert_allclose( average, expected_average, - 1, - err_msg) + rtol = 0, + atol = 1.5e-1, + err_msg=err_msg) error_msg = ( "Unexpected standard deviation for bootstrapped samples in" " Clustering Ensemble similarity" ) - assert_almost_equal( + assert_allclose( stdev, expected_stdev, - 1, - error_msg) + rtol=0, + atol=1.5e-1, + err_msg=error_msg) def test_dres_error_estimation(self, ens1): average_upper_bound = 0.3 @@ -820,7 +822,7 @@ def test_dimensionality_reduction_three_ensembles_two_identical(self, encore.reduce_dimensionality([ens1, ens2, ens1]) coordinates_ens1 = coordinates[:,np.where(details["ensemble_membership"]==1)] coordinates_ens3 = coordinates[:,np.where(details["ensemble_membership"]==3)] - assert_almost_equal(coordinates_ens1, coordinates_ens3, decimal=0, + assert_allclose(coordinates_ens1, coordinates_ens3, rtol=0, atol=1.5, err_msg="Unexpected result in dimensionality reduction: {0}".format(coordinates))