From 0599615b2e0b1452225a13c27ad6ae201aade79a Mon Sep 17 00:00:00 2001 From: yxd92326 Date: Wed, 8 Apr 2026 13:57:05 +0100 Subject: [PATCH 1/4] Register all squares on atlases before data collection --- src/murfey/client/contexts/atlas.py | 54 ++++++++++++++++++++++ src/murfey/client/contexts/spa_metadata.py | 2 +- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/murfey/client/contexts/atlas.py b/src/murfey/client/contexts/atlas.py index 6269a3507..92f75f83a 100644 --- a/src/murfey/client/contexts/atlas.py +++ b/src/murfey/client/contexts/atlas.py @@ -7,6 +7,7 @@ from murfey.client.context import Context, _atlas_destination, _get_source from murfey.client.instance_environment import MurfeyInstanceEnvironment from murfey.util.client import capture_post +from murfey.util.spa_metadata import get_grid_square_atlas_positions logger = logging.getLogger("murfey.client.contexts.atlas") @@ -158,3 +159,56 @@ def post_transfer_epu( logger.info( f"Registered data collection group for atlas {str(transferred_atlas_jpg)!r}" ) + + elif ( + environment + and "Atlas_" in transferred_file.stem + and transferred_file.suffix == ".dm" + ): + # Register all grid squares on this atlas + gs_pix_positions = get_grid_square_atlas_positions(transferred_file) + for gs, pos_data in gs_pix_positions.items(): + if pos_data: + capture_post( + base_url=str(environment.url.geturl()), + router_name="session_control.spa_router", + function_name="register_grid_square", + token=self._token, + instrument_name=environment.instrument_name, + session_id=environment.murfey_session, + gsid=int(gs), + data={ + "tag": str(transferred_file.parent), + "x_location": pos_data[0], + "y_location": pos_data[1], + "x_stage_position": pos_data[2], + "y_stage_position": pos_data[3], + "width": pos_data[4], + "height": pos_data[5], + "angle": pos_data[6], + }, + ) + if gs_pix_positions: + for p in transferred_file.parts: + if p.startswith("Sample"): + sample = int(p.replace("Sample", "")) + break + else: + logger.warning( + f"Sample could not be identified for {transferred_file}" + ) + return + capture_post( + base_url=str(environment.url.geturl()), + router_name="session_control.spa_router", + function_name="register_atlas", + token=self._token, + instrument_name=environment.instrument_name, + session_id=environment.murfey_session, + data={ + "name": f"{environment.visit}-sample-{sample}", + "acquisition_uuid": environment.acquisition_uuid, + "register_grid": True, + "tag": str(transferred_file.parent), + }, + ) diff --git a/src/murfey/client/contexts/spa_metadata.py b/src/murfey/client/contexts/spa_metadata.py index 34a58c925..b32728ccb 100644 --- a/src/murfey/client/contexts/spa_metadata.py +++ b/src/murfey/client/contexts/spa_metadata.py @@ -152,7 +152,7 @@ def post_transfer( "angle": pos_data[6], }, ) - if pos_data: + if gs_pix_positions: capture_post( base_url=str(environment.url.geturl()), router_name="session_control.spa_router", From 548e8cb9a8046b61d365daa4b150fb3dec86ac90 Mon Sep 17 00:00:00 2001 From: yxd92326 Date: Wed, 8 Apr 2026 15:16:16 +0100 Subject: [PATCH 2/4] Change grid square tags when moving atlas to dc --- src/murfey/server/api/workflow.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/murfey/server/api/workflow.py b/src/murfey/server/api/workflow.py index fddb41b6d..95c605e88 100644 --- a/src/murfey/server/api/workflow.py +++ b/src/murfey/server/api/workflow.py @@ -232,6 +232,7 @@ def register_dc_group( ) ).all(): # Case where we switch from atlas to processing + original_tag = dcg_murfey[0].tag dcg_murfey[0].tag = dcg_params.tag or dcg_murfey[0].tag if _transport_object: _transport_object.send( @@ -243,6 +244,12 @@ def register_dc_group( }, ) db.add(dcg_murfey[0]) + for grid_square in db.exec( + select(GridSquare) + .where(GridSquare.tag == original_tag) + .where(GridSquare.session_id == session_id) + ).all(): + grid_square.tag = dcg_params.tag or original_tag db.commit() else: dcg_parameters = { From 17f83da75f6e9918476d0dbc0e4907c2a91cf995 Mon Sep 17 00:00:00 2001 From: yxd92326 Date: Wed, 8 Apr 2026 16:18:22 +0100 Subject: [PATCH 3/4] Forgot to add to db --- src/murfey/server/api/workflow.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/murfey/server/api/workflow.py b/src/murfey/server/api/workflow.py index 95c605e88..4d27fe9b5 100644 --- a/src/murfey/server/api/workflow.py +++ b/src/murfey/server/api/workflow.py @@ -250,6 +250,7 @@ def register_dc_group( .where(GridSquare.session_id == session_id) ).all(): grid_square.tag = dcg_params.tag or original_tag + db.add(grid_square) db.commit() else: dcg_parameters = { From 07627c2981d8354785bcfb4aa5a65c34cea8972c Mon Sep 17 00:00:00 2001 From: yxd92326 Date: Thu, 9 Apr 2026 09:10:36 +0100 Subject: [PATCH 4/4] Should have a test for this --- src/murfey/client/contexts/atlas.py | 6 +- tests/client/contexts/test_atlas.py | 94 ++++++++++++++++++++++++++--- 2 files changed, 87 insertions(+), 13 deletions(-) diff --git a/src/murfey/client/contexts/atlas.py b/src/murfey/client/contexts/atlas.py index 92f75f83a..11ae5ae63 100644 --- a/src/murfey/client/contexts/atlas.py +++ b/src/murfey/client/contexts/atlas.py @@ -160,11 +160,7 @@ def post_transfer_epu( f"Registered data collection group for atlas {str(transferred_atlas_jpg)!r}" ) - elif ( - environment - and "Atlas_" in transferred_file.stem - and transferred_file.suffix == ".dm" - ): + elif environment and transferred_file.name == "Atlas.dm": # Register all grid squares on this atlas gs_pix_positions = get_grid_square_atlas_positions(transferred_file) for gs, pos_data in gs_pix_positions.items(): diff --git a/tests/client/contexts/test_atlas.py b/tests/client/contexts/test_atlas.py index 915f9b3eb..b132154bb 100644 --- a/tests/client/contexts/test_atlas.py +++ b/tests/client/contexts/test_atlas.py @@ -33,10 +33,7 @@ def test_atlas_context_mrc(mock_capture_post, tmp_path): atlas_mrc.parent.mkdir(parents=True) atlas_mrc.touch() - context.post_transfer( - atlas_mrc, - environment=env, - ) + context.post_transfer(atlas_mrc, environment=env) mock_capture_post.assert_called_once_with( base_url="http://localhost:8000", router_name="session_control.spa_router", @@ -72,10 +69,7 @@ def test_atlas_context_xml(mock_capture_post, tmp_path): "" ) - context.post_transfer( - atlas_xml, - environment=env, - ) + context.post_transfer(atlas_xml, environment=env) dcg_data = { "experiment_type_id": 44, # Atlas "tag": str(atlas_xml.parent), @@ -95,3 +89,87 @@ def test_atlas_context_xml(mock_capture_post, tmp_path): session_id=1, data=dcg_data, ) + + +@patch("murfey.client.contexts.atlas.capture_post") +def test_atlas_context_dm(mock_capture_post, tmp_path): + env = MurfeyInstanceEnvironment( + url=urlparse("http://localhost:8000"), + client_id=0, + sources=[tmp_path / "cm12345-6"], + default_destinations={ + tmp_path / "cm12345-6": f"{tmp_path}/destination/cm12345-6" + }, + instrument_name="m01", + visit="cm12345-6", + murfey_session=1, + acquisition_uuid="uuid1", + ) + + # Write sample dm file + atlas_dm = tmp_path / "cm12345-6/Supervisor_atlas/Sample2/Atlas/Atlas.dm" + atlas_dm.parent.mkdir(parents=True) + grid_square_values = ( + "" + "12001500" + "23" + "130560" + "0.14" + "" + ) + with open(atlas_dm, "w") as new_xml: + new_xml.write( + "<_items>" + # First tile with two grid squares + "" + f"101{grid_square_values}" + "" + f"102{grid_square_values}" + "" + # Second tile with two grid squares + "" + f"103{grid_square_values}" + "" + f"104{grid_square_values}" + "" + # Close all + "" + ) + + context = AtlasContext("tomo", tmp_path, {}, "token") + context.post_transfer(atlas_dm, environment=env) + + assert mock_capture_post.call_count == 5 + mock_capture_post.assert_any_call( + base_url="http://localhost:8000", + router_name="session_control.spa_router", + function_name="register_grid_square", + token="token", + instrument_name="m01", + session_id=1, + gsid=101, + data={ + "tag": str(atlas_dm.parent), + "x_location": 1200, + "y_location": 1500, + "x_stage_position": 2e9, + "y_stage_position": 3e9, + "width": 130, + "height": 560, + "angle": 0.14, + }, + ) + mock_capture_post.assert_any_call( + base_url="http://localhost:8000", + router_name="session_control.spa_router", + function_name="register_atlas", + token="token", + instrument_name="m01", + session_id=1, + data={ + "name": "cm12345-6-sample-2", + "acquisition_uuid": "uuid1", + "register_grid": True, + "tag": str(atlas_dm.parent), + }, + )