-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_script.py
129 lines (105 loc) · 3.72 KB
/
plot_script.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np
import sys
import glob
colors_map = {
'semi-commnet': '#dc8d6d',
'commnet': '#5785c1',
'semi-mlp': '#78b38a',
'mlp': '#ba71af'
}
def read_file(vec, file_name, scalar, term):
print(file_name)
with open(file_name, 'r') as f:
lines = f.readlines()
if len(lines) < 2:
return vec
mean_reward = False
for idx, line in enumerate(lines):
if term not in line:
continue
epoch_idx = idx
epoch_line = line
while 'Epoch' not in epoch_line:
epoch_idx -= 1
epoch_line = lines[epoch_idx]
epoch = int(epoch_line.split(' ')[1].split('\t')[0])
if not scalar:
floats = line.split('\t')[1]
left_bracket = floats.find('[')
right_bracket = floats.find(']')
floats = np.fromstring(floats[left_bracket + 2:right_bracket], dtype=float, sep=' ')
if epoch > len(vec):
vec.append([floats.mean()])
else:
vec[epoch - 1].append(floats.mean())
else:
floats = line.split('\t')[0]
if epoch > len(vec):
vec.append([float(floats.split(' ')[-1].strip())])
else:
vec[epoch - 1].append(float(floats.split(' ')[-1].strip()))
# if term == 'Mean':
# vec.append(float(floats.split(' ')[2]))
# elif term == 'Success':
# vec.append(float(floats.split(' ')[1].strip()))
# else:
# raise RuntimeError("Unkown term used as line parser.")
return vec
number = 1
def parse_plot(files, scalar=False, term='Epoch'):
global number
fig = plt.subplot(6, 1, number)
number += 2
coll = dict()
for fname in files:
f = fname.split('.')
if 'semi' in fname and 'commnet' in fname:
label = 'semi-commnet'
elif 'commnet' in fname:
label = 'commnet'
elif 'semi' in fname and 'mlp' in fname:
label = 'semi-mlp'
else:
label = 'mlp'
if label not in coll:
coll[label] = []
# if len(f) == 2:
# label = f[0]
# else:
# label = '-'.join(f[0].split('_')[1:])
coll[label] = read_file(coll[label], fname, scalar, term)
for label in coll.keys():
coll[label] = coll[label][:1000]
mean_values = []
max_values = []
min_values = []
for val in coll[label]:
mean = sum(val) / len(val)
if term == 'Success':
mean *= 100
mean_values.append(mean)
variance = np.var(val)
if term == 'Success':
variance *= 100
variance = variance if variance < 20 else 20
max_values.append(mean + variance)
min_values.append(mean - variance)
fig.plot(np.arange(len(coll[label])), mean_values, linewidth=1.5, label=label, color=colors_map[label])
fig.fill_between(np.arange(len(coll[label])), min_values, max_values, color=colors.to_rgba(colors_map[label], alpha=0.2))
term = 'Rewards' if term == 'Epoch' else term
fig.set_xlabel('Epochs')
fig.set_ylabel(term)
fig.legend()
fig.grid()
fig.set_title('StarCraft {} {}'.format(sys.argv[2], term))
files = glob.glob(sys.argv[1] + "*")
files = list(filter(lambda x: x.find(".pt") == -1, files))
# rewards
parse_plot(files, False, 'Epoch')
# success
parse_plot(files, True, 'Success')
# steps
parse_plot(files, True, 'Steps taken')
plt.show()