|
| 1 | +import operator |
| 2 | +from pint import Quantity |
| 3 | +from decimal import Decimal, ROUND_HALF_UP |
| 4 | + |
| 5 | + |
1 | 6 | """
|
2 | 7 | Global tolerance for equality comparison. Default allows 0.5% variations of generated baseline/proposed from the standard specify value.
|
3 | 8 | """
|
@@ -29,3 +34,85 @@ def std_equal(
|
29 | 34 | True iff the val is within percent_tolerance of std_val
|
30 | 35 | """
|
31 | 36 | return abs(std_val - val) <= (percent_tolerance / 100) * abs(std_val)
|
| 37 | + |
| 38 | + |
| 39 | +def std_equal_with_precision( |
| 40 | + val: Quantity | float | int, |
| 41 | + std_val: Quantity | float | int, |
| 42 | + precision: Quantity | float | int, |
| 43 | +) -> bool: |
| 44 | + """Determines whether the model value and standard value are equal with the specified precision. If any of the function inputs are a Quantity type, then all function inputs must be Quantity. |
| 45 | +
|
| 46 | + Parameters |
| 47 | + ---------- |
| 48 | + val: Quantity | float | int |
| 49 | + value extracted from model |
| 50 | + std_val : Quantity | float | int |
| 51 | + standard value from code |
| 52 | + precision: Quantity | float | int |
| 53 | + number of decimal places or significant value to round to, and intended units of the comparison |
| 54 | +
|
| 55 | + Returns |
| 56 | + ------- |
| 57 | + bool |
| 58 | + True if the modeled value is equal to the standard value within the specified precision |
| 59 | + """ |
| 60 | + # Check if all or none of the arguments are Quantity types |
| 61 | + are_quantities = [isinstance(arg, Quantity) for arg in [val, std_val, precision]] |
| 62 | + if not (all(are_quantities) or not any(are_quantities)): |
| 63 | + raise TypeError( |
| 64 | + "Arguments must be consistent in type: all Quantity or all non-Quantity." |
| 65 | + ) |
| 66 | + |
| 67 | + # Determine if the values are pint Quantities and handle accordingly |
| 68 | + if ( |
| 69 | + isinstance(val, Quantity) |
| 70 | + and isinstance(std_val, Quantity) |
| 71 | + and isinstance(precision, Quantity) |
| 72 | + ): |
| 73 | + units = precision.units |
| 74 | + val = val.to(units) |
| 75 | + std_val = std_val.to(units) |
| 76 | + val_magnitude = Decimal(str(val.magnitude)) |
| 77 | + std_val_magnitude = Decimal(str(std_val.magnitude)) |
| 78 | + precision_magnitude = Decimal(str(precision.magnitude)) |
| 79 | + else: |
| 80 | + val_magnitude = Decimal(str(val)) |
| 81 | + std_val_magnitude = Decimal(str(std_val)) |
| 82 | + precision_magnitude = Decimal(str(precision)) |
| 83 | + |
| 84 | + # Determine rounding precision based on whether precision is a whole number or a decimal |
| 85 | + if precision_magnitude.as_tuple().exponent < 0: |
| 86 | + # Decimal places (e.g., 0.01) |
| 87 | + precision_decimal_places = abs(precision_magnitude.as_tuple().exponent) |
| 88 | + rounding_precision = f"1E-{str(precision_decimal_places)}" |
| 89 | + else: |
| 90 | + # Whole number (e.g., 10, 100) |
| 91 | + rounding_precision = f"1E+{str(int(precision_magnitude.log10()))}" |
| 92 | + |
| 93 | + # Round both values to the specified precision |
| 94 | + val_rounded = val_magnitude.quantize( |
| 95 | + Decimal(rounding_precision), rounding=ROUND_HALF_UP |
| 96 | + ) |
| 97 | + std_val_rounded = std_val_magnitude.quantize( |
| 98 | + Decimal(rounding_precision), rounding=ROUND_HALF_UP |
| 99 | + ) |
| 100 | + |
| 101 | + # Compare the rounded values |
| 102 | + return val_rounded == std_val_rounded |
| 103 | + |
| 104 | + |
| 105 | +def std_conservative_outcome( |
| 106 | + val: Quantity, std_val: Quantity, conservative_operator_wrt_std: operator |
| 107 | +): |
| 108 | + """Determines if the model value has a conservative outcome compared to the standard value. |
| 109 | +
|
| 110 | + Parameters |
| 111 | + ---------- |
| 112 | + val: Quantity |
| 113 | + value extracted from model |
| 114 | + std_val : Quantity |
| 115 | + standard value from code |
| 116 | + conservative_operator_wrt_std: operator that results in a conservative outcome compared to the standard value |
| 117 | + """ |
| 118 | + return conservative_operator_wrt_std(val, std_val) |
0 commit comments