tsadmetrics 0.1.10__py3-none-any.whl → 0.1.12__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.
@@ -262,16 +262,29 @@ def transform_to_full_series(length: int, anomalies: np.array):
262
262
 
263
263
  def counting_method(y_true: np.array, y_pred: np.array, k: int):
264
264
  em,da,ma,fa = 0,0,0,0
265
- for gt,pa in zip(y_true,y_pred):
265
+ for i_gt in range(len(y_true)):
266
+ i_pa = i_gt
267
+ gt = y_true[i_gt]
268
+ pa = y_pred[i_pa]
266
269
  if gt==1 and pa==1:
267
270
  em+=1
268
271
  elif gt==0 and pa==1:
269
272
  fa+=1
270
- else:
273
+ elif gt==1 and pa==0:
274
+ anom_range = y_pred[i_gt-k:i_pa+k+1]
275
+ detected = False
276
+ for r in anom_range:
277
+ if r==1:
278
+ em+=1
279
+ detected=True
280
+ break
281
+ if not detected:
282
+ ma+=1
283
+ elif gt==0 and pa==0:
271
284
  pass
272
- b = DelayThresholdedPointAdjust(len(y_true),y_true,y_pred,k=k)
273
- da = b.tp-em
274
- ma = b.fn
285
+ # b = DelayThresholdedPointAdjust(len(y_true),y_true,y_pred,k=k)
286
+ # da = b.tp-em
287
+ # ma = b.fn
275
288
 
276
289
  return em,da,ma,fa
277
290
 
@@ -1,16 +1,42 @@
1
1
  import numpy as np
2
2
  from ._tsadeval.metrics import *
3
- from .metric_utils import transform_to_full_series
4
3
  from sklearn.metrics import auc
5
- from .binary_metrics import point_adjusted_precision, point_adjusted_recall, segment_wise_precision, segment_wise_recall
6
4
  from pate.PATE_metric import PATE
7
- def precision_at_k(y_true : np.array ,y_anomaly_scores: np.array):
8
-
5
+ def precision_at_k(y_true : np.array, y_anomaly_scores: np.array):
6
+ """
7
+ Calculate the precision at k score for anomaly detection in time series.
8
+
9
+ This metric evaluates how many of the top-k points (with highest anomaly scores)
10
+ actually correspond to true anomalies. It is particularly useful when we are
11
+ interested in identifying the most anomalous points, without needing to set a
12
+ fixed threshold.
13
+
14
+ The value of k is automatically set to the number of true anomalies present in
15
+ y_true. That is, k = sum(y_true).
16
+
17
+ Parameters:
18
+ y_true (np.array): The ground truth binary labels for the time series data.
19
+ y_anomaly_scores (np.array): The predicted anomaly scores for the time series data.
20
+ """
9
21
  m = PatK_pw(y_true,y_anomaly_scores)
10
22
 
11
23
  return m.get_score()
12
24
 
13
- def auc_roc_pw(y_true : np.array ,y_anomaly_scores: np.array):
25
+ def auc_roc_pw(y_true : np.array, y_anomaly_scores: np.array):
26
+ """
27
+ Calculate the AUC-ROC score for anomaly detection in time series.
28
+
29
+ This is the standard Area Under the Receiver Operating Characteristic Curve (AUC-ROC),
30
+ computed in a point-wise manner. That is, each point in the time series is treated
31
+ independently when calculating true positives, false positives, and false negatives.
32
+
33
+ Parameters:
34
+ y_true (np.array): Ground-truth binary labels for the time series (0 = normal, 1 = anomaly).
35
+ y_anomaly_scores (np.array): Continuous anomaly scores assigned to each point in the series.
36
+
37
+ Returns:
38
+ float: AUC-ROC score.
39
+ """
14
40
 
15
41
  m = AUC_ROC(y_true,y_anomaly_scores)
16
42
 
@@ -18,7 +44,20 @@ def auc_roc_pw(y_true : np.array ,y_anomaly_scores: np.array):
18
44
 
