forked from CORE-Robotics-Lab/MAGIC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
149 lines (120 loc) · 4.82 KB
/
plot.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np
import sys
import glob
plt.rcParams.update({'font.size': 20})
colors_map = {
'IC3Net': '#fca503',
'CommNet': '#b0b0b0',
'TarMAC-IC3Net': '#b700ff',
'GA-Comm': '#77ab3f',
'MAGIC (Our Approach)': '#0040ff',
'MAGIC w/o the Scheduler': '#ff6373'
}
def read_file(vec, file_name, 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 file_name == 'log_files/tj_medium/commnet_tj_medium_no_cur_run1.log':
epoch -= 4000
floats = line.split('\t')[0]
left_bracket = floats.find('[')
right_bracket = floats.find(']')
if left_bracket == -1 and left_bracket == -1:
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()))
else:
floats = np.fromstring(floats[left_bracket + 1:right_bracket], dtype=float, sep=' ')
if epoch > len(vec):
vec.append([floats.mean()])
else:
vec[epoch - 1].append(floats.mean())
return vec
def parse_plot(files, term='Reward'):
coll = dict()
episode_coll = dict()
for fname in files:
f = fname.split('.')
if 'ic3net' in fname and not 'tar' in fname:
label = 'IC3Net'
elif 'commnet' in fname:
label = 'CommNet'
elif 'tar_ic3net' in fname:
label = 'TarMAC-IC3Net'
elif 'gacomm' in fname:
label = 'GA-Comm'
elif 'gcomm' in fname and not 'complete' in fname:
label = 'MAGIC (Our Approach)'
elif 'gcomm' in fname and 'complete' in fname:
label = 'MAGIC w/o the Scheduler'
if label not in coll:
coll[label] = []
episode_coll[label] = []
if 'ic3net_pp_hard' in fname and not 'tar' in fname and term == 'Steps-Taken':
term = 'Steps-taken'
coll[label] = read_file(coll[label], fname, term)
episode_coll[label] = read_file(episode_coll[label], fname, 'Episode')
if 'ic3net_pp_hard' in fname and not 'tar' in fname and term == 'Steps-taken':
term = 'Steps-Taken'
for label in coll.keys():
coll[label] = coll[label][:1000]
episode_coll[label] = episode_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.std(val)/(np.sqrt(len(val)))
if term == 'Success':
variance *= 100
variance = variance if variance < 20 else 20
max_values.append(mean + variance)
min_values.append(mean - variance)
mean_episodes = []
for epi_val in episode_coll[label]:
mean_episodes.append(sum(epi_val) / len(epi_val))
print(label)
print('max: ', np.max(mean_values))
print('min: ', np.min(mean_values))
max_idx = np.argmax(mean_values)
min_idx = np.argmin(mean_values)
print('max std: ', np.std(coll[label][max_idx]))
print('min std: ', np.std(coll[label][min_idx]))
plt.plot(np.arange(len(coll[label])), mean_values, linewidth=2.0, label=label, color=colors_map[label])
plt.fill_between(np.arange(len(coll[label])), min_values, max_values, color=colors.to_rgba(colors_map[label], alpha=0.2))
# plt.plot(mean_episodes, mean_values, linewidth=1.5, label=label, color=colors_map[label])
# plt.fill_between(mean_episodes, min_values, max_values, color=colors.to_rgba(colors_map[label], alpha=0.2))
plt.xlabel('Epochs')
if term == 'Success':
term = 'Success Rate (%)'
plt.ylabel(term)
# plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=3, mode="expand", borderaxespad=0.)
# plt.legend(framealpha=1)
plt.grid()
# plt.title('GFootball {} {}'.format(sys.argv[2], term))
files = glob.glob(sys.argv[1] + "*")
# filter out files with ".pt"
files = list(filter(lambda x: x.find(".pt") == -1, files))
# 'Epoch'/ 'Steps-taken'
term = sys.argv[3]
parse_plot(files, term)
plt.show()