Skip to content

Commit f57ea80

Browse files
committed
Move CLEM atlas determination logic from a repeatedly used function into a computed field in the CLEM model instead
1 parent 1f224ab commit f57ea80

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

src/murfey/workflows/clem/register_preprocessing_results.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
import logging
1212
import traceback
1313
from collections.abc import Collection
14+
from functools import cached_property
1415
from importlib.metadata import entry_points
1516
from pathlib import Path
1617
from typing import Literal, Optional
1718

18-
from pydantic import BaseModel
19+
from pydantic import BaseModel, computed_field
1920
from sqlmodel import Session, select
2021

2122
import murfey.util.db as MurfeyDB
@@ -53,16 +54,21 @@ class CLEMPreprocessingResult(BaseModel):
5354
resolution: float
5455
extent: list[float] # [x0, x1, y0, y1]
5556

56-
57-
def _is_clem_atlas(result: CLEMPreprocessingResult):
58-
# If an image has a width/height of at least 1.5 mm, it should qualify as an atlas
59-
return (
60-
max(
61-
result.pixels_x * result.pixel_size,
62-
result.pixels_y * result.pixel_size,
57+
# Valid Pydantic decorator not supported by MyPy
58+
@computed_field # type: ignore
59+
@cached_property
60+
def is_atlas(self) -> bool:
61+
"""
62+
Incoming image sets with a width/height greater/equal to the pre-set threshold,
63+
it should qualify as an atlas.
64+
"""
65+
return (
66+
max(
67+
self.pixels_x * self.pixel_size,
68+
self.pixels_y * self.pixel_size,
69+
)
70+
>= processing_params.atlas_threshold
6371
)
64-
>= processing_params.atlas_threshold
65-
)
6672

6773

6874
COLOR_FLAGS_MURFEY = {
@@ -105,7 +111,7 @@ def _register_clem_imaging_site(
105111
# Add metadata for this series
106112
output_file = list(result.output_files.values())[0]
107113
clem_img_site.image_path = str(output_file.parent / "*tiff")
108-
clem_img_site.data_type = "atlas" if _is_clem_atlas(result) else "grid_square"
114+
clem_img_site.data_type = "atlas" if result.is_atlas else "grid_square"
109115
clem_img_site.number_of_members = result.number_of_members
110116
for col_name, value in _get_color_flags(result.output_files.keys()).items():
111117
setattr(clem_img_site, col_name, value)
@@ -188,7 +194,7 @@ def _register_dcg_and_atlas(
188194
dcg_name += f"--{result.series_name.split('--')[1]}"
189195

190196
# Determine values for atlas
191-
if _is_clem_atlas(result):
197+
if result.is_atlas:
192198
output_file = list(result.output_files.values())[0]
193199
# Register the thumbnail entries if they are provided
194200
if result.thumbnails and result.thumbnail_size is not None:
@@ -227,7 +233,7 @@ def _register_dcg_and_atlas(
227233
dcg_entry = dcg_search[0]
228234
# Update atlas if registering atlas dataset
229235
# and data collection group already exists
230-
if _is_clem_atlas(result):
236+
if result.is_atlas:
231237
atlas_message = {
232238
"session_id": session_id,
233239
"dcgid": dcg_entry.id,

0 commit comments

Comments
 (0)