CSIT-1110: Add files for PyPI migration
[csit.git] / PyPI / jumpavg / 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 """Module holding BitCountingMetadataFactory class."""
15
16 import math
17
18 from AvgStdevMetadata import AvgStdevMetadata
19 from AvgStdevMetadataFactory import AvgStdevMetadataFactory
20 from BitCountingMetadata import BitCountingMetadata
21
22
23 class BitCountingMetadataFactory(object):
24     """Class for factory which creates bit counting metadata from data.
25
26     TODO: Summarize the methods?
27     """
28
29     @staticmethod
30     def find_max_value(values):
31         """Return the max value.
32
33         This is a separate helper method,
34         because the whole set of values is usually larger than in from_data().
35
36         :param values: Run values to be processed.
37         :type values: Iterable of float
38         :returns: 0.0 or the biggest value found.
39         :rtype: float
40         """
41         max_value = 0.0
42         for value in values:
43             if isinstance(value, AvgStdevMetadata):
44                 value = value.avg
45             if value > max_value:
46                 max_value = value
47         return max_value
48
49     def __init__(self, max_value, prev_avg=None):
50         """Construct the factory instance with given arguments.
51
52         :param max_value: Maximal expected value.
53         :param prev_avg: Population average of the previous group.
54             If None, no previous average is taken into account.
55             If not None, the given previous average is used to discourage
56             consecutive groups with similar averages
57             (opposite triangle distribution is assumed).
58         :type max_value: float
59         :type prev_avg: float or None
60         """
61         self.max_value = max_value
62         self.prev_avg = prev_avg
63
64     def from_avg_stdev_metadata(self, metadata):
65         """Return new metadata object by adding bits to existing metadata.
66
67         :param metadata: Metadata to count bits for.
68         :type metadata: AvgStdevMetadata
69         :returns: The metadata with bits counted.
70         :rtype: BitCountingMetadata
71         """
72         return BitCountingMetadata(
73             max_value=self.max_value, size=metadata.size,
74             avg=metadata.avg, stdev=metadata.stdev, prev_avg=self.prev_avg)
75
76     def from_data(self, values):
77         """Return new metadata object fitting the values.
78
79         :param values: Run values to be processed.
80         :type values: Iterable of float or of AvgStdevMetadata
81         :returns: The metadata matching the values.
82         :rtype: BitCountingMetadata
83         """
84         metadata = AvgStdevMetadataFactory.from_data(values)
85         return self.from_avg_stdev_metadata(metadata)