-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_analysis.py
More file actions
92 lines (78 loc) · 3.11 KB
/
plot_analysis.py
File metadata and controls
92 lines (78 loc) · 3.11 KB
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
import os
import json
import pickle
import numpy as np
import matplotlib.pyplot as plt
import argparse
def addDateTime(s = ""):
"""
Adds the current date and time at the end of a string.
Inputs:
s -> string
Output:
S = s_Dyymmdd_HHMM
"""
import datetime
date = str(datetime.datetime.now())
date = date[2:4] + date[5:7] + date[8:10] + '_' + date[11:13] + date[14:16] + date[17:19]
return s + '_D' + date
def append_or_create_list_for_key(dict, key, ele):
if key in dict:
dict[key].append(ele)
else:
dict[key] = [ele]
def smooth(scalars, weight=0.5): # Weight between 0 and 1
last = scalars[0] # First value in the plot (first timestep)
smoothed = list()
for point in scalars:
smoothed_val = last * weight + (1 - weight) * point # Calculate smoothed value
smoothed.append(smoothed_val) # Save it
last = smoothed_val # Anchor the last smoothed value
return np.array(smoothed)
def running_avg(list_to_avg, avg_steps=100):
array_to_avg = np.asarray(list_to_avg)
array_to_avg = array_to_avg.reshape(array_to_avg.shape[0], -1)
array_cum_sum = np.copy(array_to_avg)
for i in range(1, array_to_avg.shape[1]):
array_cum_sum[:, i] = array_cum_sum[:, i - 1] + array_to_avg[:, i]
array_avged = (array_cum_sum[:, avg_steps:] - array_cum_sum[:, :-avg_steps]) / avg_steps
# array_avged = smooth(array_avged)
return array_avged
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--parent_dir', default="pitfall_ppo2_rl_justin")
args = parser.parse_args()
rl_avg_steps = 1000
metrics_to_plot = ["reward"]
parent_dir = os.path.join("results", args.parent_dir)
plot_dir = os.path.join(parent_dir, "plots")
plot_dir = addDateTime(plot_dir)
if not os.path.exists(plot_dir):
os.mkdir(plot_dir)
performance_dict = {}
dirname = parent_dir
performance_file = os.path.join(dirname, "performance.p")
model_dir = os.path.join(dirname, "models")
if not os.path.exists(performance_file):
print(performance_file)
with open(performance_file, "rb") as f:
performance = pickle.load(f)
append_or_create_list_for_key(performance_dict, "rl_baseline", performance)
for metric in metrics_to_plot:
plt.figure(figsize=(8, 6))
for key, val_old in performance_dict.items():
val = running_avg([ele[metric] for ele in val_old], rl_avg_steps)
val_mean = val.mean(axis=0)
val_std = val.std(axis=0)
line, = plt.plot(val_mean, label=key)
plt.fill_between(range(0, len(val_mean)),
val_mean + val_std, val_mean - val_std, alpha=0.2)
plt.legend()
plt.ylabel(metric)
plt.xlabel("steps")
# plt.xlim((0, len(val_mean)))
plt.xlim((0, len(val_mean)))
plt.minorticks_on()
plt.grid(True, which="both", alpha=.2)
plt.savefig(os.path.join(plot_dir, "{}.png".format(metric)))
plt.show()