252c71e8d5909c28e7764b7d496687d79073eeac
[csit.git] / resources / libraries / python / jumpavg / classify.py
1 # Copyright (c) 2021 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 the classify function
15
16 Classification os one of primary purposes of this package.
17
18 Minimal message length principle is used
19 for grouping results into the list of groups,
20 assuming each group is a population of different Gaussian distribution.
21 """
22
23 from .AvgStdevStats import AvgStdevStats
24 from .BitCountingGroupList import BitCountingGroupList
25
26
27 def classify(values):
28     """Return the values in groups of optimal bit count.
29
30     Here, a value is either a float, or an iterable of floats.
31     Such iterables represent an undivisible sequence of floats.
32
33     Internally, such sequence is replaced by AvgStdevStats
34     after maximal value is found.
35
36     :param values: Sequence of runs to classify.
37     :type values: Iterable[Union[float, Iterable[float]]]
38     :returns: Classified group list.
39     :rtype: BitCountingGroupList
40     """
41     processed_values = list()
42     max_value = 0.0
43     for value in values:
44         if isinstance(value, (float, int)):
45             if value > max_value:
46                 max_value = value
47             processed_values.append(value)
48         else:
49             for subvalue in value:
50                 if subvalue > max_value:
51                     max_value = subvalue
52             processed_values.append(AvgStdevStats.for_runs(value))
53     open_at = list()
54     closed_before = [BitCountingGroupList(max_value=max_value)]
55     for index, value in enumerate(processed_values):
56         newly_open = closed_before[index].copy()
57         newly_open.append_group_of_runs([value])
58         open_at.append(newly_open)
59         record_group_list = newly_open
60         for previous_index, old_open in enumerate(open_at[:index]):
61             new_open = old_open.copy().append_run_to_to_last_group(value)
62             open_at[previous_index] = new_open
63             if new_open.bits < record_group_list.bits:
64                 record_group_list = new_open
65         closed_before.append(record_group_list)
66     partition = closed_before[-1]
67     previous_average = partition[0].stats.avg
68     for group in partition:
69         if group.stats.avg == previous_average:
70             group.comment = u"normal"
71         elif group.stats.avg < previous_average:
72             group.comment = u"regression"
73         elif group.stats.avg > previous_average:
74             group.comment = u"progression"
75         previous_average = group.stats.avg
76     return partition