-
Notifications
You must be signed in to change notification settings - Fork 2
/
evaluation.py
303 lines (255 loc) · 13.5 KB
/
evaluation.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
import pickle
import pandas as pd
import numpy as np
from tqdm import tqdm
import argparse
from sklearn.metrics import auc
from sklearn.preprocessing import RobustScaler
from utils.data_loader import load_tods, load_PSM, load_SWaT, load_ASD, load_CompanyA, load_ECG
from utils.data_loader import normalization, _create_sequences
def _simulate_thresholds(rec_errors, n):
# maximum value of the anomaly score for all time steps in the test data
thresholds, step_size = [], abs(np.max(rec_errors) - np.min(rec_errors)) / n
th = np.min(rec_errors)
thresholds.append(th)
print(f'Threshold Range: ({np.min(rec_errors)}, {np.max(rec_errors)}) with Step Size: {step_size}')
for i in range(n):
th = th + step_size
thresholds.append(th)
return thresholds
parser = argparse.ArgumentParser(description='Settings for Daul-TF')
# Settings
parser.add_argument('--thresh_num', type=int, default=1000)
parser.add_argument('--seq_length', type=int, default=50)
parser.add_argument('--nest_length', type=int, default=10)
parser.add_argument('--step', type=int, default=1)
parser.add_argument('--dataset', type=str, default='NeurIPSTS')
parser.add_argument('--form', type=str, default='seasonal')
parser.add_argument('--data_loader', type=str, default='load_tods')
parser.add_argument('--data_num', type=int, default=0)
opts = parser.parse_args()
# check settings
if opts.dataset == 'NeurIPSTS':
print(f"Dataset: {opts.dataset}\nForm: {opts.form}\nSeq_length: {opts.seq_length}\nNest_length: {opts.nest_length}")
else:
print(f"Dataset: {opts.dataset}\nNum: {opts.data_num}\nSeq_length: {opts.seq_length}\nNest_length: {opts.nest_length}")
def total_evaluation():
# Time array
# df.index = ['Normal', 'Anomaly', '#Seq', 'Pred(%)', 'Pred', 'GT', 'Avg(RE)']
print('Time Arrays Loading...')
if opts.dataset == 'NeurIPSTS':
file_path = f'./time_arrays/{opts.dataset}_{opts.form}_time_evaluation_array.pkl'
else:
file_path = f'./time_arrays/{opts.dataset}_{opts.data_num}_time_evaluation_array.pkl'
time_array = pd.read_pickle(file_path)
print(time_array.shape)
print(time_array)
# Frequency array
# df.index = ['#SubSeq', '#GrandSeq', 'Avg(exp(RE))', 'Pred', 'GT']
print('Frequency Arrays Loading...')
if opts.dataset == 'NeurIPSTS':
file_path = f'./freq_arrays/{opts.dataset}_{opts.form}_freq_evaluation_array.pkl'
else:
file_path = f'./freq_arrays/{opts.dataset}_{opts.data_num}_freq_evaluation_array.pkl'
freq_array = pd.read_pickle(file_path)
print(freq_array.shape)
print(freq_array)
# Summation
time_rec = np.array(time_array.loc['Avg(RE)', :])
freq_rec = np.array(freq_array.loc['Avg(exp(RE))', :])
time_rec = time_rec.reshape(-1, 1)
freq_rec = freq_rec.reshape(-1, 1)
scaler = RobustScaler(unit_variance=True)
time_as = scaler.fit_transform(time_rec)
freq_as = scaler.transform(freq_rec)
time_as = normalization(time_rec)
freq_as = normalization(freq_rec)
# final_as = time_as + freq_as
final_as = time_as
# Point Adjusted Evaluation
pa_scores = {'dataset': [], 'f1': [], 'precision': [], 'recall': [], 'pr_auc': [], 'roc_auc': []}
print('##### Point Adjusted Evaluation #####')
thresholds = _simulate_thresholds(final_as, opts.thresh_num)
final_as_seq = _create_sequences(final_as, opts.seq_length, opts.step)
if opts.dataset == 'NeurIPSTS':
label = globals()[f'{opts.data_loader}'](opts.form)['label_seq']
else:
label = globals()[f'{opts.data_loader}']()['label_seq'][opts.data_num]
labels = _create_sequences(label, opts.seq_length, opts.step)
TP, TN, FP, FN = [], [], [], []
precision, recall, f1, fpr = [], [], [], []
for th in tqdm(thresholds): # for each threshold
TP_t, TN_t, FP_t, FN_t = 0, 0, 0, 0
for t in range(len(final_as_seq)): # for each sequence
# if any part of the segment has an anomaly, we consider it as anomalous sequence
true_anomalies, pred_anomalies = set(np.where(labels[t] == 1)[0]), set(np.where(final_as_seq[t] > th)[0])
if len(pred_anomalies) > 0 and len(pred_anomalies.intersection(true_anomalies)) > 0:
# correct prediction (at least partial overlap with true anomalies)
TP_t = TP_t + 1
elif len(pred_anomalies) == 0 and len(true_anomalies) == 0:
# correct rejection, no predicted anomaly on no true labels
TN_t = TN_t + 1
elif len(pred_anomalies) > 0 and len(true_anomalies) == 0:
# false alarm (i.e., predict anomalies on no true labels)
FP_t = FP_t + 1
elif len(pred_anomalies) == 0 and len(true_anomalies) > 0:
# predict no anomaly when there is at least one true anomaly within the seq.
FN_t = FN_t + 1
TP.append(TP_t)
TN.append(TN_t)
FP.append(FP_t)
FN.append(FN_t)
for i in range(len(thresholds)):
precision.append(TP[i] / (TP[i] + FP[i] + 1e-7))
recall.append(TP[i] / (TP[i] + FN[i] + 1e-7)) # recall or true positive rate (TPR)
fpr.append(FP[i] / (FP[i] + TN[i] + 1e-7))
f1.append(2 * (precision[i] * recall[i]) / (precision[i] + recall[i] + 1e-7))
highest_th_idx = np.argmax(f1)
print(f'Threshold: {thresholds[highest_th_idx]}')
print("Precision : {:0.4f}, Recall : {:0.4f}, F-score : {:0.4f} ".format(
precision[highest_th_idx], recall[highest_th_idx], f1[highest_th_idx]))
print("PR-AUC : {:0.4f}, ROC-AUC : {:0.4f}".format(auc(recall, precision), auc(fpr, recall)))
pa_scores['dataset'].append(f'ECG{opts.data_num+1}')
pa_scores['f1'].append(f1[highest_th_idx])
pa_scores['precision'].append(precision[highest_th_idx])
pa_scores['recall'].append(recall[highest_th_idx])
pa_scores['pr_auc'].append(auc(recall, precision))
pa_scores['roc_auc'].append(auc(fpr, recall))
results = pd.DataFrame(pa_scores)
print(results.groupby('dataset').mean())
# Point-Wise Evaluation
pw_scores = {'dataset': [], 'f1': [], 'precision': [], 'recall': [], 'pr_auc': [], 'roc_auc': []}
print('##### Point-Wise Evaluation #####')
TP, TN, FP, FN = [], [], [], []
precision, recall, f1, fpr = [], [], [], []
for th in tqdm(thresholds): # for each threshold
TP_t, TN_t, FP_t, FN_t = 0, 0, 0, 0
for ts in range(len(final_as)):
if label[ts] == 1:
if final_as[ts] >= th:
TP_t = TP_t + 1
elif final_as[ts] < th:
FN_t = FN_t + 1
elif label[ts] == 0:
if final_as[ts] >= th:
FP_t = FP_t + 1
elif final_as[ts] < th:
TN_t = TN_t + 1
TP.append(TP_t)
TN.append(TN_t)
FP.append(FP_t)
FN.append(FN_t)
for i in range(len(thresholds)):
precision.append(TP[i] / (TP[i] + FP[i] + 1e-8))
recall.append(TP[i] / (TP[i] + FN[i] + 1e-8)) # recall or true positive rate (TPR)
fpr.append(FP[i] / (FP[i] + TN[i] + 1e-8))
f1.append(2 * (precision[i] * recall[i]) / (precision[i] + recall[i] + 1e-8))
highest_th_idx = np.argmax(f1)
print(f'Threshold: {thresholds[highest_th_idx]}')
print("Precision : {:0.4f}, Recall : {:0.4f}, F-score : {:0.4f} ".format(
precision[highest_th_idx], recall[highest_th_idx], f1[highest_th_idx]))
print("PR-AUC : {:0.4f}, ROC-AUC : {:0.4f}".format(auc(recall, precision), auc(fpr, recall)))
pw_scores['dataset'].append(f'ECG{opts.data_num+1}')
pw_scores['f1'].append(f1[highest_th_idx])
pw_scores['precision'].append(precision[highest_th_idx])
pw_scores['recall'].append(recall[highest_th_idx])
pw_scores['pr_auc'].append(auc(recall, precision))
pw_scores['roc_auc'].append(auc(fpr, recall))
results = pd.DataFrame(pw_scores)
print(results.groupby('dataset').mean())
# Released Point-Wise Evaluation
print('##### Released Point-Wise Evaluation #####')
pr_scores = {'dataset': [], 'f1': [], 'precision': [], 'recall': [], 'pr_auc': [], 'roc_auc': []}
thresholds = _simulate_thresholds(final_as, opts.thresh_num)
final_as_seq = _create_sequences(final_as, opts.nest_length, opts.step)
labels = _create_sequences(label, opts.nest_length, opts.step)
TP, TN, FP, FN = [], [], [], []
precision, recall, f1, fpr = [], [], [], []
for th in tqdm(thresholds): # for each threshold
TP_t, TN_t, FP_t, FN_t = 0, 0, 0, 0
for t in range(len(final_as_seq)): # for each sequence
# if any part of the segment has an anomaly, we consider it as anomalous sequence
true_anomalies, pred_anomalies = set(np.where(labels[t] == 1)[0]), set(np.where(final_as_seq[t] > th)[0])
if len(pred_anomalies) > 0 and len(pred_anomalies.intersection(true_anomalies)) > 0:
# correct prediction (at least partial overlap with true anomalies)
TP_t = TP_t + 1
elif len(pred_anomalies) == 0 and len(true_anomalies) == 0:
# correct rejection, no predicted anomaly on no true labels
TN_t = TN_t + 1
elif len(pred_anomalies) > 0 and len(true_anomalies) == 0:
# false alarm (i.e., predict anomalies on no true labels)
FP_t = FP_t + 1
elif len(pred_anomalies) == 0 and len(true_anomalies) > 0:
# predict no anomaly when there is at least one true anomaly within the seq.
FN_t = FN_t + 1
TP.append(TP_t)
TN.append(TN_t)
FP.append(FP_t)
FN.append(FN_t)
for i in range(len(thresholds)):
precision.append(TP[i] / (TP[i] + FP[i] + 1e-7))
recall.append(TP[i] / (TP[i] + FN[i] + 1e-7)) # recall or true positive rate (TPR)
fpr.append(FP[i] / (FP[i] + TN[i] + 1e-7))
f1.append(2 * (precision[i] * recall[i]) / (precision[i] + recall[i] + 1e-7))
highest_th_idx = np.argmax(f1)
print(f'Threshold: {thresholds[highest_th_idx]}')
print("Precision : {:0.4f}, Recall : {:0.4f}, F-score : {:0.4f} ".format(
precision[highest_th_idx], recall[highest_th_idx], f1[highest_th_idx]))
print("PR-AUC : {:0.4f}, ROC-AUC : {:0.4f}".format(auc(recall, precision), auc(fpr, recall)))
pr_scores['dataset'].append(f'ECG{opts.data_num+1}')
pr_scores['f1'].append(f1[highest_th_idx])
pr_scores['precision'].append(precision[highest_th_idx])
pr_scores['recall'].append(recall[highest_th_idx])
pr_scores['pr_auc'].append(auc(recall, precision))
pr_scores['roc_auc'].append(auc(fpr, recall))
results = pd.DataFrame(pr_scores)
print(results.groupby('dataset').mean())
# Point-Wise Evaluation V2
print('##### Released Point-Wise Evaluation V2 #####')
prv2_scores = {'dataset': [], 'f1': [], 'precision': [], 'recall': [], 'pr_auc': [], 'roc_auc': []}
thresholds = _simulate_thresholds(final_as, opts.thresh_num)
final_as_seq = _create_sequences(final_as, opts.nest_length, opts.nest_length)
labels = _create_sequences(label, opts.nest_length, opts.nest_length)
TP, TN, FP, FN = [], [], [], []
precision, recall, f1, fpr = [], [], [], []
for th in tqdm(thresholds): # for each threshold
TP_t, TN_t, FP_t, FN_t = 0, 0, 0, 0
for t in range(len(final_as_seq)): # for each sequence
# if any part of the segment has an anomaly, we consider it as anomalous sequence
true_anomalies, pred_anomalies = set(np.where(labels[t] == 1)[0]), set(np.where(final_as_seq[t] > th)[0])
if len(pred_anomalies) > 0 and len(pred_anomalies.intersection(true_anomalies)) > 0:
# correct prediction (at least partial overlap with true anomalies)
TP_t = TP_t + 1
elif len(pred_anomalies) == 0 and len(true_anomalies) == 0:
# correct rejection, no predicted anomaly on no true labels
TN_t = TN_t + 1
elif len(pred_anomalies) > 0 and len(true_anomalies) == 0:
# false alarm (i.e., predict anomalies on no true labels)
FP_t = FP_t + 1
elif len(pred_anomalies) == 0 and len(true_anomalies) > 0:
# predict no anomaly when there is at least one true anomaly within the seq.
FN_t = FN_t + 1
TP.append(TP_t)
TN.append(TN_t)
FP.append(FP_t)
FN.append(FN_t)
for i in range(len(thresholds)):
precision.append(TP[i] / (TP[i] + FP[i] + 1e-7))
recall.append(TP[i] / (TP[i] + FN[i] + 1e-7)) # recall or true positive rate (TPR)
fpr.append(FP[i] / (FP[i] + TN[i] + 1e-7))
f1.append(2 * (precision[i] * recall[i]) / (precision[i] + recall[i] + 1e-7))
highest_th_idx = np.argmax(f1)
print(f'Threshold: {thresholds[highest_th_idx]}')
print("Precision : {:0.4f}, Recall : {:0.4f}, F-score : {:0.4f} ".format(
precision[highest_th_idx], recall[highest_th_idx], f1[highest_th_idx]))
print("PR-AUC : {:0.4f}, ROC-AUC : {:0.4f}".format(auc(recall, precision), auc(fpr, recall)))
prv2_scores['dataset'].append(f'ECG{opts.data_num+1}')
prv2_scores['f1'].append(f1[highest_th_idx])
prv2_scores['precision'].append(precision[highest_th_idx])
prv2_scores['recall'].append(recall[highest_th_idx])
prv2_scores['pr_auc'].append(auc(recall, precision))
prv2_scores['roc_auc'].append(auc(fpr, recall))
results = pd.DataFrame(prv2_scores)
print(results.groupby('dataset').mean())
if __name__ == '__main__':
total_evaluation()