MLRsearch: Support other than just two ratios
[csit.git] / resources / libraries / python / MLRsearch / ReceiveRateInterval.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 defining ReceiveRateInterval class."""
15
16 import math
17
18
19 class ReceiveRateInterval:
20     """Structure defining two Rr measurements, and their relation."""
21
22     def __init__(self, measured_low, measured_high):
23         """Store the bound measurements and call sort.
24
25         :param measured_low: Measurement for the lower bound.
26         :param measured_high: Measurement for the upper bound.
27         :type measured_low: ReceiveRateMeasurement.ReceiveRateMeasurement
28         :type measured_high: ReceiveRateMeasurement.ReceiveRateMeasurement
29         """
30         self.measured_low = measured_low
31         self.measured_high = measured_high
32         # Declare secondary quantities to appease pylint.
33         self.abs_tr_width = None
34         """Absolute width of target transmit rate. Upper minus lower."""
35         self.rel_tr_width = None
36         """Relative width of target transmit rate. Absolute divided by upper."""
37         self.sort()
38
39     def sort(self):
40         """Sort bounds by target Tr, compute secondary quantities."""
41         if self.measured_low.target_tr > self.measured_high.target_tr:
42             self.measured_low, self.measured_high = (
43                 self.measured_high, self.measured_low
44             )
45         self.abs_tr_width = (
46             self.measured_high.target_tr - self.measured_low.target_tr
47         )
48         self.rel_tr_width = self.abs_tr_width / self.measured_high.target_tr
49
50     def __str__(self):
51         """Return string as half-open interval."""
52         return f"[{self.measured_low!s};{self.measured_high!s})"
53
54     def __repr__(self):
55         """Return string evaluable as a constructor call."""
56         return f"ReceiveRateInterval(measured_low={self.measured_low!r}," \
57             f"measured_high={self.measured_high!r})"
58
59     def width_in_goals(self, relative_width_goal):
60         """Return float value.
61
62         Relative width goal is some (negative) value on logarithmic scale.
63         Current relative width is another logarithmic value.
64         Return the latter divided by the former.
65         This is useful when investigating how did surprising widths come to be.
66
67         :param relative_width_goal: Upper bound times this is the goal
68             difference between upper bound and lower bound.
69         :type relative_width_goal: float
70         :returns: Current width as logarithmic multiple of goal width [1].
71         :rtype: float
72         """
73         return math.log(1.0 - self.rel_tr_width) / math.log(
74             1.0 - relative_width_goal)