Skip to content

Commit 0828d28

Browse files
committed
Add atlas information registration routes for display purposes
1 parent 1e0b263 commit 0828d28

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

src/murfey/client/contexts/spa_metadata.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import requests
66
import xmltodict
7+
from PIL import Image
78

89
from murfey.client.context import Context
910
from murfey.client.contexts.spa import _get_grid_square_atlas_positions, _get_source
@@ -64,6 +65,38 @@ def post_transfer(
6465
visitless_path = Path(
6566
str(transferred_file).replace(f"/{environment.visit}", "")
6667
)
68+
visit_index_of_transferred_file = transferred_file.parts.index(
69+
environment.visit
70+
)
71+
atlas_xml_path = list(
72+
(
73+
Path(
74+
"/".join(
75+
transferred_file.parts[
76+
: visit_index_of_transferred_file + 1
77+
]
78+
)
79+
)
80+
/ partial_path
81+
).parent.glob("Atlas_*.xml")
82+
)[0]
83+
with open(atlas_xml_path, "rb") as atlas_xml:
84+
atlas_xml_data = xmltodict.parse(atlas_xml)
85+
atlas_original_pixel_size = atlas_xml_data["MicroscopeImage"][
86+
"SpatialScale"
87+
]["pixelSize"]["x"]["numericValue"]
88+
readout_width = float(
89+
atlas_xml_data["MicroscopeImage"]["SpatialScale"]["pixelSize"]["x"][
90+
"numericValue"
91+
]
92+
)
93+
94+
# need to calculate the pixel size of the downscaled image
95+
atlas_im = Image.open(atlas_xml_path.with_suffix(".jpg"))
96+
atlas_pixel_size = atlas_original_pixel_size * (
97+
readout_width / atlas_im.width
98+
)
99+
67100
source = _get_source(
68101
visitless_path.parent / "Images-Disc1" / visitless_path.name,
69102
environment,
@@ -90,6 +123,7 @@ def post_transfer(
90123
/ environment.samples[source].atlas
91124
),
92125
"sample": environment.samples[source].sample,
126+
"atlas_pixel_size": atlas_pixel_size,
93127
}
94128
capture_post(url, json=dcg_data)
95129
registered_grid_squares = (

src/murfey/server/__init__.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from fastapi.templating import Jinja2Templates
2424
from importlib_metadata import EntryPoint # For type hinting only
2525
from ispyb.sqlalchemy._auto_db_schema import (
26+
Atlas,
2627
AutoProcProgram,
2728
Base,
2829
DataCollection,
@@ -43,6 +44,7 @@
4344
from werkzeug.utils import secure_filename
4445

4546
import murfey
47+
import murfey.server.ispyb
4648
import murfey.server.prometheus as prom
4749
import murfey.server.websocket
4850
import murfey.util.db as db
@@ -2628,8 +2630,17 @@ def feedback_callback(header: dict, message: dict) -> None:
26282630
experimentTypeId=message["experiment_type_id"],
26292631
)
26302632
dcgid = _register(record, header)
2633+
atlas_record = Atlas(
2634+
dataCollectionGroupId=dcgid,
2635+
atlasImage=message.get("atlas", ""),
2636+
pixelSize=message.get("atlas_pixel_size", 0),
2637+
cassetteSlot=message.get("sample"),
2638+
)
2639+
if _transport_object:
2640+
atlas_id = _transport_object.do_insert_atlas(atlas_record)
26312641
murfey_dcg = db.DataCollectionGroup(
26322642
id=dcgid,
2643+
atlas_id=atlas_id,
26332644
session_id=message["session_id"],
26342645
tag=message.get("tag"),
26352646
)
@@ -2654,6 +2665,14 @@ def feedback_callback(header: dict, message: dict) -> None:
26542665
}
26552666
_transport_object.transport.ack(header)
26562667
return None
2668+
elif message["register"] == "atlas_update":
2669+
if _transport_object:
2670+
_transport_object.do_update_atlas(
2671+
message["atlas_id"],
2672+
message["atlas"],
2673+
message["atlas_pixel_size"],
2674+
message["sample"],
2675+
)
26572676
elif message["register"] == "data_collection":
26582677
murfey_session_id = message["session_id"]
26592678
ispyb_session_id = murfey.server.ispyb.get_session_id(

src/murfey/server/api/__init__.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,15 +1335,30 @@ def register_dc_group(
13351335
).all():
13361336
dcg_murfey[0].atlas = dcg_params.atlas
13371337
dcg_murfey[0].sample = dcg_params.sample
1338+
dcg_murfey[0].atlas_pixel_size = dcg_params.atlas_pixel_size
13381339
db.add(dcg_murfey[0])
13391340
db.commit()
1341+
if _transport_object:
1342+
_transport_object.send(
1343+
_transport_object.feedback_queue,
1344+
{
1345+
"register": "atlas_update",
1346+
"atlas_id": dcg_murfey.atlas_id,
1347+
"atlas": dcg_params.atlas,
1348+
"sample": dcg_params.sample,
1349+
"atlas_pixel_size": dcg_params.atlas_pixel_size,
1350+
},
1351+
)
13401352
else:
13411353
dcg_parameters = {
13421354
"start_time": str(datetime.datetime.now()),
13431355
"experiment_type": dcg_params.experiment_type,
13441356
"experiment_type_id": dcg_params.experiment_type_id,
13451357
"tag": dcg_params.tag,
13461358
"session_id": session_id,
1359+
"atlas": dcg_params.atlas,
1360+
"sample": dcg_params.sample,
1361+
"atlas_pixel_size": dcg_params.atlas_pixel_size,
13471362
}
13481363

13491364
if _transport_object:

src/murfey/server/ispyb.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import workflows.transport
1313
from fastapi import Depends
1414
from ispyb.sqlalchemy import (
15+
Atlas,
1516
AutoProcProgram,
1617
BLSample,
1718
BLSampleGroup,
@@ -91,6 +92,41 @@ def do_insert_data_collection_group(
9192
)
9293
return {"success": False, "return_value": None}
9394

95+
def do_insert_atlas(self, record: Atlas):
96+
try:
97+
with Session() as db:
98+
db.add(record)
99+
db.commit()
100+
log.info(f"Created Atlas {record.atlasId}")
101+
return {"success": True, "return_value": record.atlasId}
102+
except ispyb.ISPyBException as e:
103+
log.error(
104+
"Inserting Atlas entry caused exception '%s'.",
105+
e,
106+
exc_info=True,
107+
)
108+
return {"success": False, "return_value": None}
109+
110+
def do_update_atlas(
111+
self, atlas_id: int, atlas_image: str, pixel_size: float, slot: int
112+
):
113+
try:
114+
with Session() as db:
115+
atlas = db.query(Atlas).filter(Atlas.atlasId == atlas_id).one()
116+
atlas.atlasImage = atlas_image
117+
atlas.pixelSize = pixel_size
118+
atlas.cassetteSlot = slot
119+
db.add(atlas)
120+
db.commit()
121+
return {"success": True, "return_value": atlas.atlasId}
122+
except ispyb.ISPyBException as e:
123+
log.error(
124+
"Updating Atlas entry caused exception '%s'.",
125+
e,
126+
exc_info=True,
127+
)
128+
return {"success": False, "return_value": None}
129+
94130
def send(self, queue: str, message: dict, new_connection: bool = False):
95131
if self.transport:
96132
if not self.transport.is_connected():

src/murfey/util/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class DCGroupParameters(BaseModel):
4747
tag: str
4848
atlas: str = ""
4949
sample: Optional[int] = None
50+
atlas_pixel_size: int = 0
5051

5152

5253
class DCParameters(BaseModel):

0 commit comments

Comments
 (0)