tsadmetrics 0.1.4__py3-none-any.whl → 0.1.5__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.
- entorno/bin/activate_this.py +32 -0
- entorno/bin/rst2html.py +23 -0
- entorno/bin/rst2html4.py +26 -0
- entorno/bin/rst2html5.py +33 -0
- entorno/bin/rst2latex.py +26 -0
- entorno/bin/rst2man.py +27 -0
- entorno/bin/rst2odt.py +28 -0
- entorno/bin/rst2odt_prepstyles.py +20 -0
- entorno/bin/rst2pseudoxml.py +23 -0
- entorno/bin/rst2s5.py +24 -0
- entorno/bin/rst2xetex.py +27 -0
- entorno/bin/rst2xml.py +23 -0
- entorno/bin/rstpep2html.py +25 -0
- experiments/scripts/compute_metrics.py +187 -0
- experiments/scripts/metrics_complexity_analysis.py +109 -0
- experiments/scripts/metro_experiment.py +133 -0
- experiments/scripts/opt_metro_experiment.py +343 -0
- tests/__init__.py +0 -0
- tests/test_binary.py +759 -0
- tests/test_non_binary.py +371 -0
- tsadmetrics/_tsadeval/affiliation/__init__.py +0 -0
- tsadmetrics/_tsadeval/affiliation/_affiliation_zone.py +86 -0
- tsadmetrics/_tsadeval/affiliation/_integral_interval.py +464 -0
- tsadmetrics/_tsadeval/affiliation/_single_ground_truth_event.py +68 -0
- tsadmetrics/_tsadeval/affiliation/generics.py +135 -0
- tsadmetrics/_tsadeval/affiliation/metrics.py +114 -0
- tsadmetrics/_tsadeval/eTaPR_pkg/DataManage/File_IO.py +175 -0
- tsadmetrics/_tsadeval/eTaPR_pkg/DataManage/Range.py +50 -0
- tsadmetrics/_tsadeval/eTaPR_pkg/DataManage/Time_Plot.py +184 -0
- tsadmetrics/_tsadeval/eTaPR_pkg/DataManage/__init__.py +0 -0
- tsadmetrics/_tsadeval/eTaPR_pkg/__init__.py +0 -0
- tsadmetrics/_tsadeval/eTaPR_pkg/etapr.py +386 -0
- tsadmetrics/_tsadeval/eTaPR_pkg/tapr.py +362 -0
- tsadmetrics/_tsadeval/prts/__init__.py +0 -0
- tsadmetrics/_tsadeval/prts/base/__init__.py +0 -0
- tsadmetrics/_tsadeval/prts/base/time_series_metrics.py +165 -0
- tsadmetrics/_tsadeval/prts/basic_metrics_ts.py +121 -0
- tsadmetrics/_tsadeval/prts/time_series_metrics/__init__.py +0 -0
- tsadmetrics/_tsadeval/prts/time_series_metrics/fscore.py +61 -0
- tsadmetrics/_tsadeval/prts/time_series_metrics/precision.py +86 -0
- tsadmetrics/_tsadeval/prts/time_series_metrics/precision_recall.py +21 -0
- tsadmetrics/_tsadeval/prts/time_series_metrics/recall.py +85 -0
- {tsadmetrics-0.1.4.dist-info → tsadmetrics-0.1.5.dist-info}/METADATA +1 -1
- tsadmetrics-0.1.5.dist-info/RECORD +62 -0
- tsadmetrics-0.1.5.dist-info/top_level.txt +4 -0
- tsadmetrics-0.1.4.dist-info/RECORD +0 -20
- tsadmetrics-0.1.4.dist-info/top_level.txt +0 -1
- {tsadmetrics-0.1.4.dist-info → tsadmetrics-0.1.5.dist-info}/WHEEL +0 -0
@@ -0,0 +1,464 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
import math
|
4
|
+
from .generics import _sum_wo_nan
|
5
|
+
"""
|
6
|
+
In order to shorten the length of the variables,
|
7
|
+
the general convention in this file is to let:
|
8
|
+
- I for a predicted event (start, stop),
|
9
|
+
- Is for a list of predicted events,
|
10
|
+
- J for a ground truth event,
|
11
|
+
- Js for a list of ground truth events.
|
12
|
+
"""
|
13
|
+
|
14
|
+
def interval_length(J = (1,2)):
|
15
|
+
"""
|
16
|
+
Length of an interval
|
17
|
+
|
18
|
+
:param J: couple representating the start and stop of an interval, or None
|
19
|
+
:return: length of the interval, and 0 for a None interval
|
20
|
+
"""
|
21
|
+
if J is None:
|
22
|
+
return(0)
|
23
|
+
return(J[1] - J[0])
|
24
|
+
|
25
|
+
def sum_interval_lengths(Is = [(1,2),(3,4),(5,6)]):
|
26
|
+
"""
|
27
|
+
Sum of length of the intervals
|
28
|
+
|
29
|
+
:param Is: list of intervals represented by starts and stops
|
30
|
+
:return: sum of the interval length
|
31
|
+
"""
|
32
|
+
return(sum([interval_length(I) for I in Is]))
|
33
|
+
|
34
|
+
def interval_intersection(I = (1, 3), J = (2, 4)):
|
35
|
+
"""
|
36
|
+
Intersection between two intervals I and J
|
37
|
+
I and J should be either empty or represent a positive interval (no point)
|
38
|
+
|
39
|
+
:param I: an interval represented by start and stop
|
40
|
+
:param J: a second interval of the same form
|
41
|
+
:return: an interval representing the start and stop of the intersection (or None if empty)
|
42
|
+
"""
|
43
|
+
if I is None:
|
44
|
+
return(None)
|
45
|
+
if J is None:
|
46
|
+
return(None)
|
47
|
+
|
48
|
+
I_inter_J = (max(I[0], J[0]), min(I[1], J[1]))
|
49
|
+
if I_inter_J[0] >= I_inter_J[1]:
|
50
|
+
return(None)
|
51
|
+
else:
|
52
|
+
return(I_inter_J)
|
53
|
+
|
54
|
+
def interval_subset(I = (1, 3), J = (0, 6)):
|
55
|
+
"""
|
56
|
+
Checks whether I is a subset of J
|
57
|
+
|
58
|
+
:param I: an non empty interval represented by start and stop
|
59
|
+
:param J: a second non empty interval of the same form
|
60
|
+
:return: True if I is a subset of J
|
61
|
+
"""
|
62
|
+
if (I[0] >= J[0]) and (I[1] <= J[1]):
|
63
|
+
return True
|
64
|
+
else:
|
65
|
+
return False
|
66
|
+
|
67
|
+
def cut_into_three_func(I, J):
|
68
|
+
"""
|
69
|
+
Cut an interval I into a partition of 3 subsets:
|
70
|
+
the elements before J,
|
71
|
+
the elements belonging to J,
|
72
|
+
and the elements after J
|
73
|
+
|
74
|
+
:param I: an interval represented by start and stop, or None for an empty one
|
75
|
+
:param J: a non empty interval
|
76
|
+
:return: a triplet of three intervals, each represented by either (start, stop) or None
|
77
|
+
"""
|
78
|
+
if I is None:
|
79
|
+
return((None, None, None))
|
80
|
+
|
81
|
+
I_inter_J = interval_intersection(I, J)
|
82
|
+
if I == I_inter_J:
|
83
|
+
I_before = None
|
84
|
+
I_after = None
|
85
|
+
elif I[1] <= J[0]:
|
86
|
+
I_before = I
|
87
|
+
I_after = None
|
88
|
+
elif I[0] >= J[1]:
|
89
|
+
I_before = None
|
90
|
+
I_after = I
|
91
|
+
elif (I[0] <= J[0]) and (I[1] >= J[1]):
|
92
|
+
I_before = (I[0], I_inter_J[0])
|
93
|
+
I_after = (I_inter_J[1], I[1])
|
94
|
+
elif I[0] <= J[0]:
|
95
|
+
I_before = (I[0], I_inter_J[0])
|
96
|
+
I_after = None
|
97
|
+
elif I[1] >= J[1]:
|
98
|
+
I_before = None
|
99
|
+
I_after = (I_inter_J[1], I[1])
|
100
|
+
else:
|
101
|
+
raise ValueError('unexpected unconsidered case')
|
102
|
+
return(I_before, I_inter_J, I_after)
|
103
|
+
|
104
|
+
def get_pivot_j(I, J):
|
105
|
+
"""
|
106
|
+
Get the single point of J that is the closest to I, called 'pivot' here,
|
107
|
+
with the requirement that I should be outside J
|
108
|
+
|
109
|
+
:param I: a non empty interval (start, stop)
|
110
|
+
:param J: another non empty interval, with empty intersection with I
|
111
|
+
:return: the element j of J that is the closest to I
|
112
|
+
"""
|
113
|
+
if interval_intersection(I, J) is not None:
|
114
|
+
raise ValueError('I and J should have a void intersection')
|
115
|
+
|
116
|
+
j_pivot = None # j_pivot is a border of J
|
117
|
+
if max(I) <= min(J):
|
118
|
+
j_pivot = min(J)
|
119
|
+
elif min(I) >= max(J):
|
120
|
+
j_pivot = max(J)
|
121
|
+
else:
|
122
|
+
raise ValueError('I should be outside J')
|
123
|
+
return(j_pivot)
|
124
|
+
|
125
|
+
def integral_mini_interval(I, J):
|
126
|
+
"""
|
127
|
+
In the specific case where interval I is located outside J,
|
128
|
+
integral of distance from x to J over the interval x \in I.
|
129
|
+
This is the *integral* i.e. the sum.
|
130
|
+
It's not the mean (not divided by the length of I yet)
|
131
|
+
|
132
|
+
:param I: a interval (start, stop), or None
|
133
|
+
:param J: a non empty interval, with empty intersection with I
|
134
|
+
:return: the integral of distances d(x, J) over x \in I
|
135
|
+
"""
|
136
|
+
if I is None:
|
137
|
+
return(0)
|
138
|
+
|
139
|
+
j_pivot = get_pivot_j(I, J)
|
140
|
+
a = min(I)
|
141
|
+
b = max(I)
|
142
|
+
return((b-a)*abs((j_pivot - (a+b)/2)))
|
143
|
+
|
144
|
+
def integral_interval_distance(I, J):
|
145
|
+
"""
|
146
|
+
For any non empty intervals I, J, compute the
|
147
|
+
integral of distance from x to J over the interval x \in I.
|
148
|
+
This is the *integral* i.e. the sum.
|
149
|
+
It's not the mean (not divided by the length of I yet)
|
150
|
+
The interval I can intersect J or not
|
151
|
+
|
152
|
+
:param I: a interval (start, stop), or None
|
153
|
+
:param J: a non empty interval
|
154
|
+
:return: the integral of distances d(x, J) over x \in I
|
155
|
+
"""
|
156
|
+
# I and J are single intervals (not generic sets)
|
157
|
+
# I is a predicted interval in the range of affiliation of J
|
158
|
+
|
159
|
+
def f(I_cut):
|
160
|
+
return(integral_mini_interval(I_cut, J))
|
161
|
+
# If I_middle is fully included into J, it is
|
162
|
+
# the distance to J is always 0
|
163
|
+
def f0(I_middle):
|
164
|
+
return(0)
|
165
|
+
|
166
|
+
cut_into_three = cut_into_three_func(I, J)
|
167
|
+
# Distance for now, not the mean:
|
168
|
+
# Distance left: Between cut_into_three[0] and the point min(J)
|
169
|
+
d_left = f(cut_into_three[0])
|
170
|
+
# Distance middle: Between cut_into_three[1] = I inter J, and J
|
171
|
+
d_middle = f0(cut_into_three[1])
|
172
|
+
# Distance right: Between cut_into_three[2] and the point max(J)
|
173
|
+
d_right = f(cut_into_three[2])
|
174
|
+
# It's an integral so summable
|
175
|
+
return(d_left + d_middle + d_right)
|
176
|
+
|
177
|
+
def integral_mini_interval_P_CDFmethod__min_piece(I, J, E):
|
178
|
+
"""
|
179
|
+
Helper of `integral_mini_interval_Pprecision_CDFmethod`
|
180
|
+
In the specific case where interval I is located outside J,
|
181
|
+
compute the integral $\int_{d_min}^{d_max} \min(m, x) dx$, with:
|
182
|
+
- m the smallest distance from J to E,
|
183
|
+
- d_min the smallest distance d(x, J) from x \in I to J
|
184
|
+
- d_max the largest distance d(x, J) from x \in I to J
|
185
|
+
|
186
|
+
:param I: a single predicted interval, a non empty interval (start, stop)
|
187
|
+
:param J: ground truth interval, a non empty interval, with empty intersection with I
|
188
|
+
:param E: the affiliation/influence zone for J, represented as a couple (start, stop)
|
189
|
+
:return: the integral $\int_{d_min}^{d_max} \min(m, x) dx$
|
190
|
+
"""
|
191
|
+
if interval_intersection(I, J) is not None:
|
192
|
+
raise ValueError('I and J should have a void intersection')
|
193
|
+
if not interval_subset(J, E):
|
194
|
+
raise ValueError('J should be included in E')
|
195
|
+
if not interval_subset(I, E):
|
196
|
+
raise ValueError('I should be included in E')
|
197
|
+
|
198
|
+
e_min = min(E)
|
199
|
+
j_min = min(J)
|
200
|
+
j_max = max(J)
|
201
|
+
e_max = max(E)
|
202
|
+
i_min = min(I)
|
203
|
+
i_max = max(I)
|
204
|
+
|
205
|
+
d_min = max(i_min - j_max, j_min - i_max)
|
206
|
+
d_max = max(i_max - j_max, j_min - i_min)
|
207
|
+
m = min(j_min - e_min, e_max - j_max)
|
208
|
+
A = min(d_max, m)**2 - min(d_min, m)**2
|
209
|
+
B = max(d_max, m) - max(d_min, m)
|
210
|
+
C = (1/2)*A + m*B
|
211
|
+
return(C)
|
212
|
+
|
213
|
+
def integral_mini_interval_Pprecision_CDFmethod(I, J, E):
|
214
|
+
"""
|
215
|
+
Integral of the probability of distances over the interval I.
|
216
|
+
In the specific case where interval I is located outside J,
|
217
|
+
compute the integral $\int_{x \in I} Fbar(dist(x,J)) dx$.
|
218
|
+
This is the *integral* i.e. the sum (not the mean)
|
219
|
+
|
220
|
+
:param I: a single predicted interval, a non empty interval (start, stop)
|
221
|
+
:param J: ground truth interval, a non empty interval, with empty intersection with I
|
222
|
+
:param E: the affiliation/influence zone for J, represented as a couple (start, stop)
|
223
|
+
:return: the integral $\int_{x \in I} Fbar(dist(x,J)) dx$
|
224
|
+
"""
|
225
|
+
integral_min_piece = integral_mini_interval_P_CDFmethod__min_piece(I, J, E)
|
226
|
+
|
227
|
+
e_min = min(E)
|
228
|
+
j_min = min(J)
|
229
|
+
j_max = max(J)
|
230
|
+
e_max = max(E)
|
231
|
+
i_min = min(I)
|
232
|
+
i_max = max(I)
|
233
|
+
d_min = max(i_min - j_max, j_min - i_max)
|
234
|
+
d_max = max(i_max - j_max, j_min - i_min)
|
235
|
+
integral_linear_piece = (1/2)*(d_max**2 - d_min**2)
|
236
|
+
integral_remaining_piece = (j_max - j_min)*(i_max - i_min)
|
237
|
+
|
238
|
+
DeltaI = i_max - i_min
|
239
|
+
DeltaE = e_max - e_min
|
240
|
+
|
241
|
+
output = DeltaI - (1/DeltaE)*(integral_min_piece + integral_linear_piece + integral_remaining_piece)
|
242
|
+
return(output)
|
243
|
+
|
244
|
+
def integral_interval_probaCDF_precision(I, J, E):
|
245
|
+
"""
|
246
|
+
Integral of the probability of distances over the interval I.
|
247
|
+
Compute the integral $\int_{x \in I} Fbar(dist(x,J)) dx$.
|
248
|
+
This is the *integral* i.e. the sum (not the mean)
|
249
|
+
|
250
|
+
:param I: a single (non empty) predicted interval in the zone of affiliation of J
|
251
|
+
:param J: ground truth interval
|
252
|
+
:param E: affiliation/influence zone for J
|
253
|
+
:return: the integral $\int_{x \in I} Fbar(dist(x,J)) dx$
|
254
|
+
"""
|
255
|
+
# I and J are single intervals (not generic sets)
|
256
|
+
def f(I_cut):
|
257
|
+
if I_cut is None:
|
258
|
+
return(0)
|
259
|
+
else:
|
260
|
+
return(integral_mini_interval_Pprecision_CDFmethod(I_cut, J, E))
|
261
|
+
|
262
|
+
# If I_middle is fully included into J, it is
|
263
|
+
# integral of 1 on the interval I_middle, so it's |I_middle|
|
264
|
+
def f0(I_middle):
|
265
|
+
if I_middle is None:
|
266
|
+
return(0)
|
267
|
+
else:
|
268
|
+
return(max(I_middle) - min(I_middle))
|
269
|
+
|
270
|
+
cut_into_three = cut_into_three_func(I, J)
|
271
|
+
# Distance for now, not the mean:
|
272
|
+
# Distance left: Between cut_into_three[0] and the point min(J)
|
273
|
+
d_left = f(cut_into_three[0])
|
274
|
+
# Distance middle: Between cut_into_three[1] = I inter J, and J
|
275
|
+
d_middle = f0(cut_into_three[1])
|
276
|
+
# Distance right: Between cut_into_three[2] and the point max(J)
|
277
|
+
d_right = f(cut_into_three[2])
|
278
|
+
# It's an integral so summable
|
279
|
+
return(d_left + d_middle + d_right)
|
280
|
+
|
281
|
+
def cut_J_based_on_mean_func(J, e_mean):
|
282
|
+
"""
|
283
|
+
Helper function for the recall.
|
284
|
+
Partition J into two intervals: before and after e_mean
|
285
|
+
(e_mean represents the center element of E the zone of affiliation)
|
286
|
+
|
287
|
+
:param J: ground truth interval
|
288
|
+
:param e_mean: a float number (center value of E)
|
289
|
+
:return: a couple partitionning J into (J_before, J_after)
|
290
|
+
"""
|
291
|
+
if J is None:
|
292
|
+
J_before = None
|
293
|
+
J_after = None
|
294
|
+
elif e_mean >= max(J):
|
295
|
+
J_before = J
|
296
|
+
J_after = None
|
297
|
+
elif e_mean <= min(J):
|
298
|
+
J_before = None
|
299
|
+
J_after = J
|
300
|
+
else: # e_mean is across J
|
301
|
+
J_before = (min(J), e_mean)
|
302
|
+
J_after = (e_mean, max(J))
|
303
|
+
|
304
|
+
return((J_before, J_after))
|
305
|
+
|
306
|
+
def integral_mini_interval_Precall_CDFmethod(I, J, E):
|
307
|
+
"""
|
308
|
+
Integral of the probability of distances over the interval J.
|
309
|
+
In the specific case where interval J is located outside I,
|
310
|
+
compute the integral $\int_{y \in J} Fbar_y(dist(y,I)) dy$.
|
311
|
+
This is the *integral* i.e. the sum (not the mean)
|
312
|
+
|
313
|
+
:param I: a single (non empty) predicted interval
|
314
|
+
:param J: ground truth (non empty) interval, with empty intersection with I
|
315
|
+
:param E: the affiliation/influence zone for J, represented as a couple (start, stop)
|
316
|
+
:return: the integral $\int_{y \in J} Fbar_y(dist(y,I)) dy$
|
317
|
+
"""
|
318
|
+
# The interval J should be located outside I
|
319
|
+
# (so it's either the left piece or the right piece w.r.t I)
|
320
|
+
i_pivot = get_pivot_j(J, I)
|
321
|
+
e_min = min(E)
|
322
|
+
e_max = max(E)
|
323
|
+
e_mean = (e_min + e_max) / 2
|
324
|
+
|
325
|
+
# If i_pivot is outside E (it's possible), then
|
326
|
+
# the distance is worst that any random element within E,
|
327
|
+
# so we set the recall to 0
|
328
|
+
if i_pivot <= min(E):
|
329
|
+
return(0)
|
330
|
+
elif i_pivot >= max(E):
|
331
|
+
return(0)
|
332
|
+
# Otherwise, we have at least i_pivot in E and so d < M so min(d,M)=d
|
333
|
+
|
334
|
+
cut_J_based_on_e_mean = cut_J_based_on_mean_func(J, e_mean)
|
335
|
+
J_before = cut_J_based_on_e_mean[0]
|
336
|
+
J_after = cut_J_based_on_e_mean[1]
|
337
|
+
|
338
|
+
iemin_mean = (e_min + i_pivot)/2
|
339
|
+
cut_Jbefore_based_on_iemin_mean = cut_J_based_on_mean_func(J_before, iemin_mean)
|
340
|
+
J_before_closeE = cut_Jbefore_based_on_iemin_mean[0] # before e_mean and closer to e_min than i_pivot ~ J_before_before
|
341
|
+
J_before_closeI = cut_Jbefore_based_on_iemin_mean[1] # before e_mean and closer to i_pivot than e_min ~ J_before_after
|
342
|
+
|
343
|
+
iemax_mean = (e_max + i_pivot)/2
|
344
|
+
cut_Jafter_based_on_iemax_mean = cut_J_based_on_mean_func(J_after, iemax_mean)
|
345
|
+
J_after_closeI = cut_Jafter_based_on_iemax_mean[0] # after e_mean and closer to i_pivot than e_max ~ J_after_before
|
346
|
+
J_after_closeE = cut_Jafter_based_on_iemax_mean[1] # after e_mean and closer to e_max than i_pivot ~ J_after_after
|
347
|
+
|
348
|
+
if J_before_closeE is not None:
|
349
|
+
j_before_before_min = min(J_before_closeE) # == min(J)
|
350
|
+
j_before_before_max = max(J_before_closeE)
|
351
|
+
else:
|
352
|
+
j_before_before_min = math.nan
|
353
|
+
j_before_before_max = math.nan
|
354
|
+
|
355
|
+
if J_before_closeI is not None:
|
356
|
+
j_before_after_min = min(J_before_closeI) # == j_before_before_max if existing
|
357
|
+
j_before_after_max = max(J_before_closeI) # == max(J_before)
|
358
|
+
else:
|
359
|
+
j_before_after_min = math.nan
|
360
|
+
j_before_after_max = math.nan
|
361
|
+
|
362
|
+
if J_after_closeI is not None:
|
363
|
+
j_after_before_min = min(J_after_closeI) # == min(J_after)
|
364
|
+
j_after_before_max = max(J_after_closeI)
|
365
|
+
else:
|
366
|
+
j_after_before_min = math.nan
|
367
|
+
j_after_before_max = math.nan
|
368
|
+
|
369
|
+
if J_after_closeE is not None:
|
370
|
+
j_after_after_min = min(J_after_closeE) # == j_after_before_max if existing
|
371
|
+
j_after_after_max = max(J_after_closeE) # == max(J)
|
372
|
+
else:
|
373
|
+
j_after_after_min = math.nan
|
374
|
+
j_after_after_max = math.nan
|
375
|
+
|
376
|
+
# <-- J_before_closeE --> <-- J_before_closeI --> <-- J_after_closeI --> <-- J_after_closeE -->
|
377
|
+
# j_bb_min j_bb_max j_ba_min j_ba_max j_ab_min j_ab_max j_aa_min j_aa_max
|
378
|
+
# (with `b` for before and `a` for after in the previous variable names)
|
379
|
+
|
380
|
+
# vs e_mean m = min(t-e_min, e_max-t) d=|i_pivot-t| min(d,m) \int min(d,m)dt \int d dt \int_(min(d,m)+d)dt \int_{t \in J}(min(d,m)+d)dt
|
381
|
+
# Case J_before_closeE & i_pivot after J before t-e_min i_pivot-t min(i_pivot-t,t-e_min) = t-e_min t^2/2-e_min*t i_pivot*t-t^2/2 t^2/2-e_min*t+i_pivot*t-t^2/2 = (i_pivot-e_min)*t (i_pivot-e_min)*tB - (i_pivot-e_min)*tA = (i_pivot-e_min)*(tB-tA)
|
382
|
+
# Case J_before_closeI & i_pivot after J before t-e_min i_pivot-t min(i_pivot-t,t-e_min) = i_pivot-t i_pivot*t-t^2/2 i_pivot*t-t^2/2 i_pivot*t-t^2/2+i_pivot*t-t^2/2 = 2*i_pivot*t-t^2 2*i_pivot*tB-tB^2 - 2*i_pivot*tA + tA^2 = 2*i_pivot*(tB-tA) - (tB^2 - tA^2)
|
383
|
+
# Case J_after_closeI & i_pivot after J after e_max-t i_pivot-t min(i_pivot-t,e_max-t) = i_pivot-t i_pivot*t-t^2/2 i_pivot*t-t^2/2 i_pivot*t-t^2/2+i_pivot*t-t^2/2 = 2*i_pivot*t-t^2 2*i_pivot*tB-tB^2 - 2*i_pivot*tA + tA^2 = 2*i_pivot*(tB-tA) - (tB^2 - tA^2)
|
384
|
+
# Case J_after_closeE & i_pivot after J after e_max-t i_pivot-t min(i_pivot-t,e_max-t) = e_max-t e_max*t-t^2/2 i_pivot*t-t^2/2 e_max*t-t^2/2+i_pivot*t-t^2/2 = (e_max+i_pivot)*t-t^2 (e_max+i_pivot)*tB-tB^2 - (e_max+i_pivot)*tA + tA^2 = (e_max+i_pivot)*(tB-tA) - (tB^2 - tA^2)
|
385
|
+
#
|
386
|
+
# Case J_before_closeE & i_pivot before J before t-e_min t-i_pivot min(t-i_pivot,t-e_min) = t-e_min t^2/2-e_min*t t^2/2-i_pivot*t t^2/2-e_min*t+t^2/2-i_pivot*t = t^2-(e_min+i_pivot)*t tB^2-(e_min+i_pivot)*tB - tA^2 + (e_min+i_pivot)*tA = (tB^2 - tA^2) - (e_min+i_pivot)*(tB-tA)
|
387
|
+
# Case J_before_closeI & i_pivot before J before t-e_min t-i_pivot min(t-i_pivot,t-e_min) = t-i_pivot t^2/2-i_pivot*t t^2/2-i_pivot*t t^2/2-i_pivot*t+t^2/2-i_pivot*t = t^2-2*i_pivot*t tB^2-2*i_pivot*tB - tA^2 + 2*i_pivot*tA = (tB^2 - tA^2) - 2*i_pivot*(tB-tA)
|
388
|
+
# Case J_after_closeI & i_pivot before J after e_max-t t-i_pivot min(t-i_pivot,e_max-t) = t-i_pivot t^2/2-i_pivot*t t^2/2-i_pivot*t t^2/2-i_pivot*t+t^2/2-i_pivot*t = t^2-2*i_pivot*t tB^2-2*i_pivot*tB - tA^2 + 2*i_pivot*tA = (tB^2 - tA^2) - 2*i_pivot*(tB-tA)
|
389
|
+
# Case J_after_closeE & i_pivot before J after e_max-t t-i_pivot min(t-i_pivot,e_max-t) = e_max-t e_max*t-t^2/2 t^2/2-i_pivot*t e_max*t-t^2/2+t^2/2-i_pivot*t = (e_max-i_pivot)*t (e_max-i_pivot)*tB - (e_max-i_pivot)*tA = (e_max-i_pivot)*(tB-tA)
|
390
|
+
|
391
|
+
if i_pivot >= max(J):
|
392
|
+
part1_before_closeE = (i_pivot-e_min)*(j_before_before_max - j_before_before_min) # (i_pivot-e_min)*(tB-tA) # j_before_before_max - j_before_before_min
|
393
|
+
part2_before_closeI = 2*i_pivot*(j_before_after_max-j_before_after_min) - (j_before_after_max**2 - j_before_after_min**2) # 2*i_pivot*(tB-tA) - (tB^2 - tA^2) # j_before_after_max - j_before_after_min
|
394
|
+
part3_after_closeI = 2*i_pivot*(j_after_before_max-j_after_before_min) - (j_after_before_max**2 - j_after_before_min**2) # 2*i_pivot*(tB-tA) - (tB^2 - tA^2) # j_after_before_max - j_after_before_min
|
395
|
+
part4_after_closeE = (e_max+i_pivot)*(j_after_after_max-j_after_after_min) - (j_after_after_max**2 - j_after_after_min**2) # (e_max+i_pivot)*(tB-tA) - (tB^2 - tA^2) # j_after_after_max - j_after_after_min
|
396
|
+
out_parts = [part1_before_closeE, part2_before_closeI, part3_after_closeI, part4_after_closeE]
|
397
|
+
elif i_pivot <= min(J):
|
398
|
+
part1_before_closeE = (j_before_before_max**2 - j_before_before_min**2) - (e_min+i_pivot)*(j_before_before_max-j_before_before_min) # (tB^2 - tA^2) - (e_min+i_pivot)*(tB-tA) # j_before_before_max - j_before_before_min
|
399
|
+
part2_before_closeI = (j_before_after_max**2 - j_before_after_min**2) - 2*i_pivot*(j_before_after_max-j_before_after_min) # (tB^2 - tA^2) - 2*i_pivot*(tB-tA) # j_before_after_max - j_before_after_min
|
400
|
+
part3_after_closeI = (j_after_before_max**2 - j_after_before_min**2) - 2*i_pivot*(j_after_before_max - j_after_before_min) # (tB^2 - tA^2) - 2*i_pivot*(tB-tA) # j_after_before_max - j_after_before_min
|
401
|
+
part4_after_closeE = (e_max-i_pivot)*(j_after_after_max - j_after_after_min) # (e_max-i_pivot)*(tB-tA) # j_after_after_max - j_after_after_min
|
402
|
+
out_parts = [part1_before_closeE, part2_before_closeI, part3_after_closeI, part4_after_closeE]
|
403
|
+
else:
|
404
|
+
raise ValueError('The i_pivot should be outside J')
|
405
|
+
|
406
|
+
out_integral_min_dm_plus_d = _sum_wo_nan(out_parts) # integral on all J, i.e. sum of the disjoint parts
|
407
|
+
|
408
|
+
# We have for each point t of J:
|
409
|
+
# \bar{F}_{t, recall}(d) = 1 - (1/|E|) * (min(d,m) + d)
|
410
|
+
# Since t is a single-point here, and we are in the case where i_pivot is inside E.
|
411
|
+
# The integral is then given by:
|
412
|
+
# C = \int_{t \in J} \bar{F}_{t, recall}(D(t)) dt
|
413
|
+
# = \int_{t \in J} 1 - (1/|E|) * (min(d,m) + d) dt
|
414
|
+
# = |J| - (1/|E|) * [\int_{t \in J} (min(d,m) + d) dt]
|
415
|
+
# = |J| - (1/|E|) * out_integral_min_dm_plus_d
|
416
|
+
DeltaJ = max(J) - min(J)
|
417
|
+
DeltaE = max(E) - min(E)
|
418
|
+
C = DeltaJ - (1/DeltaE) * out_integral_min_dm_plus_d
|
419
|
+
|
420
|
+
return(C)
|
421
|
+
|
422
|
+
def integral_interval_probaCDF_recall(I, J, E):
|
423
|
+
"""
|
424
|
+
Integral of the probability of distances over the interval J.
|
425
|
+
Compute the integral $\int_{y \in J} Fbar_y(dist(y,I)) dy$.
|
426
|
+
This is the *integral* i.e. the sum (not the mean)
|
427
|
+
|
428
|
+
:param I: a single (non empty) predicted interval
|
429
|
+
:param J: ground truth (non empty) interval
|
430
|
+
:param E: the affiliation/influence zone for J
|
431
|
+
:return: the integral $\int_{y \in J} Fbar_y(dist(y,I)) dy$
|
432
|
+
"""
|
433
|
+
# I and J are single intervals (not generic sets)
|
434
|
+
# E is the outside affiliation interval of J (even for recall!)
|
435
|
+
# (in particular J \subset E)
|
436
|
+
#
|
437
|
+
# J is the portion of the ground truth affiliated to I
|
438
|
+
# I is a predicted interval (can be outside E possibly since it's recall)
|
439
|
+
def f(J_cut):
|
440
|
+
if J_cut is None:
|
441
|
+
return(0)
|
442
|
+
else:
|
443
|
+
return integral_mini_interval_Precall_CDFmethod(I, J_cut, E)
|
444
|
+
|
445
|
+
# If J_middle is fully included into I, it is
|
446
|
+
# integral of 1 on the interval J_middle, so it's |J_middle|
|
447
|
+
def f0(J_middle):
|
448
|
+
if J_middle is None:
|
449
|
+
return(0)
|
450
|
+
else:
|
451
|
+
return(max(J_middle) - min(J_middle))
|
452
|
+
|
453
|
+
cut_into_three = cut_into_three_func(J, I) # it's J that we cut into 3, depending on the position w.r.t I
|
454
|
+
# since we integrate over J this time.
|
455
|
+
#
|
456
|
+
# Distance for now, not the mean:
|
457
|
+
# Distance left: Between cut_into_three[0] and the point min(I)
|
458
|
+
d_left = f(cut_into_three[0])
|
459
|
+
# Distance middle: Between cut_into_three[1] = J inter I, and I
|
460
|
+
d_middle = f0(cut_into_three[1])
|
461
|
+
# Distance right: Between cut_into_three[2] and the point max(I)
|
462
|
+
d_right = f(cut_into_three[2])
|
463
|
+
# It's an integral so summable
|
464
|
+
return(d_left + d_middle + d_right)
|
@@ -0,0 +1,68 @@
|
|
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))
|
@@ -0,0 +1,135 @@
|
|
1
|
+
#!/usr/bin/env python3
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
from itertools import groupby
|
4
|
+
from operator import itemgetter
|
5
|
+
import math
|
6
|
+
import gzip
|
7
|
+
import glob
|
8
|
+
import os
|
9
|
+
|
10
|
+
def convert_vector_to_events(vector = [0, 1, 1, 0, 0, 1, 0]):
|
11
|
+
"""
|
12
|
+
Convert a binary vector (indicating 1 for the anomalous instances)
|
13
|
+
to a list of events. The events are considered as durations,
|
14
|
+
i.e. setting 1 at index i corresponds to an anomalous interval [i, i+1).
|
15
|
+
|
16
|
+
:param vector: a list of elements belonging to {0, 1}
|
17
|
+
:return: a list of couples, each couple representing the start and stop of
|
18
|
+
each event
|
19
|
+
"""
|
20
|
+
positive_indexes = [idx for idx, val in enumerate(vector) if val > 0]
|
21
|
+
events = []
|
22
|
+
for k, g in groupby(enumerate(positive_indexes), lambda ix : ix[0] - ix[1]):
|
23
|
+
cur_cut = list(map(itemgetter(1), g))
|
24
|
+
events.append((cur_cut[0], cur_cut[-1]))
|
25
|
+
|
26
|
+
# Consistent conversion in case of range anomalies (for indexes):
|
27
|
+
# A positive index i is considered as the interval [i, i+1),
|
28
|
+
# so the last index should be moved by 1
|
29
|
+
events = [(x, y+1) for (x,y) in events]
|
30
|
+
|
31
|
+
return(events)
|
32
|
+
|
33
|
+
def infer_Trange(events_pred, events_gt):
|
34
|
+
"""
|
35
|
+
Given the list of events events_pred and events_gt, get the
|
36
|
+
smallest possible Trange corresponding to the start and stop indexes
|
37
|
+
of the whole series.
|
38
|
+
Trange will not influence the measure of distances, but will impact the
|
39
|
+
measures of probabilities.
|
40
|
+
|
41
|
+
:param events_pred: a list of couples corresponding to predicted events
|
42
|
+
:param events_gt: a list of couples corresponding to ground truth events
|
43
|
+
:return: a couple corresponding to the smallest range containing the events
|
44
|
+
"""
|
45
|
+
if len(events_gt) == 0:
|
46
|
+
raise ValueError('The gt events should contain at least one event')
|
47
|
+
if len(events_pred) == 0:
|
48
|
+
# empty prediction, base Trange only on events_gt (which is non empty)
|
49
|
+
return(infer_Trange(events_gt, events_gt))
|
50
|
+
|
51
|
+
min_pred = min([x[0] for x in events_pred])
|
52
|
+
min_gt = min([x[0] for x in events_gt])
|
53
|
+
max_pred = max([x[1] for x in events_pred])
|
54
|
+
max_gt = max([x[1] for x in events_gt])
|
55
|
+
Trange = (min(min_pred, min_gt), max(max_pred, max_gt))
|
56
|
+
return(Trange)
|
57
|
+
|
58
|
+
def has_point_anomalies(events):
|
59
|
+
"""
|
60
|
+
Checking whether events contain point anomalies, i.e.
|
61
|
+
events starting and stopping at the same time.
|
62
|
+
|
63
|
+
:param events: a list of couples corresponding to predicted events
|
64
|
+
:return: True is the events have any point anomalies, False otherwise
|
65
|
+
"""
|
66
|
+
if len(events) == 0:
|
67
|
+
return(False)
|
68
|
+
return(min([x[1] - x[0] for x in events]) == 0)
|
69
|
+
|
70
|
+
def _sum_wo_nan(vec):
|
71
|
+
"""
|
72
|
+
Sum of elements, ignoring math.isnan ones
|
73
|
+
|
74
|
+
:param vec: vector of floating numbers
|
75
|
+
:return: sum of the elements, ignoring math.isnan ones
|
76
|
+
"""
|
77
|
+
vec_wo_nan = [e for e in vec if not math.isnan(e)]
|
78
|
+
return(sum(vec_wo_nan))
|
79
|
+
|
80
|
+
def _len_wo_nan(vec):
|
81
|
+
"""
|
82
|
+
Count of elements, ignoring math.isnan ones
|
83
|
+
|
84
|
+
:param vec: vector of floating numbers
|
85
|
+
:return: count of the elements, ignoring math.isnan ones
|
86
|
+
"""
|
87
|
+
vec_wo_nan = [e for e in vec if not math.isnan(e)]
|
88
|
+
return(len(vec_wo_nan))
|
89
|
+
|
90
|
+
def read_gz_data(filename = 'data/machinetemp_groundtruth.gz'):
|
91
|
+
"""
|
92
|
+
Load a file compressed with gz, such that each line of the
|
93
|
+
file is either 0 (representing a normal instance) or 1 (representing)
|
94
|
+
an anomalous instance.
|
95
|
+
:param filename: file path to the gz compressed file
|
96
|
+
:return: list of integers with either 0 or 1
|
97
|
+
"""
|
98
|
+
with gzip.open(filename, 'rb') as f:
|
99
|
+
content = f.read().splitlines()
|
100
|
+
content = [int(x) for x in content]
|
101
|
+
return(content)
|
102
|
+
|
103
|
+
def read_all_as_events():
|
104
|
+
"""
|
105
|
+
Load the files contained in the folder `data/` and convert
|
106
|
+
to events. The length of the series is kept.
|
107
|
+
The convention for the file name is: `dataset_algorithm.gz`
|
108
|
+
:return: two dictionaries:
|
109
|
+
- the first containing the list of events for each dataset and algorithm,
|
110
|
+
- the second containing the range of the series for each dataset
|
111
|
+
"""
|
112
|
+
filepaths = glob.glob('data/*.gz')
|
113
|
+
datasets = dict()
|
114
|
+
Tranges = dict()
|
115
|
+
for filepath in filepaths:
|
116
|
+
vector = read_gz_data(filepath)
|
117
|
+
events = convert_vector_to_events(vector)
|
118
|
+
# ad hoc cut for those files
|
119
|
+
cut_filepath = (os.path.split(filepath)[1]).split('_')
|
120
|
+
data_name = cut_filepath[0]
|
121
|
+
algo_name = (cut_filepath[1]).split('.')[0]
|
122
|
+
if not data_name in datasets:
|
123
|
+
datasets[data_name] = dict()
|
124
|
+
Tranges[data_name] = (0, len(vector))
|
125
|
+
datasets[data_name][algo_name] = events
|
126
|
+
return(datasets, Tranges)
|
127
|
+
|
128
|
+
def f1_func(p, r):
|
129
|
+
"""
|
130
|
+
Compute the f1 function
|
131
|
+
:param p: precision numeric value
|
132
|
+
:param r: recall numeric value
|
133
|
+
:return: f1 numeric value
|
134
|
+
"""
|
135
|
+
return(2*p*r/(p+r))
|