diff --git a/src/murfey/client/contexts/atlas.py b/src/murfey/client/contexts/atlas.py index 85460d8d..5c95da61 100644 --- a/src/murfey/client/contexts/atlas.py +++ b/src/murfey/client/contexts/atlas.py @@ -2,6 +2,8 @@ from pathlib import Path from typing import Optional +import xmltodict + from murfey.client.context import Context from murfey.client.contexts.spa import _get_source from murfey.client.contexts.spa_metadata import _atlas_destination @@ -44,8 +46,60 @@ def post_transfer( function_name="make_atlas_jpg", token=self._token, session_id=environment.murfey_session, - data={"path": str(transferred_atlas_name)}, + data={"path": str(transferred_atlas_name).replace("//", "/")}, ) logger.info( f"Submitted request to create JPG image of atlas {str(transferred_atlas_name)!r}" ) + elif ( + environment + and "Atlas_" in transferred_file.stem + and transferred_file.suffix == ".xml" + ): + source = _get_source(transferred_file, environment) + if source: + atlas_mrc = transferred_file.with_suffix(".mrc") + transferred_atlas_jpg = _atlas_destination( + environment, source, atlas_mrc, self._token + ) / atlas_mrc.relative_to(source.parent).with_suffix(".jpg") + + with open(transferred_file, "rb") as atlas_xml: + atlas_xml_data = xmltodict.parse(atlas_xml) + atlas_original_pixel_size = float( + atlas_xml_data["MicroscopeImage"]["SpatialScale"]["pixelSize"][ + "x" + ]["numericValue"] + ) + + # need to calculate the pixel size of the downscaled image + atlas_pixel_size = atlas_original_pixel_size * 7.8 + + 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 + + dcg_data = { + "experiment_type_id": 44, # Atlas + "tag": str(transferred_file.parent), + "atlas": str(transferred_atlas_jpg).replace("//", "/"), + "sample": sample, + "atlas_pixel_size": atlas_pixel_size, + } + capture_post( + base_url=str(environment.url.geturl()), + router_name="workflow.router", + function_name="register_dc_group", + token=self._token, + visit_name=environment.visit, + session_id=environment.murfey_session, + data=dcg_data, + ) + logger.info( + f"Registered data collection group for atlas {str(transferred_atlas_jpg)!r}" + ) diff --git a/tests/client/contexts/test_atlas.py b/tests/client/contexts/test_atlas.py new file mode 100644 index 00000000..b56f0162 --- /dev/null +++ b/tests/client/contexts/test_atlas.py @@ -0,0 +1,92 @@ +from unittest.mock import patch +from urllib.parse import urlparse + +from murfey.client.contexts.atlas import AtlasContext +from murfey.client.instance_environment import MurfeyInstanceEnvironment + + +def test_atlas_context_initialisation(tmp_path): + context = AtlasContext("tomo", tmp_path, "token") + assert context.name == "Atlas" + assert context._acquisition_software == "tomo" + assert context._basepath == tmp_path + assert context._token == "token" + + +@patch("murfey.client.contexts.atlas.capture_post") +def test_atlas_context_mrc(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="", + visit="cm12345-6", + murfey_session=1, + ) + context = AtlasContext("tomo", tmp_path, "token") + + atlas_mrc = tmp_path / "cm12345-6/Supervisor_atlas/Sample2/Atlas/Atlas_1.mrc" + atlas_mrc.parent.mkdir(parents=True) + atlas_mrc.touch() + + 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", + function_name="make_atlas_jpg", + token="token", + session_id=1, + data={"path": f"{tmp_path}/destination/{atlas_mrc.relative_to(tmp_path)}"}, + ) + + +@patch("murfey.client.contexts.atlas.capture_post") +def test_atlas_context_xml(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="", + visit="cm12345-6", + murfey_session=1, + ) + context = AtlasContext("tomo", tmp_path, "token") + + atlas_pixel_size = 4.6 + atlas_xml = tmp_path / "cm12345-6/Supervisor_atlas/Sample2/Atlas/Atlas_1.xml" + atlas_xml.parent.mkdir(parents=True) + with open(atlas_xml, "w") as new_xml: + new_xml.write( + f"{atlas_pixel_size}" + "" + ) + + context.post_transfer( + atlas_xml, + environment=env, + ) + dcg_data = { + "experiment_type_id": 44, # Atlas + "tag": str(atlas_xml.parent), + "atlas": f"{tmp_path}/destination/{atlas_xml.relative_to(tmp_path).with_suffix('.jpg')}", + "sample": 2, + "atlas_pixel_size": atlas_pixel_size * 7.8, + } + mock_capture_post.assert_called_once_with( + base_url="http://localhost:8000", + router_name="workflow.router", + function_name="register_dc_group", + token="token", + visit_name="cm12345-6", + session_id=1, + data=dcg_data, + )