tsadmetrics 0.1.9__py3-none-any.whl → 0.1.10__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.
tests/test_non_binary.py CHANGED
@@ -202,7 +202,7 @@ class TestAUCPRPA(unittest.TestCase):
202
202
  self.assertAlmostEqual(score, expected_score, places=4)
203
203
 
204
204
  score = round(auc_pr_pa(self.y_true1, self.y_pred3),2)
205
- expected_score = 0.5
205
+ expected_score = 0.75
206
206
  self.assertAlmostEqual(score, expected_score, places=4)
207
207
 
208
208
  if len(self.y_true2) == len(self.y_pred4):
@@ -252,7 +252,7 @@ class TestAUCPRSW(unittest.TestCase):
252
252
  self.assertAlmostEqual(score, expected_score, places=4)
253
253
 
254
254
  score = round(auc_pr_sw(self.y_true1, self.y_pred3),2)
255
- expected_score = 0.5
255
+ expected_score = 1
256
256
  self.assertAlmostEqual(score, expected_score, places=4)
257
257
 
258
258
 
@@ -26,19 +26,69 @@ def auc_pr_pw(y_true : np.array ,y_anomaly_scores: np.array):
26
26
 
27
27
 
28
28
  def auc_pr_pa(y_true: np.array, y_anomaly_scores: np.array):
29
- thresholds = np.unique(y_anomaly_scores)[::-1] # Descending order
30
29
  precisions = [1]
31
30
  recalls = [0]
32
- for t in thresholds[:-1]:
31
+ tps,fps,fns = [],[],[]
33
32
 
34
- y_pred = (y_anomaly_scores >= t).astype(int)
35
-
36
-
37
- precisions.append(point_adjusted_precision(y_true, y_pred))
38
- recalls.append(point_adjusted_recall(y_true, y_pred))
33
+ p_adj = PointAdjust(len(y_true),y_true,(np.array(y_anomaly_scores) >= 0.5).astype(int))
34
+ segments= p_adj.get_gt_anomalies_segmentwise()
35
+ idx = np.argsort(y_anomaly_scores)[::-1].astype(int)
36
+ y_true_sorted = np.array(y_true)[idx]
37
+ y_anomaly_scores_sorted = np.array(y_anomaly_scores)[idx]
38
+
39
+ segment_mins = []
40
+ for start,end in segments:
41
+ anoms_scores = y_anomaly_scores[start:end+1]
42
+ segment_mins.append([np.max(anoms_scores),end-start+1])
43
+
44
+ for i_t in range(len(y_anomaly_scores_sorted)):
45
+ fp,tp,fn = 0,0,0
46
+ if i_t > 0 and y_anomaly_scores_sorted[i_t] == y_anomaly_scores_sorted[i_t-1] :
47
+ tp = tps[-1]
48
+ fp = fps[-1]
49
+ fn = fns[-1]
50
+ else:
51
+ if y_true_sorted[i_t] == 0:
52
+ #FP
53
+ if len(fps)==0:
54
+ aux_y_pred = (y_anomaly_scores >= y_anomaly_scores_sorted[i_t]).astype(int)
55
+ for i in range(len(aux_y_pred)):
56
+ if aux_y_pred[i] == 1 and y_true[i] == 0:
57
+ fp+=1
58
+
59
+
60
+ else:
61
+ fp=fps[i_t-1]+1
62
+ else:
63
+ if len(fps)==0:
64
+ aux_y_pred = (y_anomaly_scores >= y_anomaly_scores_sorted[i_t]).astype(int)
65
+ for i in range(len(aux_y_pred)):
66
+ if aux_y_pred[i] == 1 and y_true[i] == 0:
67
+ fp+=1
68
+ else:
69
+ fp=fps[i_t-1]
70
+ for score, length in segment_mins:
71
+ if score >= y_anomaly_scores_sorted[i_t]:
72
+ #TP
73
+ tp+= length
74
+ else:
75
+ #FN
76
+ fn+= length
77
+ tps.append(tp)
78
+ fns.append(fn)
79
+ fps.append(fp)
80
+ for tp,fp,fn in zip(tps,fps,fns):
81
+ if tp>0:
82
+ precisions.append(tp/(tp+fp))
83
+ recalls.append(tp/(tp+fn))
84
+ else:
85
+ precisions.append(0)
86
+ recalls.append(0)
87
+
39
88
 
