From feb8207ffc3e7582f80ba35de5a375f6c5f1f39a Mon Sep 17 00:00:00 2001 From: James Graham <J.A.Graham@soton.ac.uk> Date: Thu, 7 Jul 2016 15:36:51 +0100 Subject: [PATCH] Better error messages if MDTraj can't find Scipy --- .gitignore | 1 + pycgtool/frame.py | 21 +++++++++++++++++---- pycgtool/interface.py | 10 ++++++++++ test/test_frame.py | 6 +++--- test/test_pycgtool.py | 2 +- 5 files changed, 32 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 49b0a71..a721a33 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ /*.itp /fftest.ff /env +/minenv .coverage /cover /tmp diff --git a/pycgtool/frame.py b/pycgtool/frame.py index ab2742b..a7f7c97 100644 --- a/pycgtool/frame.py +++ b/pycgtool/frame.py @@ -11,10 +11,23 @@ import numpy as np 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: import mdtraj -except ImportError: - pass +except ImportError as e: + 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 @@ -179,7 +192,7 @@ class Frame: e.args = ("Error opening file '{0}'".format(xtc),) raise 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: raise AssertionError("Number of atoms does not match between gro and xtc files.") @@ -281,7 +294,7 @@ class Frame: try: self._xtc_buffer = mdtraj.formats.XTCTrajectoryFile(filename, mode="w") 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) i = 0 diff --git a/pycgtool/interface.py b/pycgtool/interface.py index 6e3ccaa..e878d02 100644 --- a/pycgtool/interface.py +++ b/pycgtool/interface.py @@ -226,6 +226,16 @@ class Progress: self._its = 0 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): return self diff --git a/test/test_frame.py b/test/test_frame.py index 604e88f..59942f1 100644 --- a/test/test_frame.py +++ b/test/test_frame.py @@ -96,13 +96,13 @@ class FrameTest(unittest.TestCase): np.testing.assert_allclose(np.array([1.90325272, 1.90325272, 1.90325272]), 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): frame = Frame(gro="test/data/water.gro", xtc="test/data/water.xtc", xtc_reader="mdtraj") 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): frame = Frame(gro="test/data/water.gro", xtc="test/data/water.xtc", xtc_reader="mdtraj") @@ -124,7 +124,7 @@ class FrameTest(unittest.TestCase): np.testing.assert_allclose(np.array([1.90325272, 1.90325272, 1.90325272]), 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): try: os.remove("water_test2.xtc") diff --git a/test/test_pycgtool.py b/test/test_pycgtool.py index 42c36c7..f71f8ac 100644 --- a/test/test_pycgtool.py +++ b/test/test_pycgtool.py @@ -46,7 +46,7 @@ class PycgtoolTest(unittest.TestCase): path = os.path.dirname(os.path.dirname(__file__)) 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): map_only(Args("sugar"), self.config) -- GitLab