-
Notifications
You must be signed in to change notification settings - Fork 6
/
logger.py
53 lines (44 loc) · 1.41 KB
/
logger.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
import os
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
class Logger:
def __init__(self, log_file, names=None, delimiter='\t'):
assert log_file is not None
if names is None:
names = []
self.log_file = log_file
self.names = names
self.delim = delimiter
self.fields = len(names)
header = self._gather_values(self.names, prefix='#')
if os.path.isfile(log_file):
mode = 'a'
print('Warning: Log file already exisits')
else:
mode = 'w'
with open(log_file, mode) as f:
f.write(header + '\n')
def _gather_values(self, vals, prefix=''):
output = ''
for value in vals:
output = output + self.delim + str(value)
output = prefix + output
return output
def add(self, vals):
assert len(vals) == self.fields
output = self._gather_values(vals)
with open(self.log_file, 'a') as f:
f.write(output + '\n')
def plot(self):
data = np.loadtxt(self.log_file, skiprows=1)
plt.clf()
p = plt.plot(data)
plt.legend(p, self.names)
plt.grid()
plt.savefig(self.log_file+'.png', format='png')
if __name__ == '__main__':
l = Logger('test.log', names=['a', 'b', 'c'])
for i in range(4):
l.add(['a', 'b', 'c'])