From 642e87e3381da328fdba1afdf54ada2f4519d04f Mon Sep 17 00:00:00 2001 From: cadeduckworth Date: Thu, 17 Nov 2022 04:54:23 -0700 Subject: [PATCH] merged updates from PR #216 and PR #218 --- mdpow/analysis/dihedral.py | 4 ++-- mdpow/analysis/ensemble.py | 6 ++--- mdpow/tests/test_dihedral.py | 43 +++++++++++++++++++++--------------- mdpow/tests/test_ensemble.py | 17 +++++++++++++- 4 files changed, 46 insertions(+), 24 deletions(-) diff --git a/mdpow/analysis/dihedral.py b/mdpow/analysis/dihedral.py index 241c1e37..3a059a91 100644 --- a/mdpow/analysis/dihedral.py +++ b/mdpow/analysis/dihedral.py @@ -78,7 +78,7 @@ def check_dihedral_inputs(selections): for group in selections: for k in group.keys(): if len(group[k]) != 4: - msg = ''''Dihedral calculations require AtomGroups with + msg = '''Dihedral calculations require AtomGroups with only 4 atoms, %s selected''' % len(group) logger.error(msg) raise SelectionError(msg) @@ -109,4 +109,4 @@ def _single_frame(self): def _conclude_ensemble(self): for k in self._col: - self.results[k] = self._res_dict[k] + self.results[k] = self._res_dict[k] \ No newline at end of file diff --git a/mdpow/analysis/ensemble.py b/mdpow/analysis/ensemble.py index 7744a268..04d4ccc5 100644 --- a/mdpow/analysis/ensemble.py +++ b/mdpow/analysis/ensemble.py @@ -469,14 +469,14 @@ def _single_universe(self): Run on each universe in the ensemble during when self.run in called. """ - pass # pragma: no cover + raise NotImplementedError def _single_frame(self): """Calculate data from a single frame of trajectory Called on each frame for universes in the Ensemble. """ - pass # pragma: no cover + raise NotImplementedError def _prepare_ensemble(self): """For establishing data structures used in running @@ -558,4 +558,4 @@ def check_groups_from_common_ensemble(groups: List[EnsembleAtomGroup]): 'from the same Ensemble. ' f'mismatch: group[{i}].ensemble != group[{j}].ensemble') logger.error(msg) - raise ValueError(msg) + raise ValueError(msg) \ No newline at end of file diff --git a/mdpow/tests/test_dihedral.py b/mdpow/tests/test_dihedral.py index c54a110c..b40bace1 100644 --- a/mdpow/tests/test_dihedral.py +++ b/mdpow/tests/test_dihedral.py @@ -2,6 +2,8 @@ from . import tempdir as td +import sys + import py.path import pybol @@ -23,9 +25,9 @@ class TestDihedral(object): DG48910_mean = -172.9849512527183 - DG491011_mean = -3.7300197060478695 - DG48910_var = 1490.6576365537262 - DG491011_var = 128.3805265432388 + DG491011_mean = 177.74725233051953 + DG48910_var = 0.20311120667628546 + DG491011_var = 0.006976126708773456 def setup(self): self.tmpdir = td.TempDir() @@ -37,25 +39,25 @@ def teardown(self): self.tmpdir.dissolve() def test_dataframe(self): - dh1 = self.Ens.select_atoms('name C4 or name C17 or name S2 or name N3') + dh1 = self.Ens.select_atoms('name C4', 'name C17', 'name S2', 'name N3') dh_run = DihedralAnalysis([dh1]).run(start=0, stop=4, step=1) results = dh_run.results - assert results['selection'][0] == 'S2-N3-C4-C17' + assert results['selection'][0] == 'C4-C17-S2-N3' for s in results['solvent']: assert s == 'water' for i in results['interaction'][:12]: assert i == 'Coulomb' def test_selection_error(self): - dh1 = self.Ens.select_atoms('name C17 or name S2 or name N3') + dh1 = self.Ens.select_atoms('name C17', 'name S2', 'name N3') with pytest.raises(SelectionError): dh_run = DihedralAnalysis([dh1]).run(start=0, stop=4, step=1) def test_results_recursive1(self): - dh1 = self.Ens.select_atoms('name C11 or name C10 or name C9 or name C4') - dh2 = self.Ens.select_atoms('name C11 or name C10 or name C9 or name C4') + dh1 = self.Ens.select_atoms('name C11', 'name C10', 'name C9', 'name C4') + dh2 = self.Ens.select_atoms('name C11', 'name C10', 'name C9', 'name C4') dh_run1 = DihedralAnalysis([dh1]).run(start=0, stop=4, step=1) dh_run2 = DihedralAnalysis([dh2]).run(start=0, stop=4, step=1) @@ -63,30 +65,35 @@ def test_results_recursive1(self): for i in range(len(dh_run1.results['dihedral'])): assert dh_run1.results['dihedral'][i] == dh_run2.results['dihedral'][i] + @pytest.mark.skipif(sys.version_info < (3, 8), reason="scipy circvar gives wrong answers") def test_results_recursive2(self): - dh1 = self.Ens.select_atoms('name C11 or name C10 or name C9 or name C4') - dh2 = self.Ens.select_atoms('name C8 or name C4 or name C9 or name C10') + dh1 = self.Ens.select_atoms('name C11', 'name C10', 'name C9', 'name C4') + dh2 = self.Ens.select_atoms('name C8', 'name C4', 'name C9', 'name C10') dh_run = DihedralAnalysis([dh1, dh2]).run(start=0, stop=4, step=1) - dh1_result = dh_run.results.loc[dh_run.results['selection'] == 'C4-C9-C10-C11']['dihedral'] - dh2_result = dh_run.results.loc[dh_run.results['selection'] == 'C4-C8-C9-C10']['dihedral'] + dh1_result = dh_run.results.loc[dh_run.results['selection'] == 'C11-C10-C9-C4']['dihedral'] + dh2_result = dh_run.results.loc[dh_run.results['selection'] == 'C8-C4-C9-C10']['dihedral'] dh1_mean = circmean(dh1_result, high=180, low=-180) dh2_mean = circmean(dh2_result, high=180, low=-180) dh1_var = circvar(dh1_result, high=180, low=-180) dh2_var = circvar(dh2_result, high=180, low=-180) - assert_almost_equal(self.DG48910_mean, dh1_mean, 6) - assert_almost_equal(self.DG48910_var, dh1_var, 6) - assert_almost_equal(self.DG491011_mean, dh2_mean, 6) - assert_almost_equal(self.DG491011_var, dh2_var, 6) + assert_almost_equal(dh1_mean, self.DG48910_mean, 6) + assert_almost_equal(dh1_var, self.DG48910_var, 6) + assert_almost_equal(dh2_mean, self.DG491011_mean, 6) + assert_almost_equal(dh2_var, self.DG491011_var, 6) def test_ValueError_different_ensemble(self): other = Ensemble(dirname=self.tmpdir.name, solvents=['water']) - dh1 = self.Ens.select_atoms('name C11 or name C10 or name C9 or name C4') - dh2 = other.select_atoms('name C8 or name C4 or name C9 or name C10') + dh1 = self.Ens.select_atoms('name C11', 'name C10', 'name C9', 'name C4') + dh2 = other.select_atoms('name C8', 'name C4', 'name C9', 'name C10') with pytest.raises(ValueError, match='Dihedral selections from different Ensembles, '): DihedralAnalysis([dh1, dh2]) + def test_single_universe(self): + dh = self.Ens.select_atoms('name C4', 'name C17', 'name S2', 'name N3') + with pytest.raises(NotImplementedError): + DihedralAnalysis([dh])._single_universe() \ No newline at end of file diff --git a/mdpow/tests/test_ensemble.py b/mdpow/tests/test_ensemble.py index 9d8b2462..d993d579 100644 --- a/mdpow/tests/test_ensemble.py +++ b/mdpow/tests/test_ensemble.py @@ -149,6 +149,21 @@ def _conclude_universe(self): TestRun = TestAnalysis(Sim).run(start=0, step=1, stop=10) assert Sim.keys() == TestRun.key_list + def test_ensemble_analysis_run(self): + class TestAnalysis(EnsembleAnalysis): + def __init__(self, test_ensemble): + super(TestAnalysis, self).__init__(test_ensemble) + + def test_single_frame(self): + with pytest.raises(NotImplementedError) as excinfo: + TestAnalysis._single_frame(self) + assert excinfo.type == 'NotImplementedError' + + def test_single_universe(self): + with pytest.raises(NotImplementedError) as excinfo: + TestAnalysis._single_universe(self) + assert excinfo.type == 'NotImplementedError' + def test_value_error(self): ens = Ensemble(dirname=self.tmpdir.name, solvents=['water']) copy_ens = Ensemble() @@ -160,4 +175,4 @@ def test_value_error(self): dh3 = ens.select_atoms('name C4 or name C17 or name S2 or name N3') dh4 = ens.select_atoms('name C4 or name C17 or name S2 or name N3') with pytest.raises(ValueError): - dh_run = DihedralAnalysis([dh1, dh2, dh4, dh3]).run(start=0, stop=4, step=1) + dh_run = DihedralAnalysis([dh1, dh2, dh4, dh3]).run(start=0, stop=4, step=1) \ No newline at end of file