forked from ManageIQ/integration_tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.py
45 lines (36 loc) · 1.92 KB
/
stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def tol_check(ref, compare, min_error=0.05, low_val_correction=3.0):
"""Tolerance check
The tolerance check is very simple. In essence it checks to ensure
that the ``compare`` value is within ``min_error`` percentage of the ``ref`` value.
However there are special conditions.
If the ref value is zero == the compare value we will alwys return True to avoid
calculation overhead.
If the ref value is zero we check if the compare value is below the low_val_correction
threshold.
The low value correction is also used if ref is small. In this case, if one minus the
difference of the ref and low value correction / reference value yields greater error
correction, then this is used.
For example, if the reference was 1 and the compare was 2, with a min_error set to the
default, the tolerance check would return False. At low values this is probably undesirable
and so, the low_val_correction allows for a greater amount of error at low values.
As an example, with the lvc set to 3, the allowe error would be much higher, allowing the
tolerance check to pass.
The lvc will only take effect if the error it produces is greater than the ``min_error``.
Args:
ref: The reference value
compare: The comparison value
min_error: The minimum allowed error
low_val_correction: A correction value for lower values
"""
if ref == compare:
return True, min_error
elif ref == 0:
return compare <= low_val_correction, low_val_correction
else:
compared_value = float(compare)
reference_value = float(ref)
relational_error = 1.0 - ((reference_value - low_val_correction) / reference_value)
tolerance = max([relational_error, min_error])
difference = abs(reference_value - compared_value)
difference_error = difference / reference_value
return difference_error <= tolerance, tolerance