Skip to content

Commit

Permalink
merged updates from PR #216 and PR #218
Browse files Browse the repository at this point in the history
  • Loading branch information
cadeduckworth committed Nov 17, 2022
1 parent 29477ab commit 642e87e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
4 changes: 2 additions & 2 deletions mdpow/analysis/dihedral.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
6 changes: 3 additions & 3 deletions mdpow/analysis/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
43 changes: 25 additions & 18 deletions mdpow/tests/test_dihedral.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from . import tempdir as td

import sys

import py.path

import pybol
Expand All @@ -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()
Expand All @@ -37,56 +39,61 @@ 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)
assert len(dh_run1.results['dihedral']) == len(dh_run2.results['dihedral'])
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()
17 changes: 16 additions & 1 deletion mdpow/tests/test_ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)

0 comments on commit 642e87e

Please sign in to comment.