-
Notifications
You must be signed in to change notification settings - Fork 5
/
plotting_tools.py
348 lines (302 loc) · 16.5 KB
/
plotting_tools.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import os
import math
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from matplotlib.animation import FuncAnimation
sns.set()
color_codes = {'1': 'brown', '2': 'darkred', '3': 'red', '4': 'salmon', '5': 'orangered', '6': 'sienna',
'7': 'saddlebrown', '8': 'sandybrown', '9': 'peru', '10': 'darkorange', '11': 'orange',
'12': 'goldenrod', '13': 'gold', '14': 'khaki', '15': 'darkkhaki', '16': 'olive', '17': 'yellow',
'18': 'yellowgreen', '19': 'chartreuse', '20': 'lightgreen', '21': 'darkgreen', '22': 'lime',
'23': 'springgreen', '24': 'turquoise', '25': 'darkslategrey', '26': 'cyan', '27': 'lightblue',
'28': 'deepskyblue', '29': 'steelblue', '30': 'dodgerblue', '31': 'slategrey', '32': 'royalblue',
'33': 'navy', '34': 'blue', '35': 'indigo', '36': 'darkviolet', '37': 'plum', '38': 'magenta',
'39': 'hotpink', '40': 'pink'}
def plot_methods_roc(all_results, dir, env_name, method, gvd_names, gamma, target_type, bootstrap, is_recurrent=False, horizon=0):
num_distinct_gvds = len(gvd_names)
fig, axs = plt.subplots(math.ceil(num_distinct_gvds / 2), 2, figsize=(10, 14))
r, c = 0, 0
for key in all_results.keys():
if key.__contains__("cartlocation") or key.__contains__("cos1") or key.__contains__("posx"):
axs[0, 0].plot(all_results[key][0], all_results[key][1],
label="H:" + key.split("_")[-1] + " - AUC:" + str(round(all_results[key][3], 2)),
color=color_codes[key.split("_")[-1]])
elif key.__contains__("cartvelocity") or key.__contains__("sin1") or key.__contains__("posy"):
axs[0, 1].plot(all_results[key][0], all_results[key][1],
label="H:" + key.split("_")[-1] + " - AUC:" + str(round(all_results[key][3], 2)),
color=color_codes[key.split("_")[-1]])
elif key.__contains__("polelocation") or key.__contains__("cos2") or key.__contains__("velocityx"):
axs[1, 0].plot(all_results[key][0], all_results[key][1],
label="H:" + key.split("_")[-1] + " - AUC:" + str(round(all_results[key][3], 2)),
color=color_codes[key.split("_")[-1]])
elif key.__contains__("polevelocity") or key.__contains__("sin2") or key.__contains__("velocityy"):
axs[1, 1].plot(all_results[key][0], all_results[key][1],
label="H:" + key.split("_")[-1] + " - AUC:" + str(round(all_results[key][3], 2)),
color=color_codes[key.split("_")[-1]])
elif key.__contains__("velocity1") or key.__contains__("angle"):
axs[2, 0].plot(all_results[key][0], all_results[key][1],
label="H:" + key.split("_")[-1] + " - AUC:" + str(round(all_results[key][3], 2)),
color=color_codes[key.split("_")[-1]])
elif key.__contains__("velocity2") or key.__contains__("angvelocity"):
axs[2, 1].plot(all_results[key][0], all_results[key][1],
label="H:" + key.split("_")[-1] + " - AUC:" + str(round(all_results[key][3], 2)),
color=color_codes[key.split("_")[-1]])
elif key.__contains__("leftleg"):
axs[3, 0].plot(all_results[key][0], all_results[key][1],
label="H:" + key.split("_")[-1] + " - AUC:" + str(round(all_results[key][3], 2)),
color=color_codes[key.split("_")[-1]])
elif key.__contains__("rightleg"):
axs[3, 1].plot(all_results[key][0], all_results[key][1],
label="H:" + key.split("_")[-1] + " - AUC:" + str(round(all_results[key][3], 2)),
color=color_codes[key.split("_")[-1]])
for i in range(num_distinct_gvds):
axs[r, c].plot(np.arange(2), np.arange(2), label="Random", color='purple')
axs[r, c].set(xlabel='FPR', ylabel='TPR')
axs[r, c].set_title("GVD: " + gvd_names[i].split("_")[0])
axs[r, c].legend(loc='upper center', bbox_to_anchor=(0.5, -0.3), ncol=2)
if r < math.ceil(num_distinct_gvds / 2) - 1:
r += 1
else:
c += 1
r = 0
if not is_recurrent:
fig.suptitle("ROC Curve\ngamma: " + str(gamma) + "\npr type: " + target_type)
fig.tight_layout()
fig.savefig(os.path.join(dir, "ROC_AUC_" + method + "_" + env_name + "_" + target_type + "_gamma_" + str(gamma) + ".png"))
else:
if bootstrap:
fig.suptitle("ROC Curve\nhorizon: " + str(horizon) + "\npr type: " + target_type + " - bootstrapping")
fig.tight_layout()
fig.savefig(os.path.join(dir, "ROC_AUC_" + method + "_" + env_name + "_" + target_type + "_h_" + str(horizon) + "_bootstrap.png"))
else:
fig.suptitle("ROC Curve\nhorizon: " + str(horizon) + "\npr type: " + target_type)
fig.tight_layout()
fig.savefig(os.path.join(dir, "ROC_AUC_" + method + "_" + env_name + "_" + target_type + "_h_" + str(horizon) + ".png"))
# fig.show()
plt.clf()
plt.cla()
plt.close()
print("ROC AUC plot saved in", dir)
def plot_combined_roc(results, dir, env_name, method, gamma, target_type, bootstrap, merging_method, is_recurrent=False, horizon=0):
fig, axs = plt.subplots(figsize=(4, 5))
for key in results.keys():
axs.plot(results[key][0], results[key][1],
label="H:" + key.split("_")[-1] + " - AUC:" + str(round(results[key][3], 2)),
color=color_codes[key.split("_")[-1]])
for i in range(len(results)):
if i == 0:
axs.plot(np.arange(2), np.arange(2), label="Random", color='purple')
axs.set(xlabel='FPR', ylabel='TPR')
axs.legend(loc='upper center', bbox_to_anchor=(0.5, -0.3), ncol=2)
if not is_recurrent:
fig.suptitle("Combined ROC Curve\ngamma: " + str(gamma) + "\npr type: " + target_type)
fig.tight_layout()
fig.savefig(os.path.join(dir, "Combined_ROC_AUC_" + method + "_" + env_name + "_" + target_type + "_gamma_" + str(gamma) + ".png"))
else:
if bootstrap:
fig.suptitle("Combined ROC Curve\nhorizon: " + str(horizon) + "\npr type: " + target_type + " - bootstrapping" + "\ncombining method: " + merging_method)
fig.tight_layout()
fig.savefig(os.path.join(dir, "Combined_ROC_AUC_" + method + "_" + env_name + "_" + target_type + "_h_" + str(horizon) + "_" + merging_method + "_bootstrap.png"))
else:
fig.suptitle("Combined ROC Curve\nhorizon: " + str(horizon) + "\npr type: " + target_type + "\ncombining method: " + merging_method)
fig.tight_layout()
fig.savefig(os.path.join(dir, "Combined_ROC_AUC_" + method + "_" + env_name + "_" + target_type + "_h_" + str(horizon) + "_" + merging_method + ".png"))
# fig.show()
plt.clf()
plt.cla()
plt.close()
print("Combined ROC AUC plot saved in", dir)
def aucs_progress(all_results, dir, env_name, method, gamma):
for key, value in all_results.items():
plt.plot(value[-1], label=key)
plt.xlabel("Horizon")
plt.ylabel("AUC")
plt.title("AUCs progress\ngamma: " + str(gamma))
plt.legend()
plt.savefig(os.path.join(dir, "AUC_progress_" + method + "_" + env_name + "_gamma_" + str(gamma) + ".png"))
plt.clf()
plt.cla()
plt.close()
def update_plot(i, ax, value_dist, min_val, max_val):
label = 'step {0}'.format(i)
ax.clear()
ax.set_title(label)
ax.set_xlabel('Returns')
ax.set_ylabel('Actions')
plt.axis([min_val, max_val, -0.5, 1.5])
ax.yaxis.set_major_locator(MaxNLocator(integer=True))
ax.scatter(value_dist[i][0], ["left"] * 32)
# ax.scatter(value_dist[i][1], ["right"] * 32)
def plot_dist(value_dist, save_path):
for ep in range(len(value_dist)):
min_val = math.floor(value_dist[ep].min())
max_val = math.ceil(value_dist[ep].max())
fig, ax = plt.subplots(figsize=(8, 2))
fig.set_tight_layout(True)
anim = FuncAnimation(fig, update_plot, frames=np.arange(0, value_dist[ep].shape[0]), fargs=(ax, value_dist[ep], min_val, max_val), interval=100)
anim.save(save_path + '/q_dist_ep' + str(ep) + '.gif', writer='imagemagick')
plt.close(fig)
fig, ax = plt.subplots()
ax.plot(np.mean(value_dist[ep][:,0,:], axis=1), label="Left")
ax.set_xlabel("Steps")
ax.set_ylabel("Mean return")
# ax.plot(np.mean(value_dist[ep][:,1,:], axis=1), label="Right")
ax.legend()
plt.savefig(save_path + "/mean_returns_ep" + str(ep) + ".png")
plt.close(fig)
def plot_histogram_array(data, label):
x = data.numpy()
y = np.zeros(data.shape[0])
# definitions for the axes
left, width = 0.1, 0.65
bottom, height = 0.1, 0.25
spacing = 0.005
rect_scatter = [left, bottom, width, height]
rect_histx = [left, bottom + height + spacing, width, 0.2]
# start with a rectangular Figure
plt.figure(figsize=(8, 8))
ax_scatter = plt.axes(rect_scatter)
ax_scatter.tick_params(direction='in', top=True, right=True)
ax_histx = plt.axes(rect_histx)
ax_histx.tick_params(direction='in', labelbottom=False)
# the scatter plot:
ax_scatter.scatter(x, y, label=label, color='b')
# now determine nice limits by hand:
binwidth = 0.25
lim = np.ceil(np.abs([x, y]).max() / binwidth) * binwidth
ax_scatter.set_xlim((-lim, lim))
ax_scatter.set_ylim((-1, 1))
ax_scatter.set_xlabel("values")
ax_scatter.legend()
bins = np.arange(-lim, lim + binwidth, binwidth)
ax_histx.hist(x, bins=bins, color='b')
ax_histx.set_xlim(ax_scatter.get_xlim())
ax_histx.set_ylabel("count")
plt.show()
def plot_scores(scores, anomaly_appeared, dir, env_name, method, gamma, horizons, bootstrapped, is_recurrent, target_type):
for key, value in anomaly_appeared.items():
plt.axvline(x=value.index(1), label="Anomaly started (H={})".format(key))
counter = 1
summed_value = np.zeros(len(scores[next(iter(scores))][0]))
for key, value in scores.items():
summed_value += np.array(value[0])
plt.plot(value[0], color_codes[str(counter)], label=key.split("_")[0] + " - H=" + key.split("_")[-1])
counter += 7
plt.plot(summed_value, color_codes[str(counter)], label="Summation")
# counter = 0
# for key, value in scores.items():
# smoothed_value = array_smoothie(value[0], 0.9)
# plt.plot(smoothed_value, color_codes[str(counter + 1)], label=key.split("_")[0])
# counter += 2
plt.xlabel("Step")
plt.ylabel("Anomaly score")
if not is_recurrent:
plt.title("Anomaly score\ngamma: " + str(gamma))
plt.legend()
plt.savefig(os.path.join(dir, "Anomaly_scores_" + method + "_" + env_name + "_gamma_" + str(gamma) + ".png"))
else:
if not bootstrapped:
plt.title("Anomaly score\nhorizon: " + str(horizons) + "\npr type: " + target_type)
plt.legend()
plt.savefig(os.path.join(dir, "Anomaly_scores_" + method + "_" + env_name + "_" + target_type + "_h_" + str(horizons) + ".png"))
else:
plt.title("Anomaly score\nhorizon: " + str(horizons) + "\npr type: " + target_type + " - bootstrapping")
plt.legend()
plt.savefig(os.path.join(dir, "Anomaly_scores_" + method + "_" + env_name + "_" + target_type + "_h_" + str(horizons) + "_bootstrap.png"))
plt.clf()
plt.cla()
plt.close()
def array_smoothie(scalars, weight): # 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 smoothed
def plot_heatmaps(all_aucs, gvd_names, horizons, gammas, dir, env_name, ad_algo, anomaly_type):
x_axis_labels = horizons
y_axis_labels = gammas
fig, axs = plt.subplots(2, 2, figsize=(10, 10))
cartlocations, cartvelocities, polelocations, polevelocities = all_aucs
# cartlocations: (cls0, cls1, cls2, cls3, cls4, cls5, cls6, cls7, cls8, cls9)
sns.heatmap(cartlocations, cbar=False, ax=axs[0,0], xticklabels=x_axis_labels, yticklabels=y_axis_labels, linewidth=0.15, cmap="YlGnBu", vmin=0, vmax=1)
sns.heatmap(cartvelocities, cbar=False, ax=axs[0,1], xticklabels=x_axis_labels, yticklabels=y_axis_labels, linewidth=0.15, cmap="YlGnBu", vmin=0, vmax=1)
sns.heatmap(polelocations, cbar=False, ax=axs[1,0], xticklabels=x_axis_labels, yticklabels=y_axis_labels, linewidth=0.15, cmap="YlGnBu", vmin=0, vmax=1)
sns.heatmap(polevelocities, cbar=False, ax=axs[1,1], xticklabels=x_axis_labels, yticklabels=y_axis_labels, linewidth=0.15, cmap="YlGnBu", vmin=0, vmax=1)
r, c = 0, 0
for i in range(len(all_aucs)):
axs[r, c].set_xlabel("Horizon")
axs[r, c].set_ylabel("Gamma")
axs[r, c].invert_yaxis()
axs[r, c].set_title("GVD: " + gvd_names[i])
if r < 2 - 1:
r += 1
else:
c += 1
r = 0
im = plt.gca().get_children()[0]
cax = fig.add_axes([0.93, 0.3, 0.02, 0.5])
fig.colorbar(im, cax=cax)
fig.subplots_adjust(hspace=.3)
plt.suptitle("Horizon and gamma effects on AUC (AD: KNN)")
plt.show()
plt.savefig(os.path.join(dir, "heatmaps_" + env_name + "_H_lambda_" + ad_algo + "_" + anomaly_type + "anomaly" + ".png"))
def plot_losses(train_loss, test_loss, result_folder, horizon, gvd_name, info, bootstrapped):
plt.plot(train_loss, label="training loss")
plt.plot(test_loss, label="test loss")
plt.legend()
if not bootstrapped:
plt.savefig(os.path.join(result_folder, "losses_" + gvd_name + "_" + info + "_h" + str(horizon) + ".png"))
else:
plt.savefig(os.path.join(result_folder, "losses_" + gvd_name + "_" + info + "_h" + str(horizon) + "_bootstrap.png"))
plt.clf()
def rgvd_accuracy(results, result_folder, horizon, info, bootstrapped):
fig, axs = plt.subplots(int(len(results) / 2), 2, figsize=(12, 10))
r, c = 0, 0
for key in results.keys():
axs[r, c].plot(results[key][1], color='limegreen')
axs[r, c].plot(results[key][0], color='teal')
axs[r, c].set(xlabel='step', ylabel='return')
axs[r, c].set_title("GVD: " + key.split("_")[0])
labels = ["actual MC returns", "rGVD returns"]
axs[r, c].legend(labels=labels, loc='upper right', labelcolor=['teal', 'limegreen'], handlelength=0)
if r < math.ceil(len(results) / 2) - 1:
r += 1
else:
c += 1
r = 0
# fig.show()
if not bootstrapped:
fig.suptitle("Recurrent GVD accuracy\nhorizon: " + str(horizon))
fig.tight_layout()
fig.savefig(os.path.join(result_folder, "rGVD_accuracy_" + info + "_h" + str(horizon) + ".png"))
else:
fig.suptitle("Recurrent GVD accuracy\nhorizon: " + str(horizon) + "\nbootstrapped")
fig.tight_layout()
fig.savefig(os.path.join(result_folder, "rGVD_accuracy_" + info + "_h" + str(horizon) + "_bootstrap.png"))
def plot_online_anomalies(scores, real_anomaly, R, horizons, result_folder, method, env_name, target_type, bootstrap, Nw=10):
fig, axs = plt.subplots(len(horizons) + 1, figsize=(12, 10))
for h in horizons:
axs[0].plot(scores, color='red', label='anomaly scores')
axs[0].set_ylabel("Anomaly scores")
actual_ep_len = len(R[Nw, Nw+1:])
axs[1].plot(real_anomaly[str(h)][:actual_ep_len], color='limegreen', label='real anomalies in env')
axs[1].plot(R[Nw, Nw+1:], label='detected changepoints (bocd)')
axs[1].set_xlabel("Steps")
axs[1].set_ylabel("Changepoint probability")
if bootstrap:
fig.suptitle("Online Anomaly Detection\nhorizon: " + str(horizons) + "\npr type: " + target_type + " - bootstrapping")
fig.legend()
fig.tight_layout()
fig.savefig(os.path.join(result_folder, "Online_AD_" + method + "_" + env_name + "_" + target_type + "_h_" +
str(horizons) + "_bootstrap.png"))
else:
fig.suptitle("Online Anomaly Detection\nhorizon: " + str(horizons) + "\npr type: " + target_type)
fig.legend()
fig.tight_layout()
fig.savefig(os.path.join(result_folder, "Online_AD_" + method + "_" + env_name + "_" + target_type + "_h_" +
str(horizons) + ".png"))