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.
16 from AvgStdevMetadata import AvgStdevMetadata
17 from AvgStdevMetadataFactory import AvgStdevMetadataFactory
18 from BitCountingMetadata import BitCountingMetadata
21 class BitCountingMetadataFactory(object):
22 """Class for factory which creates bit counting metadata from data."""
25 def find_max_value(values):
26 """Return the max value.
28 This is a separate helper method,
29 because the whole set of values is usually larger than in from_data().
31 :param values: Run values to be processed.
32 :type values: Iterable of float
33 :returns: 0.0 or the biggest value found.
38 if isinstance(value, AvgStdevMetadata):
44 def __init__(self, max_value, prev_avg=None):
45 """Construct the factory instance with given arguments.
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
56 self.max_value = max_value
57 self.prev_avg = prev_avg
59 def from_avg_stdev_metadata(self, metadata):
60 """Return new metadata object by adding bits to existing metadata.
62 :param metadata: Metadata to count bits for.
63 :type metadata: AvgStdevMetadata
64 :returns: The metadata with bits counted.
65 :rtype: BitCountingMetadata
67 return BitCountingMetadata(
68 max_value=self.max_value, size=metadata.size,
69 avg=metadata.avg, stdev=metadata.stdev, prev_avg=self.prev_avg)
71 def from_data(self, values):
72 """Return new metadata object fitting the values.
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
79 metadata = AvgStdevMetadataFactory.from_data(values)
80 return self.from_avg_stdev_metadata(metadata)