From 3821e259ff7d93f5471d15196b7c0bfd33349f21 Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Fri, 18 Oct 2024 00:59:17 +0200 Subject: [PATCH 01/19] Update align.py added backends and aggregators for AverageStructure --- package/MDAnalysis/analysis/align.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/package/MDAnalysis/analysis/align.py b/package/MDAnalysis/analysis/align.py index 2bc80042b69..2e44dd3bef9 100644 --- a/package/MDAnalysis/analysis/align.py +++ b/package/MDAnalysis/analysis/align.py @@ -216,7 +216,7 @@ from MDAnalysis.lib.log import ProgressBar from ..due import due, Doi -from .base import AnalysisBase +from .base import AnalysisBase, ResultsGroup logger = logging.getLogger('MDAnalysis.analysis.align') @@ -896,6 +896,12 @@ class AverageStructure(AnalysisBase): """ + _analysis_algorithm_is_parallelizable = True + + @classmethod + def get_supported_backends(cls): + return ("serial", "multiprocessing", "dask") + def __init__(self, mobile, reference=None, select='all', filename=None, weights=None, tol_mass=0.1, match_atoms=True, strict=False, force=True, in_memory=False, @@ -1089,6 +1095,15 @@ def _conclude(self): if not self._verbose: logging.disable(logging.NOTSET) + def _get_aggregator(self): + return ResultsGroup( + lookup={ + "universe": ResultsGroup.ndarray_vstack, + "positions": ResultsGroup.ndarray_vstack, + "rmsd": ResultsGroup.ndarray_vstack, + } + ) + @property def universe(self): wmsg = ("The `universe` attribute was deprecated in MDAnalysis 2.0.0 " @@ -1669,4 +1684,4 @@ def get_atoms_byres(g, match_mask=np.logical_not(mismatch_mask)): logger.error(errmsg) raise SelectionError(errmsg) - return ag1, ag2 \ No newline at end of file + return ag1, ag2 From 9dcc271f9ecda12858d977695748ff04ed31a0fc Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Fri, 18 Oct 2024 01:02:33 +0200 Subject: [PATCH 02/19] Update conftest.py added AverageStructure to conftest --- testsuite/MDAnalysisTests/analysis/conftest.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/testsuite/MDAnalysisTests/analysis/conftest.py b/testsuite/MDAnalysisTests/analysis/conftest.py index fc3c8a480c7..b044024856c 100644 --- a/testsuite/MDAnalysisTests/analysis/conftest.py +++ b/testsuite/MDAnalysisTests/analysis/conftest.py @@ -14,6 +14,7 @@ from MDAnalysis.analysis.hydrogenbonds.hbond_analysis import ( HydrogenBondAnalysis, ) +from MDAnalysis.analysis.align import AverageStructure from MDAnalysis.lib.util import is_installed @@ -141,3 +142,10 @@ def client_DSSP(request): @pytest.fixture(scope='module', params=params_for_cls(HydrogenBondAnalysis)) def client_HydrogenBondAnalysis(request): return request.param + + +# MDAnalysis.analysis.align + +@pytest.fixture(scope="module", params=params_for_cls(AverageStructure)) +def client_AverageStructure(request): + return request.param From a08889487c6318de2a244b4ee52d975013e10c1f Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Fri, 18 Oct 2024 01:52:19 +0200 Subject: [PATCH 03/19] Update test_align.py added client_AverageStructure --- .../MDAnalysisTests/analysis/test_align.py | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/testsuite/MDAnalysisTests/analysis/test_align.py b/testsuite/MDAnalysisTests/analysis/test_align.py index 63dfd25db80..025658a706a 100644 --- a/testsuite/MDAnalysisTests/analysis/test_align.py +++ b/testsuite/MDAnalysisTests/analysis/test_align.py @@ -427,9 +427,9 @@ def universe(self): def reference(self): return mda.Universe(PSF, CRD) - def test_average_structure_deprecated_attrs(self, universe, reference): + def test_average_structure_deprecated_attrs(self, universe, reference, client_AverageStructure): # Issue #3278 - remove in MDAnalysis 3.0.0 - avg = align.AverageStructure(universe, reference).run(stop=2) + avg = align.AverageStructure(universe, reference).run(stop=2, **client_AverageStructure) wmsg = "The `universe` attribute was deprecated in MDAnalysis 2.0.0" with pytest.warns(DeprecationWarning, match=wmsg): @@ -444,36 +444,36 @@ def test_average_structure_deprecated_attrs(self, universe, reference): with pytest.warns(DeprecationWarning, match=wmsg): assert avg.rmsd == avg.results.rmsd - def test_average_structure(self, universe, reference): + def test_average_structure(self, universe, reference, client_AverageStructure): ref, rmsd = _get_aligned_average_positions(self.ref_files, reference) - avg = align.AverageStructure(universe, reference).run() + avg = align.AverageStructure(universe, reference).run(**client_AverageStructure) 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): + def test_average_structure_mass_weighted(self, universe, reference, client_AverageStructure): ref, rmsd = _get_aligned_average_positions(self.ref_files, reference, weights='mass') - avg = align.AverageStructure(universe, reference, weights='mass').run() + avg = align.AverageStructure(universe, reference, weights='mass').run(**client_AverageStructure) 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): + def test_average_structure_select(self, universe, reference, client_AverageStructure): 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() + avg = align.AverageStructure(universe, reference, select=select).run(**client_AverageStructure) 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): + def test_average_structure_no_ref(self, universe, client_AverageStructure): ref, rmsd = _get_aligned_average_positions(self.ref_files, universe) - avg = align.AverageStructure(universe).run() + avg = align.AverageStructure(universe).run(**client_AverageStructure) 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() + def test_average_structure_no_msf(self, universe, client_AverageStructure): + avg = align.AverageStructure(universe).run(**client_AverageStructure) assert not hasattr(avg, 'msf') def test_mismatch_atoms(self, universe): @@ -481,7 +481,7 @@ def test_mismatch_atoms(self, universe): with pytest.raises(SelectionError): align.AverageStructure(universe, u) - def test_average_structure_ref_frame(self, universe): + def test_average_structure_ref_frame(self, universe, client_AverageStructure): ref_frame = 3 u = mda.Merge(universe.atoms) @@ -492,13 +492,13 @@ def test_average_structure_ref_frame(self, universe): # back to start universe.trajectory[0] ref, rmsd = _get_aligned_average_positions(self.ref_files, u) - avg = align.AverageStructure(universe, ref_frame=ref_frame).run() + avg = align.AverageStructure(universe, ref_frame=ref_frame).run(**client_AverageStructure) 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() + def test_average_structure_in_memory(self, universe, client_AverageStructure): + avg = align.AverageStructure(universe, in_memory=True).run(**client_AverageStructure) reference_coordinates = universe.trajectory.timeseries().mean(axis=1) assert_allclose(avg.results.universe.atoms.positions, reference_coordinates, rtol=0, atol=1.5e-4) From eea17846acabdc9324a8c6133a53ed3d25baaee9 Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Fri, 18 Oct 2024 19:48:19 +0200 Subject: [PATCH 04/19] Update align.py added for AlignTraj backends and aggregator --- package/MDAnalysis/analysis/align.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/package/MDAnalysis/analysis/align.py b/package/MDAnalysis/analysis/align.py index 2e44dd3bef9..25dca86974d 100644 --- a/package/MDAnalysis/analysis/align.py +++ b/package/MDAnalysis/analysis/align.py @@ -678,6 +678,12 @@ class AlignTraj(AnalysisBase): """ + _analysis_algorithm_is_parallelizable = True + + @classmethod + def get_supported_backends(cls): + return ("serial", "dask") + def __init__(self, mobile, reference, select='all', filename=None, prefix='rmsfit_', weights=None, tol_mass=0.1, match_atoms=True, strict=False, force=True, in_memory=False, @@ -866,6 +872,12 @@ def rmsd(self): warnings.warn(wmsg, DeprecationWarning) return self.results.rmsd + def _get_aggregator(self): + return ResultsGroup( + lookup={ + "rmsd": ResultsGroup.ndarray_hstack, + } + ) class AverageStructure(AnalysisBase): """RMS-align trajectory to a reference structure using a selection, From f1206b0002a74595703c030e2b7220b6dec6b9e3 Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Fri, 18 Oct 2024 19:50:52 +0200 Subject: [PATCH 05/19] Update conftest.py added client_AlignTraj --- testsuite/MDAnalysisTests/analysis/conftest.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/testsuite/MDAnalysisTests/analysis/conftest.py b/testsuite/MDAnalysisTests/analysis/conftest.py index b044024856c..dc6297abca8 100644 --- a/testsuite/MDAnalysisTests/analysis/conftest.py +++ b/testsuite/MDAnalysisTests/analysis/conftest.py @@ -14,7 +14,7 @@ from MDAnalysis.analysis.hydrogenbonds.hbond_analysis import ( HydrogenBondAnalysis, ) -from MDAnalysis.analysis.align import AverageStructure +from MDAnalysis.analysis.align import AverageStructure, AlignTraj from MDAnalysis.lib.util import is_installed @@ -149,3 +149,8 @@ def client_HydrogenBondAnalysis(request): @pytest.fixture(scope="module", params=params_for_cls(AverageStructure)) def client_AverageStructure(request): return request.param + + +@pytest.fixture(scope="module", params=params_for_cls(AlignTraj)) +def client_AlignTraj(request): + return request.param From 6c91e91c4a8c28b7c8926a6df262f73cef0cba15 Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:09:13 +0200 Subject: [PATCH 06/19] Update test_align.py added client_AlignTraj to tests --- .../MDAnalysisTests/analysis/test_align.py | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/testsuite/MDAnalysisTests/analysis/test_align.py b/testsuite/MDAnalysisTests/analysis/test_align.py index 025658a706a..6791aeb8f02 100644 --- a/testsuite/MDAnalysisTests/analysis/test_align.py +++ b/testsuite/MDAnalysisTests/analysis/test_align.py @@ -244,10 +244,10 @@ def test_AlignTraj_outfile_default(self, universe, reference, tmpdir): finally: x._writer.close() - def test_AlignTraj_outfile_default_exists(self, universe, reference, tmpdir): + def test_AlignTraj_outfile_default_exists(self, universe, reference, tmpdir, client_AlignTraj): reference.trajectory[-1] outfile = str(tmpdir.join('align_test.dcd')) - align.AlignTraj(universe, reference, filename=outfile).run() + align.AlignTraj(universe, reference, filename=outfile).run(**client_AlignTraj) fitted = mda.Universe(PSF, outfile) # ensure default file exists @@ -262,25 +262,25 @@ def test_AlignTraj_outfile_default_exists(self, universe, reference, tmpdir): with pytest.raises(IOError): align.AlignTraj(fitted, reference, force=False) - def test_AlignTraj_step_works(self, universe, reference, tmpdir): + def test_AlignTraj_step_works(self, universe, reference, tmpdir, client_AlignTraj): reference.trajectory[-1] outfile = str(tmpdir.join('align_test.dcd')) # this shouldn't throw an exception - align.AlignTraj(universe, reference, filename=outfile).run(step=10) + align.AlignTraj(universe, reference, filename=outfile).run(step=10, **client_AlignTraj) - def test_AlignTraj_deprecated_attribute(self, universe, reference, tmpdir): + def test_AlignTraj_deprecated_attribute(self, universe, reference, tmpdir, client_AlignTraj): reference.trajectory[-1] outfile = str(tmpdir.join('align_test.dcd')) - x = align.AlignTraj(universe, reference, filename=outfile).run(stop=2) + x = align.AlignTraj(universe, reference, filename=outfile).run(stop=2, **client_AlignTraj) wmsg = "The `rmsd` attribute was deprecated in MDAnalysis 2.0.0" with pytest.warns(DeprecationWarning, match=wmsg): assert_equal(x.rmsd, x.results.rmsd) - def test_AlignTraj(self, universe, reference, tmpdir): + def test_AlignTraj(self, universe, reference, tmpdir, client_AlignTraj): reference.trajectory[-1] outfile = str(tmpdir.join('align_test.dcd')) - x = align.AlignTraj(universe, reference, filename=outfile).run() + x = align.AlignTraj(universe, reference, filename=outfile).run(**client_AlignTraj) fitted = mda.Universe(PSF, outfile) assert_allclose(x.results.rmsd[0], 6.9290, rtol=0, atol=1.5e-3) @@ -292,10 +292,10 @@ def test_AlignTraj(self, universe, reference, tmpdir): self._assert_rmsd(reference, fitted, 0, 6.929083044751061) self._assert_rmsd(reference, fitted, -1, 0.0) - def test_AlignTraj_weighted(self, universe, reference, tmpdir): + def test_AlignTraj_weighted(self, universe, reference, tmpdir, client_AlignTraj): outfile = str(tmpdir.join('align_test.dcd')) x = align.AlignTraj(universe, reference, - filename=outfile, weights='mass').run() + filename=outfile, weights='mass').run(**client_AlignTraj) fitted = mda.Universe(PSF, outfile) 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) @@ -305,7 +305,7 @@ def test_AlignTraj_weighted(self, universe, reference, tmpdir): self._assert_rmsd(reference, fitted, -1, 6.929083032629219, weights=universe.atoms.masses) - def test_AlignTraj_custom_weights(self, universe, reference, tmpdir): + def test_AlignTraj_custom_weights(self, universe, reference, tmpdir, client_AlignTraj): weights = np.zeros(universe.atoms.n_atoms) ca = universe.select_atoms('name CA') weights[ca.indices] = 1 @@ -313,17 +313,17 @@ def test_AlignTraj_custom_weights(self, universe, reference, tmpdir): outfile = str(tmpdir.join('align_test.dcd')) x = align.AlignTraj(universe, reference, - filename=outfile, select='name CA').run() + filename=outfile, select='name CA').run(**client_AlignTraj) x_weights = align.AlignTraj(universe, reference, - filename=outfile, weights=weights).run() + filename=outfile, weights=weights).run(**client_AlignTraj) 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): + def test_AlignTraj_custom_mass_weights(self, universe, reference, tmpdir, client_AlignTraj): outfile = str(tmpdir.join('align_test.dcd')) x = align.AlignTraj(universe, reference, filename=outfile, - weights=reference.atoms.masses).run() + weights=reference.atoms.masses).run(**client_AlignTraj) fitted = mda.Universe(PSF, outfile) 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) @@ -333,18 +333,18 @@ def test_AlignTraj_custom_mass_weights(self, universe, reference, tmpdir): self._assert_rmsd(reference, fitted, -1, 6.929083032629219, weights=universe.atoms.masses) - def test_AlignTraj_partial_fit(self, universe, reference, tmpdir): + def test_AlignTraj_partial_fit(self, universe, reference, tmpdir, client_AlignTraj): outfile = str(tmpdir.join('align_test.dcd')) # fitting on a partial selection should still write the whole topology align.AlignTraj(universe, reference, select='resid 1-20', - filename=outfile, weights='mass').run() + filename=outfile, weights='mass').run(**client_AlignTraj) mda.Universe(PSF, outfile) - def test_AlignTraj_in_memory(self, universe, reference, tmpdir): + def test_AlignTraj_in_memory(self, universe, reference, tmpdir, client_AlignTraj): outfile = str(tmpdir.join('align_test.dcd')) reference.trajectory[-1] x = align.AlignTraj(universe, reference, filename=outfile, - in_memory=True).run() + in_memory=True).run(**client_AlignTraj) assert x.filename is None 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) @@ -353,7 +353,7 @@ def test_AlignTraj_in_memory(self, universe, reference, tmpdir): self._assert_rmsd(reference, universe, 0, 6.929083044751061) self._assert_rmsd(reference, universe, -1, 0.0) - def test_AlignTraj_writer_kwargs(self, universe, reference, tmpdir): + def test_AlignTraj_writer_kwargs(self, universe, reference, tmpdir, client_AlignTraj): # Issue 4564 writer_kwargs = dict(precision=2) with tmpdir.as_cwd(): @@ -361,7 +361,7 @@ def test_AlignTraj_writer_kwargs(self, universe, reference, tmpdir): select='protein and name CA', filename='aligned_traj.xtc', writer_kwargs=writer_kwargs, - in_memory=False).run() + in_memory=False).run(**client_AlignTraj) assert_equal(aligner._writer.precision, 2) def _assert_rmsd(self, reference, fitted, frame, desired, weights=None): @@ -407,9 +407,9 @@ def test_alignto_partial_universe(self, universe, reference): rtol=0, atol=1.5e-3) -def _get_aligned_average_positions(ref_files, ref, select="all", **kwargs): +def _get_aligned_average_positions(ref_files, ref, select="all", **kwargs, client_AlignTraj): u = mda.Universe(*ref_files, in_memory=True) - prealigner = align.AlignTraj(u, ref, select=select, **kwargs).run() + prealigner = align.AlignTraj(u, ref, select=select, **kwargs).run(**client_AlignTraj) ag = u.select_atoms(select) reference_coordinates = u.trajectory.timeseries(asel=ag).mean(axis=1) rmsd = sum(prealigner.results.rmsd/len(u.trajectory)) From 17334142551ba3aa2d57aab8b5674dc3d143e7a2 Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:29:23 +0200 Subject: [PATCH 07/19] Update align.py PEP fixes --- package/MDAnalysis/analysis/align.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/package/MDAnalysis/analysis/align.py b/package/MDAnalysis/analysis/align.py index 25dca86974d..ff629661631 100644 --- a/package/MDAnalysis/analysis/align.py +++ b/package/MDAnalysis/analysis/align.py @@ -679,11 +679,11 @@ class AlignTraj(AnalysisBase): """ _analysis_algorithm_is_parallelizable = True - + @classmethod def get_supported_backends(cls): return ("serial", "dask") - + def __init__(self, mobile, reference, select='all', filename=None, prefix='rmsfit_', weights=None, tol_mass=0.1, match_atoms=True, strict=False, force=True, in_memory=False, @@ -909,11 +909,11 @@ class AverageStructure(AnalysisBase): """ _analysis_algorithm_is_parallelizable = True - + @classmethod def get_supported_backends(cls): return ("serial", "multiprocessing", "dask") - + def __init__(self, mobile, reference=None, select='all', filename=None, weights=None, tol_mass=0.1, match_atoms=True, strict=False, force=True, in_memory=False, @@ -1115,7 +1115,7 @@ def _get_aggregator(self): "rmsd": ResultsGroup.ndarray_vstack, } ) - + @property def universe(self): wmsg = ("The `universe` attribute was deprecated in MDAnalysis 2.0.0 " From d982d699b7028f1a7ed4d63dbbbed302d0342db2 Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:33:17 +0200 Subject: [PATCH 08/19] Update test_align.py fixing PEP --- testsuite/MDAnalysisTests/analysis/test_align.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/testsuite/MDAnalysisTests/analysis/test_align.py b/testsuite/MDAnalysisTests/analysis/test_align.py index 6791aeb8f02..2c004bdfa61 100644 --- a/testsuite/MDAnalysisTests/analysis/test_align.py +++ b/testsuite/MDAnalysisTests/analysis/test_align.py @@ -244,10 +244,14 @@ def test_AlignTraj_outfile_default(self, universe, reference, tmpdir): finally: x._writer.close() - def test_AlignTraj_outfile_default_exists(self, universe, reference, tmpdir, client_AlignTraj): + def test_AlignTraj_outfile_default_exists( + self, universe, reference, tmpdir, client_AlignTraj + ): reference.trajectory[-1] outfile = str(tmpdir.join('align_test.dcd')) - align.AlignTraj(universe, reference, filename=outfile).run(**client_AlignTraj) + align.AlignTraj(universe, reference, filename=outfile).run( + **client_AlignTraj + ) fitted = mda.Universe(PSF, outfile) # ensure default file exists @@ -262,11 +266,15 @@ def test_AlignTraj_outfile_default_exists(self, universe, reference, tmpdir, cli with pytest.raises(IOError): align.AlignTraj(fitted, reference, force=False) - def test_AlignTraj_step_works(self, universe, reference, tmpdir, client_AlignTraj): + def test_AlignTraj_step_works( + self, universe, reference, tmpdir, client_AlignTraj + ): reference.trajectory[-1] outfile = str(tmpdir.join('align_test.dcd')) # this shouldn't throw an exception - align.AlignTraj(universe, reference, filename=outfile).run(step=10, **client_AlignTraj) + align.AlignTraj(universe, reference, filename=outfile).run( + step=10, **client_AlignTraj + ) def test_AlignTraj_deprecated_attribute(self, universe, reference, tmpdir, client_AlignTraj): reference.trajectory[-1] From fd6b4a424ea384337244d62b2284e696417f3a59 Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:36:36 +0200 Subject: [PATCH 09/19] Update test_align.py fixing pep --- .../MDAnalysisTests/analysis/test_align.py | 40 ++++++++++++++----- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/testsuite/MDAnalysisTests/analysis/test_align.py b/testsuite/MDAnalysisTests/analysis/test_align.py index 2c004bdfa61..ebea035be0f 100644 --- a/testsuite/MDAnalysisTests/analysis/test_align.py +++ b/testsuite/MDAnalysisTests/analysis/test_align.py @@ -276,19 +276,27 @@ def test_AlignTraj_step_works( step=10, **client_AlignTraj ) - def test_AlignTraj_deprecated_attribute(self, universe, reference, tmpdir, client_AlignTraj): + def test_AlignTraj_deprecated_attribute( + self, universe, reference, tmpdir, client_AlignTraj + ): reference.trajectory[-1] outfile = str(tmpdir.join('align_test.dcd')) - x = align.AlignTraj(universe, reference, filename=outfile).run(stop=2, **client_AlignTraj) + x = align.AlignTraj(universe, reference, filename=outfile).run( + stop=2, **client_AlignTraj + ) wmsg = "The `rmsd` attribute was deprecated in MDAnalysis 2.0.0" with pytest.warns(DeprecationWarning, match=wmsg): assert_equal(x.rmsd, x.results.rmsd) - def test_AlignTraj(self, universe, reference, tmpdir, client_AlignTraj): + def test_AlignTraj( + self, universe, reference, tmpdir, client_AlignTraj + ): reference.trajectory[-1] outfile = str(tmpdir.join('align_test.dcd')) - x = align.AlignTraj(universe, reference, filename=outfile).run(**client_AlignTraj) + x = align.AlignTraj(universe, reference, filename=outfile).run( + **client_AlignTraj + ) fitted = mda.Universe(PSF, outfile) assert_allclose(x.results.rmsd[0], 6.9290, rtol=0, atol=1.5e-3) @@ -300,10 +308,14 @@ def test_AlignTraj(self, universe, reference, tmpdir, client_AlignTraj): self._assert_rmsd(reference, fitted, 0, 6.929083044751061) self._assert_rmsd(reference, fitted, -1, 0.0) - def test_AlignTraj_weighted(self, universe, reference, tmpdir, client_AlignTraj): + def test_AlignTraj_weighted( + self, universe, reference, tmpdir, client_AlignTraj + ): outfile = str(tmpdir.join('align_test.dcd')) x = align.AlignTraj(universe, reference, - filename=outfile, weights='mass').run(**client_AlignTraj) + filename=outfile, weights='mass').run( + **client_AlignTraj + ) fitted = mda.Universe(PSF, outfile) 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) @@ -313,7 +325,9 @@ def test_AlignTraj_weighted(self, universe, reference, tmpdir, client_AlignTraj) self._assert_rmsd(reference, fitted, -1, 6.929083032629219, weights=universe.atoms.masses) - def test_AlignTraj_custom_weights(self, universe, reference, tmpdir, client_AlignTraj): + def test_AlignTraj_custom_weights( + self, universe, reference, tmpdir, client_AlignTraj + ): weights = np.zeros(universe.atoms.n_atoms) ca = universe.select_atoms('name CA') weights[ca.indices] = 1 @@ -321,13 +335,19 @@ def test_AlignTraj_custom_weights(self, universe, reference, tmpdir, client_Alig outfile = str(tmpdir.join('align_test.dcd')) x = align.AlignTraj(universe, reference, - filename=outfile, select='name CA').run(**client_AlignTraj) + filename=outfile, select='name CA').run( + **client_AlignTraj + ) x_weights = align.AlignTraj(universe, reference, - filename=outfile, weights=weights).run(**client_AlignTraj) + filename=outfile, weights=weights).run( + **client_AlignTraj + ) 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, client_AlignTraj): + def test_AlignTraj_custom_mass_weights( + self, universe, reference, tmpdir, client_AlignTraj + ): outfile = str(tmpdir.join('align_test.dcd')) x = align.AlignTraj(universe, reference, filename=outfile, From 0022b715c7d3eae5f11df45514f32f83bd5153bb Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:38:57 +0200 Subject: [PATCH 10/19] Update test_align.py fixing pep --- .../MDAnalysisTests/analysis/test_align.py | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/testsuite/MDAnalysisTests/analysis/test_align.py b/testsuite/MDAnalysisTests/analysis/test_align.py index ebea035be0f..c4f8d3b3716 100644 --- a/testsuite/MDAnalysisTests/analysis/test_align.py +++ b/testsuite/MDAnalysisTests/analysis/test_align.py @@ -351,7 +351,9 @@ def test_AlignTraj_custom_mass_weights( outfile = str(tmpdir.join('align_test.dcd')) x = align.AlignTraj(universe, reference, filename=outfile, - weights=reference.atoms.masses).run(**client_AlignTraj) + weights=reference.atoms.masses).run( + **client_AlignTraj + ) fitted = mda.Universe(PSF, outfile) 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) @@ -361,14 +363,20 @@ def test_AlignTraj_custom_mass_weights( self._assert_rmsd(reference, fitted, -1, 6.929083032629219, weights=universe.atoms.masses) - def test_AlignTraj_partial_fit(self, universe, reference, tmpdir, client_AlignTraj): + def test_AlignTraj_partial_fit( + self, universe, reference, tmpdir, client_AlignTraj + ): outfile = str(tmpdir.join('align_test.dcd')) # fitting on a partial selection should still write the whole topology align.AlignTraj(universe, reference, select='resid 1-20', - filename=outfile, weights='mass').run(**client_AlignTraj) + filename=outfile, weights='mass').run( + **client_AlignTraj + ) mda.Universe(PSF, outfile) - def test_AlignTraj_in_memory(self, universe, reference, tmpdir, client_AlignTraj): + def test_AlignTraj_in_memory( + self, universe, reference, tmpdir, client_AlignTraj + ): outfile = str(tmpdir.join('align_test.dcd')) reference.trajectory[-1] x = align.AlignTraj(universe, reference, filename=outfile, @@ -381,7 +389,9 @@ def test_AlignTraj_in_memory(self, universe, reference, tmpdir, client_AlignTraj self._assert_rmsd(reference, universe, 0, 6.929083044751061) self._assert_rmsd(reference, universe, -1, 0.0) - def test_AlignTraj_writer_kwargs(self, universe, reference, tmpdir, client_AlignTraj): + def test_AlignTraj_writer_kwargs( + self, universe, reference, tmpdir, client_AlignTraj + ): # Issue 4564 writer_kwargs = dict(precision=2) with tmpdir.as_cwd(): From 2457c7407770547f38433d183baa83e166ce4a75 Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Fri, 18 Oct 2024 20:41:08 +0200 Subject: [PATCH 11/19] Update test_align.py fixing pep --- testsuite/MDAnalysisTests/analysis/test_align.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/testsuite/MDAnalysisTests/analysis/test_align.py b/testsuite/MDAnalysisTests/analysis/test_align.py index c4f8d3b3716..97abcc6a297 100644 --- a/testsuite/MDAnalysisTests/analysis/test_align.py +++ b/testsuite/MDAnalysisTests/analysis/test_align.py @@ -445,9 +445,12 @@ def test_alignto_partial_universe(self, universe, reference): rtol=0, atol=1.5e-3) -def _get_aligned_average_positions(ref_files, ref, select="all", **kwargs, client_AlignTraj): +def _get_aligned_average_positions( + ref_files, ref, select="all", **kwargs, client_AlignTraj +): u = mda.Universe(*ref_files, in_memory=True) - prealigner = align.AlignTraj(u, ref, select=select, **kwargs).run(**client_AlignTraj) + prealigner = align.AlignTraj(u, ref, select=select, + **kwargs).run(**client_AlignTraj) ag = u.select_atoms(select) reference_coordinates = u.trajectory.timeseries(asel=ag).mean(axis=1) rmsd = sum(prealigner.results.rmsd/len(u.trajectory)) @@ -465,9 +468,13 @@ def universe(self): def reference(self): return mda.Universe(PSF, CRD) - def test_average_structure_deprecated_attrs(self, universe, reference, client_AverageStructure): + def test_average_structure_deprecated_attrs( + self, universe, reference, client_AverageStructure + ): # Issue #3278 - remove in MDAnalysis 3.0.0 - avg = align.AverageStructure(universe, reference).run(stop=2, **client_AverageStructure) + avg = align.AverageStructure(universe, reference).run(stop=2, + **client_AverageStructure + ) wmsg = "The `universe` attribute was deprecated in MDAnalysis 2.0.0" with pytest.warns(DeprecationWarning, match=wmsg): From 0796f4800ce712c54d68fdf6147f6cceea67b4bb Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Fri, 18 Oct 2024 21:02:23 +0200 Subject: [PATCH 12/19] Update test_align.py fixing pep --- .../MDAnalysisTests/analysis/test_align.py | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/testsuite/MDAnalysisTests/analysis/test_align.py b/testsuite/MDAnalysisTests/analysis/test_align.py index 97abcc6a297..c9f6c24daec 100644 --- a/testsuite/MDAnalysisTests/analysis/test_align.py +++ b/testsuite/MDAnalysisTests/analysis/test_align.py @@ -472,9 +472,9 @@ def test_average_structure_deprecated_attrs( self, universe, reference, client_AverageStructure ): # Issue #3278 - remove in MDAnalysis 3.0.0 - avg = align.AverageStructure(universe, reference).run(stop=2, - **client_AverageStructure - ) + avg = align.AverageStructure(universe, reference).run( + stop=2,**client_AverageStructure + ) wmsg = "The `universe` attribute was deprecated in MDAnalysis 2.0.0" with pytest.warns(DeprecationWarning, match=wmsg): @@ -489,23 +489,32 @@ def test_average_structure_deprecated_attrs( with pytest.warns(DeprecationWarning, match=wmsg): assert avg.rmsd == avg.results.rmsd - def test_average_structure(self, universe, reference, client_AverageStructure): + def test_average_structure( + self, universe, reference, client_AverageStructure + ): ref, rmsd = _get_aligned_average_positions(self.ref_files, reference) - avg = align.AverageStructure(universe, reference).run(**client_AverageStructure) + avg = align.AverageStructure(universe, reference).run( + **client_AverageStructure + ) 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, client_AverageStructure): + def test_average_structure_mass_weighted( + self, universe, reference, client_AverageStructure + ): ref, rmsd = _get_aligned_average_positions(self.ref_files, reference, weights='mass') avg = align.AverageStructure(universe, reference, weights='mass').run(**client_AverageStructure) 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, client_AverageStructure): + def test_average_structure_select( + self, universe, reference, client_AverageStructure + ): 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(**client_AverageStructure) + avg = align.AverageStructure(universe, reference, + select=select).run(**client_AverageStructure) 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) @@ -537,13 +546,17 @@ def test_average_structure_ref_frame(self, universe, client_AverageStructure): # back to start universe.trajectory[0] ref, rmsd = _get_aligned_average_positions(self.ref_files, u) - avg = align.AverageStructure(universe, ref_frame=ref_frame).run(**client_AverageStructure) + avg = align.AverageStructure(universe, + ref_frame=ref_frame).run(**client_AverageStructure) 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, client_AverageStructure): - avg = align.AverageStructure(universe, in_memory=True).run(**client_AverageStructure) + def test_average_structure_in_memory( + self, universe, client_AverageStructure + ): + avg = align.AverageStructure(universe, + in_memory=True).run(**client_AverageStructure) reference_coordinates = universe.trajectory.timeseries().mean(axis=1) assert_allclose(avg.results.universe.atoms.positions, reference_coordinates, rtol=0, atol=1.5e-4) From 7023aa1b11c39a53af4e8a781c1b20a9156f4201 Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Fri, 18 Oct 2024 21:04:59 +0200 Subject: [PATCH 13/19] Update test_align.py fixing pep --- .../MDAnalysisTests/analysis/test_align.py | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/testsuite/MDAnalysisTests/analysis/test_align.py b/testsuite/MDAnalysisTests/analysis/test_align.py index c9f6c24daec..eda10f20e52 100644 --- a/testsuite/MDAnalysisTests/analysis/test_align.py +++ b/testsuite/MDAnalysisTests/analysis/test_align.py @@ -473,7 +473,7 @@ def test_average_structure_deprecated_attrs( ): # Issue #3278 - remove in MDAnalysis 3.0.0 avg = align.AverageStructure(universe, reference).run( - stop=2,**client_AverageStructure + stop=2, **client_AverageStructure ) wmsg = "The `universe` attribute was deprecated in MDAnalysis 2.0.0" @@ -503,7 +503,9 @@ def test_average_structure_mass_weighted( self, universe, reference, client_AverageStructure ): ref, rmsd = _get_aligned_average_positions(self.ref_files, reference, weights='mass') - avg = align.AverageStructure(universe, reference, weights='mass').run(**client_AverageStructure) + avg = align.AverageStructure(universe, reference, weights='mass').run( + **client_AverageStructure + ) 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) @@ -513,8 +515,9 @@ def test_average_structure_select( ): 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(**client_AverageStructure) + avg = align.AverageStructure(universe, reference, select=select).run( + **client_AverageStructure + ) 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) @@ -535,7 +538,9 @@ def test_mismatch_atoms(self, universe): with pytest.raises(SelectionError): align.AverageStructure(universe, u) - def test_average_structure_ref_frame(self, universe, client_AverageStructure): + def test_average_structure_ref_frame( + self, universe, client_AverageStructure + ): ref_frame = 3 u = mda.Merge(universe.atoms) @@ -546,8 +551,9 @@ def test_average_structure_ref_frame(self, universe, client_AverageStructure): # back to start universe.trajectory[0] ref, rmsd = _get_aligned_average_positions(self.ref_files, u) - avg = align.AverageStructure(universe, - ref_frame=ref_frame).run(**client_AverageStructure) + avg = align.AverageStructure(universe, ref_frame=ref_frame).run( + **client_AverageStructure + ) 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) @@ -555,8 +561,9 @@ def test_average_structure_ref_frame(self, universe, client_AverageStructure): def test_average_structure_in_memory( self, universe, client_AverageStructure ): - avg = align.AverageStructure(universe, - in_memory=True).run(**client_AverageStructure) + avg = align.AverageStructure(universe, in_memory=True).run( + **client_AverageStructure + ) reference_coordinates = universe.trajectory.timeseries().mean(axis=1) assert_allclose(avg.results.universe.atoms.positions, reference_coordinates, rtol=0, atol=1.5e-4) From 6d05d72201f0de649e6e49640135331ccac7ebed Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Fri, 18 Oct 2024 21:49:37 +0200 Subject: [PATCH 14/19] Update test_align.py adjusted position of inputs --- testsuite/MDAnalysisTests/analysis/test_align.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testsuite/MDAnalysisTests/analysis/test_align.py b/testsuite/MDAnalysisTests/analysis/test_align.py index eda10f20e52..80d6f2a1e8b 100644 --- a/testsuite/MDAnalysisTests/analysis/test_align.py +++ b/testsuite/MDAnalysisTests/analysis/test_align.py @@ -446,7 +446,7 @@ def test_alignto_partial_universe(self, universe, reference): def _get_aligned_average_positions( - ref_files, ref, select="all", **kwargs, client_AlignTraj + ref_files, ref, client_AlignTraj, select="all", **kwargs ): u = mda.Universe(*ref_files, in_memory=True) prealigner = align.AlignTraj(u, ref, select=select, From 40316f72aaa42880258f2e827bf2a947e50d935c Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Fri, 18 Oct 2024 21:56:18 +0200 Subject: [PATCH 15/19] Update test_align.py fix positional arguments --- testsuite/MDAnalysisTests/analysis/test_align.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/testsuite/MDAnalysisTests/analysis/test_align.py b/testsuite/MDAnalysisTests/analysis/test_align.py index 80d6f2a1e8b..4b77da41ab1 100644 --- a/testsuite/MDAnalysisTests/analysis/test_align.py +++ b/testsuite/MDAnalysisTests/analysis/test_align.py @@ -445,12 +445,9 @@ def test_alignto_partial_universe(self, universe, reference): rtol=0, atol=1.5e-3) -def _get_aligned_average_positions( - ref_files, ref, client_AlignTraj, select="all", **kwargs -): +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(**client_AlignTraj) + prealigner = align.AlignTraj(u, ref, select=select, **kwargs).run() ag = u.select_atoms(select) reference_coordinates = u.trajectory.timeseries(asel=ag).mean(axis=1) rmsd = sum(prealigner.results.rmsd/len(u.trajectory)) From b745ad09f1709ec25684c83882c0186d59ef126b Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Fri, 18 Oct 2024 22:40:35 +0200 Subject: [PATCH 16/19] Update align.py added multiprocessing to test --- package/MDAnalysis/analysis/align.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/MDAnalysis/analysis/align.py b/package/MDAnalysis/analysis/align.py index ff629661631..308b37e4288 100644 --- a/package/MDAnalysis/analysis/align.py +++ b/package/MDAnalysis/analysis/align.py @@ -682,7 +682,7 @@ class AlignTraj(AnalysisBase): @classmethod def get_supported_backends(cls): - return ("serial", "dask") + return ("serial", "multiprocessing", "dask") def __init__(self, mobile, reference, select='all', filename=None, prefix='rmsfit_', weights=None, From bcbffeafc01be27e3fca1b4eb368affc963bb943 Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Fri, 18 Oct 2024 23:03:35 +0200 Subject: [PATCH 17/19] Update align.py removed multiprocessing from AlignTraj --- package/MDAnalysis/analysis/align.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/MDAnalysis/analysis/align.py b/package/MDAnalysis/analysis/align.py index 308b37e4288..ff629661631 100644 --- a/package/MDAnalysis/analysis/align.py +++ b/package/MDAnalysis/analysis/align.py @@ -682,7 +682,7 @@ class AlignTraj(AnalysisBase): @classmethod def get_supported_backends(cls): - return ("serial", "multiprocessing", "dask") + return ("serial", "dask") def __init__(self, mobile, reference, select='all', filename=None, prefix='rmsfit_', weights=None, From fdb4f35a95b0631de536b9177953c7cf23484463 Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Sat, 11 Jan 2025 20:54:48 +0100 Subject: [PATCH 18/19] Update test_align.py test again --- .../MDAnalysisTests/analysis/test_align.py | 164 ++---------------- 1 file changed, 17 insertions(+), 147 deletions(-) diff --git a/testsuite/MDAnalysisTests/analysis/test_align.py b/testsuite/MDAnalysisTests/analysis/test_align.py index 2fcc8df3a53..ad78f7bcf77 100644 --- a/testsuite/MDAnalysisTests/analysis/test_align.py +++ b/testsuite/MDAnalysisTests/analysis/test_align.py @@ -303,18 +303,11 @@ def test_AlignTraj_outfile_default(self, universe, reference, tmpdir): x._writer.close() def test_AlignTraj_outfile_default_exists( - self, universe, reference, tmpdir, client_AlignTraj - ): - reference.trajectory[-1] - outfile = str(tmpdir.join('align_test.dcd')) - align.AlignTraj(universe, reference, filename=outfile).run( - **client_AlignTraj - ) - self, universe, reference, tmpdir + self, universe, reference, tmpdir, client_AlignTraj ): reference.trajectory[-1] outfile = str(tmpdir.join("align_test.dcd")) - align.AlignTraj(universe, reference, filename=outfile).run() + align.AlignTraj(universe, reference, filename=outfile).run(**client_AlignTraj) fitted = mda.Universe(PSF, outfile) # ensure default file exists @@ -331,24 +324,14 @@ def test_AlignTraj_outfile_default_exists( with pytest.raises(IOError): align.AlignTraj(fitted, reference, force=False) - def test_AlignTraj_step_works( - self, universe, reference, tmpdir, client_AlignTraj - ): + def test_AlignTraj_step_works(self, universe, reference, tmpdir, client_AlignTraj): reference.trajectory[-1] outfile = str(tmpdir.join("align_test.dcd")) # this shouldn't throw an exception - align.AlignTraj(universe, reference, filename=outfile).run( - step=10, **client_AlignTraj - ) + align.AlignTraj(universe, reference, filename=outfile).run(step=10) - def test_AlignTraj_deprecated_attribute( - self, universe, reference, tmpdir, client_AlignTraj - ): + def test_AlignTraj_deprecated_attribute(self, universe, reference, tmpdir, client_AlignTraj): reference.trajectory[-1] - outfile = str(tmpdir.join('align_test.dcd')) - x = align.AlignTraj(universe, reference, filename=outfile).run( - stop=2, **client_AlignTraj - ) outfile = str(tmpdir.join("align_test.dcd")) x = align.AlignTraj(universe, reference, filename=outfile).run(stop=2) @@ -356,14 +339,8 @@ def test_AlignTraj_deprecated_attribute( with pytest.warns(DeprecationWarning, match=wmsg): assert_equal(x.rmsd, x.results.rmsd) - def test_AlignTraj( - self, universe, reference, tmpdir, client_AlignTraj - ): + def test_AlignTraj(self, universe, reference, tmpdir, client_AlignTraj): reference.trajectory[-1] - outfile = str(tmpdir.join('align_test.dcd')) - x = align.AlignTraj(universe, reference, filename=outfile).run( - **client_AlignTraj - ) outfile = str(tmpdir.join("align_test.dcd")) x = align.AlignTraj(universe, reference, filename=outfile).run() fitted = mda.Universe(PSF, outfile) @@ -377,15 +354,7 @@ def test_AlignTraj( self._assert_rmsd(reference, fitted, 0, 6.929083044751061) self._assert_rmsd(reference, fitted, -1, 0.0) - def test_AlignTraj_weighted( - self, universe, reference, tmpdir, client_AlignTraj - ): - outfile = str(tmpdir.join('align_test.dcd')) - x = align.AlignTraj(universe, reference, - filename=outfile, weights='mass').run( - **client_AlignTraj - ) - def test_AlignTraj_weighted(self, universe, reference, tmpdir): + def test_AlignTraj_weighted(self, universe, reference, tmpdir, client_AlignTraj): outfile = str(tmpdir.join("align_test.dcd")) x = align.AlignTraj( universe, reference, filename=outfile, weights="mass" @@ -405,23 +374,13 @@ def test_AlignTraj_weighted(self, universe, reference, tmpdir): weights=universe.atoms.masses, ) - def test_AlignTraj_custom_weights( - self, universe, reference, tmpdir, client_AlignTraj - ): + def test_AlignTraj_custom_weights(self, universe, reference, tmpdir, client_AlignTraj): weights = np.zeros(universe.atoms.n_atoms) ca = universe.select_atoms("name CA") weights[ca.indices] = 1 outfile = str(tmpdir.join("align_test.dcd")) - x = align.AlignTraj(universe, reference, - filename=outfile, select='name CA').run( - **client_AlignTraj - ) - x_weights = align.AlignTraj(universe, reference, - filename=outfile, weights=weights).run( - **client_AlignTraj - ) x = align.AlignTraj( universe, reference, filename=outfile, select="name CA" ).run() @@ -433,16 +392,6 @@ def test_AlignTraj_custom_weights( x.results.rmsd, x_weights.results.rmsd, rtol=0, atol=1.5e-7 ) - def test_AlignTraj_custom_mass_weights( - self, universe, reference, tmpdir, client_AlignTraj - ): - outfile = str(tmpdir.join('align_test.dcd')) - x = align.AlignTraj(universe, reference, - filename=outfile, - weights=reference.atoms.masses).run( - **client_AlignTraj - ) - def test_AlignTraj_custom_mass_weights(self, universe, reference, tmpdir): outfile = str(tmpdir.join("align_test.dcd")) x = align.AlignTraj( @@ -466,25 +415,6 @@ def test_AlignTraj_custom_mass_weights(self, universe, reference, tmpdir): weights=universe.atoms.masses, ) - def test_AlignTraj_partial_fit( - self, universe, reference, tmpdir, client_AlignTraj - ): - outfile = str(tmpdir.join('align_test.dcd')) - # fitting on a partial selection should still write the whole topology - align.AlignTraj(universe, reference, select='resid 1-20', - filename=outfile, weights='mass').run( - **client_AlignTraj - ) - mda.Universe(PSF, outfile) - - def test_AlignTraj_in_memory( - self, universe, reference, tmpdir, client_AlignTraj - ): - outfile = str(tmpdir.join('align_test.dcd')) - reference.trajectory[-1] - x = align.AlignTraj(universe, reference, filename=outfile, - in_memory=True).run(**client_AlignTraj) - def test_AlignTraj_partial_fit(self, universe, reference, tmpdir): outfile = str(tmpdir.join("align_test.dcd")) # fitting on a partial selection should still write the whole topology @@ -511,17 +441,10 @@ def test_AlignTraj_in_memory(self, universe, reference, tmpdir): self._assert_rmsd(reference, universe, 0, 6.929083044751061) self._assert_rmsd(reference, universe, -1, 0.0) - def test_AlignTraj_writer_kwargs( - self, universe, reference, tmpdir, client_AlignTraj - ): + def test_AlignTraj_writer_kwargs(self, universe, reference, tmpdir): # Issue 4564 writer_kwargs = dict(precision=2) with tmpdir.as_cwd(): - aligner = align.AlignTraj(universe, reference, - select='protein and name CA', - filename='aligned_traj.xtc', - writer_kwargs=writer_kwargs, - in_memory=False).run(**client_AlignTraj) aligner = align.AlignTraj( universe, reference, @@ -584,7 +507,7 @@ def test_alignto_partial_universe(self, universe, reference): ) -def _get_aligned_average_positions(ref_files, ref, select="all", **kwargs): +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() ag = u.select_atoms(select) @@ -605,13 +528,9 @@ def universe(self): def reference(self): return mda.Universe(PSF, CRD) - def test_average_structure_deprecated_attrs( - self, universe, reference, client_AverageStructure - ): + def test_average_structure_deprecated_attrs(self, universe, reference): # Issue #3278 - remove in MDAnalysis 3.0.0 - avg = align.AverageStructure(universe, reference).run( - stop=2, **client_AverageStructure - ) + avg = align.AverageStructure(universe, reference).run(stop=2,) wmsg = "The `universe` attribute was deprecated in MDAnalysis 2.0.0" with pytest.warns(DeprecationWarning, match=wmsg): @@ -628,38 +547,8 @@ def test_average_structure_deprecated_attrs( with pytest.warns(DeprecationWarning, match=wmsg): assert avg.rmsd == avg.results.rmsd - def test_average_structure( - self, universe, reference, client_AverageStructure - ): + def test_average_structure(self, universe, reference): ref, rmsd = _get_aligned_average_positions(self.ref_files, reference) - avg = align.AverageStructure(universe, reference).run( - **client_AverageStructure - ) - 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, client_AverageStructure - ): - ref, rmsd = _get_aligned_average_positions(self.ref_files, reference, weights='mass') - avg = align.AverageStructure(universe, reference, weights='mass').run( - **client_AverageStructure - ) - 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, client_AverageStructure - ): - 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( - **client_AverageStructure - ) - assert_allclose(avg.results.universe.atoms.positions, ref, - rtol=0, atol=1.5e-4) -======= avg = align.AverageStructure(universe, reference).run() assert_allclose( avg.results.universe.atoms.positions, ref, rtol=0, atol=1.5e-4 @@ -687,16 +576,8 @@ def test_average_structure_select(self, universe, reference): ) assert_allclose(avg.results.rmsd, rmsd, rtol=0, atol=1.5e-7) - def test_average_structure_no_ref(self, universe, client_AverageStructure): + def test_average_structure_no_ref(self, universe): ref, rmsd = _get_aligned_average_positions(self.ref_files, universe) - avg = align.AverageStructure(universe).run(**client_AverageStructure) - 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, client_AverageStructure): - avg = align.AverageStructure(universe).run(**client_AverageStructure) - assert not hasattr(avg, 'msf') avg = align.AverageStructure(universe).run() assert_allclose( avg.results.universe.atoms.positions, ref, rtol=0, atol=1.5e-4 @@ -712,9 +593,7 @@ def test_mismatch_atoms(self, universe): with pytest.raises(SelectionError): align.AverageStructure(universe, u) - def test_average_structure_ref_frame( - self, universe, client_AverageStructure - ): + def test_average_structure_ref_frame(self, universe): ref_frame = 3 u = mda.Merge(universe.atoms) @@ -725,23 +604,14 @@ def test_average_structure_ref_frame( # back to start universe.trajectory[0] ref, rmsd = _get_aligned_average_positions(self.ref_files, u) - avg = align.AverageStructure(universe, ref_frame=ref_frame).run( - **client_AverageStructure - ) - assert_allclose(avg.results.universe.atoms.positions, ref, - rtol=0, atol=1.5e-4) avg = align.AverageStructure(universe, ref_frame=ref_frame).run() 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, client_AverageStructure - ): - avg = align.AverageStructure(universe, in_memory=True).run( - **client_AverageStructure - ) + 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_allclose( avg.results.universe.atoms.positions, From c37add2dc80e3791183113b8e4bc092ffdca2c45 Mon Sep 17 00:00:00 2001 From: Valerij Talagayev <82884038+talagayev@users.noreply.github.com> Date: Sat, 11 Jan 2025 22:40:12 +0100 Subject: [PATCH 19/19] Update align.py check possible fix --- package/MDAnalysis/analysis/align.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package/MDAnalysis/analysis/align.py b/package/MDAnalysis/analysis/align.py index 76303818675..ab73ed4d1dc 100644 --- a/package/MDAnalysis/analysis/align.py +++ b/package/MDAnalysis/analysis/align.py @@ -860,7 +860,8 @@ def _single_frame(self): self._writer.write(mobile_atoms) def _conclude(self): - self._writer.close() + if self._writer: + self._writer.close() if not self._verbose: logging.disable(logging.NOTSET)