Skip to content

Commit

Permalink
Extrapolate values in lookup tables for DCM (#835)
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicOram authored Oct 17, 2024
1 parent 73cbfc9 commit 9e12af0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
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

0 comments on commit 9e12af0

Please sign in to comment.