-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathobjective.py
More file actions
328 lines (289 loc) · 11 KB
/
Copy pathobjective.py
File metadata and controls
328 lines (289 loc) · 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
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
from benchopt import BaseObjective
from benchmark_utils.metrics import (
soft_precision as soft_precision_score,
soft_recall as soft_recall_score,
soft_f1 as soft_f1_score,
ctt,
ttc,
extract_anomaly_ranges,
precision_t as precision_t_score,
recall_t as recall_t_score,
f1_t as f1_t_score,
)
import numpy as np
from sklearn.metrics import (
average_precision_score,
precision_score,
recall_score,
f1_score,
zero_one_loss,
roc_auc_score,
)
class Objective(BaseObjective):
name = "Anomaly detection"
install_cmd = "conda"
requirements = ["scikit-learn"]
parameters = {
"score_metrics": [("auc_pr", "auc_roc")],
"prediction_metrics": [None],
}
detection_ranges = (1, 3, 5, 10, 20)
default_prediction_metrics = (
"precision",
"recall",
"f1",
"precision_t",
"recall_t",
"f1_t",
"ctt",
"ttc",
"zoloss",
"soft_precision",
"soft_recall",
"soft_f1",
)
def get_one_result(self):
"""Return one solution for which the objective can be computed."""
score_metrics = self._normalize_metrics(
getattr(self, "score_metrics", ("auc_pr", "auc_roc"))
)
prediction_metrics = self._expand_prediction_metrics(
getattr(self, "prediction_metrics", None)
)
result = {}
if score_metrics:
result["anomaly_scores"] = np.zeros_like(
self.y_test, dtype=float
)
if prediction_metrics:
result["anomaly_predictions"] = np.zeros_like(
self.y_test, dtype=int
)
return result
def set_data(self, X_train, y_test, X_test):
"Set the data to compute the objective."
self.X_train = X_train
self.X_test, self.y_test = X_test, y_test
def evaluate_result(
self,
anomaly_scores=None,
anomaly_predictions=None,
):
"""Evaluate the result provided by the solver.
anomaly_scores is the score-based solver output.
anomaly_predictions is optional and only needed when requesting
prediction-based metrics.
"""
score_metrics = self._normalize_metrics(
getattr(self, "score_metrics", ("auc_pr", "auc_roc"))
)
prediction_metrics = self._expand_prediction_metrics(
getattr(self, "prediction_metrics", None)
)
if score_metrics and anomaly_scores is None:
raise ValueError("score_metrics require an anomaly_scores array.")
if prediction_metrics and anomaly_predictions is None:
raise ValueError(
"prediction_metrics require an anomaly_predictions array.")
y_true, scores, predictions = self._align_inputs(
anomaly_scores=anomaly_scores,
anomaly_predictions=anomaly_predictions,
)
result = {}
if score_metrics:
result.update(
self._compute_score_metrics(
y_true=y_true,
anomaly_scores=scores,
metrics=score_metrics,
)
)
if prediction_metrics:
result.update(
self._compute_prediction_metrics(
y_true=y_true,
anomaly_predictions=predictions,
metrics=prediction_metrics,
)
)
# Setting value to 0. The actual value is not used for ranking.
result["value"] = 0.0
return result
def get_objective(self):
return dict(X_train=self.X_train, X_test=self.X_test)
def _normalize_metrics(self, metrics):
if metrics is None:
return ()
if isinstance(metrics, str):
if metrics == "all":
return ("auc_pr", "auc_roc")
return (metrics,)
return tuple(metric for metric in metrics if metric is not None)
def _expand_prediction_metrics(self, metrics):
metrics = self._normalize_prediction_metrics(metrics)
expanded = []
for metric in metrics:
if metric == "all":
metric = self.default_prediction_metrics
else:
metric = (metric,)
for name in metric:
if name in {
"soft_precision",
"soft_recall",
"soft_f1",
}:
expanded.extend(
f"{name}_{detection_range}"
for detection_range in self.detection_ranges
)
else:
expanded.append(name)
return tuple(expanded)
def _normalize_prediction_metrics(self, metrics):
if metrics is None:
return ()
if isinstance(metrics, str):
return (metrics,)
return tuple(metric for metric in metrics if metric is not None)
def _align_inputs(self, anomaly_scores, anomaly_predictions):
# flatten everything before aligning lengths.
y_true = np.asarray(self.y_test).reshape(-1)
scores = self._as_flat_array(anomaly_scores)
predictions = self._as_flat_array(anomaly_predictions)
# Only align against arrays that were returned. This keeps
# score-only and prediction-only evaluations valid.
arrays = [array for array in (
scores, predictions) if array is not None]
if not arrays:
return y_true, None, None
# Windowed solvers return fewer outputs than y_test because the
# first timestamps have no full context window. Keep the last samples,
# which correspond to the part of y_test the solver scored.
length = min([len(y_true)] + [len(array) for array in arrays])
y_true = y_true[-length:]
if scores is not None:
scores = scores[-length:]
if predictions is not None:
predictions = predictions[-length:]
# Drop invalid positions. NaN score padding and -1 prediction padding
# When both scores and predictions are present, the same mask is
# applied to keep mixed metric requests on the same timestamps.
valid = np.ones(length, dtype=bool)
if scores is not None:
valid &= ~np.isnan(scores)
if predictions is not None:
valid &= ~np.isnan(predictions)
valid &= predictions != -1
y_true = y_true[valid]
if scores is not None:
scores = scores[valid]
if predictions is not None:
predictions = predictions[valid]
return y_true, scores, predictions
def _as_flat_array(self, array):
if array is None:
return None
return np.asarray(array).reshape(-1)
def _compute_score_metrics(self, y_true, anomaly_scores, metrics):
if len(y_true) == 0:
return {metric: np.nan for metric in metrics}
result = {}
for metric in metrics:
if metric == "auc_roc":
result[metric] = self._safe_auc_roc(y_true, anomaly_scores)
elif metric == "auc_pr":
result[metric] = self._auc_pr(y_true, anomaly_scores)
else:
raise ValueError(f"Unknown score metric: {metric}")
return result
def _compute_prediction_metrics(
self,
y_true,
anomaly_predictions,
metrics,
):
if len(y_true) == 0:
return {metric: np.nan for metric in metrics}
result = {}
anomaly_ranges = None
prediction_ranges = None
for metric in metrics:
if metric == "precision":
result[metric] = precision_score(
y_true, anomaly_predictions, zero_division=0
)
elif metric == "recall":
result[metric] = recall_score(
y_true, anomaly_predictions, zero_division=0
)
elif metric == "f1":
result[metric] = f1_score(
y_true, anomaly_predictions, zero_division=0)
elif metric == "zoloss":
result[metric] = zero_one_loss(y_true, anomaly_predictions)
elif metric in {"precision_t", "recall_t", "f1_t"}:
if anomaly_ranges is None:
anomaly_ranges, prediction_ranges = self._get_ranges(
y_true, anomaly_predictions
)
if metric == "precision_t":
result[metric] = precision_t_score(
anomaly_ranges, prediction_ranges
)
elif metric == "recall_t":
result[metric] = recall_t_score(
anomaly_ranges, prediction_ranges)
else:
result[metric] = f1_t_score(
anomaly_ranges, prediction_ranges)
elif metric == "ctt":
result[metric] = ctt(y_true, anomaly_predictions)
elif metric == "ttc":
result[metric] = ttc(y_true, anomaly_predictions)
elif metric.startswith("soft_precision_"):
detection_range = self._parse_detection_range(
metric, "soft_precision")
result[metric] = soft_precision_score(
y_true,
anomaly_predictions,
detection_range=detection_range,
)
elif metric.startswith("soft_recall_"):
detection_range = self._parse_detection_range(
metric, "soft_recall")
result[metric] = soft_recall_score(
y_true,
anomaly_predictions,
detection_range=detection_range,
)
elif metric.startswith("soft_f1_"):
detection_range = self._parse_detection_range(
metric, "soft_f1")
result[metric] = soft_f1_score(
y_true,
anomaly_predictions,
detection_range=detection_range,
)
else:
raise ValueError(f"Unknown prediction metric: {metric}")
return result
def _get_ranges(self, y_true, anomaly_predictions):
return (
extract_anomaly_ranges(y_true),
extract_anomaly_ranges(anomaly_predictions),
)
def _parse_detection_range(self, metric, prefix):
suffix = metric.replace(f"{prefix}_", "", 1)
try:
return int(suffix)
except ValueError as exc:
raise ValueError(
f"Invalid detection range in prediction metric: {metric}"
) from exc
def _safe_auc_roc(self, y_true, anomaly_scores):
return roc_auc_score(y_true, anomaly_scores)
def _auc_pr(self, y_true, anomaly_scores):
if len(np.unique(y_true)) == 1:
return np.nan
return average_precision_score(y_true, anomaly_scores)