-
Notifications
You must be signed in to change notification settings - Fork 2
/
csv_logger.py
65 lines (53 loc) · 1.75 KB
/
csv_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
54
55
56
57
58
59
60
61
62
63
64
import csv
import matplotlib.pyplot as plt
import os
import ipdb
import itertools
class CSVLogger():
def __init__(self, every, fieldnames, resume=True, filename='log.csv'):
self.every = every
self.filename = filename
resume = os.path.exists(self.filename) and resume# resume
if resume:
self.csv_file = open(filename, 'a')
else:
self.csv_file = open(filename, 'w')
self.fieldnames=fieldnames
self.writer = csv.DictWriter(self.csv_file, fieldnames=fieldnames)
if not resume:
self.writer.writeheader()
self.csv_file.flush()
self.time = 0
def is_time(self):
return self.time%self.every==0
def writerow(self, row):
self.writer.writerow(row)
self.csv_file.flush()
def close(self):
self.csv_file.close()
def plot_csv(csv_path, fig_path):
with open(csv_path, 'r') as f:
reader = csv.reader(f)
dict_of_lists = {}
ks = None
for i, r in enumerate(reader):
if i == 0:
for k in r:
dict_of_lists[k] = []
ks = r
else:
for _i, v in enumerate(r):
if v == '':
break
dict_of_lists[ks[_i]].append(float(v))
fig = plt.figure()
for k in dict_of_lists:
if k == 'global_iteration' or len(dict_of_lists[k])==0:
continue
plt.clf()
plt.plot(dict_of_lists['global_iteration'], dict_of_lists[k])
plt.title(k)
plt.grid()
plt.tight_layout()
plt.savefig(os.path.join(os.path.dirname(fig_path), f'_{k}.png'), bbox_inches='tight', pad_inches=0)
plt.close(fig)