-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotting.py
More file actions
39 lines (34 loc) · 1.38 KB
/
plotting.py
File metadata and controls
39 lines (34 loc) · 1.38 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
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import statistics
import os, torch
import numpy as np
#plt.style.use(['science', 'ieee'])
#plt.rcParams["text.usetex"] = False
#plt.rcParams['figure.figsize'] = 6, 2
os.makedirs('plots', exist_ok=True)
def smooth(y, box_pts=1):
box = np.ones(box_pts)/box_pts
y_smooth = np.convolve(y, box, mode='same')
return y_smooth
def plotter(name, x_train, x_recons, ascore, labels):
os.makedirs(os.path.join('plots', name), exist_ok=True)
pdf = PdfPages(f'plots/{name}/output.pdf')
for dim in range(x_train.shape[1]):
y_t, y_p, l, a_s = x_train[:, dim], x_recons[:, dim], labels[:], ascore[:, dim]
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)
ax1.set_ylabel('Value')
ax1.set_title(f'Dimension = {dim}')
# if dim == 0: np.save(f'true{dim}.npy', y_t); np.save(f'pred{dim}.npy', y_p); np.save(f'ascore{dim}.npy', a_s)
ax1.plot(smooth(y_t), linewidth=0.2, label='True')
ax1.plot(smooth(y_p), '-', alpha=0.6, linewidth=0.3, label='Predicted')
ax3 = ax1.twinx()
ax3.plot(l, '--', linewidth=0.3, alpha=0.5)
ax3.fill_between(np.arange(l.shape[0]), l, color='blue', alpha=0.3)
if dim == 0: ax1.legend(ncol=2, bbox_to_anchor=(0.6, 1.02))
ax2.plot(smooth(a_s), linewidth=0.2, color='g')
ax2.set_xlabel('Timestamp')
ax2.set_ylabel('Anomaly Score')
pdf.savefig(fig)
plt.close()
pdf.close()