Skip to content

Commit

Permalink
Merge pull request #10 from oval-group/visdom
Browse files Browse the repository at this point in the history
Improvements on integration with visdom
  • Loading branch information
lberrada authored Feb 10, 2018
2 parents 74d6da9 + 0730a25 commit 711e6e1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
4 changes: 4 additions & 0 deletions examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def oracle(data, target):
xp.AvgMetric(tag="test", name="acc1")
xp.AvgMetric(tag="test", name="acck")

xp.plotter.set_win_opts(name="acc1", opts={'title': 'Accuracy@1'})
xp.plotter.set_win_opts(name="acck", opts={'title': 'Accuracy@k'})
xp.plotter.set_win_opts(name="loss", opts={'title': 'Loss'})

#----------------------------------------------------------
# Training
#----------------------------------------------------------
Expand Down
31 changes: 24 additions & 7 deletions logger/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,33 @@ def __init__(self, xp, visdom_opts, xlabel):
self.viz = visdom.Visdom(**visdom_opts)
self.xlabel = None if xlabel is None else str(xlabel)
self.windows = {}
self.windows_opts = defaultdict(dict)
self.append = {}
self.cache = defaultdict(Cache)

def set_win_opts(self, name, opts):
self.windows_opts[name] = opts

def _plot_xy(self, name, tag, x, y, time_idx=True):
"""
Creates a window if it does not exist yet.
Returns True if data has been sent successfully, False otherwise.
"""
tag = None if tag == 'default' else tag

if name not in list(self.windows.keys()):
if self.xlabel is None:
xlabel = 'Time (s)' if time_idx else 'Index'
opts = self.windows_opts[name]
if 'xlabel' in opts:
pass
elif self.xlabel is not None:
opts['xlabel'] = self.xlabel
else:
xlabel = self.xlabel
opts = {'legend': [tag], 'title': name, 'xlabel': xlabel}
opts['xlabel'] = 'Time (s)' if time_idx else 'Index'

if 'legend' not in opts and tag:
opts['legend'] = [tag]
if 'title' not in opts:
opts['title'] = name
self.windows[name] = self.viz.line(Y=y, X=x, opts=opts)
return True
else:
Expand Down Expand Up @@ -91,15 +104,19 @@ def plot_metric(self, metric):
name, tag = metric.name, metric.tag
cache = self.cache[metric.name_id()]
cache.update(metric)
sent = self._plot_xy(name, tag, cache.x, cache.y,
metric.time_idx)
sent = self._plot_xy(name, tag, cache.x, cache.y, metric.time_idx)
# clear cache if data has been sent successfully
if sent:
cache.clear()

def plot_config(self, config):
config = dict((str(k), v) for (k, v) in config.items())
# format dictionary with pretty print
pp = pprint.PrettyPrinter(indent=4)
pp = pprint.PrettyPrinter(indent=4, width=1)
msg = pp.pformat(config)
# format with html
msg = msg.replace('{', '')
msg = msg.replace('}', '')
msg = msg.replace('\n', '<br />')
# display dict on visdom
self.viz.text(msg)
7 changes: 7 additions & 0 deletions logger/xp.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ def get_var_dict(self):
var_dict['name'] = self.name
var_dict['name_and_dir'] = self.name_and_dir
var_dict['date_and_time'] = self.date_and_time
if self.use_visdom:
var_dict['visdom_win_opts'] = self.plotter.windows_opts
return var_dict

def to_pickle(self, filename):
Expand All @@ -224,6 +226,11 @@ def from_json(self, filename):

def to_visdom(self, visdom_opts=None, xlabel=None):
self.plotter = Plotter(self, visdom_opts, xlabel)
# restore visdom options that have been saved (if experiment loaded from file)
if hasattr(self, 'visdom_win_opts'):
windows_opts = self.__dict__.pop('visdom_win_opts')
for (name, opts) in windows_opts.items():
self.plotter.set_win_opts(name, opts)
self.plotter.plot_xp(self)


Expand Down

0 comments on commit 711e6e1

Please sign in to comment.