Skip to content

Commit ad5e512

Browse files
authored
Minor bug fixes in CLEM workflow when registering grid squares (#774)
* Added rounding when converting caculated pixel values to int * Added catch for when image dimensions might get rounded down to 0 * Updated comments on GridSquareParameters Pydantic model
1 parent 98af888 commit ad5e512

2 files changed

Lines changed: 35 additions & 18 deletions

File tree

src/murfey/util/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,11 @@ class GridSquareParameters(BaseModel):
129129
tag: str
130130
image: str = ""
131131

132+
# Actual coordinates for image centre in real space
132133
x_location: Optional[float] = None
133134
y_location: Optional[float] = None
134135

135-
# Image coordinates when overlaid on atlas (in pixels0)
136+
# Coordinates for image centre when overlaid on atlas (in pixels)
136137
x_location_scaled: Optional[int] = None
137138
y_location_scaled: Optional[int] = None
138139

src/murfey/workflows/clem/register_preprocessing_results.py

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,12 @@ def _register_clem_imaging_site(
130130
thumbnail_height / result.pixels_y, thumbnail_width / result.pixels_x
131131
)
132132
clem_img_site.thumbnail_pixel_size = result.pixel_size / scaling_factor
133-
clem_img_site.thumbnail_pixels_x = int(result.pixels_x * scaling_factor)
134-
clem_img_site.thumbnail_pixels_y = int(result.pixels_y * scaling_factor)
133+
clem_img_site.thumbnail_pixels_x = (
134+
int(round(result.pixels_x * scaling_factor)) or 1
135+
)
136+
clem_img_site.thumbnail_pixels_y = (
137+
int(round(result.pixels_y * scaling_factor)) or 1
138+
)
135139
murfey_db.add(clem_img_site)
136140
murfey_db.commit()
137141
murfey_db.close()
@@ -354,30 +358,42 @@ def _register_grid_square(
354358
and clem_img_site.y0 is not None
355359
and clem_img_site.y1 is not None
356360
):
357-
# Find pixel corresponding to image midpoint on atlas
358-
x_mid_real = (
359-
0.5 * (clem_img_site.x0 + clem_img_site.x1) - atlas_entry.x0
360-
)
361+
# Find the real coordinates of the image midpoint
362+
x_mid_real = 0.5 * (clem_img_site.x0 + clem_img_site.x1)
363+
y_mid_real = 0.5 * (clem_img_site.y0 + clem_img_site.y1)
364+
365+
# Find pixel coordinates corresponding to image midpoint on atlas
361366
x_mid_px = int(
362-
x_mid_real / atlas_width_real * atlas_entry.thumbnail_pixels_x
363-
)
364-
y_mid_real = (
365-
0.5 * (clem_img_site.y0 + clem_img_site.y1) - atlas_entry.y0
367+
round(
368+
(x_mid_real - atlas_entry.x0)
369+
/ atlas_width_real
370+
* atlas_entry.thumbnail_pixels_x
371+
)
366372
)
367373
y_mid_px = int(
368-
y_mid_real / atlas_height_real * atlas_entry.thumbnail_pixels_y
374+
round(
375+
(y_mid_real - atlas_entry.y0)
376+
/ atlas_height_real
377+
* atlas_entry.thumbnail_pixels_y
378+
)
369379
)
370380

371381
# Find the size of the image, in pixels, when overlaid on the atlas
372382
width_scaled = int(
373-
(clem_img_site.x1 - clem_img_site.x0)
374-
/ atlas_width_real
375-
* atlas_entry.thumbnail_pixels_x
383+
round(
384+
(clem_img_site.x1 - clem_img_site.x0)
385+
/ atlas_width_real
386+
* atlas_entry.thumbnail_pixels_x
387+
)
388+
or 1
376389
)
377390
height_scaled = int(
378-
(clem_img_site.y1 - clem_img_site.y0)
379-
/ atlas_height_real
380-
* atlas_entry.thumbnail_pixels_y
391+
round(
392+
(clem_img_site.y1 - clem_img_site.y0)
393+
/ atlas_height_real
394+
* atlas_entry.thumbnail_pixels_y
395+
)
396+
or 1
381397
)
382398
else:
383399
logger.warning(

0 commit comments

Comments
 (0)