tsadmetrics 0.1.16__py3-none-any.whl → 1.0.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.
Files changed (148) hide show
  1. docs/api_doc/conf.py +67 -0
  2. docs/{conf.py → full_doc/conf.py} +1 -1
  3. docs/manual_doc/conf.py +67 -0
  4. examples/example_direct_data.py +28 -0
  5. examples/example_direct_single_data.py +25 -0
  6. examples/example_file_reference.py +24 -0
  7. examples/example_global_config_file.py +13 -0
  8. examples/example_metric_config_file.py +19 -0
  9. examples/example_simple_metric.py +8 -0
  10. examples/specific_examples/AbsoluteDetectionDistance_example.py +24 -0
  11. examples/specific_examples/AffiliationbasedFScore_example.py +24 -0
  12. examples/specific_examples/AverageDetectionCount_example.py +24 -0
  13. examples/specific_examples/CompositeFScore_example.py +24 -0
  14. examples/specific_examples/DelayThresholdedPointadjustedFScore_example.py +24 -0
  15. examples/specific_examples/DetectionAccuracyInRange_example.py +24 -0
  16. examples/specific_examples/EnhancedTimeseriesAwareFScore_example.py +24 -0
  17. examples/specific_examples/LatencySparsityawareFScore_example.py +24 -0
  18. examples/specific_examples/MeanTimeToDetect_example.py +24 -0
  19. examples/specific_examples/NabScore_example.py +24 -0
  20. examples/specific_examples/PateFScore_example.py +24 -0
  21. examples/specific_examples/Pate_example.py +24 -0
  22. examples/specific_examples/PointadjustedAtKFScore_example.py +24 -0
  23. examples/specific_examples/PointadjustedAucPr_example.py +24 -0
  24. examples/specific_examples/PointadjustedAucRoc_example.py +24 -0
  25. examples/specific_examples/PointadjustedFScore_example.py +24 -0
  26. examples/specific_examples/RangebasedFScore_example.py +24 -0
  27. examples/specific_examples/SegmentwiseFScore_example.py +24 -0
  28. examples/specific_examples/TemporalDistance_example.py +24 -0
  29. examples/specific_examples/TimeTolerantFScore_example.py +24 -0
  30. examples/specific_examples/TimeseriesAwareFScore_example.py +24 -0
  31. examples/specific_examples/TotalDetectedInRange_example.py +24 -0
  32. examples/specific_examples/VusPr_example.py +24 -0
  33. examples/specific_examples/VusRoc_example.py +24 -0
  34. examples/specific_examples/WeightedDetectionDifference_example.py +24 -0
  35. tests/test_dpm.py +212 -0
  36. tests/test_ptdm.py +366 -0
  37. tests/test_registry.py +58 -0
  38. tests/test_runner.py +185 -0
  39. tests/test_spm.py +213 -0
  40. tests/test_tmem.py +198 -0
  41. tests/test_tpdm.py +369 -0
  42. tests/test_tstm.py +338 -0
  43. tsadmetrics/__init__.py +0 -21
  44. tsadmetrics/base/Metric.py +188 -0
  45. tsadmetrics/evaluation/Report.py +25 -0
  46. tsadmetrics/evaluation/Runner.py +253 -0
  47. tsadmetrics/metrics/Registry.py +141 -0
  48. tsadmetrics/metrics/__init__.py +2 -0
  49. tsadmetrics/metrics/spm/PointwiseAucPr.py +62 -0
  50. tsadmetrics/metrics/spm/PointwiseAucRoc.py +63 -0
  51. tsadmetrics/metrics/spm/PointwiseFScore.py +86 -0
  52. tsadmetrics/metrics/spm/PrecisionAtK.py +81 -0
  53. tsadmetrics/metrics/spm/__init__.py +9 -0
  54. tsadmetrics/metrics/tem/dpm/DelayThresholdedPointadjustedFScore.py +83 -0
  55. tsadmetrics/metrics/tem/dpm/LatencySparsityawareFScore.py +76 -0
  56. tsadmetrics/metrics/tem/dpm/MeanTimeToDetect.py +47 -0
  57. tsadmetrics/metrics/tem/dpm/NabScore.py +60 -0
  58. tsadmetrics/metrics/tem/dpm/__init__.py +11 -0
  59. tsadmetrics/metrics/tem/ptdm/AverageDetectionCount.py +53 -0
  60. tsadmetrics/metrics/tem/ptdm/DetectionAccuracyInRange.py +66 -0
  61. tsadmetrics/metrics/tem/ptdm/PointadjustedAtKFScore.py +80 -0
  62. tsadmetrics/metrics/tem/ptdm/TimeseriesAwareFScore.py +248 -0
  63. tsadmetrics/metrics/tem/ptdm/TotalDetectedInRange.py +65 -0
  64. tsadmetrics/metrics/tem/ptdm/WeightedDetectionDifference.py +97 -0
  65. tsadmetrics/metrics/tem/ptdm/__init__.py +12 -0
  66. tsadmetrics/metrics/tem/tmem/AbsoluteDetectionDistance.py +48 -0
  67. tsadmetrics/metrics/tem/tmem/EnhancedTimeseriesAwareFScore.py +252 -0
  68. tsadmetrics/metrics/tem/tmem/TemporalDistance.py +68 -0
  69. tsadmetrics/metrics/tem/tmem/__init__.py +9 -0
  70. tsadmetrics/metrics/tem/tpdm/CompositeFScore.py +104 -0
  71. tsadmetrics/metrics/tem/tpdm/PointadjustedAucPr.py +123 -0
  72. tsadmetrics/metrics/tem/tpdm/PointadjustedAucRoc.py +119 -0
  73. tsadmetrics/metrics/tem/tpdm/PointadjustedFScore.py +96 -0
  74. tsadmetrics/metrics/tem/tpdm/RangebasedFScore.py +236 -0
  75. tsadmetrics/metrics/tem/tpdm/SegmentwiseFScore.py +73 -0
  76. tsadmetrics/metrics/tem/tpdm/__init__.py +12 -0
  77. tsadmetrics/metrics/tem/tstm/AffiliationbasedFScore.py +68 -0
  78. tsadmetrics/metrics/tem/tstm/Pate.py +62 -0
  79. tsadmetrics/metrics/tem/tstm/PateFScore.py +61 -0
  80. tsadmetrics/metrics/tem/tstm/TimeTolerantFScore.py +85 -0
  81. tsadmetrics/metrics/tem/tstm/VusPr.py +51 -0
  82. tsadmetrics/metrics/tem/tstm/VusRoc.py +55 -0
  83. tsadmetrics/metrics/tem/tstm/__init__.py +15 -0
  84. tsadmetrics/{_tsadeval/affiliation/_integral_interval.py → utils/functions_affiliation.py} +377 -9
  85. tsadmetrics/utils/functions_auc.py +393 -0
  86. tsadmetrics/utils/functions_conversion.py +63 -0
  87. tsadmetrics/utils/functions_counting_metrics.py +26 -0
  88. tsadmetrics/{_tsadeval/latency_sparsity_aware.py → utils/functions_latency_sparsity_aware.py} +1 -1
  89. tsadmetrics/{_tsadeval/nabscore.py → utils/functions_nabscore.py} +15 -1
  90. tsadmetrics-1.0.0.dist-info/METADATA +69 -0
  91. tsadmetrics-1.0.0.dist-info/RECORD +99 -0
  92. {tsadmetrics-0.1.16.dist-info → tsadmetrics-1.0.0.dist-info}/top_level.txt +1 -1
  93. entorno/bin/activate_this.py +0 -32
  94. entorno/bin/rst2html.py +0 -23
  95. entorno/bin/rst2html4.py +0 -26
  96. entorno/bin/rst2html5.py +0 -33
  97. entorno/bin/rst2latex.py +0 -26
  98. entorno/bin/rst2man.py +0 -27
  99. entorno/bin/rst2odt.py +0 -28
  100. entorno/bin/rst2odt_prepstyles.py +0 -20
  101. entorno/bin/rst2pseudoxml.py +0 -23
  102. entorno/bin/rst2s5.py +0 -24
  103. entorno/bin/rst2xetex.py +0 -27
  104. entorno/bin/rst2xml.py +0 -23
  105. entorno/bin/rstpep2html.py +0 -25
  106. tests/test_binary.py +0 -946
  107. tests/test_non_binary.py +0 -420
  108. tests/test_utils.py +0 -49
  109. tsadmetrics/_tsadeval/affiliation/_affiliation_zone.py +0 -86
  110. tsadmetrics/_tsadeval/affiliation/_single_ground_truth_event.py +0 -68
  111. tsadmetrics/_tsadeval/affiliation/generics.py +0 -135
  112. tsadmetrics/_tsadeval/affiliation/metrics.py +0 -114
  113. tsadmetrics/_tsadeval/auc_roc_pr_plot.py +0 -295
  114. tsadmetrics/_tsadeval/discontinuity_graph.py +0 -109
  115. tsadmetrics/_tsadeval/eTaPR_pkg/DataManage/File_IO.py +0 -175
  116. tsadmetrics/_tsadeval/eTaPR_pkg/DataManage/Range.py +0 -50
  117. tsadmetrics/_tsadeval/eTaPR_pkg/DataManage/Time_Plot.py +0 -184
  118. tsadmetrics/_tsadeval/eTaPR_pkg/__init__.py +0 -0
  119. tsadmetrics/_tsadeval/eTaPR_pkg/etapr.py +0 -386
  120. tsadmetrics/_tsadeval/eTaPR_pkg/tapr.py +0 -362
  121. tsadmetrics/_tsadeval/metrics.py +0 -698
  122. tsadmetrics/_tsadeval/prts/__init__.py +0 -0
  123. tsadmetrics/_tsadeval/prts/base/__init__.py +0 -0
  124. tsadmetrics/_tsadeval/prts/base/time_series_metrics.py +0 -165
  125. tsadmetrics/_tsadeval/prts/basic_metrics_ts.py +0 -121
  126. tsadmetrics/_tsadeval/prts/time_series_metrics/__init__.py +0 -0
  127. tsadmetrics/_tsadeval/prts/time_series_metrics/fscore.py +0 -61
  128. tsadmetrics/_tsadeval/prts/time_series_metrics/precision.py +0 -86
  129. tsadmetrics/_tsadeval/prts/time_series_metrics/precision_recall.py +0 -21
  130. tsadmetrics/_tsadeval/prts/time_series_metrics/recall.py +0 -85
  131. tsadmetrics/_tsadeval/tests.py +0 -376
  132. tsadmetrics/_tsadeval/threshold_plt.py +0 -30
  133. tsadmetrics/_tsadeval/time_tolerant.py +0 -33
  134. tsadmetrics/binary_metrics.py +0 -1652
  135. tsadmetrics/metric_utils.py +0 -98
  136. tsadmetrics/non_binary_metrics.py +0 -398
  137. tsadmetrics/scripts/__init__.py +0 -0
  138. tsadmetrics/scripts/compute_metrics.py +0 -42
  139. tsadmetrics/utils.py +0 -122
  140. tsadmetrics/validation.py +0 -35
  141. tsadmetrics-0.1.16.dist-info/METADATA +0 -23
  142. tsadmetrics-0.1.16.dist-info/RECORD +0 -64
  143. tsadmetrics-0.1.16.dist-info/entry_points.txt +0 -2
  144. /tsadmetrics/{_tsadeval → base}/__init__.py +0 -0
  145. /tsadmetrics/{_tsadeval/affiliation → evaluation}/__init__.py +0 -0
  146. /tsadmetrics/{_tsadeval/eTaPR_pkg/DataManage → metrics/tem}/__init__.py +0 -0
  147. /tsadmetrics/{_tsadeval/vus_utils.py → utils/functions_vus.py} +0 -0
  148. {tsadmetrics-0.1.16.dist-info → tsadmetrics-1.0.0.dist-info}/WHEEL +0 -0
tests/test_non_binary.py DELETED
@@ -1,420 +0,0 @@
1
- import unittest
2
- from tsadmetrics import *
3
- import numpy as np
4
- import random
5
-
6
-
7
- class TestPrecisionAtK(unittest.TestCase):
8
-
9
- def setUp(self):
10
- """
11
- Configuración inicial para las pruebas.
12
- """
13
-
14
- self.y_true1 = np.array([0,0,1,1])
15
-
16
-
17
- self.y_pred1 = np.array([0.2, 0.9, 0.3, 0.8])
18
-
19
- self.y_pred2 = np.array([1, 2, 3, 4])
20
-
21
- self.y_pred3 = np.array([3, 4, 1, 2])
22
-
23
- self.y_true2 = np.array([1,1,1,0])
24
-
25
- self.y_pred4 = np.array([3, 4, 1, 2])
26
-
27
-
28
-
29
-
30
- def test_precision_at_k_score(self):
31
- """
32
- Prueba para la función precision_at_k_score.
33
- """
34
- score = round(precision_at_k(self.y_true1, self.y_pred1),2)
35
- expected_score = 0.5
36
- self.assertAlmostEqual(score, expected_score, places=4)
37
-
38
- score = round(precision_at_k(self.y_true1, self.y_pred2),2)
39
- expected_score = 1
40
- self.assertAlmostEqual(score, expected_score, places=4)
41
-
42
- score = round(precision_at_k(self.y_true1, self.y_pred3),2)
43
- expected_score = 0
44
- self.assertAlmostEqual(score, expected_score, places=4)
45
-
46
- score = round(precision_at_k(self.y_true2, self.y_pred4),2)
47
- expected_score = round(2/3,2)
48
- self.assertAlmostEqual(score, expected_score, places=4)
49
-
50
- def test_precision_at_k_score_consistency(self):
51
- try:
52
- for _ in range(100):
53
- y_true = np.random.choice([0, 1], size=(100,))
54
- y_pred = np.random.random( size=(100,))
55
-
56
- score = precision_at_k(y_true, y_pred)
57
- except Exception as e:
58
- self.fail(f"precision_at_k_score raised an exception {e}")
59
-
60
-
61
- class TestAUCROCPW(unittest.TestCase):
62
-
63
- def setUp(self):
64
- """
65
- Configuración inicial para las pruebas.
66
- """
67
-
68
- self.y_true1 = np.array([0,0,1,1])
69
-
70
-
71
- self.y_pred1 = np.array([1, 3, 2, 4])
72
-
73
- self.y_pred2 = np.array([1, 2, 3, 4])
74
-
75
- self.y_pred3 = np.array([4, 4, 4, 4])
76
-
77
-
78
- def test_auc_roc_pw(self):
79
- """
80
- Prueba para la función auc_roc_pw.
81
- """
82
- score = round(auc_roc_pw(self.y_true1, self.y_pred1),2)
83
- expected_score = 0.75
84
- self.assertAlmostEqual(score, expected_score, places=4)
85
-
86
- score = round(auc_roc_pw(self.y_true1, self.y_pred2),2)
87
- expected_score = 1
88
- self.assertAlmostEqual(score, expected_score, places=4)
89
-
90
- score = round(auc_roc_pw(self.y_true1, self.y_pred3),2)
91
- expected_score = 0.5
92
- self.assertAlmostEqual(score, expected_score, places=4)
93
-
94
-
95
- def test_auc_roc_pw_consistency(self):
96
- try:
97
- for _ in range(100):
98
- y_true = np.random.choice([0, 1], size=(100,))
99
- y_pred = np.random.random( size=(100,))
100
-
101
- score = auc_roc_pw(y_true, y_pred)
102
- except Exception as e:
103
- self.fail(f"auc_roc_pw raised an exception {e}")
104
-
105
- class TestAUCPRPW(unittest.TestCase):
106
-
107
- def setUp(self):
108
- """
109
- Configuración inicial para las pruebas.
110
- """
111
-
112
- self.y_true1 = np.array([0,0,1,1])
113
-
114
-
115
- self.y_pred1 = np.array([1, 3, 2, 4])
116
-
117
- self.y_pred2 = np.array([1, 2, 3, 4])
118
-
119
- self.y_pred3 = np.array([4, 4, 4, 4])
120
-
121
-
122
- def test_auc_pr_pw(self):
123
- """
124
- Prueba para la función auc_pr_pw.
125
- """
126
- score = round(auc_pr_pw(self.y_true1, self.y_pred1),2)
127
- expected_score = 0.83
128
- self.assertAlmostEqual(score, expected_score, places=4)
129
-
130
- score = round(auc_pr_pw(self.y_true1, self.y_pred2),2)
131
- expected_score = 1
132
- self.assertAlmostEqual(score, expected_score, places=4)
133
-
134
- score = round(auc_pr_pw(self.y_true1, self.y_pred3),2)
135
- expected_score = 0.5
136
- self.assertAlmostEqual(score, expected_score, places=4)
137
-
138
-
139
- def test_auc_pr_consistency(self):
140
- try:
141
- for _ in range(100):
142
- y_true = np.random.choice([0, 1], size=(100,))
143
- y_pred = np.random.random( size=(100,))
144
-
145
- score = auc_pr_pw(y_true, y_pred)
146
- except Exception as e:
147
- self.fail(f"auc_pr raised an exception {e}")
148
-
149
-
150
-
151
-
152
- class TestAUCPRPA(unittest.TestCase):
153
-
154
- def setUp(self):
155
- """
156
- Configuración inicial para las pruebas.
157
- """
158
-
159
- self.y_true1 = np.array([0,0,1,1])
160
-
161
-
162
- self.y_pred1 = np.array([1, 3, 2, 4])
163
-
164
- self.y_pred2 = np.array([1, 2, 3, 4])
165
-
166
- self.y_pred3 = np.array([4, 4, 4, 4])
167
-
168
- self.y_true2 = np.array([0,1,1,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,1,0,0,1,1,0
169
- ,1,1,1,0,0,1,0,0,1,0,1,1,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,1,1,1,1,1,0,1,1
170
- ,1,1,1,1,0,0,1,1,1,1,0,1,0,0,1,1,1,0,0,1,0,0,1,0,1,1])
171
-
172
-
173
- self.y_pred4 = [0.1280475, 0.12059283 ,0.29936968 ,0.85866402 ,0.74071874 ,0.22310849
174
- ,0.11281839 ,0.26133246 ,0.33696106 ,0.01442675 ,0.51962876 ,0.07828833
175
- ,0.45337844 ,0.09444483 ,0.91216588 ,0.18847595 ,0.26828481 ,0.65248919
176
- ,0.46291981 ,0.43730757 ,0.78087553 ,0.45031043 ,0.88661033 ,0.56209352
177
- ,0.45029423 ,0.17638205 ,0.9261279 ,0.58830652 ,0.01602648 ,0.73903379
178
- ,0.61831379 ,0.74779903 ,0.42682106 ,0.82583519 ,0.19709012 ,0.44925962
179
- ,0.62752415 ,0.52458327 ,0.46291768 ,0.33937527 ,0.34868777 ,0.12293847
180
- ,0.84477504 ,0.10225254 ,0.37048167 ,0.04476031 ,0.36680499 ,0.11346155
181
- ,0.10583112 ,0.09493136 ,0.54878736 ,0.68514489 ,0.5940307 ,0.14526962
182
- ,0.69385728 ,0.38888727 ,0.61495304 ,0.06795402 ,0.02894603 ,0.08293609
183
- ,0.22865685 ,0.63531487 ,0.97966126 ,0.31418622 ,0.8943095 ,0.22974177
184
- ,0.94402929 ,0.13140625 ,0.80539267 ,0.40160344 ,0.38151339 ,0.65011626
185
- ,0.71657942 ,0.93297398 ,0.32043329 ,0.54667941 ,0.90645979 ,0.98730183
186
- ,0.82351336 ,0.10404812 ,0.6962921 ,0.72890752 ,0.49700666 ,0.47461103
187
- ,0.59696079 ,0.85876179 ,0.247344 ,0.38187879 ,0.23906861 ,0.5266315
188
- ,0.08171512 ,0.27903375 ,0.61112439 ,0.20784267 ,0.90652453 ,0.87575255
189
- ,0.26972245 ,0.78780138 ,0.37649185 ,0.08467683]
190
-
191
-
192
- def test_auc_pr_pa(self):
193
- """
194
- Prueba para la función auc_pr_pa.
195
- """
196
- score = round(auc_pr_pa(self.y_true1, self.y_pred1),2)
197
- expected_score = 1.0
198
- self.assertAlmostEqual(score, expected_score, places=4)
199
-
200
- score = round(auc_pr_pa(self.y_true1, self.y_pred2),2)
201
- expected_score = 1.0
202
- self.assertAlmostEqual(score, expected_score, places=4)
203
-
204
- score = round(auc_pr_pa(self.y_true1, self.y_pred3),2)
205
- expected_score = 0.75
206
- self.assertAlmostEqual(score, expected_score, places=4)
207
-
208
-
209
- score = round(auc_pr_pa(self.y_true2, self.y_pred4),2)
210
- expected_score = 0.78
211
- self.assertAlmostEqual(score, expected_score, places=4)
212
-
213
-
214
- def test_auc_pr_pa_consistency(self):
215
- y_true, y_pred = [],[]
216
- try:
217
- for _ in range(100):
218
- y_true = np.random.choice([0, 1], size=(100,))
219
- y_pred = np.random.random( size=(100,))
220
- score = auc_pr_pa(y_true, y_pred)
221
- except Exception as e:
222
- self.fail(f"auc_roc_pr_pa raised an exception {e}")
223
-
224
-
225
- class TestAUCPRSW(unittest.TestCase):
226
-
227
- def setUp(self):
228
- """
229
- Configuración inicial para las pruebas.
230
- """
231
-
232
- self.y_true1 = np.array([0,0,1,1])
233
-
234
-
235
- self.y_pred1 = np.array([1, 3, 2, 4])
236
-
237
- self.y_pred2 = np.array([1, 2, 3, 4])
238
-
239
- self.y_pred3 = np.array([4, 4, 4, 4])
240
-
241
-
242
- def test_auc_pr_sw(self):
243
- """
244
- Prueba para la función auc_pr_sw.
245
- """
246
- score = round(auc_pr_sw(self.y_true1, self.y_pred1),2)
247
- expected_score = 1.0
248
- self.assertAlmostEqual(score, expected_score, places=4)
249
-
250
- score = round(auc_pr_sw(self.y_true1, self.y_pred2),2)
251
- expected_score = 1
252
- self.assertAlmostEqual(score, expected_score, places=4)
253
-
254
- score = round(auc_pr_sw(self.y_true1, self.y_pred3),2)
255
- expected_score = 1
256
- self.assertAlmostEqual(score, expected_score, places=4)
257
-
258
-
259
- # def test_auc_pr_sw_consistency(self):
260
- # try:
261
- # for _ in range(100):
262
- # y_true = np.random.choice([0, 1], size=(100,))
263
- # y_pred = np.random.random( size=(100,))
264
-
265
- # score = auc_pr_sw(y_true, y_pred)
266
- # except Exception as e:
267
- # self.fail(f"auc_pr_sw raised an exception {e}")
268
-
269
-
270
- class TestVUSROC(unittest.TestCase):
271
-
272
- def setUp(self):
273
- """
274
- Configuración inicial para las pruebas.
275
- """
276
-
277
- self.y_true1 = np.array([1, 1, 0, 0, 0, 0, 0, 0, 0, 0])
278
- self.y_true2 = np.array([0, 1, 0, 1, 0, 0, 0, 0, 0, 0])
279
-
280
- self.y_pred1 = np.array( [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
281
-
282
- self.y_pred2 = np.array([8, 0, 9, 1, 7, 2, 3, 4, 5, 6])
283
-
284
-
285
-
286
-
287
- def test_vus_roc(self):
288
- """
289
- Prueba para la función vus_roc.
290
- """
291
- score = round(vus_roc(self.y_true1, self.y_pred1,window=4),2)
292
- self.assertTrue(score <= 0.1)
293
-
294
- score = round(vus_roc(self.y_true2, self.y_pred2,window=4),2)
295
- self.assertTrue(score > 0.4)
296
-
297
- score = vus_roc(self.y_true2, self.y_pred2,window=0)
298
- self.assertTrue(score < 0.4)
299
-
300
-
301
- def test_vus_roc_consistency(self):
302
- try:
303
- for _ in range(10):
304
- y_true = np.random.choice([0, 1], size=(100,))
305
- y_pred = np.random.random( size=(100,))
306
- score = vus_roc(y_true, y_pred, window=4)
307
- except Exception as e:
308
- self.fail(f"auc_roc raised an exception {e}")
309
-
310
-
311
- class TestVUSPR(unittest.TestCase):
312
-
313
- def setUp(self):
314
- """
315
- Configuración inicial para las pruebas.
316
- """
317
-
318
- self.y_true1 = np.array([1, 1, 0, 0, 0, 0, 0, 0, 0, 0])
319
- self.y_true2 = np.array([0, 1, 0, 1, 0, 0, 0, 0, 0, 0])
320
-
321
- self.y_pred1 = np.array( [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
322
-
323
- self.y_pred2 = np.array([8, 0, 9, 1, 7, 2, 3, 4, 5, 6])
324
-
325
-
326
-
327
-
328
- def test_vus_pr(self):
329
- """
330
- Prueba para la función vus_pr.
331
- """
332
- score = round(vus_pr(self.y_true1, self.y_pred1,window=4),2)
333
- self.assertTrue(score <= 0.2)
334
-
335
- score = round(vus_pr(self.y_true2, self.y_pred2,window=4),2)
336
- self.assertTrue(score > 0.5)
337
-
338
- score = vus_pr(self.y_true2, self.y_pred2,window=0)
339
- self.assertTrue(score < 0.5)
340
-
341
-
342
- def test_vus_pr_consistency(self):
343
- try:
344
- for _ in range(10):
345
- y_true = np.random.choice([0, 1], size=(100,))
346
- y_pred = np.random.random( size=(100,))
347
- score = vus_pr(y_true, y_pred, window=4)
348
- except Exception as e:
349
- self.fail(f"auc_roc raised an exception {e}")
350
-
351
-
352
- class TestNonBinaryPATE(unittest.TestCase):
353
-
354
- def setUp(self):
355
- """
356
- Configuración inicial para las pruebas.
357
- """
358
- self.y_true1 = np.array([0,0,1,1])
359
-
360
-
361
- self.y_pred1 = np.array([1, 3, 2, 4])
362
-
363
- self.y_pred2 = np.array([1, 2, 3, 4])
364
-
365
- self.y_pred3 = np.array([4, 4, 4, 4])
366
-
367
- self.y_true2 = np.array([0,1,1,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,1,0,0,1,1,0
368
- ,1,1,1,0,0,1,0,0,1,0,1,1,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,1,1,1,1,1,0,1,1
369
- ,1,1,1,1,0,0,1,1,1,1,0,1,0,0,1,1,1,0,0,1,0,0,1,0,1,1])
370
-
371
-
372
- self.y_pred4 = [0.1280475, 0.12059283 ,0.29936968 ,0.85866402 ,0.74071874 ,0.22310849
373
- ,0.11281839 ,0.26133246 ,0.33696106 ,0.01442675 ,0.51962876 ,0.07828833
374
- ,0.45337844 ,0.09444483 ,0.91216588 ,0.18847595 ,0.26828481 ,0.65248919
375
- ,0.46291981 ,0.43730757 ,0.78087553 ,0.45031043 ,0.88661033 ,0.56209352
376
- ,0.45029423 ,0.17638205 ,0.9261279 ,0.58830652 ,0.01602648 ,0.73903379
377
- ,0.61831379 ,0.74779903 ,0.42682106 ,0.82583519 ,0.19709012 ,0.44925962
378
- ,0.62752415 ,0.52458327 ,0.46291768 ,0.33937527 ,0.34868777 ,0.12293847
379
- ,0.84477504 ,0.10225254 ,0.37048167 ,0.04476031 ,0.36680499 ,0.11346155
380
- ,0.10583112 ,0.09493136 ,0.54878736 ,0.68514489 ,0.5940307 ,0.14526962
381
- ,0.69385728 ,0.38888727 ,0.61495304 ,0.06795402 ,0.02894603 ,0.08293609
382
- ,0.22865685 ,0.63531487 ,0.97966126 ,0.31418622 ,0.8943095 ,0.22974177
383
- ,0.94402929 ,0.13140625 ,0.80539267 ,0.40160344 ,0.38151339 ,0.65011626
384
- ,0.71657942 ,0.93297398 ,0.32043329 ,0.54667941 ,0.90645979 ,0.98730183
385
- ,0.82351336 ,0.10404812 ,0.6962921 ,0.72890752 ,0.49700666 ,0.47461103
386
- ,0.59696079 ,0.85876179 ,0.247344 ,0.38187879 ,0.23906861 ,0.5266315
387
- ,0.08171512 ,0.27903375 ,0.61112439 ,0.20784267 ,0.90652453 ,0.87575255
388
- ,0.26972245 ,0.78780138 ,0.37649185 ,0.08467683]
389
-
390
- def test_real_pate(self):
391
- """
392
- Prueba para la función real_pate.
393
- """
394
- score = round(real_pate(self.y_true1, self.y_pred1,early=1, delay=1),2)
395
- expected_score = 0.79
396
- self.assertAlmostEqual(score, expected_score, places=4)
397
-
398
- score = round(real_pate(self.y_true1, self.y_pred2,early=1, delay=1),2)
399
- expected_score = 1.0
400
- self.assertAlmostEqual(score, expected_score, places=4)
401
-
402
- score = round(real_pate(self.y_true1, self.y_pred3,early=1, delay=1),2)
403
- expected_score = 0.75
404
- self.assertAlmostEqual(score, expected_score, places=4)
405
-
406
-
407
- score = round(real_pate(self.y_true2, self.y_pred4,early=5, delay=5),2)
408
- expected_score = 0.67
409
- self.assertAlmostEqual(score, expected_score, places=4)
410
-
411
-
412
- def test_real_pate_consistency(self):
413
- try:
414
- for _ in range(10):
415
- y_true = np.random.choice([0, 1], size=(100,))
416
- y_pred = np.random.random( size=(100,))
417
-
418
- score = real_pate(y_true, y_pred, early=5, delay=5)
419
- except Exception as e:
420
- self.fail(f"real_pate raised an exception {e}")
tests/test_utils.py DELETED
@@ -1,49 +0,0 @@
1
- import unittest
2
- from tsadmetrics import *
3
- import os
4
- import numpy as np
5
- import random
6
-
7
- class TestComputeMetrics(unittest.TestCase):
8
- def setUp(self):
9
- self.y_true = np.array([0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1])
10
- self.y_pred_binary = np.array([0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
11
- self.y_pred_non_binary = np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.12, 0.11, 0.21, 0.13, 0.4, 0.3, 0.2, 0.1, 0.32, 0.98, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9])
12
-
13
-
14
- def test_compute_metrics_binary(self):
15
- metrics = [
16
- ('point_wise_f_score', point_wise_f_score),
17
- ('segment_wise_f_score', segment_wise_f_score),
18
- ]
19
- metrics_params = {}
20
-
21
- results = compute_metrics(self.y_true, self.y_pred_binary, metrics, metrics_params)
22
-
23
- self.assertTrue('point_wise_f_score' in results['metric_name'].values)
24
- self.assertTrue('segment_wise_f_score' in results['metric_name'].values)
25
-
26
- def test_compute_metrics_non_binary(self):
27
- metrics = [
28
- ('vus_roc', vus_roc),
29
- ('vus_pr', vus_pr),
30
- ]
31
- metrics_params = {
32
- 'vus_roc': {'window': 3},
33
- 'vus_pr': {'window': 3}}
34
-
35
- results = compute_metrics(self.y_true, self.y_pred_non_binary, metrics, metrics_params, is_anomaly_score=True)
36
-
37
- self.assertTrue('vus_roc' in results['metric_name'].values)
38
- self.assertTrue('vus_pr' in results['metric_name'].values)
39
-
40
- class TestComputeMetricsFromFile(unittest.TestCase):
41
- def setUp(self):
42
- self.y_true = np.array([0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1])
43
- self.y_pred_binary = np.array([0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
44
- self.results_file = 'tests/test_data/results.csv'
45
- self.conf_file = 'tests/test_data/config.json'
46
-
47
- def test_compute_metrics_from_file(self):
48
- results_df = compute_metrics_from_file(self.results_file, self.conf_file, output_dir='tests/test_data')
49
- assert os.path.exists('tests/test_data/computed_metrics.csv'), f"Error: The file 'computed_metrics.csv' was not created."
@@ -1,86 +0,0 @@
1
- #!/usr/bin/env python3
2
- # -*- coding: utf-8 -*-
3
- from ._integral_interval import interval_intersection
4
-
5
- def t_start(j, Js = [(1,2),(3,4),(5,6)], Trange = (1,10)):
6
- """
7
- Helper for `E_gt_func`
8
-
9
- :param j: index from 0 to len(Js) (included) on which to get the start
10
- :param Js: ground truth events, as a list of couples
11
- :param Trange: range of the series where Js is included
12
- :return: generalized start such that the middle of t_start and t_stop
13
- always gives the affiliation zone
14
- """
15
- b = max(Trange)
16
- n = len(Js)
17
- if j == n:
18
- return(2*b - t_stop(n-1, Js, Trange))
19
- else:
20
- return(Js[j][0])
21
-
22
- def t_stop(j, Js = [(1,2),(3,4),(5,6)], Trange = (1,10)):
23
- """
24
- Helper for `E_gt_func`
25
-
26
- :param j: index from 0 to len(Js) (included) on which to get the stop
27
- :param Js: ground truth events, as a list of couples
28
- :param Trange: range of the series where Js is included
29
- :return: generalized stop such that the middle of t_start and t_stop
30
- always gives the affiliation zone
31
- """
32
- if j == -1:
33
- a = min(Trange)
34
- return(2*a - t_start(0, Js, Trange))
35
- else:
36
- return(Js[j][1])
37
-
38
- def E_gt_func(j, Js, Trange):
39
- """
40
- Get the affiliation zone of element j of the ground truth
41
-
42
- :param j: index from 0 to len(Js) (excluded) on which to get the zone
43
- :param Js: ground truth events, as a list of couples
44
- :param Trange: range of the series where Js is included, can
45
- be (-math.inf, math.inf) for distance measures
46
- :return: affiliation zone of element j of the ground truth represented
47
- as a couple
48
- """
49
- range_left = (t_stop(j-1, Js, Trange) + t_start(j, Js, Trange))/2
50
- range_right = (t_stop(j, Js, Trange) + t_start(j+1, Js, Trange))/2
51
- return((range_left, range_right))
52
-
53
- def get_all_E_gt_func(Js, Trange):
54
- """
55
- Get the affiliation partition from the ground truth point of view
56
-
57
- :param Js: ground truth events, as a list of couples
58
- :param Trange: range of the series where Js is included, can
59
- be (-math.inf, math.inf) for distance measures
60
- :return: affiliation partition of the events
61
- """
62
- # E_gt is the limit of affiliation/attraction for each ground truth event
63
- E_gt = [E_gt_func(j, Js, Trange) for j in range(len(Js))]
64
- return(E_gt)
65
-
66
- def affiliation_partition(Is = [(1,1.5),(2,5),(5,6),(8,9)], E_gt = [(1,2.5),(2.5,4.5),(4.5,10)]):
67
- """
68
- Cut the events into the affiliation zones
69
- The presentation given here is from the ground truth point of view,
70
- but it is also used in the reversed direction in the main function.
71
-
72
- :param Is: events as a list of couples
73
- :param E_gt: range of the affiliation zones
74
- :return: a list of list of intervals (each interval represented by either
75
- a couple or None for empty interval). The outer list is indexed by each
76
- affiliation zone of `E_gt`. The inner list is indexed by the events of `Is`.
77
- """
78
- out = [None] * len(E_gt)
79
- for j in range(len(E_gt)):
80
- E_gt_j = E_gt[j]
81
- discarded_idx_before = [I[1] < E_gt_j[0] for I in Is] # end point of predicted I is before the begin of E
82
- discarded_idx_after = [I[0] > E_gt_j[1] for I in Is] # start of predicted I is after the end of E
83
- kept_index = [not(a or b) for a, b in zip(discarded_idx_before, discarded_idx_after)]
84
- Is_j = [x for x, y in zip(Is, kept_index)]
85
- out[j] = [interval_intersection(I, E_gt[j]) for I in Is_j]
86
- return(out)
@@ -1,68 +0,0 @@
1
- #!/usr/bin/env python3
2
- # -*- coding: utf-8 -*-
3
- import math
4
- from ._affiliation_zone import (
5
- get_all_E_gt_func,
6
- affiliation_partition)
7
- from ._integral_interval import (
8
- integral_interval_distance,
9
- integral_interval_probaCDF_precision,
10
- integral_interval_probaCDF_recall,
11
- interval_length,
12
- sum_interval_lengths)
13
-
14
- def affiliation_precision_distance(Is = [(1,2),(3,4),(5,6)], J = (2,5.5)):
15
- """
16
- Compute the individual average distance from Is to a single ground truth J
17
-
18
- :param Is: list of predicted events within the affiliation zone of J
19
- :param J: couple representating the start and stop of a ground truth interval
20
- :return: individual average precision directed distance number
21
- """
22
- if all([I is None for I in Is]): # no prediction in the current area
23
- return(math.nan) # undefined
24
- return(sum([integral_interval_distance(I, J) for I in Is]) / sum_interval_lengths(Is))
25
-
26
- def affiliation_precision_proba(Is = [(1,2),(3,4),(5,6)], J = (2,5.5), E = (0,8)):
27
- """
28
- Compute the individual precision probability from Is to a single ground truth J
29
-
30
- :param Is: list of predicted events within the affiliation zone of J
31
- :param J: couple representating the start and stop of a ground truth interval
32
- :param E: couple representing the start and stop of the zone of affiliation of J
33
- :return: individual precision probability in [0, 1], or math.nan if undefined
34
- """
35
- if all([I is None for I in Is]): # no prediction in the current area
36
- return(math.nan) # undefined
37
- return(sum([integral_interval_probaCDF_precision(I, J, E) for I in Is]) / sum_interval_lengths(Is))
38
-
39
- def affiliation_recall_distance(Is = [(1,2),(3,4),(5,6)], J = (2,5.5)):
40
- """
41
- Compute the individual average distance from a single J to the predictions Is
42
-
43
- :param Is: list of predicted events within the affiliation zone of J
44
- :param J: couple representating the start and stop of a ground truth interval
45
- :return: individual average recall directed distance number
46
- """
47
- Is = [I for I in Is if I is not None] # filter possible None in Is
48
- if len(Is) == 0: # there is no prediction in the current area
49
- return(math.inf)
50
- E_gt_recall = get_all_E_gt_func(Is, (-math.inf, math.inf)) # here from the point of view of the predictions
51
- Js = affiliation_partition([J], E_gt_recall) # partition of J depending of proximity with Is
52
- return(sum([integral_interval_distance(J[0], I) for I, J in zip(Is, Js)]) / interval_length(J))
53
-
54
- def affiliation_recall_proba(Is = [(1,2),(3,4),(5,6)], J = (2,5.5), E = (0,8)):
55
- """
56
- Compute the individual recall probability from a single ground truth J to Is
57
-
58
- :param Is: list of predicted events within the affiliation zone of J
59
- :param J: couple representating the start and stop of a ground truth interval
60
- :param E: couple representing the start and stop of the zone of affiliation of J
61
- :return: individual recall probability in [0, 1]
62
- """
63
- Is = [I for I in Is if I is not None] # filter possible None in Is
64
- if len(Is) == 0: # there is no prediction in the current area
65
- return(0)
66
- E_gt_recall = get_all_E_gt_func(Is, E) # here from the point of view of the predictions
67
- Js = affiliation_partition([J], E_gt_recall) # partition of J depending of proximity with Is
68
- return(sum([integral_interval_probaCDF_recall(I, J[0], E) for I, J in zip(Is, Js)]) / interval_length(J))