tsadmetrics 0.1.1__py3-none-any.whl → 0.1.3__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,30 @@
1
+ # import numpy as np
2
+ # from matplotlib import pyplot as plt
3
+
4
+
5
+ # x = np.arange(48)
6
+
7
+ # y = np.sin(0.7 + x / 12) + np.sin(x / 4 + 29) + 0.1 * np.sin(1.25 * x) * (np.cos(np.sqrt(1.25 * x) + 2)) + x / 32 + 0.12
8
+
9
+
10
+ # figsize = (3.4, 2)
11
+ # plt.figure(figsize=figsize)
12
+
13
+ # plt.plot(x, y)
14
+
15
+ # for t in [0.5, 1, 1.5, 2, 2.5]:
16
+ # plt.plot(x, x + t - x, ".", color="dimgray")
17
+ # for i in range(len(x)):
18
+ # if t < y[i]:
19
+ # plt.plot([x[i]], [t], ".r")
20
+ # plt.plot([x[i]], [t], "xw", markersize=2)
21
+
22
+ # fs = 7
23
+ # plt.xlabel("Time", fontsize=fs)
24
+ # plt.ylabel("Anomaly score / Threshold", fontsize=fs)
25
+ # plt.xticks(fontsize=fs)
26
+ # plt.yticks(fontsize=fs)
27
+
28
+ # plt.tight_layout()
29
+ # plt.savefig("thr2.pdf")
30
+ # plt.show()
@@ -0,0 +1,33 @@
1
+ # MIT License
2
+ #
3
+ # Copyright (c) 2020 Erik Scharwächter
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ import numpy as np
24
+
25
+ def time_tolerant_recall_(A, E, d):
26
+ N_E = float(E.sum())
27
+ T = len(E)
28
+ return len([t for t in range(d, T-d) if (E[t] == 1) and np.sum(A[(t-d):(t+d)+1]) >= 1])/N_E
29
+
30
+ def time_tolerant_precision_(A, E, d):
31
+ N_A = float(A.sum())
32
+ T = len(E)
33
+ return len([t for t in range(d, T-d) if (A[t] == 1) and np.sum(E[(t-d):(t+d)+1]) >= 1])/N_A
@@ -0,0 +1,263 @@
1
+ import numpy as np
2
+
3
+ # This file includes code derived from the VUS project:
4
+ # https://github.com/TheDatumOrg/VUS
5
+ #
6
+ # No license was specified in the original repository at the time of inclusion (April 2025),
7
+ # which may imply that all rights are reserved by the original author(s).
8
+ #
9
+ # This code has been copied and modified for internal use only within the tsadmetrics project.
10
+ # If you are the author or copyright holder and would like us to remove or relicense
11
+ # this code, please contact us.
12
+ #
13
+ # This file is NOT intended for redistribution as a standalone component or as a derivative
14
+ # of the original VUS project unless proper licensing is clarified.
15
+
16
+
17
+ def range_convers_new( label):
18
+ '''
19
+ input: arrays of binary values
20
+ output: list of ordered pair [[a0,b0], [a1,b1]... ] of the inputs
21
+ '''
22
+ L = []
23
+ i = 0
24
+ j = 0
25
+ while j < len(label):
26
+ # print(i)
27
+ while label[i] == 0:
28
+ i+=1
29
+ if i >= len(label): #?
30
+ break #?
31
+ j = i+1
32
+ # print('j'+str(j))
33
+ if j >= len(label):
34
+ if j==len(label):
35
+ L.append((i,j-1))
36
+
37
+ break
38
+ while label[j] != 0:
39
+ j+=1
40
+ if j >= len(label):
41
+ L.append((i,j-1))
42
+ break
43
+ if j >= len(label):
44
+ break
45
+ L.append((i, j-1))
46
+ i = j
47
+ return L
48
+
49
+ def new_sequence(label, sequence_original, window):
50
+ a = max(sequence_original[0][0] - window // 2, 0)
51
+ sequence_new = []
52
+ for i in range(len(sequence_original) - 1):
53
+ if sequence_original[i][1] + window // 2 < sequence_original[i + 1][0] - window // 2:
54
+ sequence_new.append((a, sequence_original[i][1] + window // 2))
55
+ a = sequence_original[i + 1][0] - window // 2
56
+ sequence_new.append((a, min(sequence_original[len(sequence_original) - 1][1] + window // 2, len(label) - 1)))
57
+ return sequence_new
58
+
59
+
60
+ def sequencing(x, L, window=5):
61
+ label = x.copy().astype(float)
62
+ length = len(label)
63
+
64
+ for k in range(len(L)):
65
+ s = L[k][0]
66
+ e = L[k][1]
67
+
68
+ x1 = np.arange(e + 1, min(e + window // 2 + 1, length))
69
+ label[x1] += np.sqrt(1 - (x1 - e) / (window))
70
+
71
+ x2 = np.arange(max(s - window // 2, 0), s)
72
+ label[x2] += np.sqrt(1 - (s - x2) / (window))
73
+
74
+ label = np.minimum(np.ones(length), label)
75
+ return label
76
+
77
+
78
+ def RangeAUC_volume_opt_mem(labels_original, score, windowSize, thre=250):
79
+ window_3d = np.arange(0, windowSize + 1, 1)
80
+ P = np.sum(labels_original)
81
+ seq = range_convers_new(labels_original)
82
+ l = new_sequence(labels_original, seq, windowSize)
83
+
84
+ score_sorted = -np.sort(-score)
85
+
86
+ tpr_3d = np.zeros((windowSize + 1, thre + 2))
87
+ fpr_3d = np.zeros((windowSize + 1, thre + 2))
88
+ prec_3d = np.zeros((windowSize + 1, thre + 1))
89
+
90
+ auc_3d = np.zeros(windowSize + 1)
91
+ ap_3d = np.zeros(windowSize + 1)
92
+
93
+ tp = np.zeros(thre)
94
+ N_pred = np.zeros(thre)
95
+ p = np.zeros((thre, len(score)))
96
+
97
+ for k, i in enumerate(np.linspace(0, len(score) - 1, thre).astype(int)):
98
+ threshold = score_sorted[i]
99
+ pred = score >= threshold
100
+ p[k] = pred
101
+ N_pred[k] = np.sum(pred)
102
+
103
+ for window in window_3d:
104
+ labels_extended = sequencing(labels_original, seq, window)
105
+ L = new_sequence(labels_extended, seq, window)
106
+
107
+ TF_list = np.zeros((thre + 2, 2))
108
+ Precision_list = np.ones(thre + 1)
109
+ j = 0
110
+
111
+ for i in np.linspace(0, len(score) - 1, thre).astype(int):
112
+ labels = labels_extended.copy()
113
+ existence = 0
114
+
115
+ for seg in L:
116
+ labels[seg[0]:seg[1] + 1] = labels_extended[seg[0]:seg[1] + 1] * p[j][seg[0]:seg[1] + 1]
117
+ if (p[j][seg[0]:(seg[1] + 1)] > 0).any():
118
+ existence += 1
119
+ for seg in seq:
120
+ labels[seg[0]:seg[1] + 1] = 1
121
+
122
+ N_labels = 0
123
+ TP = 0
124
+ for seg in l:
125
+ TP += np.dot(labels[seg[0]:seg[1] + 1], p[j][seg[0]:seg[1] + 1])
126
+ N_labels += np.sum(labels[seg[0]:seg[1] + 1])
127
+
128
+ TP += tp[j]
129
+ FP = N_pred[j] - TP
130
+
131
+ existence_ratio = existence / len(L)
132
+
133
+ P_new = (P + N_labels) / 2
134
+ recall = min(TP / P_new, 1)
135
+
136
+ TPR = recall * existence_ratio
137
+
138
+ N_new = len(labels) - P_new
139
+ FPR = FP / N_new
140
+ Precision = TP / N_pred[j]
141
+ j += 1
142
+
143
+ TF_list[j] = [TPR, FPR]
144
+ Precision_list[j] = Precision
145
+
146
+ TF_list[j + 1] = [1, 1]
147
+ tpr_3d[window] = TF_list[:, 0]
148
+ fpr_3d[window] = TF_list[:, 1]
149
+ prec_3d[window] = Precision_list
150
+
151
+ width = TF_list[1:, 1] - TF_list[:-1, 1]
152
+ height = (TF_list[1:, 0] + TF_list[:-1, 0]) / 2
153
+ AUC_range = np.dot(width, height)
154
+ auc_3d[window] = (AUC_range)
155
+
156
+ width_PR = TF_list[1:-1, 0] - TF_list[:-2, 0]
157
+ height_PR = Precision_list[1:]
158
+ AP_range = np.dot(width_PR, height_PR)
159
+ ap_3d[window] = (AP_range)
160
+ return tpr_3d, fpr_3d, prec_3d, window_3d, sum(auc_3d) / len(window_3d), sum(ap_3d) / len(window_3d)
161
+
162
+ def RangeAUC_volume_opt( labels_original, score, windowSize, thre=250):
163
+ window_3d = np.arange(0, windowSize + 1, 1)
164
+ P = np.sum(labels_original)
165
+ seq = range_convers_new(labels_original)
166
+ l = new_sequence(labels_original, seq, windowSize)
167
+
168
+ score_sorted = -np.sort(-score)
169
+
170
+ tpr_3d = np.zeros((windowSize + 1, thre + 2))
171
+ fpr_3d = np.zeros((windowSize + 1, thre + 2))
172
+ prec_3d = np.zeros((windowSize + 1, thre + 1))
173
+
174
+ auc_3d = np.zeros(windowSize + 1)
175
+ ap_3d = np.zeros(windowSize + 1)
176
+
177
+ tp = np.zeros(thre)
178
+ N_pred = np.zeros(thre)
179
+
180
+ for k, i in enumerate(np.linspace(0, len(score) - 1, thre).astype(int)):
181
+ threshold = score_sorted[i]
182
+ pred = score >= threshold
183
+ N_pred[k] = np.sum(pred)
184
+
185
+ for window in window_3d:
186
+
187
+ labels_extended = sequencing(labels_original, seq, window)
188
+ L = new_sequence(labels_extended, seq, window)
189
+
190
+ TF_list = np.zeros((thre + 2, 2))
191
+ Precision_list = np.ones(thre + 1)
192
+ j = 0
193
+
194
+ for i in np.linspace(0, len(score) - 1, thre).astype(int):
195
+ threshold = score_sorted[i]
196
+ pred = score >= threshold
197
+ labels = labels_extended.copy()
198
+ existence = 0
199
+
200
+ for seg in L:
201
+ labels[seg[0]:seg[1] + 1] = labels_extended[seg[0]:seg[1] + 1] * pred[seg[0]:seg[1] + 1]
202
+ if (pred[seg[0]:(seg[1] + 1)] > 0).any():
203
+ existence += 1
204
+ for seg in seq:
205
+ labels[seg[0]:seg[1] + 1] = 1
206
+
207
+ TP = 0
208
+ N_labels = 0
209
+ for seg in l:
210
+ TP += np.dot(labels[seg[0]:seg[1] + 1], pred[seg[0]:seg[1] + 1])
211
+ N_labels += np.sum(labels[seg[0]:seg[1] + 1])
212
+
213
+ TP += tp[j]
214
+ FP = N_pred[j] - TP
215
+
216
+ existence_ratio = existence / len(L)
217
+
218
+ P_new = (P + N_labels) / 2
219
+ recall = min(TP / P_new, 1)
220
+
221
+ TPR = recall * existence_ratio
222
+ N_new = len(labels) - P_new
223
+ FPR = FP / N_new
224
+
225
+ Precision = TP / N_pred[j]
226
+
227
+ j += 1
228
+ TF_list[j] = [TPR, FPR]
229
+ Precision_list[j] = Precision
230
+
231
+ TF_list[j + 1] = [1, 1] # otherwise, range-AUC will stop earlier than (1,1)
232
+
233
+ tpr_3d[window] = TF_list[:, 0]
234
+ fpr_3d[window] = TF_list[:, 1]
235
+ prec_3d[window] = Precision_list
236
+
237
+ width = TF_list[1:, 1] - TF_list[:-1, 1]
238
+ height = (TF_list[1:, 0] + TF_list[:-1, 0]) / 2
239
+ AUC_range = np.dot(width, height)
240
+ auc_3d[window] = (AUC_range)
241
+
242
+ width_PR = TF_list[1:-1, 0] - TF_list[:-2, 0]
243
+ height_PR = Precision_list[1:]
244
+
245
+ AP_range = np.dot(width_PR, height_PR)
246
+ ap_3d[window] = AP_range
247
+
248
+ return tpr_3d, fpr_3d, prec_3d, window_3d, sum(auc_3d) / len(window_3d), sum(ap_3d) / len(window_3d)
249
+
250
+ def generate_curve(label,score,slidingWindow, version='opt', thre=250):
251
+ if version =='opt_mem':
252
+ tpr_3d, fpr_3d, prec_3d, window_3d, avg_auc_3d, avg_ap_3d = RangeAUC_volume_opt_mem(labels_original=label, score=score, windowSize=slidingWindow, thre=thre)
253
+ else:
254
+ tpr_3d, fpr_3d, prec_3d, window_3d, avg_auc_3d, avg_ap_3d = RangeAUC_volume_opt(labels_original=label, score=score, windowSize=slidingWindow, thre=thre)
255
+
256
+ X = np.array(tpr_3d).reshape(1,-1).ravel()
257
+ X_ap = np.array(tpr_3d)[:,:-1].reshape(1,-1).ravel()
258
+ Y = np.array(fpr_3d).reshape(1,-1).ravel()
259
+ W = np.array(prec_3d).reshape(1,-1).ravel()
260
+ Z = np.repeat(window_3d, len(tpr_3d[0]))
261
+ Z_ap = np.repeat(window_3d, len(tpr_3d[0])-1)
262
+
263
+ return Y, Z, X, X_ap, W, Z_ap,avg_auc_3d, avg_ap_3d
@@ -3,8 +3,8 @@ from .metric_utils import *
3
3
  from .metric_utils import get_events, calculate_intersection
4
4
 
5
5
 
6
- from _tsadeval.metrics import *
7
- from _tsadeval.prts.basic_metrics_ts import ts_fscore
6
+ from ._tsadeval.metrics import *
7
+ from ._tsadeval.prts.basic_metrics_ts import ts_fscore
8
8
  from pate.PATE_metric import PATE
9
9
  def point_wise_recall(y_true: np.array, y_pred: np.array):
10
10
  """
@@ -1,5 +1,5 @@
1
1
  import numpy as np
2
- from _tsadeval.metrics import Binary_anomalies, pointwise_to_full_series, segmentwise_to_full_series, DelayThresholdedPointAdjust
2
+ from ._tsadeval.metrics import Binary_anomalies, pointwise_to_full_series, segmentwise_to_full_series, DelayThresholdedPointAdjust
3
3
  def get_tp_tn_fp_fn_point_wise(y_true: np.array,y_pred: np.array):
4
4
  TP,TN,FP,FN=0,0,0,0
5
5
  for true,pred in zip(y_true,y_pred):
@@ -1,5 +1,5 @@
1
1
  import numpy as np
2
- from _tsadeval.metrics import *
2
+ from ._tsadeval.metrics import *
3
3
  from .metric_utils import transform_to_full_series
4
4
  from sklearn.metrics import auc
5
5
  from .binary_metrics import point_adjusted_precision, point_adjusted_recall, segment_wise_precision, segment_wise_recall
@@ -0,0 +1,23 @@
1
+ Metadata-Version: 2.1
2
+ Name: tsadmetrics
3
+ Version: 0.1.3
4
+ Summary: Librería para evaluación de detección de anomalías en series temporales
5
+ Home-page: https://github.com/pathsko/TSADmetrics
6
+ Author: Pedro Rafael Velasco Priego
7
+ Author-email: Pedro Rafael Velasco Priego <i12veprp@uco.es>
8
+ Requires-Python: >=3.8
9
+ Description-Content-Type: text/markdown
10
+ Requires-Dist: joblib==1.4.2
11
+ Requires-Dist: numpy==1.24.4
12
+ Requires-Dist: pandas==2.0.3
13
+ Requires-Dist: PATE==0.1.1
14
+ Requires-Dist: patsy==0.5.6
15
+ Requires-Dist: python-dateutil==2.9.0.post0
16
+ Requires-Dist: pytz==2024.1
17
+ Requires-Dist: scikit-learn==1.3.2
18
+ Requires-Dist: scipy==1.10.1
19
+ Requires-Dist: six==1.16.0
20
+ Requires-Dist: statsmodels==0.14.1
21
+ Requires-Dist: threadpoolctl==3.5.0
22
+ Requires-Dist: tzdata==2024.1
23
+
@@ -0,0 +1,20 @@
1
+ tsadmetrics/__init__.py,sha256=MTWOa43fgOdkMNo5NglCReRnB8hoF0ob2PIvDziCNHw,1575
2
+ tsadmetrics/binary_metrics.py,sha256=nwfPdfHAc_4tJMNlyIwMwFQRLvCU-ik9lQLqlaWLqTs,37741
3
+ tsadmetrics/metric_utils.py,sha256=Y_lOE01_uyC22wnw3_G-kKUEJdqevDIWMWvSDE8Cjms,10477
4
+ tsadmetrics/non_binary_metrics.py,sha256=JIOvkigSjHBZLKbGJj7ESe0lPM7P1JPoIUnbiMZuuLg,2896
5
+ tsadmetrics/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ tsadmetrics/utils.py,sha256=G0yWxgTZ9MBzyB0XDLrO2TMwmtm4hssDp5Sr0CG9FqY,1834
7
+ tsadmetrics/_tsadeval/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
+ tsadmetrics/_tsadeval/auc_roc_pr_plot.py,sha256=PHqJUXq2qI248XV9o04D8SsUJgowetaKq0Cu5bYrIAE,12689
9
+ tsadmetrics/_tsadeval/discontinuity_graph.py,sha256=Ci65l_DPi6HTtb8NvQJe1najgGrRuEpOMWvSyi2AeR0,4088
10
+ tsadmetrics/_tsadeval/latency_sparsity_aware.py,sha256=92wt6ARSXL5Y-281joNaSu1E7hnkIbl3m6gyzODTYBE,12092
11
+ tsadmetrics/_tsadeval/metrics.py,sha256=d-1VpJu_mp8gZjW2FeD7eqkFKEkGsYcsy6DcSGK4kSk,24100
12
+ tsadmetrics/_tsadeval/nabscore.py,sha256=8H4cgzzjXrbQzpI-YKEJj31eSGSROrT7NNC86n9d2yY,11696
13
+ tsadmetrics/_tsadeval/tests.py,sha256=KjFPlEHWYkxHXtaEs1_WiTgATEtr7kPKQbgsljSxJ8o,12697
14
+ tsadmetrics/_tsadeval/threshold_plt.py,sha256=ExgxIcsDMmgLNveNug5fimEhEe6Km0g68npQj-7oWOE,726
15
+ tsadmetrics/_tsadeval/time_tolerant.py,sha256=duq3B58ohjS6QkWdNUuCQFt2xmCJ0dMWTVzOr6E3H0A,1486
16
+ tsadmetrics/_tsadeval/vus_utils.py,sha256=XL5tV9hxBW8aGkobT84cp2FdHNuNZ3PUgaplwHsDjNk,7868
17
+ tsadmetrics-0.1.3.dist-info/METADATA,sha256=OhHUxt6YdnWC49fL3j-UP-HjfGrNudEJwwrA1Mju07s,756
18
+ tsadmetrics-0.1.3.dist-info/WHEEL,sha256=iAkIy5fosb7FzIOwONchHf19Qu7_1wCWyFNR5gu9nU0,91
19
+ tsadmetrics-0.1.3.dist-info/top_level.txt,sha256=rRMFvkwJRUuenl0__YY_3BNr-rkdhAdj28iICkpC5a4,12
20
+ tsadmetrics-0.1.3.dist-info/RECORD,,
@@ -1,2 +0,0 @@
1
- from .utils import get_events
2
-
@@ -1,10 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: tsadmetrics
3
- Version: 0.1.1
4
- Summary: Librería para evaluación de detección de anomalías en series temporales
5
- Home-page: https://github.com/pathsko/TSADmetrics
6
- Author: Pedro Rafael Velasco Priego
7
- Author-email: Pedro Rafael Velasco Priego <i12veprp@uco.es>
8
- Requires-Python: >=3.6
9
- Description-Content-Type: text/markdown
10
-
@@ -1,11 +0,0 @@
1
- tsadmetrics/__init__.py,sha256=z6NknOSgSmwBxsWO6FcxA3yWSVHsCGyqi_f_XaCfDqc,1682
2
- tsadmetrics/binary_metrics.py,sha256=6yp4Dqg0_M_bJvyoWVgjAr4rLnGIDzHd7CzCRKqJVso,37739
3
- tsadmetrics/metric_utils.py,sha256=HtcgQ3sqnfoOQ-QnDctNiaRGDXRuFvGzMGhoS2HWYIg,10476
4
- tsadmetrics/non_binary_metrics.py,sha256=2F_qsV7wf-IMAB158uA9U0DC5l2apTAyK6XPN2roI7k,2895
5
- tsadmetrics/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- tsadmetrics/ts_aware_utils.py,sha256=RbgSAVLWeMtA0ZrvBw-_lT0b0ygFn9a1TxSW5cQAwmg,31
7
- tsadmetrics/utils.py,sha256=G0yWxgTZ9MBzyB0XDLrO2TMwmtm4hssDp5Sr0CG9FqY,1834
8
- tsadmetrics-0.1.1.dist-info/METADATA,sha256=FA9RKtLnaQgRv9EQ4hRuS2yRU4vfkWxA0wL5rw6Z0a0,350
9
- tsadmetrics-0.1.1.dist-info/WHEEL,sha256=iAkIy5fosb7FzIOwONchHf19Qu7_1wCWyFNR5gu9nU0,91
10
- tsadmetrics-0.1.1.dist-info/top_level.txt,sha256=rRMFvkwJRUuenl0__YY_3BNr-rkdhAdj28iICkpC5a4,12
11
- tsadmetrics-0.1.1.dist-info/RECORD,,