Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ TomographyMetadataContext = "murfey.client.contexts.tomo_metadata:TomographyMeta
"spa.ctf_estimated" = "murfey.workflows.spa.ctf_estimation:ctf_estimated"
"spa.flush_spa_preprocess" = "murfey.workflows.spa.flush_spa_preprocess:flush_spa_preprocess"
"spa.motion_corrected" = "murfey.workflows.spa.motion_correction:motion_corrected"
"spa.register_grid_square" = "murfey.workflows.spa.register_grid_square:run"
"spa.smartem_atlas" = "murfey.workflows.spa.smartem_atlas:smartem_atlas"
"sxt.process_tilt_series" = "murfey.workflows.sxt.process_sxt_tilt_series:run"
"sxt.register_roi" = "murfey.workflows.sxt.sxt_metadata:run"
Expand Down
12 changes: 10 additions & 2 deletions src/murfey/server/api/session_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
from murfey.workflows.spa.atlas import atlas_jpg_from_mrc
from murfey.workflows.spa.flush_spa_preprocess import (
register_foil_hole as _register_foil_hole,
register_grid_square as _register_grid_square,
)
from murfey.workflows.tomo.tomo_metadata import (
register_batch_position_in_database,
Expand Down Expand Up @@ -457,7 +456,16 @@ def register_grid_square(
grid_square_params: GridSquareParameters,
db=murfey_db,
):
return _register_grid_square(session_id, gsid, grid_square_params, db)
if murfey.server._transport_object:
murfey.server._transport_object.send(
murfey.server._transport_object.feedback_queue,
{
"register": "spa.register_grid_square",
"session_id": session_id,
"gsid": gsid,
"grid_square_params": grid_square_params.model_dump(),
},
)


@spa_router.post("/sessions/{session_id}/grid_square/{gs_name}/foil_hole")
Expand Down
192 changes: 2 additions & 190 deletions src/murfey/workflows/spa/flush_spa_preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
from typing import Optional

from PIL import Image
from sqlalchemy import desc
from sqlalchemy.exc import NoResultFound
from sqlmodel import Session, select

try:
from smartem_backend.api_client import SmartEMAPIClient
from smartem_common.schemas import (
FoilHoleData as SmartEMFoilHoleData,
GridSquareData as SmartEMGridSquareData,
GridSquareMetadata as SmartEMGridSquareMetadata,
)
from smartem_common.schemas import FoilHoleData as SmartEMFoilHoleData

from murfey.util.config import get_smartem_keycloak_client

Expand Down Expand Up @@ -51,194 +46,11 @@
grid_square_data,
grid_square_from_file,
)
from murfey.workflows.spa.register_grid_square import register_grid_square

logger = logging.getLogger("murfey.workflows.spa.flush_spa_preprocess")


def register_grid_square(
session_id: int,
gsid: int,
grid_square_params: GridSquareParameters,
murfey_db: Session,
):
# Calculate scaled down version of the image for registration to ISPyB first
if grid_square_params.x_location is not None:
grid_square_params.x_location_scaled = int(grid_square_params.x_location / 7.8)
if grid_square_params.y_location is not None:
grid_square_params.y_location_scaled = int(grid_square_params.y_location / 7.8)
if grid_square_params.height is not None:
grid_square_params.height_scaled = int(grid_square_params.height / 7.8)
if grid_square_params.width is not None:
grid_square_params.width_scaled = int(grid_square_params.width / 7.8)

