Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extrapolate values in lookup tables for DCM #835

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/dodal/devices/util/lookup_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ async def energy_distance_table(lookup_table_path: str) -> np.ndarray:


def linear_interpolation_lut(filename: str) -> Callable[[float], float]:
"""Returns a callable that converts values by linear interpolation of lookup table values"""
"""Returns a callable that converts values by linear interpolation of lookup table
values.

If the value falls outside the lookup table then the closest value will be used."""
LOGGER.info(f"Using lookup table {filename}")
s_and_t_vals = zip(*loadtxt(filename, comments=["#", "Units"]), strict=False)

Expand All @@ -54,10 +57,6 @@ def linear_interpolation_lut(filename: str) -> Callable[[float], float]:
)

def s_to_t2(s: float) -> float:
if s < s_values[0] or s > s_values[len(s_values) - 1]:
raise ValueError(
f"Lookup table does not support extrapolation from file {filename}, s={s}"
)
return float(interp(s, s_values, t_values))

return s_to_t2
8 changes: 4 additions & 4 deletions tests/devices/unit_tests/util/test_lookup_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ def test_linear_interpolation_reverse_order(s, expected_t):
assert actual_t == expected_t, f"actual {actual_t} != expected {expected_t}"


@mark.parametrize("s", [(1.999,), (5.501,)])
def test_linear_interpolation_rejects_extrapolation(s):
@mark.parametrize("s, expected_t", [(1.0, 1.0), (7.0, 8.0)])
def test_linear_interpolation_extrapolates_returning_the_last_value(s, expected_t):
lut_converter = linear_interpolation_lut(
"tests/test_data/test_beamline_dcm_roll_converter.txt"
)
with pytest.raises(ValueError):
lut_converter(s)
actual_t = lut_converter(s)
assert actual_t == expected_t, f"actual {actual_t} != expected {expected_t}"


def test_linear_interpolation_rejects_non_monotonic_increasing():
Expand Down
Loading