Skip to content

Commit 4245c8f

Browse files
committed
Updated DataCollectionGroup and GridSquare registration logic for CLEM workflow to make use of scaled down thumbnails instead of full-sized TIFFs
1 parent 7fb882a commit 4245c8f

File tree

1 file changed

+63
-22
lines changed

1 file changed

+63
-22
lines changed

src/murfey/workflows/clem/register_preprocessing_results.py

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class CLEMPreprocessingResult(BaseModel):
4040
]
4141
thumbnails: dict[
4242
Literal["gray", "red", "green", "blue", "cyan", "magenta", "yellow"], Path
43-
]
43+
] = {}
44+
thumbnail_size: Optional[tuple[int, int]] = None # height, width
4445
metadata: Path
4546
parent_lif: Optional[Path] = None
4647
parent_tiffs: dict[
@@ -57,7 +58,10 @@ class CLEMPreprocessingResult(BaseModel):
5758
def _is_clem_atlas(result: CLEMPreprocessingResult):
5859
# If an image has a width/height of at least 1.5 mm, it should qualify as an atlas
5960
return (
60-
max(result.pixels_x * result.pixel_size, result.pixels_y * result.pixel_size)
61+
max(
62+
result.pixels_x * result.pixel_size,
63+
result.pixels_y * result.pixel_size,
64+
)
6165
>= processing_params.atlas_threshold
6266
)
6367

@@ -152,17 +156,29 @@ def _register_clem_image_series(
152156
murfey_db.commit()
153157

154158
# Add metadata for this series
155-
clem_img_series.search_string = str(output_file.parent / "*tiff")
159+
clem_img_series.image_search_string = str(output_file.parent / "*tiff")
156160
clem_img_series.data_type = "atlas" if _is_clem_atlas(result) else "grid_square"
157161
clem_img_series.number_of_members = result.number_of_members
158-
clem_img_series.pixels_x = result.pixels_x
159-
clem_img_series.pixels_y = result.pixels_y
160-
clem_img_series.pixel_size = result.pixel_size
162+
clem_img_series.image_pixels_x = result.pixels_x
163+
clem_img_series.image_pixels_y = result.pixels_y
164+
clem_img_series.image_pixel_size = result.pixel_size
161165
clem_img_series.units = result.units
162166
clem_img_series.x0 = result.extent[0]
163167
clem_img_series.x1 = result.extent[1]
164168
clem_img_series.y0 = result.extent[2]
165169
clem_img_series.y1 = result.extent[3]
170+
# Register thumbnails if they are present
171+
if result.thumbnails and result.thumbnail_size:
172+
thumbnail = list(result.thumbnails.values())[0]
173+
clem_img_series.thumbnail_search_string = str(thumbnail.parent / "*.png")
174+
175+
thumbnail_height, thumbnail_width = result.thumbnail_size
176+
scaling_factor = min(
177+
thumbnail_height / result.pixels_y, thumbnail_width / result.pixels_x
178+
)
179+
clem_img_series.thumbnail_pixel_size = result.pixel_size / scaling_factor
180+
clem_img_series.thumbnail_pixels_x = int(result.pixels_x * scaling_factor)
181+
clem_img_series.thumbnail_pixels_y = int(result.pixels_y * scaling_factor)
166182
murfey_db.add(clem_img_series)
167183
murfey_db.commit()
168184
murfey_db.close()
@@ -192,8 +208,23 @@ def _register_dcg_and_atlas(
192208
# Determine values for atlas
193209
if _is_clem_atlas(result):
194210
output_file = list(result.output_files.values())[0]
195-
atlas_name = str(output_file.parent / "*.tiff")
196-
atlas_pixel_size = result.pixel_size
211+
# Register the thumbnail entries if they are provided
212+
if result.thumbnails and result.thumbnail_size is not None:
213+
# Glob path to the thumbnail files
214+
thumbnail = list(result.thumbnails.values())[0]
215+
atlas_name = str(thumbnail.parent / "*.png")
216+
217+
# Work out the scaling factor used
218+
thumbnail_height, thumbnail_width = result.thumbnail_size
219+
scaling_factor = min(
220+
thumbnail_width / result.pixels_x,
221+
thumbnail_height / result.pixels_y,
222+
)
223+
atlas_pixel_size = result.pixel_size / scaling_factor
224+
# Otherwise, register the TIFF files themselves
225+
else:
226+
atlas_name = str(output_file.parent / "*.tiff")
227+
atlas_pixel_size = result.pixel_size
197228
else:
198229
atlas_name = ""
199230
atlas_pixel_size = 0.0
@@ -311,8 +342,6 @@ def _register_grid_square(
311342
and atlas_entry.x1 is not None
312343
and atlas_entry.y0 is not None
313344
and atlas_entry.y1 is not None
314-
and atlas_entry.pixels_x is not None
315-
and atlas_entry.pixels_y is not None
316345
):
317346
atlas_width_real = atlas_entry.x1 - atlas_entry.x0
318347
atlas_height_real = atlas_entry.y1 - atlas_entry.y0
@@ -321,32 +350,40 @@ def _register_grid_square(
321350
return
322351

323352
for clem_img_series in clem_img_series_to_register:
353+
# Register datasets using thumbnail sizes and scales
324354
if (
325355
clem_img_series.x0 is not None
326356
and clem_img_series.x1 is not None
327357
and clem_img_series.y0 is not None
328358
and clem_img_series.y1 is not None
359+
and clem_img_series.thumbnail_pixels_x is not None
360+
and clem_img_series.thumbnail_pixels_y is not None
361+
and clem_img_series.thumbnail_pixel_size is not None
329362
):
330363
# Find pixel corresponding to image midpoint on atlas
331364
x_mid_real = (
332365
0.5 * (clem_img_series.x0 + clem_img_series.x1) - atlas_entry.x0
333366
)
334-
x_mid_px = int(x_mid_real / atlas_width_real * atlas_entry.pixels_x)
367+
x_mid_px = int(
368+
x_mid_real / atlas_width_real * clem_img_series.thumbnail_pixels_x
369+
)
335370
y_mid_real = (
336371
0.5 * (clem_img_series.y0 + clem_img_series.y1) - atlas_entry.y0
337372
)
338-
y_mid_px = int(y_mid_real / atlas_height_real * atlas_entry.pixels_y)
373+
y_mid_px = int(
374+
y_mid_real / atlas_height_real * clem_img_series.thumbnail_pixels_y
375+
)
339376

340-
# Find the number of pixels in width and height the image corresponds to on the atlas
377+
# Find the size of the image, in pixels, when overlaid the atlas
341378
width_scaled = int(
342379
(clem_img_series.x1 - clem_img_series.x0)
343380
/ atlas_width_real
344-
* atlas_entry.pixels_x
381+
* clem_img_series.thumbnail_pixels_x
345382
)
346383
height_scaled = int(
347384
(clem_img_series.y1 - clem_img_series.y0)
348385
/ atlas_height_real
349-
* atlas_entry.pixels_y
386+
* clem_img_series.thumbnail_pixels_y
350387
)
351388
else:
352389
logger.warning(
@@ -361,14 +398,18 @@ def _register_grid_square(
361398
x_location_scaled=x_mid_px,
362399
y_location=clem_img_series.y0,
363400
y_location_scaled=y_mid_px,
364-
height=clem_img_series.pixels_x,
365-
height_scaled=height_scaled,
366-
width=clem_img_series.pixels_y,
401+
readout_area_x=clem_img_series.image_pixels_x,
402+
readout_area_y=clem_img_series.image_pixels_y,
403+
thumbnail_size_x=clem_img_series.thumbnail_pixels_x,
404+
thumbnail_size_y=clem_img_series.thumbnail_pixels_y,
405+
width=clem_img_series.image_pixels_x,
367406
width_scaled=width_scaled,
368-
x_stage_position=clem_img_series.x0,
369-
y_stage_position=clem_img_series.y0,
370-
pixel_size=clem_img_series.pixel_size,
371-
image=clem_img_series.search_string,
407+
height=clem_img_series.image_pixels_y,
408+
height_scaled=height_scaled,
409+
x_stage_position=0.5 * (clem_img_series.x0 + clem_img_series.x1),
410+
y_stage_position=0.5 * (clem_img_series.y0 + clem_img_series.y1),
411+
pixel_size=clem_img_series.image_pixel_size,
412+
image=clem_img_series.thumbnail_search_string,
372413
)
373414
# Register or update the grid square entry as required
374415
if grid_square_result := murfey_db.exec(

0 commit comments

Comments
 (0)