Skip to content

Commit

Permalink
improve filter_by_parameter logging and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanielloNTIA committed Mar 25, 2024
1 parent 6087a8b commit 73fab88
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
12 changes: 9 additions & 3 deletions scos_actions/calibration/tests/test_sensor_calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,17 @@ def test_get_calibration_dict_within_range(self):
clock_rate_lookup_by_sample_rate=[],
sensor_uid="TESTING",
)

lookup_fail_value = 250.0
with pytest.raises(CalibrationException) as e_info:
_ = cal.get_calibration_dict({"sample_rate": 100.0, "frequency": 250.0})
_ = cal.get_calibration_dict(
{"sample_rate": 100.0, "frequency": lookup_fail_value}
)
assert e_info.value.args[0] == (
f"Could not locate calibration data at 250.0"
+ f"\nAttempted lookup using key '250.0'"
f"Could not locate calibration data at {lookup_fail_value}"
+ "\nAttempted lookup using keys: "
+ f"\n\tstr({lookup_fail_value}).lower() = {str(lookup_fail_value).lower()}"
+ f"\n\tstr(int({lookup_fail_value})) = {int(lookup_fail_value)}"
+ f"\nUsing calibration data: {cal.calibration_data['100.0']}"
)

Expand Down
22 changes: 16 additions & 6 deletions scos_actions/calibration/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
class TestCalibrationUtils:
def test_filter_by_parameter_out_of_range(self):
calibrations = {200.0: {"some_cal_data"}, 300.0: {"more cal data"}}

# Also checks error output when missing value is an integer
test_value = 400
with pytest.raises(CalibrationException) as e_info:
_ = filter_by_parameter(calibrations, 400.0)
_ = filter_by_parameter(calibrations, test_value)
assert (
e_info.value.args[0]
== f"Could not locate calibration data at 400.0"
+ f"\nAttempted lookup using key '400.0' and 400.0"
== f"Could not locate calibration data at {test_value}"
+ "\nAttempted lookup using keys: "
+ f"\n\tstr({test_value}).lower() = {str(test_value).lower()}"
+ f"\n\tstr(float({test_value})) = {float(test_value)}"
+ f"\nUsing calibration data: {calibrations}"
)

Expand All @@ -20,11 +25,16 @@ def test_filter_by_parameter_in_range_requires_match(self):
200.0: {"Gain": "Gain at 200.0"},
300.0: {"Gain": "Gain at 300.0"},
}

# Check looking up a missing value with a float
test_value = 150.0
with pytest.raises(CalibrationException) as e_info:
_ = filter_by_parameter(calibrations, 150.0)
_ = filter_by_parameter(calibrations, test_value)
assert e_info.value.args[0] == (
f"Could not locate calibration data at 150.0"
+ f"\nAttempted lookup using key '150.0' and 150.0"
f"Could not locate calibration data at {test_value}"
+ "\nAttempted lookup using keys: "
+ f"\n\tstr({test_value}).lower() = {str(test_value).lower()}"
+ f"\n\tstr(int({test_value})) = {int(test_value)}"
+ f"\nUsing calibration data: {calibrations}"
)

Expand Down
7 changes: 4 additions & 3 deletions scos_actions/calibration/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ def filter_by_parameter(calibrations: dict, value: Union[float, int, bool]) -> d
"""
try:
filtered_data = calibrations.get(str(value).lower(), None)
attempts = f"\n\tstr({value}).lower() = {str(value).lower()}"
if filtered_data is None and isinstance(value, int):
# Try equivalent float for ints, i.e., match "1.0" to 1
filtered_data = calibrations.get(str(float(value)), None)
attempts += f"\n\tstr(float({value})) = {str(float(value))}"
if filtered_data is None and isinstance(value, float) and value.is_integer():
# Check for, e.g., key '25' if value is '25.0'
filtered_data = calibrations.get(str(int(value)), None)
attempts += f"\n\tstr(int({value})) = {str(int(value))}"
if filtered_data is None:
raise KeyError
else:
Expand All @@ -70,9 +73,7 @@ def filter_by_parameter(calibrations: dict, value: Union[float, int, bool]) -> d
except KeyError:
msg = (
f"Could not locate calibration data at {value}"
+ f"\nAttempted lookup using key '{str(value).lower()}'"
+ f"{f' and {float(value)}' if isinstance(value, int) else ''}"
+ f"{f' and {int(value)}' if isinstance(value, float) and value.is_integer() else ''}"
+ f"\nAttempted lookup using keys: {attempts}"
+ f"\nUsing calibration data: {calibrations}"
)
raise CalibrationEntryMissingException(msg)

0 comments on commit 73fab88

Please sign in to comment.