80df0ef33359790e07031f1961d1131f08098566
[csit.git] / resources / libraries / python / MLRsearch / NdrPdrResult.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 """Module defining NdrPdrResult class."""
15
16 from ReceiveRateInterval import ReceiveRateInterval
17
18
19 class NdrPdrResult(object):
20     """Two measurement intervals, return value of search algorithms.
21
22     Partial fraction is NOT part of the result. Pdr interval should be valid
23     for all partial fractions implied by the interval."""
24
25     def __init__(self, ndr_interval, pdr_interval):
26         """Store the measured intervals after checking argument types.
27
28         :param ndr_interval: Object containing data for NDR part of the result.
29         :param pdr_interval: Object containing data for PDR part of the result.
30         :type ndr_interval: ReceiveRateInterval.ReceiveRateInterval
31         :type pdr_interval: ReceiveRateInterval.ReceiveRateInterval
32         """
33         # TODO: Type checking is not very pythonic,
34         # perhaps users can fix wrong usage without it?
35         if not isinstance(ndr_interval, ReceiveRateInterval):
36             raise TypeError("ndr_interval, is not a ReceiveRateInterval: "
37                             "{ndr!r}".format(ndr=ndr_interval))
38         if not isinstance(pdr_interval, ReceiveRateInterval):
39             raise TypeError("pdr_interval, is not a ReceiveRateInterval: "
40                             "{pdr!r}".format(pdr=pdr_interval))
41         self.ndr_interval = ndr_interval
42         self.pdr_interval = pdr_interval
43
44     def width_in_goals(self, relative_width_goal):
45         """Return a debug string related to current widths in logarithmic scale.
46
47         :param relative_width_goal: Upper bound times this is the goal
48             difference between upper bound and lower bound.
49         :type relative_width_goal: float
50         :returns: Message containing NDR and PDR widths in goals.
51         :rtype: str
52         """
53         return "ndr {ndr_in_goals}; pdr {pdr_in_goals}".format(
54             ndr_in_goals=self.ndr_interval.width_in_goals(relative_width_goal),
55             pdr_in_goals=self.pdr_interval.width_in_goals(relative_width_goal))
56
57     def __str__(self):
58         """Return string as tuple of named values."""
59         return "NDR={ndr!s};PDR={pdr!s}".format(
60             ndr=self.ndr_interval, pdr=self.pdr_interval)
61
62     def __repr__(self):
63         """Return string evaluable as a constructor call."""
64         return "NdrPdrResult(ndr_interval={ndr!r},pdr_interval={pdr!r})".format(
65             ndr=self.ndr_interval, pdr=self.pdr_interval)