Skip to content

Commit

Permalink
Merge pull request #9 from TomVeniat/master
Browse files Browse the repository at this point in the history
Add reset parameter and pattern matching to log_with_tag
  • Loading branch information
lberrada authored Dec 6, 2017
2 parents b62308a + 5311fdd commit 74d6da9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
26 changes: 18 additions & 8 deletions logger/xp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time
import json
import sys
import fnmatch

from builtins import dict
from collections import defaultdict, OrderedDict
Expand Down Expand Up @@ -144,16 +145,22 @@ def log_config(self, config_dict, to_visdom=True):
if to_visdom and self.use_visdom:
self.plotter.plot_config(config_dict)

def log_with_tag(self, tag, idx=None):
def log_with_tag(self, tag, idx=None, reset=False):
""" Log metrics from each tag matching the given pattern.
tag parameter must be in Unix shell-style wildcards format.
"""
matched_tags = fnmatch.filter(self.metrics, tag)
assert matched_tags, "Could not find any tag matching with {}".format(tag)

# log all metrics with given tag except Parents
# (to avoid logging twice the information)
for metric in self.metrics[tag].values():
if isinstance(metric, ParentWrapper_):
continue
self.log_metric(metric, idx)
for match in matched_tags:
# log all metrics with given tag except Parents
# (to avoid logging twice the information)
for metric in self.metrics[match].values():
if isinstance(metric, ParentWrapper_):
continue
self.log_metric(metric, idx, reset)

def log_metric(self, metric, idx=None):
def log_metric(self, metric, idx=None, reset=False):

# log only child metrics
if isinstance(metric, ParentWrapper_):
Expand All @@ -169,6 +176,9 @@ def log_metric(self, metric, idx=None):
if self.use_visdom and metric.to_plot:
self.plotter.plot_metric(metric)

if reset:
metric.reset()

def log_and_reset_metric(self, metric, idx=None):
self.log_metric(metric, idx)
metric.reset()
Expand Down
21 changes: 21 additions & 0 deletions test/test_xp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import json
import sys
import numpy as np

from logger.xp import Experiment

Expand Down Expand Up @@ -42,15 +43,35 @@ def test_log_with_tag(self):
children=(xp.AvgMetric(name='child1'),
xp.SumMetric(name='child2')))
timer = xp.TimeMetric(tag='my_tag', name='timer')
other_metric = xp.BestMetric(tag='other_tag', name='simple')

metrics.update(child1=0.1, child2=0.5)
timer.update(1.)
other_metric.update(42)

xp.log_with_tag('my_tag')

assert list(xp.logged['child1_my_tag'].values()) == [0.1]
assert list(xp.logged['child2_my_tag'].values()) == [0.5]
assert list(xp.logged['timer_my_tag'].values()) == \
[1. - timer._timer.start]
assert list(xp.logged['simple_other_tag'].values()) == []

assert xp.get_metric('child1', 'my_tag').count == 1
assert xp.get_metric('child2', 'my_tag').count == 1
assert timer._timer.start != timer._timer.current

xp.log_with_tag('*', reset=True)

assert list(xp.logged['child1_my_tag'].values()) == [0.1, 0.1]
assert list(xp.logged['child2_my_tag'].values()) == [0.5, 0.5]
assert len(xp.logged['timer_my_tag'].values()) == 2
assert list(xp.logged['simple_other_tag'].values()) == [42]

assert xp.get_metric('child1', 'my_tag').count == 0
assert xp.get_metric('child2', 'my_tag').count == 0
assert timer._timer.start == timer._timer.current
assert other_metric._val == -1 * np.inf

def test_log_metric(self):

Expand Down

0 comments on commit 74d6da9

Please sign in to comment.