1 # Copyright (c) 2019 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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """Module defining ReceiveRateInterval class."""
18 from .ReceiveRateMeasurement import ReceiveRateMeasurement
21 class ReceiveRateInterval:
22 """Structure defining two Rr measurements, and their relation."""
24 def __init__(self, measured_low, measured_high):
25 """Store the bound measurements after checking argument types.
27 :param measured_low: Measurement for the lower bound.
28 :param measured_high: Measurement for the upper bound.
29 :type measured_low: ReceiveRateMeasurement.ReceiveRateMeasurement
30 :type measured_high: ReceiveRateMeasurement.ReceiveRateMeasurement
32 # TODO: Type checking is not very pythonic,
33 # perhaps users can fix wrong usage without it?
34 if not isinstance(measured_low, ReceiveRateMeasurement):
36 f"measured_low is not a ReceiveRateMeasurement: "
39 if not isinstance(measured_high, ReceiveRateMeasurement):
41 f"measured_high is not a ReceiveRateMeasurement: "
44 self.measured_low = measured_low
45 self.measured_high = measured_high
46 # Declare secondary quantities to appease pylint.
47 self.abs_tr_width = None
48 """Absolute width of target transmit rate. Upper minus lower."""
49 self.rel_tr_width = None
50 """Relative width of target transmit rate. Absolute divided by upper."""
54 """Sort bounds by target Tr, compute secondary quantities."""
55 if self.measured_low.target_tr > self.measured_high.target_tr:
56 self.measured_low, self.measured_high = (
57 self.measured_high, self.measured_low
60 self.measured_high.target_tr - self.measured_low.target_tr
62 self.rel_tr_width = self.abs_tr_width / self.measured_high.target_tr
64 def width_in_goals(self, relative_width_goal):
65 """Return float value.
67 Relative width goal is some (negative) value on logarithmic scale.
68 Current relative width is another logarithmic value.
69 Return the latter divided by the former.
70 This is useful when investigating how did surprising widths come to be.
72 :param relative_width_goal: Upper bound times this is the goal
73 difference between upper bound and lower bound.
74 :type relative_width_goal: float
75 :returns: Current width as logarithmic multiple of goal width [1].
78 return math.log(1.0 - self.rel_tr_width) / math.log(
79 1.0 - relative_width_goal)
82 """Return string as half-open interval."""
83 return f"[{self.measured_low!s};{self.measured_high!s})"
86 """Return string evaluable as a constructor call."""
87 return f"ReceiveRateInterval(measured_low={self.measured_low!r}," \
88 f"measured_high={self.measured_high!r})"