Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed duplicated code in tinydb_hashfs.py #523

Merged
merged 2 commits into from
Jul 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 25 additions & 42 deletions sacred/observers/tinydb_hashfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,29 @@ def __deepcopy__(self, memo):
return BufferedReaderWrapper(f)


def get_db_file_manager(root_dir):
fs = HashFS(os.path.join(root_dir, 'hashfs'), depth=3,
width=2, algorithm='md5')

# Setup Serialisation object for non list/dict objects
serialization_store = SerializationMiddleware()
serialization_store.register_serializer(DateTimeSerializer(), 'TinyDate')
serialization_store.register_serializer(FileSerializer(fs), 'TinyFile')

if opt.has_numpy:
serialization_store.register_serializer(NdArraySerializer(),
'TinyArray')
if opt.has_pandas:
serialization_store.register_serializer(DataFrameSerializer(),
'TinyDataFrame')
serialization_store.register_serializer(SeriesSerializer(),
'TinySeries')

db = TinyDB(os.path.join(root_dir, 'metadata.json'),
storage=serialization_store)
return db, fs


class TinyDbObserver(RunObserver):

VERSION = "TinyDbObserver-{}".format(__version__)
Expand All @@ -60,28 +83,7 @@ def create(path='./runs_db', overwrite=None):
if not os.path.exists(root_dir):
os.makedirs(root_dir)

fs = HashFS(os.path.join(root_dir, 'hashfs'), depth=3,
width=2, algorithm='md5')

# Setup Serialisation object for non list/dict objects
serialization_store = SerializationMiddleware()
serialization_store.register_serializer(DateTimeSerializer(),
'TinyDate')
serialization_store.register_serializer(FileSerializer(fs),
'TinyFile')

if opt.has_numpy:
serialization_store.register_serializer(NdArraySerializer(),
'TinyArray')
if opt.has_pandas:
serialization_store.register_serializer(DataFrameSerializer(),
'TinyDataFrame')
serialization_store.register_serializer(SeriesSerializer(),
'TinySeries')

db = TinyDB(os.path.join(root_dir, 'metadata.json'),
storage=serialization_store)

db, fs = get_db_file_manager(root_dir)
return TinyDbObserver(db, fs, overwrite=overwrite, root=root_dir)

def __init__(self, db, fs, overwrite=None, root=None):
Expand Down Expand Up @@ -237,26 +239,7 @@ def __init__(self, path):
if not os.path.exists(root_dir):
raise IOError('Path does not exist: %s' % path)

fs = HashFS(os.path.join(root_dir, 'hashfs'), depth=3,
width=2, algorithm='md5')

# Setup Serialisation for non list/dict objects
serialization_store = SerializationMiddleware()
serialization_store.register_serializer(DateTimeSerializer(),
'TinyDate')
serialization_store.register_serializer(FileSerializer(fs),
'TinyFile')
if opt.has_numpy:
serialization_store.register_serializer(NdArraySerializer(),
'TinyArray')
if opt.has_pandas:
serialization_store.register_serializer(DataFrameSerializer(),
'TinyDataFrame')
serialization_store.register_serializer(SeriesSerializer(),
'TinySeries')

db = TinyDB(os.path.join(root_dir, 'metadata.json'),
storage=serialization_store)
db, fs = get_db_file_manager(root_dir)

self.db = db
self.runs = db.table('runs')
Expand Down