Skip to content

Commit ccfd83c

Browse files
committed
Change converted LUT format
1 parent c32c59f commit ccfd83c

File tree

3 files changed

+25
-40
lines changed

3 files changed

+25
-40
lines changed

src/daq_config_server/converters/_converter_utils.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,27 @@ def parse_value(value: str, convert_to: type | None = None) -> Any:
2626

2727
def parse_lut_to_dict(
2828
contents: str,
29-
*params: tuple[str, str, type | None],
30-
) -> dict[str, dict[str, str] | list[dict[str, Any]]]:
31-
data: list[dict[str, Any]] = []
29+
*params: tuple[str, type | None],
30+
) -> dict[str, list[str] | list[list[Any]]]:
31+
"""Converts a lookup table to a dict, containing the names of each column and
32+
the rows as a 2D list.
33+
34+
Any args after the contents should be a tuple of (column name, type | None).
35+
If a type is provided, the values in that column will be converted to that type.
36+
Otherwise, the type will be inferred. Please include units in the column name.
37+
"""
38+
data: list[list[Any]] = []
39+
column_names = [param[0] for param in params]
40+
types = [param[1] for param in params]
3241
data_dict = {
33-
"units": {key: unit for param in params for key, unit in [param[:2]]},
42+
"column_names": column_names,
3443
"data": data,
3544
}
3645
for line in remove_comments(contents.splitlines()):
3746
if line.startswith("Units"):
3847
continue
3948
data.append(
40-
{
41-
params[i][0]: parse_value(value, params[i][2])
42-
for i, value in enumerate(line.split())
43-
}
49+
[parse_value(value, types[i]) for i, value in enumerate(line.split())]
4450
)
4551
data_dict["data"] = data
4652
return data_dict

src/daq_config_server/converters/_converters.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,21 @@ def xml_to_dict(contents: str) -> dict[str, Any]:
8585
def detector_xy_lut_to_dict(contents: str):
8686
return parse_lut_to_dict(
8787
contents,
88-
("detector_distances", "mm", float),
89-
("beam_centre_x", "mm", float),
90-
("beam_centre_y", "mm", float),
88+
("detector_distances_mm", float),
89+
("beam_centre_x_mm", float),
90+
("beam_centre_y_mm", float),
9191
)
9292

9393

9494
def beamline_pitch_lut_to_dict(contents: str):
9595
return parse_lut_to_dict(
96-
contents, ("bragg_angle", "degrees", float), ("pitch", "mrad", float)
96+
contents, ("bragg_angle_deg", float), ("pitch_mrad", float)
9797
)
9898

9999

100100
def beamline_roll_lut_to_dict(contents: str):
101-
return parse_lut_to_dict(
102-
contents, ("bragg_angle", "degrees", float), ("roll", "mrad", float)
103-
)
101+
return parse_lut_to_dict(contents, ("bragg_angle_deg", float), ("roll_mrad", float))
104102

105103

106104
def undulator_energy_gap_lut_to_dict(contents: str):
107-
return parse_lut_to_dict(contents, ("energy", "eV", int), ("gap", "mm", float))
105+
return parse_lut_to_dict(contents, ("energy_eV", int), ("gap_mm", float))

tests/unit_tests/test_converters.py

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,11 @@ def test_parse_lut_to_dict_gives_expected_result_and_can_be_jsonified():
4343
with open(TestDataPaths.TEST_GOOD_LUT_PATH) as f:
4444
contents = f.read()
4545
expected = {
46-
"units": {
47-
"energy": "eV",
48-
"gap": "mm",
49-
},
50-
"data": [
51-
{
52-
"energy": 5700,
53-
"gap": 5.4606,
54-
},
55-
{
56-
"energy": 5760,
57-
"gap": 5.5,
58-
},
59-
{
60-
"energy": 6000,
61-
"gap": 5.681,
62-
},
63-
{
64-
"energy": 6500,
65-
"gap": 6.045,
66-
},
67-
],
46+
"column_names": ["energy_eV", "gap_mm"],
47+
"data": [[5700, 5.4606], [5760, 5.5], [6000, 5.681], [6500, 6.045]],
6848
}
69-
result = parse_lut_to_dict(contents, ("energy", "eV", int), ("gap", "mm", float))
49+
result = parse_lut_to_dict(contents, ("energy_eV", int), ("gap_mm", float))
50+
print(result)
7051
assert result == expected
7152
json.dumps(result)
7253

@@ -75,7 +56,7 @@ def test_parsing_bad_lut_causes_error():
7556
with open(TestDataPaths.TEST_BAD_LUT_PATH) as f:
7657
contents = f.read()
7758
with pytest.raises(IndexError):
78-
parse_lut_to_dict(contents, ("energy", "eV", int), ("gap", "mm", float))
59+
parse_lut_to_dict(contents, ("energy_eV", int), ("gap_mm", float))
7960

8061

8162
def test_display_config_to_dict_gives_expected_result_and_can_be_jsonified():

0 commit comments

Comments
 (0)