diff --git a/logger/xp.py b/logger/xp.py index 8fae320..a0465d1 100644 --- a/logger/xp.py +++ b/logger/xp.py @@ -2,6 +2,7 @@ import time import json import sys +import fnmatch from builtins import dict from collections import defaultdict, OrderedDict @@ -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_): @@ -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() diff --git a/test/test_xp.py b/test/test_xp.py index 3f0e46a..abbdbcf 100644 --- a/test/test_xp.py +++ b/test/test_xp.py @@ -3,6 +3,7 @@ import os import json import sys +import numpy as np from logger.xp import Experiment @@ -42,8 +43,11 @@ 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') @@ -51,6 +55,23 @@ def test_log_with_tag(self): 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):