Skip to content

Commit

Permalink
rely on GSDReader for pickling instead, gsd>2 dependency removed
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxuanzhuang committed Jun 3, 2020
1 parent 31e08a4 commit 5bbc9b2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ env:
- MAIN_CMD="pytest ${PYTEST_LIST}"
- SETUP_CMD="${PYTEST_FLAGS}"
- BUILD_CMD="pip install -e package/ && (cd testsuite/ && python setup.py build)"
- CONDA_MIN_DEPENDENCIES="mmtf-python mock six biopython networkx cython matplotlib scipy griddataformats hypothesis gsd>=2.1.1 codecov"
- CONDA_MIN_DEPENDENCIES="mmtf-python mock six biopython networkx cython matplotlib scipy griddataformats hypothesis gsd codecov"
- CONDA_DEPENDENCIES="${CONDA_MIN_DEPENDENCIES} seaborn>=0.7.0 clustalw=2.1 netcdf4 scikit-learn joblib>=0.12 chemfiles tqdm>=4.43.0"
- CONDA_CHANNELS='biobuilds conda-forge'
- CONDA_CHANNEL_PRIORITY=True
Expand Down
26 changes: 19 additions & 7 deletions package/MDAnalysis/coordinates/GSD.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@

from . import base

class GSDReader(base.ReaderBase):
class GSDReader(base.ReaderBase, base._ExAsciiPickle):
"""Reader for the GSD format.
"""
Expand All @@ -76,23 +76,27 @@ def __init__(self, filename, **kwargs):
super(GSDReader, self).__init__(filename, **kwargs)
self.filename = filename
self.open_trajectory()
self.n_atoms = self._file[0].particles.N
self.n_atoms = self._f[0].particles.N
self.ts = self._Timestep(self.n_atoms, **self._ts_kwargs)
self._read_next_timestep()

def open_trajectory(self) :
def open_trajectory(self):
"""opens the trajectory file using gsd.hoomd module"""
self._frame = -1
self._file = gsd.hoomd.open(self.filename,mode='rb')
self._f = gsd.hoomd.open(self.filename,mode='rb')

def open_trajectory_for_pickle(self):
"""opens the trajectory file while not reset frame"""
self._f = gsd.hoomd.open(self.filename, mode='rb')

def close(self):
"""close reader"""
self._file.file.close()
self._f.file.close()

@property
def n_frames(self):
"""number of frames in trajectory"""
return len(self._file)
return len(self._f)

def _reopen(self):
"""reopen trajectory"""
Expand All @@ -101,7 +105,7 @@ def _reopen(self):

def _read_frame(self, frame):
try :
myframe = self._file[frame]
myframe = self._f[frame]
except IndexError:
raise_from(IOError, None)

Expand Down Expand Up @@ -131,3 +135,11 @@ def _read_frame(self, frame):
def _read_next_timestep(self) :
"""read next frame in trajectory"""
return self._read_frame(self._frame + 1)

# def __getstate__(self):
# """Implement the pickle protocol."""
# return dict(name=self.filename)
#
# def __setstate__(self, state):
# """Implement the pickle protocol."""
# self.__init__(state['name'])
21 changes: 21 additions & 0 deletions package/MDAnalysis/coordinates/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2106,6 +2106,27 @@ def __setstate__(self, state):

del self._pickle_fn

class _ExAsciiPickle(object):
# For external file reader, e.g. GSD
def __getstate__(self):
# Shallow copy of state of self
# shallow ie don't recursively copy all objects,
# just copy the references that __dict__ holds
stuff = self.__dict__.copy()
# don't pass the file handle over
del stuff['_f']
# instead pass enough metadata to reconstruct
stuff['_pickle_fn'] = self.filename
# TODO: what other state does Reader hold?
# TODO: reconstruct file handle position
return stuff

def __setstate__(self, state):
self.__dict__.update(state)
self.open_trajectory_for_pickle()
del self._pickle_fn


class ReaderBase(ProtoReader):
"""Base class for trajectory readers that extends :class:`ProtoReader` with a
:meth:`__del__` method.
Expand Down

0 comments on commit 5bbc9b2

Please sign in to comment.