-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_results.py
102 lines (71 loc) · 2.75 KB
/
plot_results.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
import pandas as pd
import os
import json
import matplotlib.pyplot as plt
import collections
import argparse
from tqdm import tqdm
parser = argparse.ArgumentParser(description='Plot results from different trainings. You should use as input a folder in which multiple dirs store "result.json" and "parameter_attributes.json"')
parser.add_argument('-input_dir', type=str, required=True,
help='The direcotry in which the training folders are stored')
parser.add_argument('--out_dir',type=str,default="out/",
help='Output dir to which save images')
args = parser.parse_args()
input_dir=args.input_dir
out_dir=os.path.join(input_dir, args.out_dir)
if not os.path.exists(out_dir):
os.mkdir(out_dir)
def flatten(d, parent_key='', sep='/'):
items = []
for k, v in d.items():
new_key = parent_key + sep + k if parent_key else k
if isinstance(v, collections.MutableMapping):
items.extend(flatten(v, new_key, sep=sep).items())
else:
items.append((new_key, v))
return dict(items)
res_dict={}
# traverse root directory, and list directories as dirs and files as files
for root, dirs, files in os.walk(input_dir):
if "result.json" in files:
pt=os.path.join(root,"result.json")
with open(pt,"r+") as f:
d=f.readlines()
d=[json.loads(elem) for elem in d]
d=[flatten(elem) for elem in d]
d=pd.DataFrame(d)
pt = os.path.join(root, "parameter_attributes.json")
with open(pt,"r+") as f:
pa=json.load(f)
k=f"{pa['training_alg']}_coop{pa['coop_rl_vehicle_num']}_self{pa['selfish_rl_vehicle_num']}_human{pa['human_vehicle_num']}"
res_dict[k]=dict(
results=d,
params=pa,
)
to_plot=[
'Actions',
'Delays',
'Jerks',
"Rewards"
]
for field in tqdm(to_plot):
for k,v in res_dict.items():
# gca stands for 'get current axis'
plt.clf()
ax = plt.gca()
df=v["results"]
if f"custom_metrics/{field}/RL_coop_mean" in df.keys() :
df.reset_index().plot(kind='line', y=f"custom_metrics/{field}/RL_coop_mean",x="index", ax=ax)
if f"custom_metrics/{field}/all_mean" in df.keys() :
df.reset_index().plot(kind='line', y=f"custom_metrics/{field}/all_mean",x="index", ax=ax)
if f"custom_metrics/{field}/RL_selfish_mean" in df.keys() :
df.reset_index().plot(kind='line', y=f"custom_metrics/{field}/RL_selfish_mean", x="index", ax=ax)
ax.set_title(k)
ax.set_xlabel("generation")
ax.set_ylabel(field)
if "/" in k:
key=k.split("/")[-1]
else:
key=k
pt=os.path.join(out_dir,f"{key}_{field}")
plt.savefig(pt)