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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """Module holding BitCountingMetadataFactory class."""
18 from AvgStdevMetadata import AvgStdevMetadata
19 from AvgStdevMetadataFactory import AvgStdevMetadataFactory
20 from BitCountingMetadata import BitCountingMetadata
23 class BitCountingMetadataFactory(object):
24 """Class for factory which creates bit counting metadata from data.
26 TODO: Summarize the methods?
30 def find_max_value(values):
31 """Return the max value.
33 This is a separate helper method,
34 because the whole set of values is usually larger than in from_data().
36 :param values: Run values to be processed.
37 :type values: Iterable of float
38 :returns: 0.0 or the biggest value found.
43 if isinstance(value, AvgStdevMetadata):
49 def __init__(self, max_value, prev_avg=None):
50 """Construct the factory instance with given arguments.
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
61 self.max_value = max_value
62 self.prev_avg = prev_avg
64 def from_avg_stdev_metadata(self, metadata):
65 """Return new metadata object by adding bits to existing metadata.
67 :param metadata: Metadata to count bits for.
68 :type metadata: AvgStdevMetadata
69 :returns: The metadata with bits counted.
70 :rtype: BitCountingMetadata
72 return BitCountingMetadata(
73 max_value=self.max_value, size=metadata.size,
74 avg=metadata.avg, stdev=metadata.stdev, prev_avg=self.prev_avg)
76 def from_data(self, values):
77 """Return new metadata object fitting the values.
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
84 metadata = AvgStdevMetadataFactory.from_data(values)
85 return self.from_avg_stdev_metadata(metadata)