Skip to content

Commit

Permalink
Fix/sacred observer (#57)
Browse files Browse the repository at this point in the history
* updated git_info discovery to be that of sacred
* update base_dir discovery to be that of sacred
* updated resource tracking to be more sacred-like with a list of `resources` property and `filename: md5` pairs in properties
  • Loading branch information
jakubczakon authored Oct 4, 2019
1 parent 530297f commit 0a697fd
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 34 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
# The short X.Y version
version = '0.13'
# The full version, including alpha/beta/rc tags
release = '0.13.2'
release = '0.13.3'

# -- General configuration ---------------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ And the best thing is you can extend it yourself or... tell us to do it for you
monitoring.fastai <user_guide/monitoring/fastai>
monitoring.metrics <user_guide/monitoring/metrics>
monitoring.fairness <user_guide/monitoring/fairness>
monitoring.sacred <user_guide/monitoring/sacred>
monitoring.skopt <user_guide/monitoring/skopt>
monitoring.utils <user_guide/monitoring/utils>
sync.with_json <user_guide/sync/with_json>
Expand Down
6 changes: 6 additions & 0 deletions docs/user_guide/monitoring/sacred.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Sacred
===========

.. automodule:: neptunecontrib.monitoring.sacred
:members:
:show-inheritance:
42 changes: 12 additions & 30 deletions neptunecontrib/monitoring/sacred.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#

import collections
import os

import neptune
from sacred.dependencies import get_digest
Expand Down Expand Up @@ -74,26 +73,28 @@ def run(C, gamma, _run):
Go to the app and see the experiment. For example, https://ui.neptune.ml/jakub-czakon/examples/e/EX-263
"""

def __init__(self, project_name, api_token=None, base_dir='.', source_extensions=None):
def __init__(self, project_name, api_token=None, source_extensions=None):
neptune.init(project_qualified_name=project_name, api_token=api_token)

self.resources = {}
self.base_dir = base_dir

if source_extensions:
self.source_extensions = source_extensions
else:
self.source_extensions = ['.py', '.R', '.cpp', '.yaml', '.yml']
self.source_extensions = ['**/*.py', '**/*.yaml', '**/*.yml']

def started_event(self, ex_info, command, host_info, start_time, config, meta_info, _id):

neptune.create_experiment(name=ex_info['name'],
params=_flatten_dict(config),
upload_source_files=_get_filepaths(dirpath=self.base_dir,
extensions=self.source_extensions),
upload_source_files=self.source_extensions,
properties={'mainfile': ex_info['mainfile'],
'dependencies': str(ex_info['dependencies']),
'sacred_id': str(_id),
**_str_dict_values(host_info),
**_str_dict_values(_flatten_dict(meta_info))})
**_str_dict_values(_flatten_dict(meta_info)),
**_str_dict_values(_flatten_dict(ex_info))},
git_info=neptune.utils.get_git_info(ex_info['base_dir']))

def completed_event(self, stop_time, result):
if result:
Expand All @@ -111,38 +112,19 @@ def artifact_event(self, name, filename, metadata=None, content_type=None):

def resource_event(self, filename):
if filename not in self.resources:
new_prefix = self._create_new_prefix()
self.resources[filename] = new_prefix
md5 = get_digest(filename)
self.resources[filename] = md5

neptune.set_property('{}data_path'.format(new_prefix), filename)
neptune.set_property('{}data_version'.format(new_prefix), md5)
neptune.set_property('resources', str(list(self.resources.keys())))
neptune.set_property(filename, self.resources[filename])

def log_metrics(self, metrics_by_name, info):
for metric_name, metric_ptr in metrics_by_name.items():
for step, value in zip(metric_ptr["steps"], metric_ptr["values"]):
neptune.log_metric(metric_name, x=step, y=value)

def _create_new_prefix(self):
existing_prefixes = self.resources.values()
if existing_prefixes:
prefix_ids = [int(prefix.replace('resource', '')) for prefix in existing_prefixes]
new_prefix = 'resource{}'.format(max(prefix_ids) + 1)
else:
new_prefix = 'resource0'
return new_prefix


def _get_filepaths(dirpath, extensions):
files = []
for r, _, f in os.walk(dirpath):
for file in f:
if any(file.endswith(ext) for ext in extensions):
files.append(os.path.join(r, file))
return files


def _flatten_dict(d, parent_key='', sep='_'):
def _flatten_dict(d, parent_key='', sep=' '):
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
Expand Down
2 changes: 1 addition & 1 deletion pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ load-plugins=pylintfileheader
# W0511 Allow TODO/FIXME comments.
# W0703 Allow too broad except clause (Exception).
# I0011 Do not show Locally disabled warnings in report
disable=R,C0103,C0111,W0401,W0511,W0614,W0703,I0011,W0613,E0401,C0411
disable=R,C0103,C0111,W0401,W0511,W0614,W0703,I0011,W0613,E0401,C0411,E0611

# Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def main():

setup(
name='neptune-contrib',
version='0.13.2',
version='0.13.3',
description='Neptune Python library contributions',
author='neptune.ml',
author_email='[email protected]',
Expand Down
2 changes: 1 addition & 1 deletion test_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
neptune-client
mock==2.0.0
pylint==1.9.4
pytest==4.1.0
pytest==5.2.0
pytest-cov==2.6.1
pytest-xdist==1.25.0
tox==3.6.1
Expand Down

0 comments on commit 0a697fd

Please sign in to comment.