19
45
 
20
46
  def auc_pr_pw(y_true : np.array ,y_anomaly_scores: np.array):
21
-
47
+ """
48
+ Calculate the AUC-PR score for anomaly detection in time series.
49
+
50
+ This is the standard Area Under the Precision-Recall Curve (AUC-PR),
51
+ computed in a point-wise manner. That is, each point in the time series is treated
52
+ independently when calculating precision and recall.
53
+
54
+ Parameters:
55
+ y_true (np.array): Ground-truth binary labels for the time series (0 = normal, 1 = anomaly).
56
+ y_anomaly_scores (np.array): Continuous anomaly scores assigned to each point in the series.
57
+
58
+ Returns:
59
+ float: AUC-PR score.
60
+ """
22
61
  m = AUC_PR_pw(y_true,y_anomaly_scores)
23
62
 
24
63
  return m.get_score()
@@ -26,6 +65,25 @@ def auc_pr_pw(y_true : np.array ,y_anomaly_scores: np.array):
26
65
 
27
66
 
28
67
  def auc_pr_pa(y_true: np.array, y_anomaly_scores: np.array):
68
+ """
69
+ Calculate the AUC-PR score using point-adjusted evaluation for anomaly detection in time series.
70
+
71
+ This is the standard Area Under the Precision-Recall Curve (AUC-PR), but instead of computing
72
+ precision and recall point-wise, it uses a point-adjusted approach. Specifically, for each
73
+ ground-truth anomalous segment, if at least one point within that segment is predicted as anomalous,
74
+ the entire segment is considered correctly detected. The adjusted predictions are then compared
75
+ to the ground-truth labels to compute true positives, false positives, and false negatives,
76
+ which are used to construct the PR curve.
77
+
78
+ Parameters:
79
+ y_true (np.array): Ground-truth binary labels for the time series (0 = normal, 1 = anomaly).
80
+ y_anomaly_scores (np.array): Continuous anomaly scores assigned to each point in the series.
81
+
82
+ Returns:
83
+ float: AUC-PR score (with point-adjusted evaluation).
84
+ """
85
+
86
+
29
87
  precisions = [1]
30
88
  recalls = [0]
31
89
  tps,fps,fns = [],[],[]
@@ -96,6 +154,25 @@ def auc_pr_pa(y_true: np.array, y_anomaly_scores: np.array):
96
154
 
97
155
 
98
156
  def auc_pr_sw(y_true: np.array, y_anomaly_scores: np.array):
157
+ """
158
+ Calculate the AUC-PR score using segment-wise evaluation for anomaly detection in time series.
159
+
160
+ This is the standard Area Under the Precision-Recall Curve (AUC-PR), but it uses a segment-wise
161
+ adjustment when computing precision and recall. In this evaluation, each contiguous segment of
162
+ anomalous ground-truth points is treated as a single unit. A true positive is counted if at least
163
+ one predicted anomaly overlaps with the segment. A false negative occurs when a segment is
164
+ completely missed, and a false positive is recorded for each predicted anomalous segment
165
+ that does not overlap with any ground-truth anomaly. These adjusted counts are then used
166
+ to compute precision and recall for constructing the PR curve.
167
+
168
+ Parameters:
169
+ y_true (np.array): Ground-truth binary labels for the time series (0 = normal, 1 = anomaly).
170
+ y_anomaly_scores (np.array): Continuous anomaly scores assigned to each point in the series.
171
+
172
+ Returns:
173
+ float: AUC-PR score (with segment-wise evaluation).
174
+ """
175
+
99
176
  precisions = [1]
100
177
  recalls = [0]
101
178
  tps,fps,fns = [],[],[]
@@ -185,14 +262,51 @@ def auc_pr_sw(y_true: np.array, y_anomaly_scores: np.array):
185
262
 
186
263
 
187
264
  def vus_roc(y_true : np.array ,y_anomaly_scores: np.array, window=4):
