Skip to content

Commit 13bf5aa

Browse files
committed
Modified '_register_imaging_site' so that it returns the ImagingSite entry; modified the subsequent helper functions to use the returned ImagingSite entry instead of performing new queries
1 parent 9e1d4af commit 13bf5aa

1 file changed

Lines changed: 49 additions & 52 deletions

File tree

src/murfey/workflows/clem/register_preprocessing_results.py

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,20 @@ def _register_clem_imaging_site(
118118
result: CLEMPreprocessingResult,
119119
murfey_db: Session,
120120
):
121-
def _register(
121+
"""
122+
Creates an ImagingSite database entry for the current CLEM preprocessing result
123+
if one doesn't already exist, or modifies the existing one if it does. Each entry
124+
corresponds to a unique site on the sample grid, and results containing denoised
125+
data will supersede existing rows for the same position that contain only raw
126+
data. Returns the created/queried entry.
127+
"""
128+
129+
def _populate(
122130
entry: MurfeyDB.ImagingSite,
123131
result: CLEMPreprocessingResult,
124132
):
125133
"""
126-
Helper function to update the ImagingSite column values with.
134+
Helper function to populate the ImagingSite column values.
127135
"""
128136

129137
# Is this an atlas or grid square
@@ -174,20 +182,20 @@ def _register(
174182
session_id=session_id,
175183
site_name=result.site_name,
176184
)
177-
clem_img_site = _register(clem_img_site, result)
185+
clem_img_site = _populate(clem_img_site, result)
178186

179187
# Prepare to overwrite existing entry if current result is a denoised dataset
180188
if result.is_denoised:
181189
# Proceed with overwrite if current result is different from existing entry
182190
output_file = list(result.output_files.values())[0]
183191
if str(output_file.parent / "*.tiff") != clem_img_site.image_path:
184-
clem_img_site = _register(clem_img_site, result)
192+
clem_img_site = _populate(clem_img_site, result)
185193

194+
# Commit changes and return entry
186195
murfey_db.add(clem_img_site)
187196
murfey_db.commit()
188-
murfey_db.close()
189-
190197
logger.info(f"CLEM preprocessing results registered for {result.series_name!r} ")
198+
return clem_img_site
191199

192200

193201
def _determine_collection_mode(
@@ -219,9 +227,15 @@ def _register_dcg_and_atlas(
219227
session_id: int,
220228
instrument_name: str,
221229
visit_name: str,
222-
result: CLEMPreprocessingResult,
230+
imaging_site: MurfeyDB.ImagingSite,
223231
murfey_db: Session,
224232
):
233+
"""
234+
Takes an ImagingSite entry and uses it to create and register DataCollectionGroup
235+
entries in ISPyB if they don't already exist, or to populate existing entries.
236+
After doing so, it will register the DataCollectionGroup ID in Murfey and add it
237+
to the ImagingSite entry.
238+
"""
225239
# Determine variables to register data collection group and atlas with
226240
proposal_code = "".join(char for char in visit_name.split("-")[0] if char.isalpha())
227241
proposal_number = "".join(
@@ -230,36 +244,29 @@ def _register_dcg_and_atlas(
230244
visit_number = visit_name.split("-")[-1]
231245

232246
# Generate name/tag for data colleciton group based on series name
233-
dcg_name = result.site_name.split("--")[0]
234-
if result.site_name.split("--")[1].isdigit():
235-
dcg_name += f"--{result.site_name.split('--')[1]}"
247+
dcg_name = imaging_site.site_name.split("--")[0]
248+
if imaging_site.site_name.split("--")[1].isdigit():
249+
dcg_name += f"--{imaging_site.site_name.split('--')[1]}"
236250

237251
# Determine values for atlas
238-
if result.is_atlas:
239-
output_file = list(result.output_files.values())[0]
240-
# Register the thumbnail entries if they are provided
241-
if result.thumbnails and result.thumbnail_size is not None:
242-
# Glob path to the thumbnail files
243-
thumbnail = list(result.thumbnails.values())[0]
244-
atlas_name = str(thumbnail.parent / "*.png")
245-
246-
# Work out the scaling factor used
247-
thumbnail_height, thumbnail_width = result.thumbnail_size
248-
scaling_factor = min(
249-
thumbnail_width / result.pixels_x,
250-
thumbnail_height / result.pixels_y,
251-
)
252-
atlas_pixel_size = result.pixel_size / scaling_factor
252+
if is_atlas := imaging_site.data_type == "atlas":
253+
# Register using thumbnail values if they are provided
254+
if (
255+
imaging_site.thumbnail_path is not None
256+
and imaging_site.thumbnail_pixel_size is not None
257+
):
258+
atlas_name: str | None = imaging_site.thumbnail_path
259+
atlas_pixel_size: float | None = imaging_site.thumbnail_pixel_size
253260
# Otherwise, register the TIFF files themselves
254261
else:
255-
atlas_name = str(output_file.parent / "*.tiff")
256-
atlas_pixel_size = result.pixel_size
262+
atlas_name = imaging_site.image_path
263+
atlas_pixel_size = imaging_site.image_pixel_size
257264
# Translate colour flags into ISPyB convention
258265
color_flags = {
259-
COLOR_FLAGS_MURFEY_TO_ISPYB[key]: int(value)
260-
for key, value in _get_color_flags(result.output_files.keys()).items()
266+
COLOR_FLAGS_MURFEY_TO_ISPYB[key]: getattr(imaging_site, key, 0)
267+
for key in COLOR_FLAGS_MURFEY_TO_ISPYB.keys()
261268
}
262-
collection_mode = _determine_collection_mode(result.output_files.keys())
269+
collection_mode = imaging_site.collection_mode
263270
else:
264271
atlas_name = ""
265272
atlas_pixel_size = 0.0
@@ -272,9 +279,8 @@ def _register_dcg_and_atlas(
272279
.where(MurfeyDB.DataCollectionGroup.tag == dcg_name)
273280
).all():
274281
dcg_entry = dcg_search[0]
275-
# Update atlas if registering atlas dataset
276-
# and data collection group already exists
277-
if result.is_atlas:
282+
# Update if current dataset is atlas and data collection group exists
283+
if is_atlas:
278284
atlas_message = {
279285
"session_id": session_id,
280286
"dcgid": dcg_entry.id,
@@ -330,32 +336,24 @@ def _register_dcg_and_atlas(
330336
.where(MurfeyDB.DataCollectionGroup.tag == dcg_name)
331337
).one()
332338

333-
clem_img_site = murfey_db.exec(
334-
select(MurfeyDB.ImagingSite)
335-
.where(MurfeyDB.ImagingSite.session_id == session_id)
336-
.where(MurfeyDB.ImagingSite.site_name == result.site_name)
337-
).one()
338-
339-
clem_img_site.dcg_id = dcg_entry.id
340-
clem_img_site.dcg_name = dcg_entry.tag
341-
murfey_db.add(clem_img_site)
339+
imaging_site.dcg_id = dcg_entry.id
340+
imaging_site.dcg_name = dcg_entry.tag
341+
murfey_db.add(imaging_site)
342342
murfey_db.commit()
343-
murfey_db.close()
344343

345344

346345
def _register_grid_square(
347346
session_id: int,
348-
result: CLEMPreprocessingResult,
347+
imaging_site: MurfeyDB.ImagingSite,
349348
murfey_db: Session,
350349
):
351350
# Skip this step if no transport manager object is configured
352351
if _transport_object is None:
353352
logger.error("Unable to find transport manager")
354353
return
355-
# Load all entries for the current data collection group
356-
dcg_name = result.site_name.split("--")[0]
357-
if result.site_name.split("--")[1].isdigit():
358-
dcg_name += f"--{result.site_name.split('--')[1]}"
354+
if (dcg_name := imaging_site.dcg_name) is None:
355+
logger.warning("Current imaging site has no data collection group name")
356+
return
359357

360358
# Check if an atlas has been registered
361359
if not (
@@ -540,7 +538,6 @@ def _register_grid_square(
540538
logger.info(
541539
f"No grid squares to register for data collection group {dcg_name!r} yet"
542540
)
543-
murfey_db.close()
544541
return
545542

546543

@@ -583,7 +580,7 @@ def run(message: dict, murfey_db: Session) -> dict[str, bool]:
583580
return {"success": False, "requeue": False}
584581
try:
585582
# Register items in Murfey database
586-
_register_clem_imaging_site(
583+
clem_img_site = _register_clem_imaging_site(
587584
session_id=session_id,
588585
result=result,
589586
murfey_db=murfey_db,
@@ -601,7 +598,7 @@ def run(message: dict, murfey_db: Session) -> dict[str, bool]:
601598
session_id=session_id,
602599
instrument_name=murfey_session.instrument_name,
603600
visit_name=murfey_session.visit,
604-
result=result,
601+
imaging_site=clem_img_site,
605602
murfey_db=murfey_db,
606603
)
607604
except Exception:
@@ -616,7 +613,7 @@ def run(message: dict, murfey_db: Session) -> dict[str, bool]:
616613
# Register CLEM image series as grid squares
617614
_register_grid_square(
618615
session_id=session_id,
619-
result=result,
616+
imaging_site=clem_img_site,
620617
murfey_db=murfey_db,
621618
)
622619
except Exception:

0 commit comments

Comments
 (0)