|
| 1 | +import logging |
| 2 | + |
| 3 | +from sqlmodel.orm.session import Session as SQLModelSession, select |
| 4 | + |
| 5 | +from murfey.server import _transport_object |
| 6 | +from murfey.util import sanitise |
| 7 | +from murfey.util.db import ( |
| 8 | + DataCollectionGroup, |
| 9 | + SxtRoi, |
| 10 | +) |
| 11 | +from murfey.util.models import SearchMapParameters |
| 12 | + |
| 13 | +logger = logging.getLogger("murfey.workflows.sxt.sxt_metadata") |
| 14 | + |
| 15 | + |
| 16 | +def register_sxt_roi( |
| 17 | + session_id: int, |
| 18 | + roi_name: str, |
| 19 | + roi_parameters: SearchMapParameters, |
| 20 | + murfey_db: SQLModelSession, |
| 21 | +) -> dict[str, bool]: |
| 22 | + dcg = murfey_db.exec( |
| 23 | + select(DataCollectionGroup) |
| 24 | + .where(DataCollectionGroup.session_id == session_id) |
| 25 | + .where(DataCollectionGroup.tag == roi_parameters.tag) |
| 26 | + ).one() |
| 27 | + roi_query = murfey_db.exec( |
| 28 | + select(SxtRoi) |
| 29 | + .where(SxtRoi.name == roi_name) |
| 30 | + .where(SxtRoi.tag == roi_parameters.tag) |
| 31 | + .where(SxtRoi.session_id == SxtRoi) |
| 32 | + ).all() |
| 33 | + if roi_query: |
| 34 | + # See if there is already a search map with this name and update if so |
| 35 | + roi = roi_query[0] |
| 36 | + roi.x_stage_position = roi_parameters.x_stage_position or roi.x_stage_position |
| 37 | + roi.y_stage_position = roi_parameters.y_stage_position or roi.y_stage_position |
| 38 | + roi.height = roi_parameters.height or roi.height |
| 39 | + roi.width = roi_parameters.width or roi.width |
| 40 | + roi.pixel_size = roi_parameters.pixel_size or roi.pixel_size |
| 41 | + roi.image = roi_parameters.image or roi.image |
| 42 | + if _transport_object: |
| 43 | + _transport_object.do_update_sxt_roi(roi.id, roi_parameters) |
| 44 | + else: |
| 45 | + logger.info(f"Registering new sxt roi {sanitise(roi_name)}") |
| 46 | + if _transport_object: |
| 47 | + roi_ispyb_response = _transport_object.do_insert_sxt_roi( |
| 48 | + dcg.atlas_id, roi_parameters |
| 49 | + ) |
| 50 | + else: |
| 51 | + # mock up response so that below still works |
| 52 | + roi_ispyb_response = {"success": False, "return_value": None} |
| 53 | + # Register new search map |
| 54 | + roi = SxtRoi( |
| 55 | + id=( |
| 56 | + roi_ispyb_response["return_value"] |
| 57 | + if roi_ispyb_response["success"] |
| 58 | + else None |
| 59 | + ), |
| 60 | + name=roi_name, |
| 61 | + session_id=session_id, |
| 62 | + tag=roi_parameters.tag, |
| 63 | + x_stage_position=roi_parameters.x_stage_position, |
| 64 | + y_stage_position=roi_parameters.y_stage_position, |
| 65 | + pixel_size=roi_parameters.pixel_size, |
| 66 | + width=roi_parameters.width, |
| 67 | + height=roi_parameters.height, |
| 68 | + thumbnail_size_x=1024, |
| 69 | + thumbnail_size_y=1024, |
| 70 | + image=roi_parameters.image or "", |
| 71 | + ) |
| 72 | + |
| 73 | + if all( |
| 74 | + [ |
| 75 | + roi.x_stage_position, |
| 76 | + roi.y_stage_position, |
| 77 | + roi.pixel_size, |
| 78 | + dcg.atlas_pixel_size, |
| 79 | + dcg.atlas_stage_x, |
| 80 | + dcg.atlas_height, |
| 81 | + ] |
| 82 | + ): |
| 83 | + # Convert from stage position to pixel locations |
| 84 | + roi.x_location = ( |
| 85 | + roi.x_stage_position - dcg.atlas_stage_x |
| 86 | + ) / dcg.atlas_pixel_size |
| 87 | + roi.y_location = ( |
| 88 | + roi.y_stage_position - dcg.atlas_stage_y |
| 89 | + ) / dcg.atlas_pixel_size |
| 90 | + |
| 91 | + # Scaling from different pixel size of atlas and roi, and atlas thumbnailing |
| 92 | + roi_parameters.x_location = roi.x_location |
| 93 | + roi_parameters.y_location = roi.y_location |
| 94 | + roi_parameters.height_on_atlas = ( |
| 95 | + roi.height |
| 96 | + * (roi.pixel_size / dcg.atlas_pixel_size) |
| 97 | + * (1024 / dcg.atlas_height) |
| 98 | + ) |
| 99 | + roi_parameters.width_on_atlas = ( |
| 100 | + roi.width |
| 101 | + * (roi.pixel_size / dcg.atlas_pixel_size) |
| 102 | + * (1024 / dcg.atlas_width) |
| 103 | + ) |
| 104 | + if _transport_object: |
| 105 | + _transport_object.do_update_sxt_roi(roi.id, roi_parameters) |
| 106 | + else: |
| 107 | + logger.info( |
| 108 | + f"Unable to register roi {sanitise(roi.name)} position yet: " |
| 109 | + f"roi pixel size {sanitise(str(roi.pixel_size))}, " |
| 110 | + f"atlas pixel size {sanitise(str(dcg.atlas_pixel_size))}" |
| 111 | + ) |
| 112 | + murfey_db.add(roi) |
| 113 | + murfey_db.commit() |
| 114 | + murfey_db.close() |
| 115 | + return {"success": True} |
| 116 | + |
| 117 | + |
| 118 | +def run(message: dict, murfey_db: SQLModelSession) -> dict[str, bool]: |
| 119 | + return register_sxt_roi( |
| 120 | + message["session_id"], |
| 121 | + message["roi_name"], |
| 122 | + SearchMapParameters(**message["roi_info"]), |
| 123 | + murfey_db, |
| 124 | + ) |
0 commit comments