CSIT-PERF: New trigger design
[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 from BitCountingGroup import BitCountingGroup
15 from BitCountingGroupList import BitCountingGroupList
16 from BitCountingMetadataFactory import BitCountingMetadataFactory
17 from ClassifiedMetadataFactory import ClassifiedMetadataFactory
18
19
20 class BitCountingClassifier(object):
21
22     @staticmethod
23     def classify(values):
24         """Return the values in groups of optimal bit count.
25
26         TODO: Could we return BitCountingGroupList and let caller process it?
27
28         :param values: Sequence of runs to classify.
29         :type values: Iterable of float or of AvgStdevMetadata
30         :returns: Classified group list.
31         :rtype: list of BitCountingGroup
32         """
33         max_value = BitCountingMetadataFactory.find_max_value(values)
34         factory = BitCountingMetadataFactory(max_value)
35         opened_at = []
36         closed_before = [BitCountingGroupList()]
37         for index, value in enumerate(values):
38             singleton = BitCountingGroup(factory, [value])
39             newly_opened = closed_before[index].with_group_appended(singleton)
40             opened_at.append(newly_opened)
41             record_group_list = newly_opened
42             for previous in range(index):
43                 previous_opened_list = opened_at[previous]
44                 still_opened = (
45                     previous_opened_list.with_value_added_to_last_group(value))
46                 opened_at[previous] = still_opened
47                 if still_opened.bits < record_group_list.bits:
48                     record_group_list = still_opened
49             closed_before.append(record_group_list)
50         partition = closed_before[-1]
51         previous_average = partition[0].metadata.avg
52         for group in partition:
53             if group.metadata.avg == previous_average:
54                 group.metadata = ClassifiedMetadataFactory.with_classification(
55                     group.metadata, "normal")
56             elif group.metadata.avg < previous_average:
57                 group.metadata = ClassifiedMetadataFactory.with_classification(
58                     group.metadata, "regression")
59             elif group.metadata.avg > previous_average:
60                 group.metadata = ClassifiedMetadataFactory.with_classification(
61                     group.metadata, "progression")
62             previous_average = group.metadata.avg
63         return partition.group_list