-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathaggregations.py
More file actions
323 lines (265 loc) · 11.9 KB
/
aggregations.py
File metadata and controls
323 lines (265 loc) · 11.9 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import collections
import math
import statistics
from collections.abc import Callable
from collections.abc import Iterable
from typing import Any
from typing import Optional
from apache_beam.ml.anomaly.base import DEFAULT_MISSING_LABEL
from apache_beam.ml.anomaly.base import DEFAULT_NORMAL_LABEL
from apache_beam.ml.anomaly.base import DEFAULT_OUTLIER_LABEL
from apache_beam.ml.anomaly.base import AggregationFn
from apache_beam.ml.anomaly.base import AnomalyPrediction
from apache_beam.ml.anomaly.specifiable import specifiable
class _AggModelIdMixin:
def __init__(self, agg_model_id: Optional[str] = None):
self._agg_model_id = agg_model_id
def _set_agg_model_id_if_unset(self, agg_model_id: str) -> None:
if self._agg_model_id is None:
self._agg_model_id = agg_model_id
def add_model_id(self, result_dict):
result_dict["model_id"] = self._agg_model_id
class _SourcePredictionMixin:
def __init__(self, include_source_predictions):
self._include_source_predictions = include_source_predictions
def add_source_predictions(self, result_dict, source_predictions):
if self._include_source_predictions:
result_dict["source_predictions"] = list(source_predictions)
class LabelAggregation(AggregationFn, _AggModelIdMixin, _SourcePredictionMixin):
"""Aggregates anomaly predictions based on their labels.
This is an abstract base class for `AggregationFn`s that combine multiple
`AnomalyPrediction` objects into a single `AnomalyPrediction` based on
the labels of the input predictions.
Args:
agg_func (Callable[[Iterable[int]], int]): A function that aggregates
a collection of anomaly labels (integers) into a single label.
agg_model_id (Optional[str]): The model id used in aggregated predictions.
Defaults to None.
include_source_predictions (bool): If True, include the input predictions in
the `source_predictions` of the output. Defaults to False.
"""
def __init__(
self,
agg_func: Callable[[Iterable[int]], int],
agg_model_id: Optional[str] = None,
include_source_predictions: bool = False,
normal_label: int = DEFAULT_NORMAL_LABEL,
outlier_label: int = DEFAULT_OUTLIER_LABEL,
missing_label: int = DEFAULT_MISSING_LABEL,
):
self._agg = agg_func
self._normal_label = normal_label
self._outlier_label = outlier_label
self._missing_label = missing_label
_AggModelIdMixin.__init__(self, agg_model_id)
_SourcePredictionMixin.__init__(self, include_source_predictions)
def apply(
self, predictions: Iterable[AnomalyPrediction]) -> AnomalyPrediction:
"""Applies the label aggregation function to a list of predictions.
Args:
predictions (Iterable[AnomalyPrediction]): A collection of
`AnomalyPrediction` objects to be aggregated.
Returns:
AnomalyPrediction: A single `AnomalyPrediction` object with the
aggregated label. The aggregated label is determined as follows:
- If there are any non-missing and non-error labels, the `agg_func` is
applied to aggregate them.
- If all labels are error labels (`None`), the aggregated label is also
`None`.
- If there are a mix of missing and error labels, the aggregated label
is the `missing_label`.
"""
result_dict: dict[str, Any] = {}
_AggModelIdMixin.add_model_id(self, result_dict)
_SourcePredictionMixin.add_source_predictions(
self, result_dict, predictions)
labels = [
prediction.label for prediction in predictions if
prediction.label is not None and prediction.label != self._missing_label
]
if len(labels) > 0:
# apply aggregation_fn if there is any non-None and non-missing label
result_dict["label"] = self._agg(labels)
elif all(map(lambda x: x.label is None, predictions)):
# all are error labels (None) -- all scores are error
result_dict["label"] = None
else:
# some missing labels with some error labels (None)
result_dict["label"] = self._missing_label
return AnomalyPrediction(**result_dict)
class ScoreAggregation(AggregationFn, _AggModelIdMixin, _SourcePredictionMixin):
"""Aggregates anomaly predictions based on their scores.
This is an abstract base class for `AggregationFn`s that combine multiple
`AnomalyPrediction` objects into a single `AnomalyPrediction` based on
the scores of the input predictions.
Args:
agg_func (Callable[[Iterable[float]], float]): A function that aggregates
a collection of anomaly scores (floats) into a single score.
agg_model_id (Optional[str]): The model id used in aggregated predictions.
Defaults to None.
include_source_predictions (bool): If True, include the input predictions in
the `source_predictions` of the output. Defaults to False.
"""
def __init__(
self,
agg_func: Callable[[Iterable[float]], float],
agg_model_id: Optional[str] = None,
include_source_predictions: bool = False):
self._agg = agg_func
_AggModelIdMixin.__init__(self, agg_model_id)
_SourcePredictionMixin.__init__(self, include_source_predictions)
def apply(
self, predictions: Iterable[AnomalyPrediction]) -> AnomalyPrediction:
"""Applies the score aggregation function to a list of predictions.
Args:
predictions (Iterable[AnomalyPrediction]): A collection of
`AnomalyPrediction` objects to be aggregated.
Returns:
AnomalyPrediction: A single `AnomalyPrediction` object with the
aggregated score. The aggregated score is determined as follows:
- If there are any non-missing and non-error scores, the `agg_func` is
applied to aggregate them.
- If all scores are error scores (`None`), the aggregated score is also
`None`.
- If there are a mix of missing (`NaN`) and error scores (`None`), the
aggregated score is `NaN`.
"""
result_dict: dict[str, Any] = {}
_AggModelIdMixin.add_model_id(self, result_dict)
_SourcePredictionMixin.add_source_predictions(
self, result_dict, predictions)
scores = [
prediction.score for prediction in predictions
if prediction.score is not None and not math.isnan(prediction.score)
]
if len(scores) > 0:
# apply aggregation_fn if there is any non-None and non-NaN score
result_dict["score"] = self._agg(scores)
elif all(map(lambda x: x.score is None, predictions)):
# all are error scores (None)
result_dict["score"] = None
else:
# some missing scores (NaN) with some error scores (None)
result_dict["score"] = float("NaN")
return AnomalyPrediction(**result_dict)
@specifiable
class MajorityVote(LabelAggregation):
"""Aggregates anomaly labels using majority voting.
This `AggregationFn` implements a majority voting strategy to combine
anomaly labels from multiple `AnomalyPrediction` objects. It counts the
occurrences of normal and outlier labels and selects the label with the
higher count as the aggregated label. In case of a tie, a tie-breaker
label is used.
Example:
If input labels are [normal, outlier, outlier, normal, outlier], and
normal_label=0, outlier_label=1, then the aggregated label will be
outlier (1) because outliers have a majority (3 vs 2).
Args:
normal_label (int): The integer label for normal predictions. Defaults to 0.
outlier_label (int): The integer label for outlier predictions. Defaults to
1.
tie_breaker (int): The label to return if there is a tie in votes.
Defaults to 0 (normal_label).
**kwargs: Additional keyword arguments to pass to the base
`LabelAggregation` class.
"""
def __init__(self, tie_breaker=DEFAULT_NORMAL_LABEL, **kwargs):
self._tie_breaker = tie_breaker
def inner(predictions: Iterable[int]) -> int:
counters = collections.Counter(predictions)
if counters[self._normal_label] < counters[self._outlier_label]:
vote = self._outlier_label
elif counters[self._normal_label] > counters[self._outlier_label]:
vote = self._normal_label
else:
vote = self._tie_breaker
return vote
super().__init__(agg_func=inner, **kwargs)
# And scheme
@specifiable
class AllVote(LabelAggregation):
"""Aggregates anomaly labels using an "all vote" (AND) scheme.
This `AggregationFn` implements an "all vote" strategy. It aggregates
anomaly labels such that the result is considered an outlier only if all
input `AnomalyPrediction` objects are labeled as outliers.
Example:
If input labels are [outlier, outlier, outlier], and outlier_label=1,
then the aggregated label will be outlier (1).
If input labels are [outlier, normal, outlier], and outlier_label=1,
then the aggregated label will be normal (0).
Args:
normal_label (int): The integer label for normal predictions. Defaults to 0.
outlier_label (int): The integer label for outlier predictions. Defaults to
1.
**kwargs: Additional keyword arguments to pass to the base
`LabelAggregation` class.
"""
def __init__(self, **kwargs):
def inner(predictions: Iterable[int]) -> int:
return self._outlier_label if all(
map(lambda p: p == self._outlier_label,
predictions)) else self._normal_label
super().__init__(agg_func=inner, **kwargs)
# Or scheme
@specifiable
class AnyVote(LabelAggregation):
"""Aggregates anomaly labels using an "any vote" (OR) scheme.
This `AggregationFn` implements an "any vote" strategy. It aggregates
anomaly labels such that the result is considered an outlier if at least
one of the input `AnomalyPrediction` objects is labeled as an outlier.
Example:
If input labels are [normal, normal, outlier], and outlier_label=1,
then the aggregated label will be outlier (1).
If input labels are [normal, normal, normal], and outlier_label=1,
then the aggregated label will be normal (0).
Args:
normal_label (int): The integer label for normal predictions. Defaults to 0.
outlier_label (int): The integer label for outlier predictions. Defaults to
1.
**kwargs: Additional keyword arguments to pass to the base
`LabelAggregation` class.
"""
def __init__(self, **kwargs):
def inner(predictions: Iterable[int]) -> int:
return self._outlier_label if any(
map(lambda p: p == self._outlier_label,
predictions)) else self._normal_label
super().__init__(agg_func=inner, **kwargs)
@specifiable
class AverageScore(ScoreAggregation):
"""Aggregates anomaly scores by calculating their average.
This `AggregationFn` computes the average of the anomaly scores from a
collection of `AnomalyPrediction` objects.
Args:
**kwargs: Additional keyword arguments to pass to the base
`ScoreAggregation` class.
"""
def __init__(self, **kwargs):
super().__init__(agg_func=statistics.mean, **kwargs)
@specifiable
class MaxScore(ScoreAggregation):
"""Aggregates anomaly scores by selecting the maximum score.
This `AggregationFn` selects the highest anomaly score from a collection
of `AnomalyPrediction` objects as the aggregated score.
Args:
**kwargs: Additional keyword arguments to pass to the base
`ScoreAggregation` class.
"""
def __init__(self, **kwargs):
super().__init__(agg_func=max, **kwargs)