Removed CachedReader

This commit is contained in:
Sebastian Kloth 2023-11-05 19:39:02 +01:00
parent 1aa5eed106
commit cf28882418

View File

@ -32,18 +32,11 @@ class WrongTopologyError(Exception):
def open_with_mdanalysis(
topology, trajectory, cached=False, index_file=None, charges=None, masses=None
topology, trajectory, index_file=None, charges=None, masses=None
):
"""Open the topology and trajectory with mdanalysis."""
uni = mdanalysis.Universe(topology, trajectory, convert_units=False)
if cached is not False:
if cached is True:
maxsize = 128
else:
maxsize = cached
reader = CachedReader(uni.trajectory, maxsize)
else:
reader = BaseReader(uni.trajectory)
reader = BaseReader(uni.trajectory)
reader.universe = uni
if topology.endswith(".tpr"):
charges = uni.atoms.charges
@ -274,11 +267,6 @@ class BaseReader:
self._nojump_matrixes = mats
def __init__(self, rd):
"""
Args:
filename: Trajectory file to open.
reindex (bool, opt.): If True, regenerate the index file if necessary.
"""
self.rd = rd
self._nojump_matrixes = None
if path.exists(nojump_load_filename(self)):
@ -291,39 +279,5 @@ class BaseReader:
return len(self.rd)
def __checksum__(self):
if hasattr(self.rd, "cache"):
# Has an pygmx reader
return checksum(self.filename, str(self.rd.cache))
elif hasattr(self.rd, "_xdr"):
# Has an mdanalysis reader
cache = array("L", self.rd._xdr.offsets.tobytes())
return checksum(self.filename, str(cache))
class CachedReader(BaseReader):
"""A reader that has a least-recently-used cache for frames."""
@property
def cache_info(self):
"""Get Information about the lru cache."""
return self._get_item.cache_info()
def clear_cache(self):
"""Clear the cache of the frames."""
self._get_item.cache_clear()
def __init__(self, rd, maxsize):
"""
Args:
filename (str): Trajectory file that will be opened.
maxsize: Maximum size of the lru_cache or None for infinite cache.
"""
super().__init__(rd)
self._get_item = lru_cache(maxsize=maxsize)(self._get_item)
def _get_item(self, item):
"""Buffer function for lru_cache, since __getitem__ can not be cached."""
return super().__getitem__(item)
def __getitem__(self, item):
return self._get_item(item)
cache = array("L", self.rd._xdr.offsets.tobytes())
return checksum(self.filename, str(cache))