Skip to content

Commit 6ef2ef6

Browse files
committed
Ensure the existance of all higher levels before inserting tomo metadata
1 parent c436d34 commit 6ef2ef6

File tree

4 files changed

+49
-27
lines changed

4 files changed

+49
-27
lines changed

src/murfey/client/contexts/tomo_metadata.py

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@
1717
requests.get, requests.post, requests.put, requests.delete = authorised_requests()
1818

1919

20+
def ensure_dcg_exists(transferred_file: Path, environment: MurfeyInstanceEnvironment):
21+
# Make sure we have a data collection group
22+
source = _get_source(transferred_file, environment=environment)
23+
if not source:
24+
return None
25+
dcg_tag = str(source).replace(f"/{environment.visit}", "")
26+
url = f"{str(environment.url.geturl())}{url_path_for('workflow.router', 'register_dc_group', visit_name=environment.visit, session_id=environment.murfey_session)}"
27+
dcg_data = {
28+
"experiment_type": "single particle",
29+
"experiment_type_id": 37,
30+
"tag": dcg_tag,
31+
}
32+
capture_post(url, json=dcg_data)
33+
return dcg_tag
34+
35+
2036
class TomographyMetadataContext(Context):
2137
def __init__(self, acquisition_software: str, basepath: Path):
2238
super().__init__("Tomography_metadata", acquisition_software)
@@ -106,6 +122,7 @@ def post_transfer(
106122

107123
elif transferred_file.name == "SearchMap.xml" and environment:
108124
logger.info("Tomography session search map xml found")
125+
dcg_tag = ensure_dcg_exists(transferred_file, environment)
109126
with open(transferred_file, "r") as sm_xml:
110127
sm_data = xmltodict.parse(sm_xml.read())
111128

@@ -174,7 +191,6 @@ def post_transfer(
174191
}
175192

176193
source = _get_source(transferred_file, environment=environment)
177-
visitless_source = str(source).replace(f"/{environment.visit}", "")
178194
image_path = (
179195
_file_transferred_to(
180196
environment, source, transferred_file.parent / "SearchMap.jpg"
@@ -183,11 +199,11 @@ def post_transfer(
183199
else ""
184200
)
185201

186-
sm_url = f"{str(environment.url.geturl())}{url_path_for('session_control.tomography_router', 'register_search_map', session_id=environment.murfey_session, sm_name=transferred_file.parent.name)}"
202+
sm_url = f"{str(environment.url.geturl())}{url_path_for('session_control.tomo_router', 'register_search_map', session_id=environment.murfey_session, sm_name=transferred_file.parent.name)}"
187203
capture_post(
188204
sm_url,
189205
json={
190-
"tag": visitless_source,
206+
"tag": dcg_tag,
191207
"x_stage_position": float(stage_position["X"]),
192208
"y_stage_position": float(stage_position["Y"]),
193209
"pixel_size": sm_pixel_size,
@@ -201,36 +217,30 @@ def post_transfer(
201217

202218
elif transferred_file.name == "SearchMap.dm" and environment:
203219
logger.info("Tomography session search map dm found")
220+
dcg_tag = ensure_dcg_exists(transferred_file, environment)
204221
with open(transferred_file, "r") as sm_xml:
205222
sm_data = xmltodict.parse(sm_xml.read())
206223

207-
source = _get_source(transferred_file, environment=environment)
208-
visitless_source = str(source).replace(f"/{environment.visit}", "")
209-
if not visitless_source:
210-
return
211-
212224
# This bit gets SearchMap size
213225
sm_width = int(sm_data["TileSetXml"]["ImageSize"]["a:width"])
214226
sm_height = int(sm_data["TileSetXml"]["ImageSize"]["a:height"])
215227

216-
sm_url = f"{str(environment.url.geturl())}{url_path_for('session_control.tomography_router', 'register_search_map', session_id=environment.murfey_session, sm_name=transferred_file.parent.name)}"
228+
sm_url = f"{str(environment.url.geturl())}{url_path_for('session_control.tomo_router', 'register_search_map', session_id=environment.murfey_session, sm_name=transferred_file.parent.name)}"
217229
capture_post(
218230
sm_url,
219231
json={
220-
"tag": visitless_source,
232+
"tag": dcg_tag,
221233
"height": sm_height,
222234
"width": sm_width,
223235
},
224236
)
225237

226238
elif transferred_file.name == "BatchPositionsList.xml" and environment:
239+
logger.info("Tomography session batch positions list found")
240+
dcg_tag = ensure_dcg_exists(transferred_file, environment)
227241
with open(transferred_file) as xml:
228242
for_parsing = xml.read()
229243
batch_xml = xmltodict.parse(for_parsing)
230-
source = _get_source(transferred_file, environment=environment)
231-
visitless_source = str(source).replace(f"/{environment.visit}", "")
232-
if not visitless_source:
233-
return
234244

235245
for batch_position in batch_xml["BatchPositionsList"]["BatchPositions"][
236246
"BatchPositionParameters"
@@ -243,11 +253,22 @@ def post_transfer(
243253
batch_stage_location_y = float(
244254
batch_position["PositionOnTileSet"]["StagePositionY"]
245255
)
246-
bp_url = f"{str(environment.url.geturl())}{url_path_for('session_control.tomography_router', 'register_batch_position', session_id=environment.murfey_session, batch_name=batch_name)}"
256+
257+
# Always need search map before batch position
258+
sm_url = f"{str(environment.url.geturl())}{url_path_for('session_control.tomo_router', 'register_search_map', session_id=environment.murfey_session, sm_name=search_map_name)}"
259+
capture_post(
260+
sm_url,
261+
json={
262+
"tag": dcg_tag,
263+
},
264+
)
265+
266+
# Then register batch position
267+
bp_url = f"{str(environment.url.geturl())}{url_path_for('session_control.tomo_router', 'register_batch_position', session_id=environment.murfey_session, batch_name=batch_name)}"
247268
capture_post(
248269
bp_url,
249270
json={
250-
"tag": visitless_source,
271+
"tag": dcg_tag,
251272
"x_stage_position": batch_stage_location_x,
252273
"y_stage_position": batch_stage_location_y,
253274
"x_beamshift": 0,
@@ -268,11 +289,12 @@ def post_transfer(
268289
beamshift_position_x = float(beamshift["PositionX"])
269290
beamshift_position_y = float(beamshift["PositionY"])
270291

271-
bp_url = f"{str(environment.url.geturl())}{url_path_for('session_control.tomography_router', 'register_batch_position', session_id=environment.murfey_session, batch_name=beamshift_name)}"
292+
# Registration of beamshifted position
293+
bp_url = f"{str(environment.url.geturl())}{url_path_for('session_control.tomo_router', 'register_batch_position', session_id=environment.murfey_session, batch_name=beamshift_name)}"
272294
capture_post(
273295
bp_url,
274296
json={
275-
"tag": visitless_source,
297+
"tag": dcg_tag,
276298
"x_stage_position": batch_stage_location_x,
277299
"y_stage_position": batch_stage_location_y,
278300
"x_beamshift": beamshift_position_x,

src/murfey/server/api/session_control.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,14 @@ def register_foil_hole(
370370
return _register_foil_hole(session_id, gs_name, foil_hole_params, db)
371371

372372

373-
tomography_router = APIRouter(
374-
prefix="/session_control/tomography",
373+
tomo_router = APIRouter(
374+
prefix="/session_control/tomo",
375375
dependencies=[Depends(validate_instrument_token)],
376-
tags=["Session Control: Tomography"],
376+
tags=["Session Control: CryoET"],
377377
)
378378

379379

380-
@tomography_router.post("/sessions/{session_id}/search_map/{sm_name}")
380+
@tomo_router.post("/sessions/{session_id}/search_map/{sm_name}")
381381
def register_search_map(
382382
session_id: MurfeySessionID,
383383
sm_name: str,
@@ -387,7 +387,7 @@ def register_search_map(
387387
return register_search_map_in_database(session_id, sm_name, search_map_params, db)
388388

389389

390-
@tomography_router.post("/sessions/{session_id}/batch_position/{batch_name}")
390+
@tomo_router.post("/sessions/{session_id}/batch_position/{batch_name}")
391391
def register_batch_position(
392392
session_id: MurfeySessionID,
393393
batch_name: str,

src/murfey/server/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class Settings(BaseSettings):
8181

8282
app.include_router(murfey.server.api.session_control.router)
8383
app.include_router(murfey.server.api.session_control.spa_router)
84-
app.include_router(murfey.server.api.session_control.tomography_router)
84+
app.include_router(murfey.server.api.session_control.tomo_router)
8585

8686
app.include_router(murfey.server.api.session_info.router)
8787
app.include_router(murfey.server.api.session_info.correlative_router)

src/murfey/util/route_manifest.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -818,15 +818,15 @@ murfey.server.api.session_control.spa_router:
818818
type: int
819819
methods:
820820
- POST
821-
murfey.server.api.session_control.tomography_router:
822-
- path: /session_control/tomography/sessions/{session_id}/search_map/{sm_name}
821+
murfey.server.api.session_control.tomo_router:
822+
- path: /session_control/tomo/sessions/{session_id}/search_map/{sm_name}
823823
function: register_search_map
824824
path_params:
825825
- name: sm_name
826826
type: str
827827
methods:
828828
- POST
829-
- path: /session_control/tomography/sessions/{session_id}/batch_position/{batch_name}
829+
- path: /session_control/tomo/sessions/{session_id}/batch_position/{batch_name}
830830
function: register_batch_position
831831
path_params:
832832
- name: batch_name

0 commit comments

Comments
 (0)