Skip to content

Commit

Permalink
changed dihedral atom group selection strings in test_dihedral.py (#218)
Browse files Browse the repository at this point in the history
* fix #214 
* changed dihedral atom group selection strings in test_dihedral.py to achieve an ordered selection format
  and consistent with original intended selection order
* changed dihedral atom group selection order in test_dataframe to match assert atom group order
* updated reference values calculated for circmean, circvar for use in test_results_recursive2()
* changed order for arguments in assert_almost_equal() for use in test_results_recursive2() to align with
   format in numpy.testing docs, (actual, desired) for readability, does not affect accuracy or testing purposes
* pytest skipif added in test_dihedral.py for test_results_recursive2() to skip test in python<3.8 due to 
  scipy circvar giving incorrect answers in the python<3.8 runners
  • Loading branch information
cadeduckworth authored Nov 17, 2022
1 parent ada0691 commit 39fd002
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 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,55 +39,56 @@ 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])
Expand Down

0 comments on commit 39fd002

Please sign in to comment.