-
Notifications
You must be signed in to change notification settings - Fork 4
/
alarms.py
67 lines (56 loc) · 1.89 KB
/
alarms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import evaluate.settings as settings
from .metric import Metric, get_alarms
class TruePositiveAlarms(Metric):
_name = "TPA"
_description = "True positive alarms (TPA) counts the number of continuous alarms that overlap with at least a single attack."
_requires = []
_requires_timed_dataset = True
_requires_attacks = True
_higher_is_better = True
@classmethod
def calculate(
cls,
truth=None,
predicted=None,
dataset=None,
attacks=None,
ergs=None,
):
assert dataset is not None and attacks is not None
count = 0
for start, end in get_alarms(dataset):
for attack in attacks:
if (
start <= attack["end"] + settings.alarm_gracetime
and end >= attack["start"] - settings.alarm_gracetime
): # any overlap
count += 1
break
return {cls._name: count}
class FalsePositiveAlarms(Metric):
_name = "FPA"
_description = "False positive alarms (FPA) counts the number of continuous alarms that do not overlap with any attack."
_requires = []
_requires_timed_dataset = True
_requires_attacks = True
_higher_is_better = False
@classmethod
def calculate(
cls,
truth=None,
predicted=None,
dataset=None,
attacks=None,
ergs=None,
):
assert dataset is not None and attacks is not None
fpa = set(get_alarms(dataset))
for start, end in get_alarms(dataset):
for attack in attacks:
if (
start <= attack["end"] + settings.alarm_gracetime
and end >= attack["start"] - settings.alarm_gracetime
): # any overlap
fpa.remove((start, end))
break
return {cls._name: len(fpa)}