-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw.py
83 lines (72 loc) · 2.04 KB
/
draw.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
import os
import csv
from argparse import ArgumentParser
import numpy as np
from matplotlib import pyplot as plt
def smooth(arr, n):
end = -(len(arr)%n)
if end == 0:
end = None
arr = np.reshape(arr[:end], (-1, n))
arr = np.mean(arr, axis=1)
return arr
def drawall(name, x, metrics, labels, n=100, recent=0):
dir ='save_graph/%s/' % name
if not os.path.exists(dir):
os.makedirs(dir)
x = smooth(x[-recent:], n)
for i, metric in enumerate(metrics):
metrics[i] = smooth(metric[-recent:], n)
def draw(x, y, ylabel):
plt.figure(figsize=(15,5))
plt.plot(x, y)
plt.xlabel('episode')
plt.ylabel(ylabel)
plt.savefig(dir+'/'+ylabel)
plt.clf()
for i, metric in enumerate(metrics):
draw(x, metric, labels[i])
if __name__ == '__main__':
root_dir = 'save_graph/'
parser = ArgumentParser()
parser.add_argument('--n', type=int, default=50)
parser.add_argument('--recent', type=int, default=0)
args = parser.parse_args()
files= []
for f in os.listdir():
if f[-4:] == '.csv':
files.append(f[:-4])
for name in files:
filename = name + '.csv'
if not os.path.exists(filename):
continue
episodes = []
rewards = []
scores = []
timesteps = []
pmaxs = []
metrics = [
rewards,
scores,
timesteps,
pmaxs,
]
labels = [
'reward_sum',
'score',
'timestep',
'pmax'
]
try:
with open(filename, 'r') as f:
read = csv.reader(f)
for i, row in enumerate(read):
episodes.append(i)
rewards.append(float(row[0]))
scores.append(int(float(row[1])))
timesteps.append(int(float(row[2])))
pmaxs.append(float(row[3]))
drawall(name, episodes, metrics, labels, n=args.n, recent=args.recent)
print(name, sum(timesteps), max(scores))
except Exception as e:
print(str(e))