if grid_square_params.sample is not None:
dcg = murfey_db.exec(
select(DataCollectionGroup)
.where(DataCollectionGroup.session_id == session_id)
.where(DataCollectionGroup.sample == grid_square_params.sample)
.order_by(desc(DataCollectionGroup.id))
).first()
else:
dcg = murfey_db.exec(
select(DataCollectionGroup)
.where(DataCollectionGroup.session_id == session_id)
.where(DataCollectionGroup.tag == grid_square_params.tag)
.order_by(desc(DataCollectionGroup.id))
).first()
grid_square_query = murfey_db.exec(
select(GridSquare)
.where(GridSquare.name == gsid)
.where(GridSquare.tag == dcg.tag)
.where(GridSquare.session_id == session_id)
).all()
if grid_square_query:
# Grid square already exists in the murfey database
grid_square = grid_square_query[0]
grid_square.x_location = grid_square_params.x_location or grid_square.x_location
grid_square.y_location = grid_square_params.y_location or grid_square.y_location
grid_square.x_stage_position = (
grid_square_params.x_stage_position or grid_square.x_stage_position
)
grid_square.y_stage_position = (
grid_square_params.y_stage_position or grid_square.y_stage_position
)
grid_square.readout_area_x = (
grid_square_params.readout_area_x or grid_square.readout_area_x
)
grid_square.readout_area_y = (
grid_square_params.readout_area_y or grid_square.readout_area_y
)
grid_square.thumbnail_size_x = (
grid_square_params.thumbnail_size_x or grid_square.thumbnail_size_x
)
grid_square.thumbnail_size_y = (
grid_square_params.thumbnail_size_y or grid_square.thumbnail_size_y
)
grid_square.pixel_size = grid_square_params.pixel_size or grid_square.pixel_size
grid_square.image = grid_square_params.image or grid_square.image
if murfey.server._transport_object:
murfey.server._transport_object.do_update_grid_square(
grid_square.id, grid_square_params
)
else:
# No existing grid square in the murfey database
if murfey.server._transport_object:
dcg = murfey_db.exec(
select(DataCollectionGroup)
.where(DataCollectionGroup.session_id == session_id)
.where(DataCollectionGroup.tag == grid_square_params.tag)
).one()
gs_ispyb_response = murfey.server._transport_object.do_insert_grid_square(
dcg.atlas_id, gsid, grid_square_params
)
else:
# mock up response so that below still works
gs_ispyb_response = {"success": False, "return_value": None}
secured_grid_square_image_path = secure_path(Path(grid_square_params.image))
if secured_grid_square_image_path and secured_grid_square_image_path.is_file():
jpeg_size = Image.open(secured_grid_square_image_path).size
else:
jpeg_size = (0, 0)
grid_square = GridSquare(
id=(
gs_ispyb_response["return_value"]
if gs_ispyb_response["success"]
else None
),
name=gsid,
session_id=session_id,
tag=grid_square_params.tag,
x_location=grid_square_params.x_location,
y_location=grid_square_params.y_location,
x_stage_position=grid_square_params.x_stage_position,
y_stage_position=grid_square_params.y_stage_position,
readout_area_x=grid_square_params.readout_area_x,
readout_area_y=grid_square_params.readout_area_y,
thumbnail_size_x=grid_square_params.thumbnail_size_x or jpeg_size[0],
thumbnail_size_y=grid_square_params.thumbnail_size_y or jpeg_size[1],
pixel_size=grid_square_params.pixel_size,
image=str(secured_grid_square_image_path),
)
murfey_db.add(grid_square)
murfey_db.commit()

if SMARTEM_ACTIVE:
try:
murfey_session = murfey_db.exec(
select(MurfeySession).where(MurfeySession.id == session_id)
).one()
machine_config = get_machine_config(
instrument_name=murfey_session.instrument_name
)[murfey_session.instrument_name]
if machine_config.smartem_api_url:
if dcg.smartem_grid_uuid:
secured_grid_square_image_path_full_res: Path | None = None
if grid_square_params.image:
secured_grid_square_image_path_full_res = secure_path(
Path(grid_square_params.image)
)
if secured_grid_square_image_path_full_res.with_suffix(
".tiff"
).is_file():
secured_grid_square_image_path_full_res = (
secured_grid_square_image_path_full_res.with_suffix(
".tiff"
)
)
else:
secured_grid_square_image_path_full_res = (
secured_grid_square_image_path_full_res.with_suffix(
".mrc"
)
)
smartem_client = SmartEMAPIClient(
base_url=machine_config.smartem_api_url,
logger=logger,
keycloak_client=keycloak_client,
)
gs_data = SmartEMGridSquareData(
gridsquare_id=str(gsid),
grid_uuid=dcg.smartem_grid_uuid,
center_x=(
int(grid_square_params.x_location)
if grid_square_params.x_location is not None
else None
),
center_y=(
int(grid_square_params.y_location)
if grid_square_params.y_location is not None
else None
),
size_width=grid_square_params.width,
size_height=grid_square_params.height,
**(
{"uuid": grid_square.smartem_uuid}
if grid_square.smartem_uuid
else {}
),
metadata=SmartEMGridSquareMetadata(
atlas_node_id=0,
stage_position=None,
state=None,
rotation=None,
image_path=secured_grid_square_image_path_full_res,
selected=False,
unusable=False,
),
)
if grid_square.smartem_uuid:
smartem_client.update_gridsquare(gs_data)
else:
response = smartem_client.create_grid_gridsquare(gs_data)
grid_square.smartem_uuid = response.uuid
murfey_db.add(grid_square)
murfey_db.commit()
except Exception:
logger.warning("Failed to register grid square with smartem", exc_info=True)

murfey_db.close()


def register_foil_hole(
session_id: int,
gs_name: int,
Expand Down
Loading