188
-
265
+ """
266
+ Calculate the VUS-ROC (Volume Under the ROC Surface) score for anomaly detection in time series.
267
+
268
+ This metric extends the classical AUC-ROC by introducing a temporal tolerance parameter `l`, which
269
+ smooths the binary ground-truth labels. The idea is to allow a flexible evaluation that tolerates
270
+ small misalignments in the detection of anomalies. The final score is computed by integrating
271
+ the ROC-AUC over different values of the tolerance parameter, from 0 to `window`, thus producing
272
+ a volume under the ROC surface.
273
+
274
+ Parameters:
275
+ y_true (np.array): Ground-truth binary labels (0 = normal, 1 = anomaly).
276
+ y_anomaly_scores (np.array): Anomaly scores for each time point.
277
+ window (int): Maximum temporal tolerance `l` used to smooth the evaluation.
278
+
279
+ Returns:
280
+ float: VUS-ROC score.
281
+
282
+ For more information, see the original paper:
283
+ https://dl.acm.org/doi/10.14778/3551793.3551830
284
+ """
189
285
  m = VUS_ROC(y_true,y_anomaly_scores,max_window=window)
190
286
 
191
287
  return m.get_score()
192
288
 
193
289
 
194
290
  def vus_pr(y_true : np.array ,y_anomaly_scores: np.array, window=4):
195
-
291
+ """
292
+ Calculate the VUS-PR (Volume Under the PR Surface) score for anomaly detection in time series.
293
+
294
+ This metric is an extension of the classical AUC-PR, incorporating a temporal tolerance parameter `l`
295
+ that smooths the binary ground-truth labels. It allows for some flexibility in the detection of
296
+ anomalies that are temporally close to the true events. The final metric integrates the PR-AUC
297
+ over several levels of temporal tolerance (from 0 to `window`), yielding a volume under the PR surface.
298
+
299
+ Parameters:
300
+ y_true (np.array): Ground-truth binary labels (0 = normal, 1 = anomaly).
301
+ y_anomaly_scores (np.array): Anomaly scores for each time point.
302
+ window (int): Maximum temporal tolerance `l` used to smooth the evaluation.
303
+
304
+ Returns:
305
+ float: VUS-PR score.
306
+
307
+ For more information, see the original paper:
308
+ https://dl.acm.org/doi/10.14778/3551793.3551830
309
+ """
196
310
  m = VUS_PR(y_true,y_anomaly_scores,max_window=window)
197
311
 
198
312
  return m.get_score()
@@ -200,17 +314,29 @@ def vus_pr(y_true : np.array ,y_anomaly_scores: np.array, window=4):
200
314
 
201
315
  def real_pate(y_true: np.array, y_anomaly_scores: np.array, early: int, delay: int):
202
316
  """
203
- Calculate PATE score for anomaly detection in time series.
204
- 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.
317
+ Calculate PATE score for anomaly detection in time series using real-valued anomaly scores.
318
+
319
+ This version of PATE evaluates real-valued anomaly scores by assigning weights to predictions
320
+ based on their temporal proximity to the true anomaly intervals. It defines an early buffer of
321
+ length `early` before each anomaly and a delay buffer of length `delay` after it. Detections with
322
+ high scores within the anomaly interval receive full weight, while those in the buffer zones are
323
+ assigned linearly decaying weights depending on their distance from the interval. Scores outside
324
+ these zones contribute to false positives, and intervals with insufficient detection are penalized
325
+ as false negatives.
326
+
327
+ The final PATE score aggregates these weighted contributions across all time steps, yielding
328
+ a smooth performance measure that is sensitive to both the timing and confidence of the predictions.
329
+
330
+ For more information, see the original paper:
331
+ https://arxiv.org/abs/2405.12096
205
332
 
206
333
  Parameters:
207
- y_true (np.array): The ground truth binary labels for the time series data.
208
- y_anomaly_scores (np.array): The predicted binary labels for the time series data.
209
- early (int): The maximum number of time steps before an anomaly must be predicted to be considered early.
210
- delay (int): The maximum number of time steps after an anomaly must be predicted to be considered delayed.
334
+ y_true (np.array): Ground truth binary labels (0 = normal, 1 = anomaly).
335
+ y_anomaly_scores (np.array): Real-valued anomaly scores for each time point.
336
+ early (int): Length of the early buffer zone before each anomaly interval.
337
+ delay (int): Length of the delay buffer zone after each anomaly interval.
211
338
 
212
339
  Returns:
213
- float: The PATE score.
340
+ float: The real-valued PATE score.
214
341
  """