40
89
  recalls.append(1)
41
90
  precisions.append(0)
91
+
42
92
  auc_value = auc(recalls, precisions)
43
93
  return auc_value
44
94
 
@@ -46,17 +96,91 @@ def auc_pr_pa(y_true: np.array, y_anomaly_scores: np.array):
46
96
 
47
97
 
48
98
  def auc_pr_sw(y_true: np.array, y_anomaly_scores: np.array):
49
- thresholds = np.unique(y_anomaly_scores)[::-1] # Descending order
50
99
  precisions = [1]
51
100
  recalls = [0]
101
+ tps,fps,fns = [],[],[]
102
+
103
+
104
+ segments = []
105
+ i=0
106
+ while i < len(y_true):
107
+ if y_true[i] == 1:
108
+ start = i
109
+ end = i
110
+ while i < len(y_true) and y_true[i] == 1:
111
+ end = i
112
+ i += 1
113
+ segments.append([start,end])
114
+ i+=1
115
+ idx = np.argsort(y_anomaly_scores)[::-1].astype(int)
116
+ y_anomaly_scores_sorted = np.array(y_anomaly_scores)[idx]
52
117
 
53
- for t in thresholds[:-1]:
54
- y_pred = (y_anomaly_scores >= t).astype(int)
55
- precisions.append(segment_wise_precision(y_true, y_pred))
56
- recalls.append(segment_wise_recall(y_true, y_pred))
57
- recalls.append(1)
58
- precisions.append(0)
118
+ segment_mins = []
119
+ for start,end in segments:
120
+ anoms_scores = y_anomaly_scores[start:end+1]
121
+ segment_mins.append([np.max(anoms_scores),[start,end]])
122
+
123
+ for i_t in range(len(y_anomaly_scores_sorted)):
124
+ fp,tp,fn = 0,0,0
125
+
126
+
127
+ aux_y_pred = (y_anomaly_scores >= y_anomaly_scores_sorted[i_t]).astype(int)
128
+ for score,seg in segment_mins:
129
+ start,end = seg
130
+ if score >= y_anomaly_scores_sorted[i_t]:
131
+ #TP
132
+ tp+= 1
133
+ if aux_y_pred[start]== 1:
134
+ # Extender hacia la izquierda
135
+ i = start - 1
136
+ while i >= 0 and aux_y_pred[i] == 1:
137
+ aux_y_pred[i] = 0
138
+ i -= 1
139
+
140
+ if aux_y_pred[end] == 1:
141
+ # Extender hacia la derecha
142
+ i = end + 1
143
+ while i < len(aux_y_pred) and aux_y_pred[i] == 1:
144
+ aux_y_pred[i] = 0
145
+ i += 1
146
+ aux_y_pred[start:end+1] = 0
147
+
148
+ else:
149
+ #FN
150
+ fn+= 1
151
+
152
+ if np.sum(aux_y_pred)>0:
153
+ fpsegments = []
154
+ i=0
155
+ while i < len(aux_y_pred):
156
+ if aux_y_pred[i] == 1:
157
+ start = i
158
+ end = i
159
+ while i < len(aux_y_pred) and aux_y_pred[i] == 1:
160
+ end = i
161
+ i += 1
162
+ fpsegments.append([start,end])
163
+ i+=1
164
+ fp = len(fpsegments)
165
+ else:
166
+ fp = 0
167
+
168
+
169
+ tps.append(tp)
170
+ fns.append(fn)
171
+ fps.append(fp)
172
+ for tp,fp,fn in zip(tps,fps,fns):
173
+ if tp>0:
174
+ precisions.append(tp/(tp+fp))
175
+ recalls.append(tp/(tp+fn))
176
+ else:
177
+ precisions.append(0)
178
+ recalls.append(0)
179
+
180
+
181
+
59
182
  auc_value = auc(recalls, precisions)
