diff --git a/.gitignore b/.gitignore index 49b0a7199f78b7ce0cfe5028b025213b1a267a06..a721a33b1495dbbe1ad625893fef6f760e70c133 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 ab2742bc2e408624add81e3ce9bfcb4d14841222..a7f7c97ed543438f06e97f82f610d7d80eb67eab 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 6e3ccaa8da615983e5f7753003e8dc645f2f2617..e878d02c052b003776bee8fafd671234a721d2bb 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 604e88f32671f2c4ead547e18f975f2a1cab7f83..59942f1323465667b56aa187aad4ee48bbf20f55 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 42c36c7a432fc967d45dde137810b733436b7cd2..f71f8aca045cddd641614f5a5df0768cfbb72e72 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)