-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy patheval_methods.py
182 lines (166 loc) · 6.23 KB
/
eval_methods.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
# -*- coding: utf-8 -*-
import numpy as np
import sklearn.metrics
def calc_point2point(predict, actual):
"""
calculate f1 score by predict and actual.
Args:
predict (np.ndarray): the predict label
actual (np.ndarray): np.ndarray
"""
TP = np.sum(predict * actual)
TN = np.sum((1 - predict) * (1 - actual))
FP = np.sum(predict * (1 - actual))
FN = np.sum((1 - predict) * actual)
precision = TP / (TP + FP + 0.00001)
recall = TP / (TP + FN + 0.00001)
f1 = 2 * precision * recall / (precision + recall + 0.00001)
return f1, precision, recall, TP, TN, FP, FN
def adjust_predicts(score, label,
threshold=None,
pred=None,
calc_latency=False):
"""
Calculate adjusted predict labels using given `score`, `threshold` (or given `pred`) and `label`.
Args:
score (np.ndarray): The anomaly score
label (np.ndarray): The ground-truth label
threshold (float): The threshold of anomaly score.
A point is labeled as "anomaly" if its score is lower than the threshold.
pred (np.ndarray or None): if not None, adjust `pred` and ignore `score` and `threshold`,
calc_latency (bool):
Returns:
np.ndarray: predict labels
"""
if len(score) != len(label):
raise ValueError("score and label must have the same length")
score = np.asarray(score)
label = np.asarray(label)
latency = 0
if pred is None:
predict = score < threshold
else:
predict = pred
actual = label > 0.1
anomaly_state = False
anomaly_count = 0
for i in range(len(score)):
if actual[i] and predict[i] and not anomaly_state:
anomaly_state = True
anomaly_count += 1
for j in range(i, 0, -1):
if not actual[j]:
break
else:
if not predict[j]:
predict[j] = True
latency += 1
elif not actual[i]:
anomaly_state = False
if anomaly_state:
predict[i] = True
if calc_latency:
return predict, latency / (anomaly_count + 1e-4)
else:
return predict
def calc_seq(score, label, threshold, calc_latency=False):
"""
Calculate f1 score for a score sequence
"""
if calc_latency:
predict, latency = adjust_predicts(score, label, threshold, calc_latency=calc_latency)
t = list(calc_point2point(predict, label))
t.append(latency)
return t
else:
predict = adjust_predicts(score, label, threshold, calc_latency=calc_latency)
return calc_point2point(predict, label)
# here for our refined best-f1 search method
def get_best_f1(score, label):
'''
:param score: 1-D array, input score, tot_length
:param label: 1-D array, standard label for anomaly
:return: list for results, threshold
'''
assert score.shape == label.shape
print('***computing best f1***')
search_set = []
tot_anomaly = 0
for i in range(label.shape[0]):
tot_anomaly += (label[i] > 0.5)
flag = 0
cur_anomaly_len = 0
cur_min_anomaly_score = 1e5
for i in range(label.shape[0]):
if label[i] > 0.5:
# here for an anomaly
if flag == 1:
cur_anomaly_len += 1
cur_min_anomaly_score = score[i] if score[i] < cur_min_anomaly_score else cur_min_anomaly_score
else:
flag = 1
cur_anomaly_len = 1
cur_min_anomaly_score = score[i]
else:
# here for normal points
if flag == 1:
flag = 0
search_set.append((cur_min_anomaly_score, cur_anomaly_len, True))
search_set.append((score[i], 1, False))
else:
search_set.append((score[i], 1, False))
if flag == 1:
search_set.append((cur_min_anomaly_score, cur_anomaly_len, True))
search_set.sort(key=lambda x: x[0])
best_f1_res = - 1
threshold = 1
P = 0
TP = 0
best_P = 0
best_TP = 0
for i in range(len(search_set)):
P += search_set[i][1]
if search_set[i][2]: # for an anomaly point
TP += search_set[i][1]
precision = TP / (P + 1e-5)
recall = TP / (tot_anomaly + 1e-5)
f1 = 2 * precision * recall / (precision + recall + 1e-5)
if f1 > best_f1_res:
best_f1_res = f1
threshold = search_set[i][0]
best_P = P
best_TP = TP
print('*** best_f1 ***: ', best_f1_res)
print('*** threshold ***: ', threshold)
return (best_f1_res,
best_TP / (best_P + 1e-5),
best_TP / (tot_anomaly + 1e-5),
best_TP,
score.shape[0] - best_P - tot_anomaly + best_TP,
best_P - best_TP,
tot_anomaly - best_TP), threshold
# calculate evaluation metrics (best-F1, AUROC, AP) under point-adjust approach.
def get_adjusted_composite_metrics(score, label):
score = -score # change the recons prob to anomaly score, higher anomaly score means more anomalous
# adjust the score for segment detection. i.e., for each ground-truth anomaly segment, use the maximum score
# as the score of all points in that segment. This corresponds to point-adjust f1-score.
assert len(score) == len(label)
splits = np.where(label[1:] != label[:-1])[0] + 1
is_anomaly = label[0] == 1
pos = 0
for sp in splits:
if is_anomaly:
score[pos:sp] = np.max(score[pos:sp])
is_anomaly = not is_anomaly
pos = sp
sp = len(label)
if is_anomaly:
score[pos:sp] = np.max(score[pos:sp])
# now get the adjust score for segment evaluation.
fpr, tpr, _ = sklearn.metrics.roc_curve(y_true=label, y_score=score, drop_intermediate=False)
auroc = sklearn.metrics.auc(fpr, tpr)
precision, recall, _ = sklearn.metrics.precision_recall_curve(y_true=label, probas_pred=score)
# validate best f1
f1 = np.max(2 * precision * recall / (precision + recall + 1e-5))
ap = sklearn.metrics.average_precision_score(y_true=label, y_score=score, average=None)
return auroc, ap, f1, precision, recall, fpr, tpr