Skip to content

Commit 119b489

Browse files
committed
Register colours and collection mode in CLEMImageSeries table upon arrival of message, then translate it across to Atlas and GridSquare tables in ISPyB
1 parent b1ba654 commit 119b489

2 files changed

Lines changed: 100 additions & 69 deletions

File tree

src/murfey/workflows/clem/register_preprocessing_results.py

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,27 @@ def _is_clem_atlas(result: CLEMPreprocessingResult):
6767
)
6868

6969

70+
COLOR_FLAGS_MURFEY = {
71+
"gray": "has_grey",
72+
"red": "has_red",
73+
"green": "has_green",
74+
"blue": "has_blue",
75+
"cyan": "has_cyan",
76+
"magenta": "has_magenta",
77+
"yellow": "has_yellow",
78+
}
79+
80+
81+
def _get_color_flags(
82+
colors: Collection[str] | None = None,
83+
):
84+
colors = colors or []
85+
color_flags = dict.fromkeys(COLOR_FLAGS_MURFEY.values(), False)
86+
for color in colors:
87+
color_flags[COLOR_FLAGS_MURFEY[color]] = True
88+
return color_flags
89+
90+
7091
def _register_clem_image_series(
7192
session_id: int,
7293
result: CLEMPreprocessingResult,
@@ -160,6 +181,11 @@ def _register_clem_image_series(
160181
clem_img_series.image_search_string = str(output_file.parent / "*tiff")
161182
clem_img_series.data_type = "atlas" if _is_clem_atlas(result) else "grid_square"
162183
clem_img_series.number_of_members = result.number_of_members
184+
for col_name, value in _get_color_flags(result.output_files.keys()):
185+
setattr(clem_img_series, col_name, value)
186+
clem_img_series.collection_mode = _determine_collection_mode(
187+
result.output_files.keys()
188+
)
163189
clem_img_series.image_pixels_x = result.pixels_x
164190
clem_img_series.image_pixels_y = result.pixels_y
165191
clem_img_series.image_pixel_size = result.pixel_size
@@ -187,27 +213,6 @@ def _register_clem_image_series(
187213
logger.info(f"CLEM preprocessing results registered for {result.series_name!r} ")
188214

189215

190-
color_columns = {
191-
"gray": "hasGrey",
192-
"red": "hasRed",
193-
"green": "hasGreen",
194-
"blue": "hasBlue",
195-
"cyan": "hasCyan",
196-
"magenta": "hasMagenta",
197-
"yellow": "hasYellow",
198-
}
199-
200-
201-
def _get_color_flags(
202-
colors: Collection[str] | None = None,
203-
):
204-
colors = colors or []
205-
color_flags = dict.fromkeys(color_columns.values(), 0)
206-
for color in colors:
207-
color_flags[color_columns[color]] = 1
208-
return color_flags
209-
210-
211216
def _determine_collection_mode(
212217
colors: Collection[str] | None = None,
213218
):
@@ -223,6 +228,16 @@ def _determine_collection_mode(
223228
return "Fluorescent"
224229

225230

231+
def _snake_to_camel_case(string: str):
232+
parts = string.split("_")
233+
return parts[0] + "".join(part.capitalize() for part in parts[1:])
234+
235+
236+
COLOR_FLAGS_MURFEY_TO_ISPYB = {
237+
value: _snake_to_camel_case(value) for value in COLOR_FLAGS_MURFEY.values()
238+
}
239+
240+
226241
def _register_dcg_and_atlas(
227242
session_id: int,
228243
instrument_name: str,
@@ -262,7 +277,11 @@ def _register_dcg_and_atlas(
262277
else:
263278
atlas_name = str(output_file.parent / "*.tiff")
264279
atlas_pixel_size = result.pixel_size
265-
color_flags = _get_color_flags(result.output_files.keys())
280+
# Translate colour flags into ISPyB convention
281+
color_flags = {
282+
COLOR_FLAGS_MURFEY_TO_ISPYB[key]: int(value)
283+
for key, value in _get_color_flags(result.output_files.keys()).items()
284+
}
266285
collection_mode = _determine_collection_mode(result.output_files.keys())
267286
else:
268287
atlas_name = ""
@@ -455,8 +474,13 @@ def _register_grid_square(
455474
y_stage_position=0.5 * (clem_img_series.y0 + clem_img_series.y1),
456475
pixel_size=clem_img_series.image_pixel_size,
457476
image=clem_img_series.thumbnail_search_string,
458-
collection_mode=_determine_collection_mode(result.output_files.keys()),
477+
collection_mode=clem_img_series.collection_mode,
459478
)
479+
# Construct colour flags for ISPyB
480+
color_flags = {
481+
ispyb_color_flags: int(getattr(clem_img_series, murfey_color_flags, 0))
482+
for murfey_color_flags, ispyb_color_flags in COLOR_FLAGS_MURFEY_TO_ISPYB.items()
483+
}
460484
# Register or update the grid square entry as required
461485
if grid_square_result := murfey_db.exec(
462486
select(MurfeyDB.GridSquare)
@@ -481,7 +505,7 @@ def _register_grid_square(
481505
_transport_object.do_update_grid_square(
482506
grid_square_id=grid_square_entry.id,
483507
grid_square_parameters=grid_square_params,
484-
color_flags=_get_color_flags(result.output_files.keys()),
508+
color_flags=color_flags,
485509
)
486510
else:
487511
# Look up data collection group for current series
@@ -495,7 +519,7 @@ def _register_grid_square(
495519
atlas_id=dcg_entry.atlas_id,
496520
grid_square_id=clem_img_series.id,
497521
grid_square_parameters=grid_square_params,
498-
color_flags=_get_color_flags(result.output_files.keys()),
522+
color_flags=color_flags,
499523
)
500524
# Register to Murfey
501525
grid_square_entry = MurfeyDB.GridSquare(

tests/workflows/clem/test_register_preprocessing_results.py

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212

1313
import murfey.util.db as MurfeyDB
1414
from murfey.workflows.clem.register_preprocessing_results import (
15+
COLOR_FLAGS_MURFEY_TO_ISPYB,
1516
_determine_collection_mode,
1617
_get_color_flags,
1718
_register_clem_image_series,
1819
_register_dcg_and_atlas,
1920
_register_grid_square,
21+
_snake_to_camel_case,
2022
run,
2123
)
2224
from tests.conftest import ExampleVisit, get_or_create_db_entry
@@ -118,75 +120,68 @@ def generate_preprocessing_messages(
118120
return messages
119121

120122

121-
def test_register_clem_image_series():
122-
_register_clem_image_series
123-
124-
125123
@pytest.mark.parametrize(
126124
"test_params",
127125
(
128126
(
129127
["gray"],
130128
{
131-
"hasGrey": 1,
129+
"has_grey": True,
132130
},
133131
),
134132
(
135133
["gray", "red"],
136134
{
137-
"hasGrey": 1,
138-
"hasRed": 1,
135+
"has_grey": True,
136+
"has_red": True,
139137
},
140138
),
141139
(
142140
["red", "green", "blue"],
143141
{
144-
"hasRed": 1,
145-
"hasGreen": 1,
146-
"hasBlue": 1,
142+
"has_red": True,
143+
"has_green": True,
144+
"has_blue": True,
147145
},
148146
),
149147
(
150148
["cyan", "magenta", "yellow"],
151149
{
152-
"hasCyan": 1,
153-
"hasMagenta": 1,
154-
"hasYellow": 1,
150+
"has_cyan": True,
151+
"has_magenta": True,
152+
"has_yellow": True,
155153
},
156154
),
157155
),
158156
)
159-
def test_get_color_flags(test_params: tuple[list[str], dict[str, int]]):
160-
# Unpack test params
157+
def test_get_color_flags(test_params: tuple[list[str], dict[str, bool]]):
161158
colors, positive_flags = test_params
162159
expected_result = dict.fromkeys(
163160
(
164-
"hasGrey",
165-
"hasRed",
166-
"hasGreen",
167-
"hasBlue",
168-
"hasCyan",
169-
"hasMagenta",
170-
"hasYellow",
161+
"has_grey",
162+
"has_red",
163+
"has_green",
164+
"has_blue",
165+
"has_cyan",
166+
"has_magenta",
167+
"has_yellow",
171168
),
172-
0,
169+
False,
173170
)
174171
for flag, value in positive_flags.items():
175172
expected_result[flag] = value
176173
assert _get_color_flags(colors) == expected_result
177174

178175

176+
def test_register_clem_image_series():
177+
_register_clem_image_series
178+
179+
179180
@pytest.mark.parametrize(
180181
"test_params",
181182
(
182-
(
183-
["gray"],
184-
"Bright Field",
185-
),
186-
(
187-
["gray", "blue"],
188-
"Bright Field and Fluorescent",
189-
),
183+
(["gray"], "Bright Field"),
184+
(["gray", "blue"], "Bright Field and Fluorescent"),
190185
(["red", "green", "blue"], "Fluorescent"),
191186
),
192187
)
@@ -195,6 +190,25 @@ def test_determine_collection_mode(test_params: tuple[list[str], str]):
195190
assert _determine_collection_mode(colors) == expected_result
196191

197192

193+
@pytest.mark.parametrize(
194+
"test_params",
195+
(
196+
("has_grey", "hasGrey"),
197+
("has_red", "hasRed"),
198+
("has_green", "hasGreen"),
199+
("has_blue", "hasBlue"),
200+
("has_cyan", "hasCyan"),
201+
("has_magenta", "hasMagenta"),
202+
("has_yellow", "hasYellow"),
203+
),
204+
)
205+
def test_snake_to_camel_case(
206+
test_params: tuple[str, str],
207+
):
208+
string, expected_result = test_params
209+
assert _snake_to_camel_case(string) == expected_result
210+
211+
198212
def test_register_dcg_and_atlas():
199213
_register_dcg_and_atlas
200214

@@ -267,18 +281,8 @@ def test_run(
267281
"test_params",
268282
(
269283
# Reverse list order? | Colors
270-
(
271-
False,
272-
[
273-
"gray",
274-
],
275-
),
276-
(
277-
True,
278-
[
279-
"gray",
280-
],
281-
),
284+
(False, ["gray"]),
285+
(True, ["gray"]),
282286
(False, ["red", "green", "blue"]),
283287
(True, ["cyan", "magenta", "yellow"]),
284288
(False, ["gray", "red", "green", "blue"]),
@@ -417,7 +421,10 @@ def test_run_with_db(
417421
assert len(ispyb_atlas_search) == 1
418422

419423
# Determine the color flags and collection mode
420-
color_flags = _get_color_flags(colors)
424+
color_flags = {
425+
COLOR_FLAGS_MURFEY_TO_ISPYB[flag]: int(value)
426+
for flag, value in _get_color_flags(colors).items()
427+
}
421428
collection_mode = _determine_collection_mode(colors)
422429

423430
ispyb_atlas = ispyb_atlas_search[0]

0 commit comments

Comments
 (0)