Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 8cd7d5b

Browse files
committed
[coor/traj_info_cache] fix a bug in creating the LRU cache
This failed if the user has not yet created a config directory by invoking pyemma.config.save(), eg. in fairly new installations, since the LRU db name is derived from the main db, which is being hold in memory, in case no dir exists.
1 parent e1bd9b2 commit 8cd7d5b

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

Diff for: pyemma/coordinates/data/util/traj_info_backends.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,16 @@ def set(self, traj_info):
205205

206206
self._update_time_stamp(hash_value=traj_info.hash_value)
207207

208-
current_size = os.stat(self.filename).st_size
209-
if (self.num_entries >= config.traj_info_max_entries or
210-
# current_size is in bytes, while traj_info_max_size is in MB
211-
1.*current_size / 1024**2 >= config.traj_info_max_size):
212-
logger.info("Cleaning database because it has too much entries or is too large.\n"
213-
"Entries: %s. Size: %.2fMB. Configured max_entires: %s. Max_size: %sMB"
214-
% (self.num_entries, (current_size*1.0 / 1024**2),
215-
config.traj_info_max_entries, config.traj_info_max_size))
216-
self._clean(n=self.clean_n_entries)
208+
if self.filename is not None:
209+
current_size = os.stat(self.filename).st_size
210+
if (self.num_entries >= config.traj_info_max_entries or
211+
# current_size is in bytes, while traj_info_max_size is in MB
212+
1.*current_size / 1024**2 >= config.traj_info_max_size):
213+
logger.info("Cleaning database because it has too much entries or is too large.\n"
214+
"Entries: %s. Size: %.2fMB. Configured max_entires: %s. Max_size: %sMB"
215+
% (self.num_entries, (current_size*1.0 / 1024**2),
216+
config.traj_info_max_entries, config.traj_info_max_size))
217+
self._clean(n=self.clean_n_entries)
217218

218219
def get(self, key):
219220
cursor = self._database.execute("SELECT * FROM traj_info WHERE hash=?", (key,))
@@ -231,12 +232,15 @@ def _database_from_key(self, key):
231232
database has to be locked for updates and multiple processes want to write,
232233
each process has to wait until the lock has been released.
233234
234-
By default the LRU databases will be stored in a sub directory "tra_info_usage"
235+
By default the LRU databases will be stored in a sub directory "traj_info_usage"
235236
lying next to the main database.
236237
237238
:param key: hash of the TrajInfo instance
238239
:return: str, database path
239240
"""
241+
if not self.filename:
242+
return None
243+
240244
from pyemma.util.files import mkdir_p
241245
hash_value_long = int(key, 16)
242246
# bin hash to one of either 10 different databases
@@ -250,6 +254,9 @@ def _update_time_stamp(self, hash_value):
250254
""" timestamps are being stored distributed over several lru databases.
251255
The timestamp is a time.time() snapshot (float), which are seconds since epoch."""
252256
db_name = self._database_from_key(hash_value)
257+
if not db_name:
258+
db_name=':memory:'
259+
253260
import sqlite3
254261

255262
with sqlite3.connect(db_name) as conn:

Diff for: pyemma/coordinates/tests/test_traj_info_cache.py

+10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from pyemma.coordinates.data.feature_reader import FeatureReader
3636
from pyemma.coordinates.data.numpy_filereader import NumPyFileReader
3737
from pyemma.coordinates.data.py_csv_reader import PyCSVReader
38+
from pyemma.coordinates.data.util.traj_info_backends import SqliteDB
3839
from pyemma.coordinates.data.util.traj_info_cache import TrajectoryInfoCache
3940
from pyemma.coordinates.tests.util import create_traj
4041
from pyemma.datasets import get_bpti_test_data
@@ -276,6 +277,15 @@ def test_max_size(self):
276277
self.assertLessEqual(os.stat(self.db.database_filename).st_size / 1024, config.traj_info_max_size)
277278
self.assertGreater(self.db.num_entries, 0)
278279

280+
def test_no_working_directory(self):
281+
import sqlite3
282+
# this is the case as long as the user has not yet created a config directory via config.save()
283+
self.db._database = SqliteDB(filename=None)
284+
285+
286+
# trigger caching
287+
pyemma.coordinates.source(xtcfiles, top=pdbfile)
288+
279289
@unittest.skip("not yet functional")
280290
def test_no_sqlite(self):
281291
def import_mock(name, *args):

0 commit comments

Comments
 (0)