215
-
216
342
  return PATE(y_true, y_anomaly_scores, early, delay, binary_scores=False)
tsadmetrics/utils.py CHANGED
@@ -2,7 +2,7 @@ import numpy as np
2
2
  import pandas as pd
3
3
  import time
4
4
 
5
- def compute_metrics(y_true: np.array,y_pred: np.array,metrics: list, metrics_params: dict, is_anomaly_score = False, verbose = False):
5
+ def compute_metrics(y_true: np.array,y_pred: np.array, metrics: list, metrics_params: dict, is_anomaly_score = False, verbose = False):
6
6
  """
7
7
  Computes the specified metrics for the given true and predicted values.
8
8
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tsadmetrics
3
- Version: 0.1.10
3
+ Version: 0.1.12
4
4
  Summary: =?unknown-8bit?q?Librer=C3=ADa_para_evaluaci=C3=B3n_de_detecci=C3=B3n_de_anomal=C3=ADas?= en series temporales
5
5
  Home-page: https://github.com/pathsko/TSADmetrics
6
6
  Author: Pedro Rafael Velasco Priego
@@ -1,3 +1,4 @@
1
+ docs/conf.py,sha256=skVqctOiByesc7wNDW5DpjyTxUCP0wxlpWA1fsJYZhk,1384
1
2
  entorno/bin/activate_this.py,sha256=45dnJsdtOWIt5LtVSBmBfB8E7AlKcnhnZe9e3WGclak,1199
2
3
  entorno/bin/rst2html.py,sha256=h4RydG-iAectsUra0lNFGwB4_1mngxrtPPgQrxUWQ3A,643
3
4
  entorno/bin/rst2html4.py,sha256=Xiv3Zb1gk4jT7DYFVlf5w4LJtI5ZI3pW3b1KLxyPS5A,765
@@ -12,14 +13,14 @@ entorno/bin/rst2xetex.py,sha256=spisB81JgqAmMAkjdTaP8awFQS_Zuob9HIcbMi1kOS8,922
12
13
  entorno/bin/rst2xml.py,sha256=uoIfpn3prnir2tzqdycsAjOg-OWw663XOK47IeHCZdY,651
13
14
  entorno/bin/rstpep2html.py,sha256=sthYQHEgYfj4JqwG45URwVbRAs-HYuwKget7SUwp9fc,719
14
15
  tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
15
- tests/test_binary.py,sha256=3uLdXzdbQcqLRfXeKDmt2g2XQTU5lZOFQoiy-r9Olqo,29801
16
+ tests/test_binary.py,sha256=dj9BsKBo5rpWw4JGiKKoVkg4rIW4YylTie2VxH2DAGo,29787
16
17
  tests/test_non_binary.py,sha256=syANlwm0DKsL6geGeq6nQI6ZVe6T_YXWTyk2-Hmck4s,11308
17
18
  tsadmetrics/__init__.py,sha256=MTWOa43fgOdkMNo5NglCReRnB8hoF0ob2PIvDziCNHw,1575
18
- tsadmetrics/binary_metrics.py,sha256=nwfPdfHAc_4tJMNlyIwMwFQRLvCU-ik9lQLqlaWLqTs,37741
19
- tsadmetrics/metric_utils.py,sha256=Y_lOE01_uyC22wnw3_G-kKUEJdqevDIWMWvSDE8Cjms,10477
20
- tsadmetrics/non_binary_metrics.py,sha256=hmARpwaYNl_u36uOHcTZqO3nd0LkHpJjPBtbqT6yP_g,6739
19
+ tsadmetrics/binary_metrics.py,sha256=vpUczjPPv1GhTDnFL2fNsHnCuZSRmmGQVsj5te2c6Ss,60116
20
+ tsadmetrics/metric_utils.py,sha256=fm8v0X37_AlqWpkcUT9r3680QsjLljrHe2YuXkRLAZ4,10873
21
+ tsadmetrics/non_binary_metrics.py,sha256=yo620BWZIq-OkBqQV7t7ynjGhcuX6QWQ6iq_7eJq9gI,13074
21
22
  tsadmetrics/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
22
- tsadmetrics/utils.py,sha256=fV5sJE094C_GjBbqrI34Wpy-4hcZtXc9y207ffQB7Mc,2360
23
+ tsadmetrics/utils.py,sha256=15X_RkHdCxhu_-OH8fEm3gRVQ4tTMqCkNaQsQoloEYQ,2361
23
24
  tsadmetrics/_tsadeval/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
24
25
  tsadmetrics/_tsadeval/auc_roc_pr_plot.py,sha256=PHqJUXq2qI248XV9o04D8SsUJgowetaKq0Cu5bYrIAE,12689
25
26
  tsadmetrics/_tsadeval/discontinuity_graph.py,sha256=Ci65l_DPi6HTtb8NvQJe1najgGrRuEpOMWvSyi2AeR0,4088
@@ -52,7 +53,7 @@ tsadmetrics/_tsadeval/prts/time_series_metrics/fscore.py,sha256=pJz4iuPyVGNvwsaR
52
53
  tsadmetrics/_tsadeval/prts/time_series_metrics/precision.py,sha256=jLkcMg7UNl25SHtZUBGkP-RV8HsvaZCtjakryl7PFWU,3204
53
54
  tsadmetrics/_tsadeval/prts/time_series_metrics/precision_recall.py,sha256=OhUJSm_I7VZ_gX_SSg8AYUq3_NW9rMIy7lAVsnOFw4Q,417
54
55
  tsadmetrics/_tsadeval/prts/time_series_metrics/recall.py,sha256=LL-0pPer3ymovVRlktaHo5XDzpgiDhWOVfdPOzKR6og,3152
55
- tsadmetrics-0.1.10.dist-info/METADATA,sha256=qYloOTiFkW1RZqlJMvIAm6rjYg_atf4i11aF6lrCQXU,831
56
- tsadmetrics-0.1.10.dist-info/WHEEL,sha256=iAkIy5fosb7FzIOwONchHf19Qu7_1wCWyFNR5gu9nU0,91
57
- tsadmetrics-0.1.10.dist-info/top_level.txt,sha256=WHaYe-ubr_88yhxe-SaZC8HuAMvlSjXCo-wIdkTeKtA,26
58
- tsadmetrics-0.1.10.dist-info/RECORD,,
56
+ tsadmetrics-0.1.12.dist-info/METADATA,sha256=cVz915nkQa7ViK8Va6rQlW5Z5U6ABoP47rWQmpMnqaY,831
57
+ tsadmetrics-0.1.12.dist-info/WHEEL,sha256=iAkIy5fosb7FzIOwONchHf19Qu7_1wCWyFNR5gu9nU0,91
58
+ tsadmetrics-0.1.12.dist-info/top_level.txt,sha256=s2VIr_ePl-WZbYt9FsYbsDGM7J-Qc5cgpwEOeQ3FVpM,31
59
+ tsadmetrics-0.1.12.dist-info/RECORD,,
@@ -1,3 +1,4 @@
1
+ docs
1
2
  entorno
2
3
  tests
3
4
  tsadmetrics