CSIT-1110: Integrate anomaly detection into PAL
[csit.git] / resources / tools / presentation / new / jumpavg / BitCountingMetadataFactory.py
1 # Copyright (c) 2018 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 import math
15
16 from AvgStdevMetadata import AvgStdevMetadata
17 from AvgStdevMetadataFactory import AvgStdevMetadataFactory
18 from BitCountingMetadata import BitCountingMetadata
19
20
21 class BitCountingMetadataFactory(object):
22     """Class for factory which creates bit counting metadata from data."""
23
24     @staticmethod
25     def find_max_value(values):
26         """Return the max value.
27
28         This is a separate helper method,
29         because the whole set of values is usually larger than in from_data().
30
31         :param values: Run values to be processed.
32         :type values: Iterable of float
33         :returns: 0.0 or the biggest value found.
34         :rtype: float
35         """
36         max_value = 0.0
37         for value in values:
38             if isinstance(value, AvgStdevMetadata):
39                 value = value.avg
40             if value > max_value:
41                 max_value = value
42         return max_value
43
44     def __init__(self, max_value, prev_avg=None):
45         """Construct the factory instance with given arguments.
46
47         :param max_value: Maximal expected value.
48         :param prev_avg: Population average of the previous group.
49             If None, no previous average is taken into account.
50             If not None, the given previous average is used to discourage
51             consecutive groups with similar averages
52             (opposite triangle distribution is assumed).
53         :type max_value: float
54         :type prev_avg: float or None
55         """
56         self.max_value = max_value
57         self.prev_avg = prev_avg
58
59     def from_avg_stdev_metadata(self, metadata):
60         """Return new metadata object by adding bits to existing metadata.
61
62         :param metadata: Metadata to count bits for.
63         :type metadata: AvgStdevMetadata
64         :returns: The metadata with bits counted.
65         :rtype: BitCountingMetadata
66         """
67         return BitCountingMetadata(
68             max_value=self.max_value, size=metadata.size,
69             avg=metadata.avg, stdev=metadata.stdev, prev_avg=self.prev_avg)
70
71     def from_data(self, values):
72         """Return new metadata object fitting the values.
73
74         :param values: Run values to be processed.
75         :type values: Iterable of float or of AvgStdevMetadata
76         :returns: The metadata matching the values.
77         :rtype: BitCountingMetadata
78         """
79         metadata = AvgStdevMetadataFactory.from_data(values)
80         return self.from_avg_stdev_metadata(metadata)