tsadmetrics 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,962 @@
1
+ import numpy as np
2
+ from .metric_utils import *
3
+ from .metric_utils import get_events, calculate_intersection
4
+
5
+
6
+ from _tsadeval.metrics import *
7
+ from _tsadeval.prts.basic_metrics_ts import ts_fscore
8
+ from pate.PATE_metric import PATE
9
+ def point_wise_recall(y_true: np.array, y_pred: np.array):
10
+ """
11
+ Calculate point-wise recall for anomaly detection in time series.
12
+ Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
13
+
14
+ Parameters:
15
+ y_true (np.array): The ground truth binary labels for the time series data.
16
+ y_pred (np.array): The predicted binary labels for the time series data.
17
+
18
+ Returns:
19
+ float: The point-wise recall score, which is the ratio of true positives to the sum of true positives and false negatives.
20
+ """
21
+ m = Pointwise_metrics(len(y_true),y_true,y_pred)
22
+ m.set_confusion()
23
+ TP,FN = m.tp,m.fn
24
+ #TP, _, FP, FN = get_tp_tn_fp_fn_point_wise(y_true, y_pred)
25
+ if TP == 0:
26
+ return 0
27
+ return TP / (TP + FN)
28
+
29
+ def point_wise_precision(y_true: np.array, y_pred: np.array):
30
+ """
31
+ Calculate point-wise precision for anomaly detection in time series.
32
+ Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
33
+
34
+ Parameters:
35
+ y_true (np.array): The ground truth binary labels for the time series data.
36
+ y_pred (np.array): The predicted binary labels for the time series data.
37
+
38
+ Returns:
39
+ float: The point-wise precision score, which is the ratio of true positives to the sum of true positives and false positives.
40
+ """
41
+ #TP, _, FP, FN = get_tp_tn_fp_fn_point_wise(y_true, y_pred)
42
+ m = Pointwise_metrics(len(y_true),y_true,y_pred)
43
+ m.set_confusion()
44
+ TP,FP = m.tp,m.fp
45
+ if TP == 0:
46
+ return 0
47
+ return TP / (TP + FP)
48
+
49
+ def point_wise_f_score(y_true: np.array, y_pred: np.array, beta=1):
50
+ """
51
+ Calculate point-wise F-score for anomaly detection in time series.
52
+ Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
53
+
54
+ Parameters:
55
+ y_true (np.array): The ground truth binary labels for the time series data.
56
+ y_pred (np.array): The predicted binary labels for the time series data.
57
+ betha (float): The beta value, which determines the weight of precision in the combined score.
58
+ Default is 1, which gives equal weight to precision and recall.
59
+
60
+ Returns:
61
+ float: The point-wise F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
62
+ """
63
+ precision = point_wise_precision(y_true, y_pred)
64
+ recall = point_wise_recall(y_true, y_pred)
65
+
66
+ if precision == 0 or recall == 0:
67
+ return 0
68
+
69
+ return ((1 + beta**2) * precision * recall) / (beta**2 * precision + recall)
70
+
71
+
72
+ def point_adjusted_recall(y_true: np.array, y_pred: np.array):
73
+ """
74
+ Calculate point-adjusted recall for anomaly detection in time series.
75
+ Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
76
+
77
+ Parameters:
78
+ y_true (np.array): The ground truth binary labels for the time series data.
79
+ y_pred (np.array): The predicted binary labels for the time series data.
80
+
81
+ Returns:
82
+ float: The point-adjusted recall score, which is the ratio of true positives to the sum of true positives and false negatives.
83
+ """
84
+ if np.sum(y_pred) == 0:
85
+ return 0
86
+ m = PointAdjust(len(y_true),y_true,y_pred)
87
+ #m.adjust()
88
+ TP,FN = m.tp,m.fn
89
+ if TP == 0:
90
+ return 0
91
+ return TP / (TP + FN)
92
+
93
+ def point_adjusted_precision(y_true: np.array, y_pred: np.array):
94
+ """
95
+ Calculate point-adjusted precision for anomaly detection in time series.
96
+ Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
97
+
98
+ Parameters:
99
+ y_true (np.array): The ground truth binary labels for the time series data.
100
+ y_pred (np.array): The predicted binary labels for the time series data.
101
+
102
+ Returns:
103
+ float: The point-adjusted precision score, which is the ratio of true positives to the sum of true positives and false positives.
104
+ """
105
+ if np.sum(y_pred) == 0:
106
+ return 0
107
+ m = PointAdjust(len(y_true),y_true,y_pred)
108
+ #m.adjust()
109
+ TP,FP = m.tp,m.fp
110
+ if TP == 0:
111
+ return 0
112
+ return TP / (TP + FP)
113
+
114
+ def point_adjusted_f_score(y_true: np.array, y_pred: np.array, beta=1):
115
+ """
116
+ Calculate point-adjusted F-score for anomaly detection in time series.
117
+ Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
118
+
119
+ Parameters:
120
+ y_true (np.array): The ground truth binary labels for the time series data.
121
+ y_pred (np.array): The predicted binary labels for the time series data.
122
+ beta (float): The beta value, which determines the weight of precision in the combined score.
123
+ Default is 1, which gives equal weight to precision and recall.
124
+
125
+ Returns:
126
+ float: The point-adjusted F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
127
+ """
128
+ precision = point_adjusted_precision(y_true, y_pred)
129
+ recall = point_adjusted_recall(y_true, y_pred)
130
+
131
+ if precision == 0 or recall == 0:
132
+ return 0
133
+
134
+ return ((1 + beta**2) * precision * recall) / (beta**2 * precision + recall)
135
+
136
+
137
+
138
+ def delay_th_point_adjusted_recall(y_true: np.array, y_pred: np.array, k: int):
139
+ """
140
+ Calculate delay thresholded point-adjusted recall for anomaly detection in time series.
141
+ Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
142
+
143
+ Parameters:
144
+ y_true (np.array): The ground truth binary labels for the time series data.
145
+ y_pred (np.array): The predicted binary labels for the time series data.
146
+ k (int): The maximum number of time steps within which an anomaly must be predicted to be considered detected.
147
+
148
+ Returns:
149
+ float: The delay thresholded point-adjusted recall score, which is the ratio of true positives to the sum of true positives and false negatives.
150
+ """
151
+ if np.sum(y_pred) == 0:
152
+ return 0
153
+ m = DelayThresholdedPointAdjust(len(y_true),y_true,y_pred,k=k)
154
+ TP,FN = m.tp,m.fn
155
+ if TP == 0:
156
+ return 0
157
+ return TP / (TP + FN)
158
+
159
+ def delay_th_point_adjusted_precision(y_true: np.array, y_pred: np.array, k: int):
160
+ """
161
+ Calculate delay thresholded point-adjusted precision for anomaly detection in time series.
162
+ Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
163
+
164
+ Parameters:
165
+ y_true (np.array): The ground truth binary labels for the time series data.
166
+ y_pred (np.array): The predicted binary labels for the time series data.
167
+ k (int): The maximum number of time steps within which an anomaly must be predicted to be considered detected.
168
+
169
+ Returns:
170
+ float: The delay thresholded point-adjusted precision score, which is the ratio of true positives to the sum of true positives and false positives.
171
+ """
172
+ if np.sum(y_pred) == 0:
173
+ return 0
174
+ m = DelayThresholdedPointAdjust(len(y_true),y_true,y_pred,k=k)
175
+ TP,FP = m.tp,m.fp
176
+ if TP == 0:
177
+ return 0
178
+ return TP / (TP + FP)
179
+
180
+ def delay_th_point_adjusted_f_score(y_true: np.array, y_pred: np.array, k: int, beta=1):
181
+ """
182
+ Calculate delay thresholded point-adjusted F-score for anomaly detection in time series.
183
+ Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
184
+
185
+ Parameters:
186
+ y_true (np.array): The ground truth binary labels for the time series data.
187
+ y_pred (np.array): The predicted binary labels for the time series data.
188
+ k (int): The maximum number of time steps within which an anomaly must be predicted to be considered detected.
189
+ beta (float): The beta value, which determines the weight of precision in the combined score.
190
+ Default is 1, which gives equal weight to precision and recall.
191
+
192
+ Returns:
193
+ float: The delay thresholded point-adjusted F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
194
+ """
195
+ precision = delay_th_point_adjusted_precision(y_true, y_pred, k)
196
+ recall = delay_th_point_adjusted_recall(y_true, y_pred, k)
197
+
198
+ if precision == 0 or recall == 0:
199
+ return 0
200
+
201
+ return ((1 + beta**2) * precision * recall) / (beta**2 * precision + recall)
202
+
203
+
204
+ def point_adjusted_at_k_recall(y_true: np.array, y_pred: np.array, k: float):
205
+ """
206
+ Calculate k percent point-adjusted at K% recall for anomaly detection in time series.
207
+ Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
208
+
209
+ Parameters:
210
+ y_true (np.array): The ground truth binary labels for the time series data.
211
+ y_pred (np.array): The predicted binary labels for the time series data.
212
+ k (float): The minimum percentage of the anomaly that must be detected to consider the anomaly as detected.
213
+
214
+ Returns:
215
+ float: The point-adjusted recall score, which is the ratio of true positives to the sum of true positives and false negatives.
216
+ """
217
+ #TP, _, FP, FN = get_tp_tn_fp_fn_point_adjusted_at_k(y_true, y_pred, k)
218
+ m = PointAdjustKPercent(len(y_true),y_true,y_pred,k=k)
219
+ TP,FN = m.tp,m.fn
220
+ if TP == 0:
221
+ return 0
222
+ return TP / (TP + FN)
223
+
224
+ def point_adjusted_at_k_precision(y_true: np.array, y_pred: np.array, k: float):
225
+ """
226
+ Calculate point-adjusted at K% precision for anomaly detection in time series.
227
+ Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
228
+
229
+ Parameters:
230
+ y_true (np.array): The ground truth binary labels for the time series data.
231
+ y_pred (np.array): The predicted binary labels for the time series data.
232
+ k (float): The minimum percentage of the anomaly that must be detected to consider the anomaly as detected.
233
+
234
+ Returns:
235
+ float: The point-adjusted precision score, which is the ratio of true positives to the sum of true positives and false positives.
236
+ """
237
+ #TP, _, FP, _ = get_tp_tn_fp_fn_point_adjusted_at_k(y_true, y_pred, k)
238
+ m = PointAdjustKPercent(len(y_true),y_true,y_pred,k=k)
239
+ TP,FP = m.tp,m.fp
240
+ if TP == 0:
241
+ return 0
242
+ return TP / (TP + FP)
243
+
244
+ def point_adjusted_at_k_f_score(y_true: np.array, y_pred: np.array, k: float, beta=1):
245
+ """
246
+ Calculate point-adjusted at K% F-score for anomaly detection in time series.
247
+ Implementation of https://link.springer.com/article/10.1007/s10618-023-00988-8
248
+
249
+ Parameters:
250
+ y_true (np.array): The ground truth binary labels for the time series data.
251
+ y_pred (np.array): The predicted binary labels for the time series data.
252
+ k (float): The minimum percentage of the anomaly that must be detected to consider the anomaly as detected.
253
+ beta (float): The beta value, which determines the weight of precision in the combined score.
254
+ Default is 1, which gives equal weight to precision and recall.
255
+
256
+ Returns:
257
+ float: The point-adjusted F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
258
+ """
259
+ precision = point_adjusted_at_k_precision(y_true, y_pred, k)
260
+ recall = point_adjusted_at_k_recall(y_true, y_pred, k)
261
+
262
+ if precision == 0 or recall == 0:
263
+ return 0
264
+
265
+ return ((1 + beta**2) * precision * recall) / (beta**2 * precision + recall)
266
+
267
+
268
+ def latency_sparsity_aw_recall(y_true: np.array, y_pred: np.array, ni: int):
269
+ """
270
+ Calculate latency and sparsity aware recall for anomaly detection in time series.
271
+ Implementation of https://dl.acm.org/doi/10.1145/3447548.3467174
272
+
273
+ Parameters:
274
+ y_true (np.array): The ground truth binary labels for the time series data.
275
+ y_pred (np.array): The predicted binary labels for the time series data.
276
+ ni (int): The batch size used in the implementation to handle latency and sparsity.
277
+
278
+ Returns:
279
+ float: The latency and sparsity aware recall score, which is the ratio of true positives to the sum of true positives and false negatives.
280
+ """
281
+ if np.sum(y_pred) == 0:
282
+ return 0
283
+ m = LatencySparsityAware(len(y_true),y_true,y_pred,tw=ni)
284
+ TP,FN = m.tp, m.fn
285
+ if TP == 0:
286
+ return 0
287
+ return TP / (TP + FN)
288
+
289
+ def latency_sparsity_aw_precision(y_true: np.array, y_pred: np.array, ni: int):
290
+ """
291
+ Calculate latency and sparsity aware precision for anomaly detection in time series.
292
+ Implementation of https://dl.acm.org/doi/10.1145/3447548.3467174
293
+
294
+ Parameters:
295
+ y_true (np.array): The ground truth binary labels for the time series data.
296
+ y_pred (np.array): The predicted binary labels for the time series data.
297
+ ni (int): The batch size used in the implementation to handle latency and sparsity.
298
+
299
+ Returns:
300
+ float: The latency and sparsity aware precision score, which is the ratio of true positives to the sum of true positives and false positives.
301
+ """
302
+ if np.sum(y_pred) == 0:
303
+ return 0
304
+ m = LatencySparsityAware(len(y_true),y_true,y_pred,tw=ni)
305
+ TP,FP = m.tp, m.fp
306
+ if TP == 0:
307
+ return 0
308
+ return TP / (TP + FP)
309
+
310
+ def latency_sparsity_aw_f_score(y_true: np.array, y_pred: np.array, ni: int, beta=1):
311
+ """
312
+ Calculate latency and sparsity aware F-score for anomaly detection in time series.
313
+ Implementation of https://dl.acm.org/doi/10.1145/3447548.3467174
314
+
315
+ Parameters:
316
+ y_true (np.array): The ground truth binary labels for the time series data.
317
+ y_pred (np.array): The predicted binary labels for the time series data.
318
+ ni (int): The batch size used in the implementation to handle latency and sparsity.
319
+ beta (float): The beta value, which determines the weight of precision in the combined score.
320
+ Default is 1, which gives equal weight to precision and recall.
321
+
322
+ Returns:
323
+ float: The latency and sparsity aware F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
324
+ """
325
+ if np.sum(y_pred) == 0:
326
+ return 0
327
+
328
+ recall = latency_sparsity_aw_recall(y_true,y_pred,ni)
329
+ precision = latency_sparsity_aw_precision(y_true,y_pred,ni)
330
+ if precision == 0 or recall == 0:
331
+ return 0
332
+ return ((1 + beta**2) * precision * recall) / (beta**2 * precision + recall)
333
+
334
+
335
+ def segment_wise_recall(y_true: np.array, y_pred: np.array):
336
+ """
337
+ Calculate segment-wise recall for anomaly detection in time series.
338
+ Implementation of https://arxiv.org/pdf/1802.04431
339
+
340
+ Parameters:
341
+ y_true (np.array): The ground truth binary labels for the time series data.
342
+ y_pred (np.array): The predicted binary labels for the time series data.
343
+
344
+ Returns:
345
+ float: The segment-wise recall score, which is the ratio of true positives to the sum of true positives and false negatives.
346
+ """
347
+ #TP, _, FN = get_tp_fp_fn_segment_wise(y_true, y_pred)
348
+ m = Segmentwise_metrics(len(y_true),y_true,y_pred)
349
+ TP,FN = m.tp,m.fn
350
+ if TP == 0:
351
+ return 0
352
+ return TP / (TP + FN)
353
+
354
+ def segment_wise_precision(y_true: np.array, y_pred: np.array):
355
+ """
356
+ Calculate segment-wise precision for anomaly detection in time series.
357
+ Implementation of https://arxiv.org/pdf/1802.04431
358
+
359
+ Parameters:
360
+ y_true (np.array): The ground truth binary labels for the time series data.
361
+ y_pred (np.array): The predicted binary labels for the time series data.
362
+
363
+ Returns:
364
+ float: The segment-wise precision score, which is the ratio of true positives to the sum of true positives and false positives.
365
+ """
366
+ #TP, FP, _ = get_tp_fp_fn_segment_wise(y_true, y_pred)
367
+ m = Segmentwise_metrics(len(y_true),y_true,y_pred)
368
+ TP,FP = m.tp,m.fp
369
+ if TP == 0:
370
+ return 0
371
+ return TP / (TP + FP)
372
+
373
+ def segment_wise_f_score(y_true: np.array, y_pred: np.array, beta=1):
374
+ """
375
+ Calculate segment-wise F-score for anomaly detection in time series.
376
+ Implementation of https://arxiv.org/pdf/1802.04431
377
+
378
+ Parameters:
379
+ y_true (np.array): The ground truth binary labels for the time series data.
380
+ y_pred (np.array): The predicted binary labels for the time series data.
381
+ beta (float): The beta value, which determines the weight of precision in the combined score.
382
+ Default is 1, which gives equal weight to precision and recall.
383
+
384
+ Returns:
385
+ float: The segment-wise F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
386
+
387
+ """
388
+ m = Segmentwise_metrics(len(y_true),y_true,y_pred)
389
+ TP,FN,FP = m.tp,m.fn,m.fp
390
+ if TP==0:
391
+ return 0
392
+
393
+ precision = TP / (TP + FP)
394
+ recall = TP / (TP + FN)
395
+
396
+ if precision == 0 or recall == 0:
397
+ return 0
398
+ return ((1 + beta**2) * precision * recall) / (beta**2 * precision + recall)
399
+
400
+ def composite_f_score(y_true: np.array, y_pred: np.array, beta=1):
401
+ """
402
+ Calculate composite F-score for anomaly detection in time series.
403
+ Implementation of https://ieeexplore.ieee.org/document/9525836
404
+
405
+ Parameters:
406
+ y_true (np.array): The ground truth binary labels for the time series data.
407
+ y_pred (np.array): The predicted binary labels for the time series data.
408
+ beta (float): The beta value, which determines the weight of precision in the combined score.
409
+ Default is 1, which gives equal weight to precision and recall.
410
+
411
+ Returns:
412
+ float: The composite F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
413
+
414
+ """
415
+ m = Composite_f(len(y_true),y_true,y_pred)
416
+ #Point wise precision
417
+ precision = m.precision()#point_wise_precision(y_true,y_pred)
418
+
419
+ #Segment wise recall
420
+ recall = m.recall()#segment_wise_recall(y_true,y_pred)
421
+
422
+ if precision==0 or recall==0:
423
+ return 0
424
+
425
+ return ((1 + beta**2) * precision * recall) / (beta**2 * precision + recall)
426
+
427
+ def time_tolerant_recall(y_true: np.array, y_pred: np.array, t: int) -> float:
428
+ """
429
+ Calculate time tolerant recall for anomaly detection in time series.
430
+ Implementation of https://arxiv.org/pdf/1802.04431
431
+
432
+ Parameters:
433
+ y_true (np.array): The ground truth binary labels for the time series data.
434
+ y_pred (np.array): The predicted binary labels for the time series data.
435
+ t (int): The time tolerance parameter
436
+
437
+ Returns:
438
+ float: The time tolerant recall score, which is the ratio of true positives to the sum of true positives and false negatives.
439
+ """
440
+ if np.sum(y_pred) == 0:
441
+ return 0
442
+
443
+ m = Time_Tolerant(len(y_true),y_true,y_pred,d=t)
444
+ return m.recall()
445
+
446
+ def time_tolerant_precision(y_true: np.array, y_pred: np.array, t: int) -> float:
447
+ """
448
+ Calculate time tolerant precision for anomaly detection in time series.
449
+ Implementation of https://arxiv.org/pdf/1802.04431
450
+
451
+ Parameters:
452
+ y_true (np.array): The ground truth binary labels for the time series data.
453
+ y_pred (np.array): The predicted binary labels for the time series data.
454
+ t (int): The time tolerance parameter
455
+
456
+ Returns:
457
+ float: The time tolerant precision score, which is the ratio of true positives to the sum of true positives and false positives.
458
+ """
459
+ if np.sum(y_pred) == 0:
460
+ return 0
461
+ m = Time_Tolerant(len(y_true),y_true,y_pred, d=t)
462
+ return m.precision()
463
+
464
+
465
+ def time_tolerant_f_score(y_true: np.array, y_pred: np.array,t: int, beta=1):
466
+ """
467
+ Calculate time tolerant F-score for anomaly detection in time series.
468
+ Implementation of https://arxiv.org/pdf/1802.04431
469
+
470
+ Parameters:
471
+ y_true (np.array): The ground truth binary labels for the time series data.
472
+ y_pred (np.array): The predicted binary labels for the time series data.
473
+ t (int): The time tolerance parameter
474
+ beta (float): The beta value, which determines the weight of precision in the combined score.
475
+ Default is 1, which gives equal weight to precision and recall.
476
+
477
+ Returns:
478
+ float: The time tolerant F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
479
+
480
+ """
481
+ precision = time_tolerant_precision(y_true,y_pred,t)
482
+ recall = time_tolerant_recall(y_true,y_pred,t)
483
+ if precision==0 or recall==0:
484
+ return 0
485
+ return ((1 + beta**2) * precision * recall) / (beta**2 * precision + recall)
486
+
487
+
488
+ def range_based_recall(y_true: np.array, y_pred: np.array, alpha: float, bias='flat', cardinality_mode='one'):
489
+ """
490
+ Calculate range-based recall for anomaly detection in time series.
491
+
492
+ Parameters:
493
+ y_true (np.array): The ground truth binary labels for the time series data.
494
+ y_pred (np.array): The predicted binary labels for the time series data.
495
+ alpha (float): A parameter that controls the length of the range considered for true positives.
496
+ bias (str): The type of bias to apply for weighting (flat, front-end, back-end, middle).
497
+ cardinality (str, optional): ["one", "reciprocal", "udf_gamma"]. Defaults to "one".
498
+
499
+ Returns:
500
+ float: The range-based recall score.
501
+ """
502
+ if np.sum(y_pred) == 0:
503
+ return 0
504
+ m = Range_PR(len(y_true),y_true,y_pred,cardinality=cardinality_mode, alpha=alpha,bias=bias)
505
+ return m.recall()
506
+
507
+
508
+
509
+ def range_based_precision(y_true: np.array, y_pred: np.array, alpha: float, bias='flat', cardinality_mode='one'):
510
+ """
511
+ Calculate range-based precision for anomaly detection in time series.
512
+
513
+ Parameters:
514
+ y_true (np.array): The ground truth binary labels for the time series data.
515
+ y_pred (np.array): The predicted binary labels for the time series data.
516
+ alpha (float): A parameter that controls the length of the range considered for true positives.
517
+ bias (str): The type of bias to apply for weighting (flat, front-end, back-end, middle).
518
+ cardinality (str, optional): ["one", "reciprocal", "udf_gamma"]. Defaults to "one".
519
+
520
+ Returns:
521
+ float: The range-based precision score.
522
+ """
523
+ if np.sum(y_pred) == 0:
524
+ return 0
525
+ m = Range_PR(len(y_true),y_true,y_pred,cardinality=cardinality_mode, alpha=alpha,bias=bias)
526
+ return m.precision()
527
+
528
+
529
+
530
+
531
+
532
+
533
+ def range_based_f_score(y_true: np.array, y_pred: np.array, p_alpha: float, r_alpha: float, p_bias='flat', r_bias='flat', cardinality_mode='one', beta=1) -> float:
534
+ """
535
+ Calculate range-based F-score for anomaly detection in time series.
536
+
537
+ Parameters:
538
+ y_true (np.array): The ground truth binary labels for the time series data.
539
+ y_pred (np.array): The predicted binary labels for the time series data.
540
+ alpha (float): A parameter that controls the length of the range considered for true positives.
541
+ p_bias: string, default="flat"
542
+ Positional bias for precision. This should be "flat", "front", "middle", or "back"
543
+ r_bias: string, default="flat"
544
+ Positional bias for recall. This should be "flat", "front", "middle", or "back"
545
+ cardinality_mode (str, optional): ["one", "reciprocal", "udf_gamma"]. Defaults to "one".
546
+ beta (float): The beta value, which determines the weight of precision in the combined score.
547
+ Default is 1, which gives equal weight to precision and recall.
548
+
549
+ Returns:
550
+ float: The range-based F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
551
+ """
552
+ if np.sum(y_pred) == 0:
553
+ return 0
554
+ f = ts_fscore(y_true, y_pred, beta=beta, p_alpha=p_alpha, r_alpha=r_alpha, cardinality=cardinality_mode, p_bias=p_bias, r_bias=r_bias)
555
+ return f
556
+
557
+
558
+
559
+
560
+ def ts_aware_recall(y_true: np.array, y_pred: np.array, alpha: float, delta: float, theta: float, past_range: bool = False):
561
+ """
562
+ Calculate time series aware recall for anomaly detection in time series.
563
+
564
+ Parameters:
565
+ y_true (np.array): The ground truth binary labels for the time series data.
566
+ y_pred (np.array): The predicted binary labels for the time series data.
567
+ alpha (float): A parameter that controls the length of the range considered for true positives.
568
+ past_range: Determines if the range is considered in the past or future as specified in https://www.mdpi.com/2079-9292/11/8/1213
569
+
570
+ Returns:
571
+ float: The time series aware recall score.
572
+ """
573
+ m = TaF(len(y_true),y_true,y_pred,alpha=alpha,theta=theta,delta=delta,past_range=past_range)
574
+ return m.recall()
575
+
576
+
577
+
578
+
579
+ def ts_aware_precision(y_true: np.array, y_pred: np.array,alpha: float, delta: float, theta: float, past_range: bool = False):
580
+ """
581
+ Calculate time series aware precision for anomaly detection in time series.
582
+
583
+ Parameters:
584
+ y_true (np.array): The ground truth binary labels for the time series data.
585
+ y_pred (np.array): The predicted binary labels for the time series data.
586
+ alpha (float): A parameter that controls the length of the range considered for true positives.
587
+ past_range: Determines if the range is considered in the past or future as specified in https://www.mdpi.com/2079-9292/11/8/1213
588
+
589
+ Returns:
590
+ float: The time series aware precision score.
591
+ """
592
+ m = TaF(len(y_true),y_true,y_pred,alpha=alpha,theta=theta,delta=delta,past_range=past_range)
593
+ return m.precision()
594
+
595
+
596
+
597
+
598
+
599
+ def ts_aware_f_score(y_true: np.array, y_pred: np.array, beta: float, alpha: float, delta: float, theta: float, past_range: bool = False):
600
+ """
601
+ Calculate time series aware F-score for anomaly detection in time series.
602
+
603
+ Parameters:
604
+ y_true (np.array): The ground truth binary labels for the time series data.
605
+ y_pred (np.array): The predicted binary labels for the time series data.
606
+ alpha (float): A parameter that controls the length of the range considered for true positives.
607
+ beta (float): The beta value, which determines the weight of precision in the combined score.
608
+ Default is 1, which gives equal weight to precision and recall.
609
+ past_range: Determines if the range is considered in the past or future as specified in https://www.mdpi.com/2079-9292/11/8/1213
610
+
611
+ Returns:
612
+ float: The time series aware F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
613
+ """
614
+
615
+ m = TaF(len(y_true),y_true,y_pred,alpha=alpha,theta=theta,delta=delta,past_range=past_range)
616
+ precision = m.precision()
617
+ recall = m.recall()
618
+ if precision==0 or recall==0:
619
+ return 0
620
+
621
+ return ((1 + beta**2) * precision * recall) / (beta**2 * precision + recall)
622
+
623
+
624
+
625
+
626
+
627
+ def enhanced_ts_aware_recall(y_true: np.array, y_pred: np.array, theta: float):
628
+ """
629
+ Calculate enhanced time series aware recall for anomaly detection in time series.
630
+
631
+ Parameters:
632
+ y_true (np.array): The ground truth binary labels for the time series data.
633
+ y_pred (np.array): The predicted binary labels for the time series data.
634
+ alpha (float): A parameter that controls the length of the range considered for true positives.
635
+
636
+ Returns:
637
+ float: The time series aware recall score.
638
+ """
639
+ if np.sum(y_pred) == 0:
640
+ return 0
641
+ m = eTaF(len(y_true),y_true,y_pred,theta_p=theta)
642
+ return m.recall()
643
+
644
+
645
+
646
+
647
+ def enhanced_ts_aware_precision(y_true: np.array, y_pred: np.array, theta: float):
648
+ """
649
+ Calculate enhanced time series aware precision for anomaly detection in time series.
650
+
651
+ Parameters:
652
+ y_true (np.array): The ground truth binary labels for the time series data.
653
+ y_pred (np.array): The predicted binary labels for the time series data.
654
+ alpha (float): A parameter that controls the length of the range considered for true positives.
655
+
656
+ Returns:
657
+ float: The time series aware precision score.
658
+ """
659
+ if np.sum(y_pred) == 0:
660
+ return 0
661
+ m = eTaF(len(y_true),y_true,y_pred,theta_p=theta)
662
+ return m.precision()
663
+
664
+
665
+
666
+
667
+
668
+ def enhanced_ts_aware_f_score(y_true: np.array, y_pred: np.array, beta: float, theta_p: float, theta_r: float):
669
+ """
670
+ Calculate enhanced time series aware F-score for anomaly detection in time series.
671
+
672
+ Parameters:
673
+ y_true (np.array): The ground truth binary labels for the time series data.
674
+ y_pred (np.array): The predicted binary labels for the time series data.
675
+ beta (float): The beta value, which determines the weight of precision in the combined score.
676
+ Default is 1, which gives equal weight to precision and recall.
677
+
678
+ Returns:
679
+ float: The time series aware F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
680
+ """
681
+ if np.sum(y_pred) == 0:
682
+ return 0
683
+ m = eTaF(len(y_true),y_true,y_pred,theta_p=theta_p, theta_r=theta_r)
684
+ return m.result['f1']
685
+
686
+
687
+
688
+ def affiliation_based_recall(y_true: np.array, y_pred: np.array):
689
+ """
690
+ Calculate affiliation based recall for anomaly detection in time series.
691
+
692
+ Parameters:
693
+ y_true (np.array): The ground truth binary labels for the time series data.
694
+ y_pred (np.array): The predicted binary labels for the time series data.
695
+ beta (float): The beta value, which determines the weight of precision in the combined score.
696
+ Default is 1, which gives equal weight to precision and recall.
697
+
698
+ Returns:
699
+ float: The time series aware F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
700
+ """
701
+ if np.sum(y_pred) == 0:
702
+ return 0
703
+ m = Affiliation(len(y_true),y_true,y_pred)
704
+ s = m.get_score()
705
+ return m.r
706
+
707
+
708
+ def affiliation_based_precision(y_true: np.array, y_pred: np.array):
709
+ """
710
+ Calculate affiliation based F-score for anomaly detection in time series.
711
+
712
+ Parameters:
713
+ y_true (np.array): The ground truth binary labels for the time series data.
714
+ y_pred (np.array): The predicted binary labels for the time series data.
715
+ beta (float): The beta value, which determines the weight of precision in the combined score.
716
+ Default is 1, which gives equal weight to precision and recall.
717
+
718
+ Returns:
719
+ float: The time series aware F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
720
+ """
721
+ if np.sum(y_pred) == 0:
722
+ return 0
723
+ m = Affiliation(len(y_true),y_true,y_pred)
724
+ s = m.get_score()
725
+ return m.p
726
+
727
+
728
+ def affiliation_based_f_score(y_true: np.array, y_pred: np.array, beta=1):
729
+ """
730
+ Calculate affiliation based F-score for anomaly detection in time series.
731
+
732
+ Parameters:
733
+ y_true (np.array): The ground truth binary labels for the time series data.
734
+ y_pred (np.array): The predicted binary labels for the time series data.
735
+ beta (float): The beta value, which determines the weight of precision in the combined score.
736
+ Default is 1, which gives equal weight to precision and recall.
737
+
738
+ Returns:
739
+ float: The time series aware F-score, which is the harmonic mean of precision and recall, adjusted by the beta value.
740
+ """
741
+ if np.sum(y_pred) == 0:
742
+ return 0
743
+ m = Affiliation(len(y_true),y_true,y_pred)
744
+ return m.get_score(beta)
745
+
746
+
747
+ def nab_score(y_true: np.array, y_pred: np.array):
748
+ """
749
+ Calculate NAB score for anomaly detection in time series.
750
+
751
+ Parameters:
752
+ y_true (np.array): The ground truth binary labels for the time series data.
753
+ y_pred (np.array): The predicted binary labels for the time series data.
754
+
755
+
756
+ Returns:
757
+ float: The nab score, which is the harmonic mean of precision and recall, adjusted by the beta value.
758
+ """
759
+
760
+ m = NAB_score(len(y_true),y_true,y_pred)
761
+ return m.get_score()
762
+
763
+ def temporal_distance(y_true: np.array, y_pred: np.array, distance: int = 0):
764
+ """
765
+ Calculate temporal distane for anomaly detection in time series.
766
+
767
+ Parameters:
768
+ y_true (np.array): The ground truth binary labels for the time series data.
769
+ y_pred (np.array): The predicted binary labels for the time series data.
770
+ distance (int): The distance type parameter for the temporal distance calculation.
771
+ 0: Euclidean distance
772
+ 1: Squared Euclidean distance
773
+
774
+
775
+ Returns:
776
+ float: The temporal distance, which is the harmonic mean of precision and recall, adjusted by the beta value.
777
+ """
778
+
779
+ m = Temporal_Distance(len(y_true),y_true,y_pred,distance=distance)
780
+ return m.get_score()
781
+
782
+ def average_detection_count(y_true: np.array, y_pred: np.array):
783
+ """
784
+ Calculate average detection count for anomaly detection in time series.
785
+
786
+ Parameters:
787
+ y_true (np.array): The ground truth binary labels for the time series data.
788
+ y_pred (np.array): The predicted binary labels for the time series data.
789
+
790
+
791
+ Returns:
792
+ float: The average detection count.
793
+ """
794
+
795
+ b = Binary_detection(len(y_true),y_true,y_pred)
796
+ azs = b.get_gt_anomalies_segmentwise()
797
+ a_points = b.get_gt_anomalies_ptwise()
798
+
799
+ counts = []
800
+ for az in azs:
801
+ count = 0
802
+ for ap in a_points:
803
+ if ap >= az[0] and ap <= az[1]:
804
+ count+=1
805
+ counts.append(count)
806
+
807
+ return np.mean(counts)
808
+
809
+ def absolute_detection_distance(y_true: np.array, y_pred: np.array):
810
+ """
811
+ Calculate absolute detection distance for anomaly detection in time series.
812
+
813
+ Parameters:
814
+ y_true (np.array): The ground truth binary labels for the time series data.
815
+ y_pred (np.array): The predicted binary labels for the time series data.
816
+
817
+
818
+ Returns:
819
+ float: The absolute detection distance.
820
+ """
821
+
822
+ b = Binary_detection(len(y_true),y_true,y_pred)
823
+ azs = b.get_gt_anomalies_segmentwise()
824
+ a_points = b.get_gt_anomalies_ptwise()
825
+
826
+ distance = 0
827
+ for az in azs:
828
+ for ap in a_points:
829
+ if ap >= az[0] and ap <= az[1]:
830
+ center = int((az[0] + az[1]) / 2)
831
+ distance+=abs(ap - center)
832
+
833
+ return distance/len(a_points)
834
+
835
+
836
+ def total_detected_in_range(y_true: np.array, y_pred: np.array, k: int):
837
+ """
838
+ Calculate total detected in range for anomaly detection in time series.
839
+
840
+ Parameters:
841
+ y_true (np.array): The ground truth binary labels for the time series data.
842
+ y_pred (np.array): The predicted binary labels for the time series data.
843
+ k (int): The maximum number of time steps within which an anomaly must be predicted to be considered detected.
844
+
845
+ Returns:
846
+ float: The total detected in range.
847
+ """
848
+ if np.sum(y_pred) == 0:
849
+ return 0
850
+ em,da,ma,_ = counting_method(y_true, y_pred, k)
851
+
852
+
853
+ return (em + da)/(em + da + ma)
854
+
855
+
856
+ def detection_accuracy_in_range(y_true: np.array, y_pred: np.array, k: int):
857
+ """
858
+ Calculate detection accuracy in range for anomaly detection in time series.
859
+
860
+ Parameters:
861
+ y_true (np.array): The ground truth binary labels for the time series data.
862
+ y_pred (np.array): The predicted binary labels for the time series data.
863
+ k (int): The maximum number of time steps within which an anomaly must be predicted to be considered detected.
864
+
865
+ Returns:
866
+ float: The total detected in range.
867
+ """
868
+ if np.sum(y_pred) == 0:
869
+ return 0
870
+ em,da,_,fa = counting_method(y_true, y_pred, k)
871
+
872
+
873
+ return (em + da)/(em + da + fa)
874
+
875
+
876
+ def weighted_detection_difference(y_true: np.array, y_pred: np.array, k: int):
877
+ """
878
+ Calculate weighted detection difference for anomaly detection in time series.
879
+ The weighted detection difference is the difference between the number of true positives and the number of false positives, weighted by the number of true positives.
880
+
881
+ Parameters:
882
+ y_true (np.array): The ground truth binary labels for the time series data.
883
+ y_pred (np.array): The predicted binary labels for the time series data.
884
+ k (int): The maximum number of time steps within which an anomaly must be predicted to be considered detected.
885
+
886
+ Returns:
887
+ float: The weighted detection difference.
888
+ """
889
+ if np.sum(y_pred) == 0:
890
+ return 0
891
+
892
+ def gaussian(dt,tmax):
893
+ if dt < tmax:
894
+ return 1- dt/tmax
895
+ else:
896
+ return -1
897
+
898
+ tmax = len(y_true)
899
+
900
+ ones_indices = np.where(y_true == 1)[0]
901
+
902
+ y_modified = y_true.astype(float).copy()
903
+
904
+ for i in range(len(y_true)):
905
+ if y_true[i] == 0:
906
+ dt = np.min(np.abs(ones_indices - i)) if len(ones_indices) > 0 else tmax
907
+ y_modified[i] = gaussian(dt, tmax)
908
+
909
+ ws = 0
910
+ wf = 0
911
+ for i in range(len(y_pred)):
912
+ if y_pred[i] != 1:
913
+ ws+=y_modified[i]
914
+ else:
915
+ wf+=y_modified[i]
916
+
917
+ _,_,_,fa = counting_method(y_true, y_pred, k)
918
+
919
+
920
+ return ws - wf*fa
921
+
922
+
923
+ def binary_pate(y_true: np.array, y_pred: np.array, early: int, delay: int):
924
+ """
925
+ Calculate PATE score for anomaly detection in time series.
926
+ The PATE score is the ratio of the number of true positives to the sum of true positives, false positives, and false negatives, within a given early and delay range.
927
+
928
+ Parameters:
929
+ y_true (np.array): The ground truth binary labels for the time series data.
930
+ y_pred (np.array): The predicted binary labels for the time series data.
931
+ early (int): The maximum number of time steps before an anomaly must be predicted to be considered early.
932
+ delay (int): The maximum number of time steps after an anomaly must be predicted to be considered delayed.
933
+
934
+ Returns:
935
+ float: The PATE score.
936
+ """
937
+
938
+ return PATE(y_true, y_pred, early, delay, binary_scores=True)
939
+
940
+ def mean_time_to_detect(y_true: np.array, y_pred: np.array):
941
+ """
942
+ Calculate mean time to detect for anomaly detection in time series.
943
+ The mean time to detect is the average number of time steps between the ground truth anomaly and the predicted anomaly.
944
+
945
+ Parameters:
946
+ y_true (np.array): The ground truth binary labels for the time series data.
947
+ y_pred (np.array): The predicted binary labels for the time series data.
948
+
949
+ Returns:
950
+ float: The mean time to detect.
951
+ """
952
+
953
+ b = Binary_detection(len(y_true),y_true,y_pred)
954
+ a_events = b.get_gt_anomalies_segmentwise()
955
+ t_sum = 0
956
+ for _,b in a_events:
957
+ for i in range(b,len(y_pred)):
958
+ if y_pred[i] == 1:
959
+ t_sum+=i-b
960
+ break
961
+
962
+ return t_sum/len(a_events)