183
+
60
184
  return auc_value
61
185
 
62
186
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tsadmetrics
3
- Version: 0.1.9
3
+ Version: 0.1.10
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
@@ -13,11 +13,11 @@ entorno/bin/rst2xml.py,sha256=uoIfpn3prnir2tzqdycsAjOg-OWw663XOK47IeHCZdY,651
13
13
  entorno/bin/rstpep2html.py,sha256=sthYQHEgYfj4JqwG45URwVbRAs-HYuwKget7SUwp9fc,719
14
14
  tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
15
15
  tests/test_binary.py,sha256=3uLdXzdbQcqLRfXeKDmt2g2XQTU5lZOFQoiy-r9Olqo,29801
16
- tests/test_non_binary.py,sha256=PRvsgGbwbRXVNkYBk2-oJw8ekLW4DWJkEZhAWO0L9sk,11309
16
+ tests/test_non_binary.py,sha256=syANlwm0DKsL6geGeq6nQI6ZVe6T_YXWTyk2-Hmck4s,11308
17
17
  tsadmetrics/__init__.py,sha256=MTWOa43fgOdkMNo5NglCReRnB8hoF0ob2PIvDziCNHw,1575
18
18
  tsadmetrics/binary_metrics.py,sha256=nwfPdfHAc_4tJMNlyIwMwFQRLvCU-ik9lQLqlaWLqTs,37741
19
19
  tsadmetrics/metric_utils.py,sha256=Y_lOE01_uyC22wnw3_G-kKUEJdqevDIWMWvSDE8Cjms,10477
20
- tsadmetrics/non_binary_metrics.py,sha256=JIOvkigSjHBZLKbGJj7ESe0lPM7P1JPoIUnbiMZuuLg,2896
20
+ tsadmetrics/non_binary_metrics.py,sha256=hmARpwaYNl_u36uOHcTZqO3nd0LkHpJjPBtbqT6yP_g,6739
21
21
  tsadmetrics/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
22
22
  tsadmetrics/utils.py,sha256=fV5sJE094C_GjBbqrI34Wpy-4hcZtXc9y207ffQB7Mc,2360
23
23
  tsadmetrics/_tsadeval/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -52,7 +52,7 @@ tsadmetrics/_tsadeval/prts/time_series_metrics/fscore.py,sha256=pJz4iuPyVGNvwsaR
52
52
  tsadmetrics/_tsadeval/prts/time_series_metrics/precision.py,sha256=jLkcMg7UNl25SHtZUBGkP-RV8HsvaZCtjakryl7PFWU,3204
53
53
  tsadmetrics/_tsadeval/prts/time_series_metrics/precision_recall.py,sha256=OhUJSm_I7VZ_gX_SSg8AYUq3_NW9rMIy7lAVsnOFw4Q,417
54
54
  tsadmetrics/_tsadeval/prts/time_series_metrics/recall.py,sha256=LL-0pPer3ymovVRlktaHo5XDzpgiDhWOVfdPOzKR6og,3152
55
- tsadmetrics-0.1.9.dist-info/METADATA,sha256=tFW_YWxVHIkXA2qGqORTi2HGHfZ1akAE5aAKQrh7yhI,830
56
- tsadmetrics-0.1.9.dist-info/WHEEL,sha256=iAkIy5fosb7FzIOwONchHf19Qu7_1wCWyFNR5gu9nU0,91
57
- tsadmetrics-0.1.9.dist-info/top_level.txt,sha256=WHaYe-ubr_88yhxe-SaZC8HuAMvlSjXCo-wIdkTeKtA,26
58
- tsadmetrics-0.1.9.dist-info/RECORD,,
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,,