Skip to content
Snippets Groups Projects
Commit feb8207f authored by James Graham's avatar James Graham
Browse files

Better error messages if MDTraj can't find Scipy

parent 6d520fef
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
/*.itp /*.itp
/fftest.ff /fftest.ff
/env /env
/minenv
.coverage .coverage
/cover /cover
/tmp /tmp
......
...@@ -11,10 +11,23 @@ import numpy as np ...@@ -11,10 +11,23 @@ import numpy as np
from simpletraj import trajectory from simpletraj import trajectory
class MDTrajState:
# __slots__ = ["found", "missing_scipy", "not_found"]
_default = "XTC output requires both MDTraj and Scipy - "
found = _default + "both are present"
missing_scipy = _default + "Scipy is missing"
not_found = _default + "both are missing"
try: try:
import mdtraj import mdtraj
except ImportError: except ImportError as e:
pass if "no module named 'scipy'" in repr(e).lower():
mdtraj_state = MDTrajState.missing_scipy
else:
mdtraj_state = MDTrajState.not_found
else:
mdtraj_state = MDTrajState.found
from .util import backup_file from .util import backup_file
...@@ -179,7 +192,7 @@ class Frame: ...@@ -179,7 +192,7 @@ class Frame:
e.args = ("Error opening file '{0}'".format(xtc),) e.args = ("Error opening file '{0}'".format(xtc),)
raise raise
except NameError as e: except NameError as e:
raise ImportError("No module named 'mdtraj'") from e raise ImportError(mdtraj_state) from e
if self.xtc.n_atoms != self.natoms: if self.xtc.n_atoms != self.natoms:
raise AssertionError("Number of atoms does not match between gro and xtc files.") raise AssertionError("Number of atoms does not match between gro and xtc files.")
...@@ -281,7 +294,7 @@ class Frame: ...@@ -281,7 +294,7 @@ class Frame:
try: try:
self._xtc_buffer = mdtraj.formats.XTCTrajectoryFile(filename, mode="w") self._xtc_buffer = mdtraj.formats.XTCTrajectoryFile(filename, mode="w")
except NameError as e: except NameError as e:
raise ImportError("No module named 'mdtraj'") from e raise ImportError(mdtraj_state) from e
xyz = np.ndarray((1, self.natoms, 3), dtype=np.float32) xyz = np.ndarray((1, self.natoms, 3), dtype=np.float32)
i = 0 i = 0
......
...@@ -226,6 +226,16 @@ class Progress: ...@@ -226,6 +226,16 @@ class Progress:
self._its = 0 self._its = 0
self._start_time = time.clock() self._start_time = time.clock()
def __len__(self):
"""
Maximum iterator length.
This length will be reached if the iterator is not stopped by a False dowhile condition or KeyboardInterrupt.
:return: Maximum length of iterator
"""
return self._maxits - self._its
def __iter__(self): def __iter__(self):
return self return self
......
...@@ -96,13 +96,13 @@ class FrameTest(unittest.TestCase): ...@@ -96,13 +96,13 @@ class FrameTest(unittest.TestCase):
np.testing.assert_allclose(np.array([1.90325272, 1.90325272, 1.90325272]), np.testing.assert_allclose(np.array([1.90325272, 1.90325272, 1.90325272]),
frame.box) frame.box)
@unittest.skipIf(not mdtraj_present, "MDTRAJ not present") @unittest.skipIf(not mdtraj_present, "MDTRAJ or Scipy not present")
def test_frame_read_xtc_mdtraj_numframes(self): def test_frame_read_xtc_mdtraj_numframes(self):
frame = Frame(gro="test/data/water.gro", xtc="test/data/water.xtc", frame = Frame(gro="test/data/water.gro", xtc="test/data/water.xtc",
xtc_reader="mdtraj") xtc_reader="mdtraj")
self.assertEqual(12, frame.numframes) self.assertEqual(12, frame.numframes)
@unittest.skipIf(not mdtraj_present, "MDTRAJ not present") @unittest.skipIf(not mdtraj_present, "MDTRAJ or Scipy not present")
def test_frame_read_xtc_mdtraj(self): def test_frame_read_xtc_mdtraj(self):
frame = Frame(gro="test/data/water.gro", xtc="test/data/water.xtc", frame = Frame(gro="test/data/water.gro", xtc="test/data/water.xtc",
xtc_reader="mdtraj") xtc_reader="mdtraj")
...@@ -124,7 +124,7 @@ class FrameTest(unittest.TestCase): ...@@ -124,7 +124,7 @@ class FrameTest(unittest.TestCase):
np.testing.assert_allclose(np.array([1.90325272, 1.90325272, 1.90325272]), np.testing.assert_allclose(np.array([1.90325272, 1.90325272, 1.90325272]),
frame.box) frame.box)
@unittest.skipIf(not mdtraj_present, "MDTRAJ not present") @unittest.skipIf(not mdtraj_present, "MDTRAJ or Scipy not present")
def test_frame_write_xtc_mdtraj(self): def test_frame_write_xtc_mdtraj(self):
try: try:
os.remove("water_test2.xtc") os.remove("water_test2.xtc")
......
...@@ -46,7 +46,7 @@ class PycgtoolTest(unittest.TestCase): ...@@ -46,7 +46,7 @@ class PycgtoolTest(unittest.TestCase):
path = os.path.dirname(os.path.dirname(__file__)) path = os.path.dirname(os.path.dirname(__file__))
self.assertEqual(0, subprocess.check_call([os.path.join(path, "pycgtool.py"), "-h"], stdout=subprocess.PIPE)) self.assertEqual(0, subprocess.check_call([os.path.join(path, "pycgtool.py"), "-h"], stdout=subprocess.PIPE))
@unittest.skipIf(not mdtraj_present, "MDTRAJ not present") @unittest.skipIf(not mdtraj_present, "MDTRAJ or Scipy not present")
def test_map_only(self): def test_map_only(self):
map_only(Args("sugar"), self.config) map_only(Args("sugar"), self.config)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment