tsadmetrics 0.1.17__py3-none-any.whl → 1.0.1__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 (143) hide show
  1. {docs_api → docs/add_docs/api_doc}/conf.py +3 -26
  2. {docs_manual → docs/add_docs/full_doc}/conf.py +2 -25
  3. docs/add_docs/manual_doc/conf.py +67 -0
  4. docs/conf.py +1 -1
  5. examples/example_direct_data.py +28 -0
  6. examples/example_direct_single_data.py +25 -0
  7. examples/example_file_reference.py +24 -0
  8. examples/example_global_config_file.py +13 -0
  9. examples/example_metric_config_file.py +19 -0
  10. examples/example_simple_metric.py +8 -0
  11. examples/specific_examples/AbsoluteDetectionDistance_example.py +24 -0
  12. examples/specific_examples/AffiliationbasedFScore_example.py +24 -0
  13. examples/specific_examples/AverageDetectionCount_example.py +24 -0
  14. examples/specific_examples/CompositeFScore_example.py +24 -0
  15. examples/specific_examples/DelayThresholdedPointadjustedFScore_example.py +24 -0
  16. examples/specific_examples/DetectionAccuracyInRange_example.py +24 -0
  17. examples/specific_examples/EnhancedTimeseriesAwareFScore_example.py +24 -0
  18. examples/specific_examples/LatencySparsityawareFScore_example.py +24 -0
  19. examples/specific_examples/MeanTimeToDetect_example.py +24 -0
  20. examples/specific_examples/NabScore_example.py +24 -0
  21. examples/specific_examples/PateFScore_example.py +24 -0
  22. examples/specific_examples/Pate_example.py +24 -0
  23. examples/specific_examples/PointadjustedAtKFScore_example.py +24 -0
  24. examples/specific_examples/PointadjustedAucPr_example.py +24 -0
  25. examples/specific_examples/PointadjustedAucRoc_example.py +24 -0
  26. examples/specific_examples/PointadjustedFScore_example.py +24 -0
  27. examples/specific_examples/RangebasedFScore_example.py +24 -0
  28. examples/specific_examples/SegmentwiseFScore_example.py +24 -0
  29. examples/specific_examples/TemporalDistance_example.py +24 -0
  30. examples/specific_examples/TimeTolerantFScore_example.py +24 -0
  31. examples/specific_examples/TimeseriesAwareFScore_example.py +24 -0
  32. examples/specific_examples/TotalDetectedInRange_example.py +24 -0
  33. examples/specific_examples/VusPr_example.py +24 -0
  34. examples/specific_examples/VusRoc_example.py +24 -0
  35. examples/specific_examples/WeightedDetectionDifference_example.py +24 -0
  36. tsadmetrics/__init__.py +0 -21
  37. tsadmetrics/base/Metric.py +188 -0
  38. tsadmetrics/evaluation/Report.py +25 -0
  39. tsadmetrics/evaluation/Runner.py +253 -0
  40. tsadmetrics/metrics/Registry.py +141 -0
  41. tsadmetrics/metrics/__init__.py +2 -0
  42. tsadmetrics/metrics/spm/PointwiseAucPr.py +62 -0
  43. tsadmetrics/metrics/spm/PointwiseAucRoc.py +63 -0
  44. tsadmetrics/metrics/spm/PointwiseFScore.py +86 -0
  45. tsadmetrics/metrics/spm/PrecisionAtK.py +81 -0
  46. tsadmetrics/metrics/spm/__init__.py +9 -0
  47. tsadmetrics/metrics/tem/dpm/DelayThresholdedPointadjustedFScore.py +83 -0
  48. tsadmetrics/metrics/tem/dpm/LatencySparsityawareFScore.py +76 -0
  49. tsadmetrics/metrics/tem/dpm/MeanTimeToDetect.py +47 -0
  50. tsadmetrics/metrics/tem/dpm/NabScore.py +60 -0
  51. tsadmetrics/metrics/tem/dpm/__init__.py +11 -0
  52. tsadmetrics/metrics/tem/ptdm/AverageDetectionCount.py +53 -0
  53. tsadmetrics/metrics/tem/ptdm/DetectionAccuracyInRange.py +66 -0
  54. tsadmetrics/metrics/tem/ptdm/PointadjustedAtKFScore.py +80 -0
  55. tsadmetrics/metrics/tem/ptdm/TimeseriesAwareFScore.py +248 -0
  56. tsadmetrics/metrics/tem/ptdm/TotalDetectedInRange.py +65 -0
  57. tsadmetrics/metrics/tem/ptdm/WeightedDetectionDifference.py +97 -0
  58. tsadmetrics/metrics/tem/ptdm/__init__.py +12 -0
  59. tsadmetrics/metrics/tem/tmem/AbsoluteDetectionDistance.py +48 -0
  60. tsadmetrics/metrics/tem/tmem/EnhancedTimeseriesAwareFScore.py +252 -0
  61. tsadmetrics/metrics/tem/tmem/TemporalDistance.py +68 -0
  62. tsadmetrics/metrics/tem/tmem/__init__.py +9 -0
  63. tsadmetrics/metrics/tem/tpdm/CompositeFScore.py +104 -0
  64. tsadmetrics/metrics/tem/tpdm/PointadjustedAucPr.py +123 -0
  65. tsadmetrics/metrics/tem/tpdm/PointadjustedAucRoc.py +119 -0
  66. tsadmetrics/metrics/tem/tpdm/PointadjustedFScore.py +96 -0
  67. tsadmetrics/metrics/tem/tpdm/RangebasedFScore.py +236 -0
  68. tsadmetrics/metrics/tem/tpdm/SegmentwiseFScore.py +73 -0
  69. tsadmetrics/metrics/tem/tpdm/__init__.py +12 -0
  70. tsadmetrics/metrics/tem/tstm/AffiliationbasedFScore.py +68 -0
  71. tsadmetrics/metrics/tem/tstm/Pate.py +62 -0
  72. tsadmetrics/metrics/tem/tstm/PateFScore.py +61 -0
  73. tsadmetrics/metrics/tem/tstm/TimeTolerantFScore.py +85 -0
  74. tsadmetrics/metrics/tem/tstm/VusPr.py +51 -0
  75. tsadmetrics/metrics/tem/tstm/VusRoc.py +55 -0
  76. tsadmetrics/metrics/tem/tstm/__init__.py +15 -0
  77. tsadmetrics/{_tsadeval/affiliation/_integral_interval.py → utils/functions_affiliation.py} +377 -9
  78. tsadmetrics/utils/functions_auc.py +393 -0
  79. tsadmetrics/utils/functions_conversion.py +63 -0
  80. tsadmetrics/utils/functions_counting_metrics.py +26 -0
  81. tsadmetrics/{_tsadeval/latency_sparsity_aware.py → utils/functions_latency_sparsity_aware.py} +1 -1
  82. tsadmetrics/{_tsadeval/nabscore.py → utils/functions_nabscore.py} +15 -1
  83. tsadmetrics-1.0.1.dist-info/METADATA +83 -0
  84. tsadmetrics-1.0.1.dist-info/RECORD +91 -0
  85. tsadmetrics-1.0.1.dist-info/top_level.txt +3 -0
  86. entorno/bin/activate_this.py +0 -32
  87. entorno/bin/rst2html.py +0 -23
  88. entorno/bin/rst2html4.py +0 -26
  89. entorno/bin/rst2html5.py +0 -33
  90. entorno/bin/rst2latex.py +0 -26
  91. entorno/bin/rst2man.py +0 -27
  92. entorno/bin/rst2odt.py +0 -28
  93. entorno/bin/rst2odt_prepstyles.py +0 -20
  94. entorno/bin/rst2pseudoxml.py +0 -23
  95. entorno/bin/rst2s5.py +0 -24
  96. entorno/bin/rst2xetex.py +0 -27
  97. entorno/bin/rst2xml.py +0 -23
  98. entorno/bin/rstpep2html.py +0 -25
  99. tests/test_binary.py +0 -946
  100. tests/test_non_binary.py +0 -450
  101. tests/test_utils.py +0 -49
  102. tsadmetrics/_tsadeval/affiliation/_affiliation_zone.py +0 -86
  103. tsadmetrics/_tsadeval/affiliation/_single_ground_truth_event.py +0 -68
  104. tsadmetrics/_tsadeval/affiliation/generics.py +0 -135
  105. tsadmetrics/_tsadeval/affiliation/metrics.py +0 -114
  106. tsadmetrics/_tsadeval/auc_roc_pr_plot.py +0 -295
  107. tsadmetrics/_tsadeval/discontinuity_graph.py +0 -109
  108. tsadmetrics/_tsadeval/eTaPR_pkg/DataManage/File_IO.py +0 -175
  109. tsadmetrics/_tsadeval/eTaPR_pkg/DataManage/Range.py +0 -50
  110. tsadmetrics/_tsadeval/eTaPR_pkg/DataManage/Time_Plot.py +0 -184
  111. tsadmetrics/_tsadeval/eTaPR_pkg/DataManage/__init__.py +0 -0
  112. tsadmetrics/_tsadeval/eTaPR_pkg/__init__.py +0 -0
  113. tsadmetrics/_tsadeval/eTaPR_pkg/etapr.py +0 -386
  114. tsadmetrics/_tsadeval/eTaPR_pkg/tapr.py +0 -362
  115. tsadmetrics/_tsadeval/metrics.py +0 -698
  116. tsadmetrics/_tsadeval/prts/__init__.py +0 -0
  117. tsadmetrics/_tsadeval/prts/base/__init__.py +0 -0
  118. tsadmetrics/_tsadeval/prts/base/time_series_metrics.py +0 -165
  119. tsadmetrics/_tsadeval/prts/basic_metrics_ts.py +0 -121
  120. tsadmetrics/_tsadeval/prts/time_series_metrics/__init__.py +0 -0
  121. tsadmetrics/_tsadeval/prts/time_series_metrics/fscore.py +0 -61
  122. tsadmetrics/_tsadeval/prts/time_series_metrics/precision.py +0 -86
  123. tsadmetrics/_tsadeval/prts/time_series_metrics/precision_recall.py +0 -21
  124. tsadmetrics/_tsadeval/prts/time_series_metrics/recall.py +0 -85
  125. tsadmetrics/_tsadeval/tests.py +0 -376
  126. tsadmetrics/_tsadeval/threshold_plt.py +0 -30
  127. tsadmetrics/_tsadeval/time_tolerant.py +0 -33
  128. tsadmetrics/binary_metrics.py +0 -1652
  129. tsadmetrics/metric_utils.py +0 -98
  130. tsadmetrics/non_binary_metrics.py +0 -372
  131. tsadmetrics/scripts/__init__.py +0 -0
  132. tsadmetrics/scripts/compute_metrics.py +0 -42
  133. tsadmetrics/utils.py +0 -124
  134. tsadmetrics/validation.py +0 -35
  135. tsadmetrics-0.1.17.dist-info/METADATA +0 -54
  136. tsadmetrics-0.1.17.dist-info/RECORD +0 -66
  137. tsadmetrics-0.1.17.dist-info/entry_points.txt +0 -2
  138. tsadmetrics-0.1.17.dist-info/top_level.txt +0 -6
  139. {tests → tsadmetrics/base}/__init__.py +0 -0
  140. /tsadmetrics/{_tsadeval → evaluation}/__init__.py +0 -0
  141. /tsadmetrics/{_tsadeval/affiliation → metrics/tem}/__init__.py +0 -0
  142. /tsadmetrics/{_tsadeval/vus_utils.py → utils/functions_vus.py} +0 -0
  143. {tsadmetrics-0.1.17.dist-info → tsadmetrics-1.0.1.dist-info}/WHEEL +0 -0
@@ -1,376 +0,0 @@
1
- from .metrics import *
2
-
3
- import unittest
4
-
5
-
6
- class Binary_detection_tester(unittest.TestCase):
7
- def test_unsorted(self):
8
- self.assertRaises(AssertionError, Binary_detection, 10, [2, 3, 4], [3, 4, 2])
9
- self.assertRaises(AssertionError, Binary_detection, 10, [3, 4, 2], [2, 3, 4])
10
- self.assertRaises(AssertionError, Binary_detection, 10, [[1, 8]], [[5, 6], [1, 2]])
11
- self.assertRaises(AssertionError, Binary_detection, 10, [[5, 6], [1, 2]], [[1, 8]])
12
-
13
- def test_nonunique(self):
14
- self.assertRaises(AssertionError, Binary_detection, 10, [2, 4, 4], [2, 3, 4])
15
- self.assertRaises(AssertionError, Binary_detection, 10, [2, 3, 4], [2, 4, 4])
16
-
17
- def test_long_anom(self):
18
- self.assertRaises(AssertionError, Binary_detection, 4, [1], [2, 3, 4])
19
- self.assertRaises(AssertionError, Binary_detection, 4, [[2, 4]], [1])
20
- self.assertRaises(AssertionError, Binary_detection, 4, [-1], [1])
21
-
22
- def test_point_to_seq(self):
23
- anom1 = [3, 4, 5, 7, 8, 11]
24
- anom2 = [[3, 5], [7, 8], [11, 11]]
25
- d = Binary_detection(12, anom1, anom2)
26
-
27
- self.assertTrue(np.array_equal(np.array(anom1), d.get_predicted_anomalies_ptwise()))
28
- self.assertTrue(np.array_equal(np.array(anom2), d.get_gt_anomalies_segmentwise()))
29
-
30
- def test_anomaly_full_seires(self):
31
- anom1 = [3, 4, 5, 7, 8, 11]
32
- d = Binary_detection(12, anom1, anom1)
33
-
34
- self.assertTrue(
35
- np.array_equal(d.get_gt_anomalies_full_series(), np.array([0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1]))
36
- )
37
- self.assertTrue(
38
- np.array_equal(d.get_predicted_anomalies_full_series(), np.array([0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1]))
39
- )
40
-
41
- def test_empty_anom(self):
42
- anom1 = [3, 4, 5, 7, 8]
43
- anom2 = []
44
-
45
- d = Binary_detection(12, anom1, anom2)
46
- self.assertEqual(0, len(d.get_predicted_anomalies_ptwise()))
47
- self.assertEqual(0, len(d.get_predicted_anomalies_segmentwise()))
48
-
49
-
50
- class Confusion_metrics_tester(unittest.TestCase):
51
- def test_metrics(self):
52
- self.assertEqual(0.75, recall(tp=3, fn=1))
53
- self.assertEqual(0.75, precision(tp=3, fp=1))
54
- self.assertEqual(0.6, f1_score(tp=3, fn=1, fp=3))
55
- self.assertEqual(0.625, f1_from_pr(p=1, r=0.25, beta=0.5))
56
- self.assertAlmostEqual(5 / 9, f1_from_pr(p=1, r=0.5, beta=2))
57
-
58
- def test_requires_names(self):
59
- self.assertRaises(TypeError, recall, 3, 4)
60
- self.assertRaises(TypeError, precision, 3, 4)
61
- self.assertRaises(TypeError, f1_score, 3, 4, 5)
62
-
63
- def test_zerodivision(self):
64
- self.assertEqual(0, recall(tp=0, fn=0))
65
- self.assertEqual(0, precision(tp=0, fp=0))
66
- self.assertEqual(0, f1_score(tp=0, fp=1, fn=1))
67
-
68
-
69
- class Metrics_tester(unittest.TestCase):
70
- def test_PW(self):
71
- pw = Pointwise_metrics(10, [1, 2, 3, 4], [4, 5, 6])
72
-
73
- self.assertEqual(pw.tp, 1)
74
- self.assertEqual(pw.fp, 2)
75
- self.assertEqual(pw.fn, 3)
76
-
77
- def test_PA(self):
78
- pa = PointAdjust(10, [1, 2, 3, 4, 9], [4, 5, 6])
79
- self.assertEqual(pa.tp, 4)
80
- self.assertEqual(pa.fp, 2)
81
- self.assertEqual(pa.fn, 1)
82
-
83
- pa = PointAdjust(10, [1, 2, 3, 4, 5, 6, 7], [4])
84
- self.assertEqual(pa.get_score(), 1)
85
-
86
- pa = PointAdjust(10, [1, 2, 3, 4, 5, 6, 7], [9])
87
- self.assertEqual(pa.get_score(), 0)
88
-
89
- def test_dtPA(self):
90
- pa = DelayThresholdedPointAdjust(10, [1, 2, 3, 4, 9], [4, 5, 6], k=3)
91
- self.assertEqual(pa.tp, 4)
92
- self.assertEqual(pa.fp, 2)
93
- self.assertEqual(pa.fn, 1)
94
-
95
- pa = DelayThresholdedPointAdjust(10, [1, 2, 3, 4, 9], [4, 5, 6], k=2)
96
- self.assertEqual(pa.tp, 0)
97
- self.assertEqual(pa.fp, 2)
98
- self.assertEqual(pa.fn, 5)
99
-
100
- pa = DelayThresholdedPointAdjust(10, [1, 2, 3, 4, 5, 6, 7], [4], k=3)
101
- self.assertEqual(pa.get_score(), 1)
102
-
103
- pa = DelayThresholdedPointAdjust(10, [1, 2, 3, 4, 5, 6, 7], [4], k=2)
104
- self.assertEqual(pa.get_score(), 0)
105
-
106
- def test_pakf(self):
107
- pa = PointAdjustKPercent(10, [1, 2, 3, 4, 9], [4, 5, 6], k=0.5)
108
- self.assertEqual(pa.tp, 1)
109
- self.assertEqual(pa.fp, 2)
110
- self.assertEqual(pa.fn, 4)
111
-
112
- pa = PointAdjustKPercent(10, [1, 2, 3, 4, 9], [4, 5, 6], k=0.1)
113
- self.assertEqual(pa.tp, 4)
114
- self.assertEqual(pa.fp, 2)
115
- self.assertEqual(pa.fn, 1)
116
-
117
- def test_lspa(self):
118
- pa = LatencySparsityAware(10, [2, 3, 4, 5, 9], [4, 7], tw=1)
119
- self.assertAlmostEqual(pa.get_score(), f1_score(tp=pa.tp, fn=pa.fn, fp=pa.fp), 4)
120
- self.assertEqual(pa.tp, 2)
121
- self.assertEqual(pa.fp, 1)
122
- self.assertEqual(pa.fn, 3)
123
-
124
- pa = LatencySparsityAware(10, [2, 3, 4, 5, 9], [4, 7], tw=2)
125
- self.assertAlmostEqual(pa.get_score(), f1_score(tp=pa.tp, fn=pa.fn, fp=pa.fp), 4)
126
- self.assertEqual(pa.tp, 1)
127
- self.assertEqual(pa.fp, 1)
128
- self.assertEqual(pa.fn, 2)
129
-
130
- def test_Segment(self):
131
- s = Segmentwise_metrics(10, [[1, 2], [4, 4], [7, 9]], [[0, 6]])
132
- self.assertEqual(s.tp, 2)
133
- self.assertEqual(s.fp, 0)
134
- self.assertEqual(s.fn, 1)
135
-
136
- s = Segmentwise_metrics(10, [[1, 2], [4, 4], [7, 9]], [[6, 6], [8, 8]])
137
- self.assertEqual(s.tp, 1)
138
- self.assertEqual(s.fp, 1)
139
- self.assertEqual(s.fn, 2)
140
-
141
- s = Segmentwise_metrics(10, [[1, 2], [4, 4], [7, 9]], [])
142
- self.assertEqual(s.tp, 0)
143
- self.assertEqual(s.fp, 0)
144
- self.assertEqual(s.fn, 3)
145
-
146
- s = Segmentwise_metrics(10, [[1, 2], [4, 4], [7, 9]], [[0, 9]])
147
- self.assertEqual(s.tp, 3)
148
- self.assertEqual(s.fp, 0)
149
- self.assertEqual(s.fn, 0)
150
-
151
- def test_CF(self):
152
- c = Composite_f(10, [0, 2, 3, 5, 7, 9], [3, 6])
153
- f = c.get_score()
154
- self.assertEqual(c.p, 0.5)
155
- self.assertEqual(c.r, 0.2)
156
-
157
- def test_affiliation(self):
158
- a = Affiliation(10, [2, 3], [2])
159
- f = a.get_score()
160
- self.assertEqual(a.p, 1)
161
- self.assertTrue(a.r < 1)
162
-
163
- a = Affiliation(10, [2, 3], [2, 3, 4])
164
- f = a.get_score()
165
- self.assertTrue(a.p < 1)
166
- self.assertEqual(a.r, 1)
167
-
168
- def test_range_pr(self):
169
- r = Range_PR(10, [2, 3], [2])
170
- f = r.get_score()
171
- self.assertEqual(r.p, 1)
172
- self.assertTrue(r.r < 1)
173
-
174
- r2 = Range_PR(10, [2, 3], [2, 3])
175
- f2 = r2.get_score()
176
- self.assertTrue(f2 > f)
177
-
178
- r = Range_PR(10, [2, 3], [2, 3, 4])
179
- f = r.get_score()
180
- self.assertTrue(r.p < 1)
181
- self.assertEqual(r.r, 1)
182
-
183
- def test_NAB(self):
184
- n = NAB_score(10, [[3, 6]], [3])
185
- self.assertAlmostEqual(n.get_score(), 100)
186
-
187
- n = NAB_score(10, [[3, 6]], [])
188
- self.assertAlmostEqual(n.get_score(), 0)
189
-
190
- n = NAB_score(10, [[3, 6]], [1])
191
- self.assertAlmostEqual(n.get_score(), -100 * 0.11 / 2)
192
-
193
- n = NAB_score(10, [3, 6], [1])
194
- self.assertTrue(np.isnan(n.get_score()))
195
-
196
- def test_ttol(self):
197
- t = Time_Tolerant(10, [3, 4, 8], [1, 2, 3], d=2)
198
- self.assertAlmostEqual(t.recall(), 2 / 3)
199
- self.assertAlmostEqual(t.precision(), 1)
200
-
201
- t = Time_Tolerant(10, [4, 5], [6], d=1)
202
- self.assertAlmostEqual(t.recall(), 1 / 2)
203
- self.assertAlmostEqual(t.precision(), 1)
204
-
205
- def test_TaF(self):
206
- t = TaF(10, [4, 5, 6], [4, 5, 6])
207
- self.assertEqual(t.get_score(), 1)
208
-
209
- t = TaF(10, [4, 5, 6], [1, 2, 3])
210
- self.assertEqual(t.get_score(), 0)
211
-
212
- t = TaF(10, [4, 5, 6], [7, 8, 9])
213
- self.assertEqual(t.get_score(), 0)
214
- t = TaF(10, [4, 5, 6], [7, 8, 9], delta=1)
215
- self.assertTrue(t.get_score() > 0)
216
-
217
- t1 = TaF(10, [4, 5, 8, 9], [4, 5])
218
- t2 = TaF(10, [4, 5, 8, 9], [5, 8])
219
- self.assertTrue(t1.get_score() < t2.get_score())
220
-
221
- def test_eTaF(self):
222
- t = eTaF(10, [4, 5, 6], [4, 5, 6])
223
- self.assertEqual(t.get_score(), 1)
224
-
225
- t = eTaF(10, [4, 5, 6], [1, 2, 3])
226
- self.assertEqual(t.get_score(), 0)
227
-
228
- t = eTaF(10, [4, 5, 6], [7, 8, 9])
229
- self.assertTrue(t.get_score() == 0)
230
-
231
- t1 = eTaF(10, [4, 5, 8, 9], [4, 5])
232
- t2 = eTaF(10, [4, 5, 8, 9], [5, 8])
233
- self.assertTrue(t1.get_score() < t2.get_score())
234
-
235
- def test_temp_dist(self):
236
- t = Temporal_Distance(10, [4, 5, 6], [4, 5, 6])
237
- self.assertEqual(t.get_score(), 0)
238
-
239
- t = Temporal_Distance(10, [4, 6], [4, 5, 6])
240
- self.assertEqual(t.get_score(), 1)
241
-
242
- t = Temporal_Distance(10, [4], [4, 5, 6])
243
- self.assertEqual(t.get_score(), 3)
244
-
245
- t = Temporal_Distance(10, [4, 5, 6], [8])
246
- self.assertEqual(t.get_score(), 11)
247
-
248
- t = Temporal_Distance(10, [4, 5, 6], [])
249
- self.assertEqual(t.get_score(), 30)
250
-
251
-
252
- class Threshold_metric_tester(unittest.TestCase):
253
- # def test_roc(self):
254
- # a = aucroc(true = [0,0,1,1], score = [0.1,0.4,0.35,0.8])
255
- def test_auc_pr(self):
256
- gt = [[2, 3]]
257
- anomaly_score = [1, 3, 2, 4]
258
- auc_pr = AUC_PR_pw(gt, anomaly_score)
259
-
260
- score = auc_pr.get_score()
261
- self.assertAlmostEqual(score, 0.83, 2)
262
-
263
- anomaly_score = [1, 2, 3, 4]
264
- auc_pr = AUC_PR_pw(gt, anomaly_score)
265
- score = auc_pr.get_score()
266
- self.assertEqual(score, 1)
267
-
268
- anomaly_score = [4, 3, 1, 1]
269
- auc_pr = AUC_PR_pw(gt, anomaly_score)
270
- score = auc_pr.get_score()
271
- self.assertEqual(score, 0.5)
272
-
273
- def test_auc_roc(self):
274
- gt = [[2, 3]]
275
- anomaly_score = [1, 3, 2, 4]
276
- auc_roc = AUC_ROC(gt, anomaly_score)
277
-
278
- score = auc_roc.get_score()
279
- self.assertAlmostEqual(score, 0.75, 2)
280
-
281
- anomaly_score = [1, 2, 3, 4]
282
- auc_roc = AUC_ROC(gt, anomaly_score)
283
- score = auc_roc.get_score()
284
- self.assertEqual(score, 1)
285
-
286
- anomaly_score = [4, 4, 4, 4]
287
- auc_roc = AUC_ROC(gt, anomaly_score)
288
- score = auc_roc.get_score()
289
- self.assertEqual(score, 0.5)
290
-
291
- def test_vus_pr(self):
292
- gt = [[0, 1]]
293
- anomaly_score = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
294
- vus_pr = VUS_PR(gt, anomaly_score, max_window=4)
295
-
296
- score = vus_pr.get_score()
297
- self.assertTrue(score <= 0.2)
298
-
299
- gt = [[1, 3]]
300
- anomaly_score = [8, 0, 9, 1, 7, 2, 3, 4, 5, 6]
301
- vus_pr = VUS_PR(gt, anomaly_score, max_window=4)
302
- score = vus_pr.get_score()
303
- self.assertTrue(score > 0.5)
304
- vus_pr = VUS_PR(gt, anomaly_score, max_window=0)
305
- score = vus_pr.get_score()
306
- self.assertTrue(score < 0.5)
307
-
308
- def test_vus_roc(self):
309
- gt = [[0, 1]]
310
- anomaly_score = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
311
- vus = VUS_ROC(gt, anomaly_score, max_window=4)
312
-
313
- score = vus.get_score()
314
- self.assertTrue(score <= 0.1)
315
-
316
- gt = [[1, 3]]
317
- anomaly_score = [8, 0, 9, 1, 7, 2, 3, 4, 5, 6]
318
- vus = VUS_ROC(gt, anomaly_score, max_window=4)
319
- score = vus.get_score()
320
- self.assertTrue(score > 0.4)
321
- vus = VUS_ROC(gt, anomaly_score, max_window=0)
322
- score = vus.get_score()
323
- self.assertTrue(score < 0.4)
324
-
325
- def test_PatK(self):
326
- gt = [[2, 3]]
327
-
328
- anomaly_score = [1, 4, 2, 3]
329
- patk = PatK_pw(gt, anomaly_score)
330
- score = patk.get_score()
331
- self.assertEqual(score, 0.5)
332
-
333
- anomaly_score = [1, 2, 3, 4]
334
- patk = PatK_pw(gt, anomaly_score)
335
- score = patk.get_score()
336
- self.assertEqual(score, 1)
337
-
338
- anomaly_score = [3, 4, 1, 2]
339
- patk = PatK_pw(gt, anomaly_score)
340
- score = patk.get_score()
341
- self.assertEqual(score, 0)
342
-
343
- anomaly_score = [3, 4, 1, 2]
344
- patk = PatK_pw([1, 2, 3], anomaly_score)
345
- score = patk.get_score()
346
- self.assertAlmostEqual(score, 2 / 3)
347
-
348
- anomaly_score = [2, 1, 1, 0]
349
- patk = PatK_pw([0, 1], anomaly_score)
350
- score = patk.get_score()
351
- self.assertAlmostEqual(score, 2 / 3)
352
-
353
- patk = PatK_pw([], [0, 1, 2, 4])
354
- self.assertRaises(AssertionError, patk.get_score)
355
-
356
- def test_best_threshold_pw(self):
357
- gt = [[2, 3]]
358
-
359
- anomaly_score = [1, 3, 2, 4]
360
- metric = Best_threshold_pw(gt, anomaly_score)
361
- score = metric.get_score()
362
- self.assertAlmostEqual(score, 2 * 2 / 3 * 1 / (1 + 2 / 3))
363
-
364
- anomaly_score = [2, 3, 1, 4]
365
- metric = Best_threshold_pw(gt, anomaly_score)
366
- score = metric.get_score()
367
- self.assertAlmostEqual(score, 2 * 1 / 2 * 1 / (1 + 1 / 2))
368
-
369
- anomaly_score = [4, 3, 1, 2]
370
- metric = Best_threshold_pw(gt, anomaly_score)
371
- score = metric.get_score()
372
- self.assertAlmostEqual(score, 2 * 1 / 2 * 1 / (1 + 1 / 2))
373
-
374
-
375
- if __name__ == "__main__":
376
- unittest.main()
@@ -1,30 +0,0 @@
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()
@@ -1,33 +0,0 @@
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