-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
86 lines (70 loc) · 3.04 KB
/
utilities.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import numpy as np
class Report:
def __init__(self, agent=None):
if agent is not None:
self.agent = agent
self.plots = []
self.averages = {}
self.scores = []
self.wall_time = {}
@staticmethod
def from_dict(**kwargs):
import pandas as pd
report = []
if kwargs is not None:
for key, val in kwargs.items():
report.append((key, val))
return pd.DataFrame.from_items(report)
def add_plot(self, data, labels=('Episode', 'Score'), rolling_window=100):
import matplotlib.pyplot as plt
import pandas as pd
# plot the scores
fig = plt.figure()
ax = fig.add_subplot(111)
if labels is not None:
x_label, y_label = [label for label in labels]
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.plot([13 for x in range(0, len(data) + 1)], label='Goal')
plt.plot(np.arange(1, len(data) + 1, step=1), data, label='Episode')
rolling_mean = pd.Series(data).rolling(rolling_window).mean()
plt.plot(rolling_mean, label='Rolling 100 Episode Average')
plt.legend()
self.plots.append(plt)
def show_plots(self):
for plot in self.plots:
plot.show()
def run(self, exe, **kwargs):
self.scores, self.averages = exe(**dict(kwargs, agent=self.agent, log_time=self.wall_time, report=self))
self.show_plots()
return self
def __str__(self):
from tabulate import tabulate
import pandas as pd
import datetime
report = [('Agent', self.agent)]
avg_table = pd.DataFrame.from_records([(key, val) for key, val in self.averages.items()])
report += [('Averages', tabulate(avg_table, tablefmt='fancy_grid', headers=["Ep", "Score"], showindex='never'))]
wall_time_table = pd.DataFrame.from_records([(key, str(datetime.timedelta(seconds=val))) for key, val in self.wall_time.items()] + [('Total', datetime.timedelta(seconds=sum(self.wall_time.values())))])
report += [('Wall Time', tabulate(wall_time_table, tablefmt='fancy_grid', headers=["Fn", "Time"], showindex='never'))]
report += [('Episodes', len(self.scores))]
report_table = pd.DataFrame.from_records(report)
return tabulate(report_table, tablefmt='fancy_grid', showindex='never')
def timeit(method):
def timed(*args, **kw):
import datetime
from timeit import default_timer as timer
t_0 = timer()
result = method(*args, **kw)
t_end = timer()
seconds = datetime.timedelta(seconds=t_end - t_0)
if 'log_time' in kw:
name = kw.get('log_name', method.__name__.upper())
print("\n\nTimeDelta: {}".format(str(t_end - t_0)))
print("Seconds : {}".format(str(seconds)))
kw['log_time'][name] = t_end - t_0
else:
print('\n\nExecution Time')
print('\r{}\t{}'.format(method.__name__, seconds))
return result
return timed