feat(jumpavg): support small values via unit param
[csit.git] / resources / libraries / python / jumpavg / classify.py
1 # Copyright (c) 2023 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 is 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 typing import Iterable, Optional, Union
24
25 from .avg_stdev_stats import AvgStdevStats
26 from .bit_counting_group_list import BitCountingGroupList
27
28
29 def classify(
30     values: Iterable[Union[float, Iterable[float]]],
31     unit: Optional[float] = None,
32     sbps: Optional[float] = None,
33 ) -> BitCountingGroupList:
34     """Return the values in groups of optimal bit count.
35
36     Here, a value is either a float, or an iterable of floats.
37     Such iterables represent an undivisible sequence of floats.
38     Int is accepted anywhere instead of float.
39
40     Internally, such sequence is replaced by AvgStdevStats
41     after maximal value is found.
42
43     If the values are smaller than expected (below one unit),
44     the underlying assumption break down and the classification is wrong.
45     Use the "unit" parameter to hint at what the input resolution is.
46
47     If the correct value of unit is not known beforehand,
48     the argument "sbps" (Significant Bits Per Sample) can be used
49     to set unit such that maximal sample value is this many ones in binary.
50     If neither "unit" nor "sbps" are given, "sbps" of 12 is used by default.
51
52     :param values: Sequence of runs to classify.
53     :param unit: Typical resolution of the values.
54         Zero and None means no unit given.
55     :param sbps: Significant Bits Per Sample. None on zero means 12.
56         If units is not set, this is used to compute unit from max sample value.
57     :type values: Iterable[Union[float, Iterable[float]]]
58     :type unit: Optional[float]
59     :type sbps: Optional[float]
60     :returns: Classified group list.
61     :rtype: BitCountingGroupList
62     """
63     processed_values = []
64     max_value = 0.0
65     for value in values:
66         if isinstance(value, (float, int)):
67             if value > max_value:
68                 max_value = value
69             processed_values.append(value)
70         else:
71             for subvalue in value:
72                 if subvalue > max_value:
73                     max_value = subvalue
74             processed_values.append(AvgStdevStats.for_runs(value))
75     if not unit:
76         if not sbps:
77             sbps = 12.0
78         max_in_units = pow(2.0, sbps + 1.0) - 1.0
79         unit = max_value / max_in_units
80     # Glist means group list (BitCountingGroupList).
81     open_glists = []
82     record_glist = BitCountingGroupList(max_value=max_value, unit=unit)
83     for value in processed_values:
84         new_open_glist = record_glist.copy_fast().append_group_of_runs([value])
85         record_glist = new_open_glist
86         for old_open_glist in open_glists:
87             old_open_glist.append_run_to_to_last_group(value)
88             if old_open_glist.bits < record_glist.bits:
89                 record_glist = old_open_glist
90         open_glists.append(new_open_glist)
91     previous_average = record_glist[0].stats.avg
92     for group in record_glist:
93         if group.stats.avg < previous_average:
94             group.comment = "regression"
95         elif group.stats.avg > previous_average:
96             group.comment = "progression"
97         previous_average = group.stats.avg
98     return record_glist