Skip to content

Commit e463087

Browse files
committed
add compat.openmm to handle OpenMM imports
1 parent fb518ba commit e463087

7 files changed

Lines changed: 36 additions & 54 deletions

File tree

‎paths_cli/compat/openmm.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# should be able to remove this try block when we drop OpenMM < 7.6
2+
try:
3+
import openmm as mm
4+
from openmm import unit
5+
except ImportError:
6+
try:
7+
from simtk import openmm as mm
8+
from simtk import unit
9+
except ImportError:
10+
HAS_OPENMM = False
11+
mm = None
12+
u = None
13+
else: # -no-cov-
14+
HAS_OPENMM = True
15+
else:
16+
HAS_OPENMM = True
17+
18+

‎paths_cli/compiling/engines.py‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@
44
from .tools import custom_eval_int
55
from paths_cli.compiling.plugins import EngineCompilerPlugin, CategoryPlugin
66

7-
try:
8-
from simtk import openmm as mm
9-
except ImportError:
10-
HAS_OPENMM = False
11-
else:
12-
HAS_OPENMM = True
13-
147
def load_openmm_xml(filename):
8+
from openpathsampling.integration_tools import HAS_OPENMM
9+
from openpathsampling.integration_tools import openmm as mm
1510
if not HAS_OPENMM: # -no-cov-
1611
raise RuntimeError("OpenMM does not seem to be installed")
1712

‎paths_cli/tests/compiling/test_cvs.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def setup(self):
2424
self.kwargs = "indices: [[4, 6, 8, 14]]"
2525

2626
def test_build_mdtraj_function_cv(self):
27-
_ = pytest.importorskip('simtk.unit')
27+
_ = pytest.importorskip('mdtraj')
2828
yml = self.yml.format(kwargs=self.kwargs, func="compute_dihedrals")
2929
dct = yaml.load(yml, Loader=yaml.FullLoader)
3030
cv = MDTRAJ_CV_PLUGIN(dct)

‎paths_cli/tests/compiling/test_engines.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
from paths_cli.compiling.engines import *
66
from paths_cli.compiling.engines import _openmm_options
77
from paths_cli.compiling.errors import InputError
8+
from paths_cli.compat.openmm import HAS_OPENMM, mm, unit
89
import openpathsampling as paths
910

1011
from openpathsampling.engines import openmm as ops_openmm
1112
import mdtraj as md
1213

13-
1414
class TestOpenMMEngineBuilder(object):
1515
def setup(self):
1616
self.cwd = os.getcwd()
@@ -24,9 +24,9 @@ def teardown(self):
2424
os.chdir(self.cwd)
2525

2626
def _create_files(self, tmpdir):
27-
mm = pytest.importorskip('simtk.openmm')
27+
if not HAS_OPENMM:
28+
pytest.skip('openmm not installed')
2829
openmmtools = pytest.importorskip('openmmtools')
29-
unit = pytest.importorskip('simtk.unit')
3030
ad = openmmtools.testsystems.AlanineDipeptideVacuum()
3131
integrator = openmmtools.integrators.VVVRIntegrator(
3232
300*unit.kelvin, 1.0/unit.picosecond, 2.0*unit.femtosecond
@@ -41,7 +41,8 @@ def _create_files(self, tmpdir):
4141
trj.save(os.path.join(tmpdir, "ad.pdb"))
4242

4343
def test_load_openmm_xml(self, tmpdir):
44-
mm = pytest.importorskip('simtk.openmm')
44+
if not HAS_OPENMM:
45+
pytest.skip('openmm not installed')
4546
self._create_files(tmpdir)
4647
os.chdir(tmpdir)
4748
for fname in ['system.xml', 'integrator.xml', 'ad.pdb']:

‎paths_cli/tests/wizard/conftest.py‎

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,7 @@
33
import openpathsampling as paths
44
import mdtraj as md
55

6-
# should be able to remove this try block when we drop OpenMM < 7.6
7-
try:
8-
import openmm as mm
9-
from openmm import unit as u
10-
except ImportError:
11-
try:
12-
from simtk import openmm as mm
13-
from simtk import unit as u
14-
except ImportError:
15-
HAS_OPENMM = False
16-
else: # -no-cov-
17-
HAS_OPENMM = True
18-
else:
19-
HAS_OPENMM = True
6+
from paths_cli.compat.openmm import HAS_OPENMM, mm, unit
207

218
# TODO: this isn't wizard-specific, and should be moved somwhere more
229
# generally useful (like, oh, maybe openpathsampling.tests.fixtures?)
@@ -34,12 +21,14 @@ def ad_openmm(tmpdir):
3421
md = pytest.importorskip('mdtraj')
3522
testsystem = openmmtools.testsystems.AlanineDipeptideVacuum()
3623
integrator = openmmtools.integrators.VVVRIntegrator(
37-
300 * u.kelvin,
38-
1.0 / u.picosecond,
39-
2.0 * u.femtosecond
24+
300 * unit.kelvin,
25+
1.0 / unit.picosecond,
26+
2.0 * unit.femtosecond
27+
)
28+
traj = md.Trajectory(
29+
[testsystem.positions.value_in_unit(unit.nanometer)],
30+
topology=testsystem.mdtraj_topology
4031
)
41-
traj = md.Trajectory([testsystem.positions.value_in_unit(u.nanometer)],
42-
topology=testsystem.mdtraj_topology)
4332
files = {'integrator.xml': integrator,
4433
'system.xml': testsystem.system}
4534
with tmpdir.as_cwd():

‎paths_cli/tests/wizard/test_openmm.py‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,7 @@
88
_load_openmm_xml, _load_topology, openmm, OPENMM_SERIALIZATION_URL
99
)
1010

11-
# should be able to remove this try block when we drop OpenMM < 7.6
12-
try:
13-
import openmm as mm
14-
except ImportError:
15-
try:
16-
from simtk import openmm as mm
17-
except ImportError:
18-
HAS_OPENMM = False
19-
else:
20-
HAS_OPENMM = True # -no-cov-
21-
else:
22-
HAS_OPENMM = True
11+
from paths_cli.compat.openmm import mm, HAS_OPENMM
2312

2413
def test_helper_url():
2514
assert_url(OPENMM_SERIALIZATION_URL)

‎paths_cli/wizard/openmm.py‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,7 @@
22
from paths_cli.wizard.core import get_object
33

44
# should be able to simplify this try block when we drop OpenMM < 7.6
5-
try:
6-
import openmm as mm
7-
except ImportError:
8-
try:
9-
from simtk import openmm as mm
10-
except ImportError:
11-
HAS_OPENMM = False
12-
else: # -no-cov-
13-
HAS_OPENMM = True
14-
else:
15-
HAS_OPENMM = True
5+
from paths_cli.compat.openmm import mm, HAS_OPENMM
166

177
OPENMM_SERIALIZATION_URL=(
188
"http://docs.openmm.org/latest/api-python/generated/"

0 commit comments

Comments
 (0)