|
| 1 | +import time |
| 2 | +from collections import OrderedDict |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import pandas as pd |
| 6 | +import matplotlib.pyplot as plt |
| 7 | + |
| 8 | +class Metric(object): |
| 9 | + """Named sequence of x and y values, with optional plotting helpers.""" |
| 10 | + |
| 11 | + def __init__(self, name): |
| 12 | + self.name = name |
| 13 | + self.reset() |
| 14 | + |
| 15 | + def collect(self, x, y): |
| 16 | + self.xdata.append(x) |
| 17 | + self.ydata.append(y) |
| 18 | + |
| 19 | + def plot(self, ax): |
| 20 | + self.plot_obj, = ax.plot(self.xdata, self.ydata, 'o-', label=self.name) |
| 21 | + |
| 22 | + def refresh(self): |
| 23 | + self.plot_obj.set_data(self.xdata, self.ydata) |
| 24 | + |
| 25 | + def reset(self): |
| 26 | + self.xdata = [] |
| 27 | + self.ydata = [] |
| 28 | + |
| 29 | + |
| 30 | +class Reporter(object): |
| 31 | + """Collect metrics, analyze and report summary statistics.""" |
| 32 | + |
| 33 | + def __init__(self, metrics=[], live_plot=False): |
| 34 | + self.metrics = OrderedDict() |
| 35 | + self.live_plot = live_plot |
| 36 | + |
| 37 | + for name in metrics: |
| 38 | + self.metrics[name] = Metric(name) |
| 39 | + |
| 40 | + if self.live_plot: |
| 41 | + if not plt.isinteractive(): |
| 42 | + plt.ion() |
| 43 | + self.plot() |
| 44 | + |
| 45 | + print "Reporter.__init__(): Initialized with metrics: {}".format(metrics) # [debug] |
| 46 | + |
| 47 | + def collect(self, name, x, y): |
| 48 | + if not name in self.metrics: |
| 49 | + self.metrics[name] = Metric(name) |
| 50 | + if self.live_plot: |
| 51 | + self.metrics[name].plot(self.ax) |
| 52 | + self.ax.legend() # add new metric to legend |
| 53 | + print "Reporter.collect(): New metric added: {}".format(name) # [debug] |
| 54 | + self.metrics[name].collect(x, y) |
| 55 | + if self.live_plot: |
| 56 | + self.metrics[name].refresh() |
| 57 | + |
| 58 | + def plot(self): |
| 59 | + if not hasattr(self, 'fig') or not hasattr(self, 'ax'): |
| 60 | + self.fig, self.ax = plt.subplots() |
| 61 | + for name in self.metrics: |
| 62 | + self.metrics[name].plot(self.ax) |
| 63 | + #self.ax.set_autoscalex_on(True) |
| 64 | + #self.ax.set_autoscaley_on(True) |
| 65 | + self.ax.grid() |
| 66 | + self.ax.legend() |
| 67 | + else: |
| 68 | + for name in self.metrics: |
| 69 | + self.metrics[name].refresh() |
| 70 | + self.refresh_plot() |
| 71 | + |
| 72 | + def refresh_plot(self): |
| 73 | + self.ax.relim() |
| 74 | + self.ax.autoscale_view() |
| 75 | + self.fig.canvas.draw() |
| 76 | + self.fig.canvas.flush_events() |
| 77 | + plt.draw() |
| 78 | + |
| 79 | + def show_plot(self): |
| 80 | + if plt.isinteractive(): |
| 81 | + plt.ioff() |
| 82 | + self.plot() |
| 83 | + plt.show() |
| 84 | + |
| 85 | + def summary(self): |
| 86 | + return [pd.Series(metric.ydata, index=metric.xdata, name=name) for name, metric in self.metrics.iteritems()] |
| 87 | + |
| 88 | + def reset(self): |
| 89 | + for name in self.metrics: |
| 90 | + self.metrics[name].reset() |
| 91 | + if self.live_plot: |
| 92 | + self.metrics[name].refresh() |
| 93 | + |
| 94 | + |
| 95 | +def test_reporter(): |
| 96 | + plt.ion() |
| 97 | + rep = Reporter(metrics=['reward', 'flubber'], live_plot=True) |
| 98 | + for i in xrange(100): |
| 99 | + rep.collect('reward', i, np.random.random()) |
| 100 | + if i % 10 == 1: |
| 101 | + rep.collect('flubber', i, np.random.random() * 2 + 1) |
| 102 | + rep.refresh_plot() |
| 103 | + time.sleep(0.01) |
| 104 | + rep.plot() |
| 105 | + summary = rep.summary() |
| 106 | + print "Summary ({} metrics):-".format(len(summary)) |
| 107 | + for metric in summary: |
| 108 | + print "Name: {}, samples: {}, type: {}".format(metric.name, len(metric), metric.dtype) |
| 109 | + print "Mean: {}, s.d.: {}".format(metric.mean(), metric.std()) |
| 110 | + #print metric[:5] # [debug] |
| 111 | + plt.ioff() |
| 112 | + plt.show() |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == '__main__': |
| 116 | + test_reporter() |
0 commit comments