CSIT-1110: Add files for PyPI migration
[csit.git] / resources / tools / presentation / new / jumpavg / BitCountingClassifier.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 BitCountingClassifier class.
15
16 This is the main class to be used by callers."""
17
18 from AbstractGroupClassifier import AbstractGroupClassifier
19 from BitCountingGroup import BitCountingGroup
20 from BitCountingGroupList import BitCountingGroupList
21 from BitCountingMetadataFactory import BitCountingMetadataFactory
22 from ClassifiedMetadataFactory import ClassifiedMetadataFactory
23
24
25 class BitCountingClassifier(AbstractGroupClassifier):
26     """Classifier using Minimal Description Length principle."""
27
28     def classify(self, values):
29         """Return the values in groups of optimal bit count.
30
31         The current implementation could be a static method,
32         but we might support options in later versions,
33         for example for chosing encodings.
34
35         :param values: Sequence of runs to classify.
36         :type values: Iterable of float or of AvgStdevMetadata
37         :returns: Classified group list.
38         :rtype: BitCountingGroupList
39         """
40         max_value = BitCountingMetadataFactory.find_max_value(values)
41         factory = BitCountingMetadataFactory(max_value)
42         opened_at = []
43         closed_before = [BitCountingGroupList()]
44         for index, value in enumerate(values):
45             singleton = BitCountingGroup(factory, [value])
46             newly_opened = closed_before[index].with_group_appended(singleton)
47             opened_at.append(newly_opened)
48             record_group_list = newly_opened
49             for previous in range(index):
50                 previous_opened_list = opened_at[previous]
51                 still_opened = (
52                     previous_opened_list.with_value_added_to_last_group(value))
53                 opened_at[previous] = still_opened
54                 if still_opened.bits < record_group_list.bits:
55                     record_group_list = still_opened
56             closed_before.append(record_group_list)
57         partition = closed_before[-1]
58         previous_average = partition[0].metadata.avg
59         for group in partition:
60             if group.metadata.avg == previous_average:
61                 group.metadata = ClassifiedMetadataFactory.with_classification(
62                     group.metadata, "normal")
63             elif group.metadata.avg < previous_average:
64                 group.metadata = ClassifiedMetadataFactory.with_classification(
65                     group.metadata, "regression")
66             elif group.metadata.avg > previous_average:
67                 group.metadata = ClassifiedMetadataFactory.with_classification(
68                     group.metadata, "progression")
69             previous_average = group.metadata.avg
70         return partition