Skip to content

Commit 1533125

Browse files
committed
Merge branch 'atlas-dcg' into match-atlas-dcgs
2 parents b6ba908 + 2da2b4d commit 1533125

File tree

2 files changed

+147
-1
lines changed

2 files changed

+147
-1
lines changed

src/murfey/client/contexts/atlas.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from pathlib import Path
33
from typing import Optional
44

5+
import xmltodict
6+
57
from murfey.client.context import Context, _atlas_destination
68
from murfey.client.contexts.spa import _get_source
79
from murfey.client.instance_environment import MurfeyInstanceEnvironment
@@ -43,8 +45,60 @@ def post_transfer(
4345
function_name="make_atlas_jpg",
4446
token=self._token,
4547
session_id=environment.murfey_session,
46-
data={"path": str(transferred_atlas_name)},
48+
data={"path": str(transferred_atlas_name).replace("//", "/")},
4749
)
4850
logger.info(
4951
f"Submitted request to create JPG image of atlas {str(transferred_atlas_name)!r}"
5052
)
53+
elif (
54+
environment
55+
and "Atlas_" in transferred_file.stem
56+
and transferred_file.suffix == ".xml"
57+
):
58+
source = _get_source(transferred_file, environment)
59+
if source:
60+
atlas_mrc = transferred_file.with_suffix(".mrc")
61+
transferred_atlas_jpg = _atlas_destination(
62+
environment, source, self._token
63+
) / atlas_mrc.relative_to(source.parent).with_suffix(".jpg")
64+
65+
with open(transferred_file, "rb") as atlas_xml:
66+
atlas_xml_data = xmltodict.parse(atlas_xml)
67+
atlas_original_pixel_size = float(
68+
atlas_xml_data["MicroscopeImage"]["SpatialScale"]["pixelSize"][
69+
"x"
70+
]["numericValue"]
71+
)
72+
73+
# need to calculate the pixel size of the downscaled image
74+
atlas_pixel_size = atlas_original_pixel_size * 7.8
75+
76+
for p in transferred_file.parts:
77+
if p.startswith("Sample"):
78+
sample = int(p.replace("Sample", ""))
79+
break
80+
else:
81+
logger.warning(
82+
f"Sample could not be identified for {transferred_file}"
83+
)
84+
return
85+
86+
dcg_data = {
87+
"experiment_type_id": 44, # Atlas
88+
"tag": str(transferred_file.parent),
89+
"atlas": str(transferred_atlas_jpg).replace("//", "/"),
90+
"sample": sample,
91+
"atlas_pixel_size": atlas_pixel_size,
92+
}
93+
capture_post(
94+
base_url=str(environment.url.geturl()),
95+
router_name="workflow.router",
96+
function_name="register_dc_group",
97+
token=self._token,
98+
visit_name=environment.visit,
99+
session_id=environment.murfey_session,
100+
data=dcg_data,
101+
)
102+
logger.info(
103+
f"Registered data collection group for atlas {str(transferred_atlas_jpg)!r}"
104+
)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from unittest.mock import patch
2+
from urllib.parse import urlparse
3+
4+
from murfey.client.contexts.atlas import AtlasContext
5+
from murfey.client.instance_environment import MurfeyInstanceEnvironment
6+
7+
8+
def test_atlas_context_initialisation(tmp_path):
9+
context = AtlasContext("tomo", tmp_path, "token")
10+
assert context.name == "Atlas"
11+
assert context._acquisition_software == "tomo"
12+
assert context._basepath == tmp_path
13+
assert context._token == "token"
14+
15+
16+
@patch("murfey.client.contexts.atlas.capture_post")
17+
def test_atlas_context_mrc(mock_capture_post, tmp_path):
18+
env = MurfeyInstanceEnvironment(
19+
url=urlparse("http://localhost:8000"),
20+
client_id=0,
21+
sources=[tmp_path / "cm12345-6"],
22+
default_destinations={
23+
tmp_path / "cm12345-6": f"{tmp_path}/destination/cm12345-6"
24+
},
25+
instrument_name="",
26+
visit="cm12345-6",
27+
murfey_session=1,
28+
)
29+
context = AtlasContext("tomo", tmp_path, "token")
30+
31+
atlas_mrc = tmp_path / "cm12345-6/Supervisor_atlas/Sample2/Atlas/Atlas_1.mrc"
32+
atlas_mrc.parent.mkdir(parents=True)
33+
atlas_mrc.touch()
34+
35+
context.post_transfer(
36+
atlas_mrc,
37+
environment=env,
38+
)
39+
mock_capture_post.assert_called_once_with(
40+
base_url="http://localhost:8000",
41+
router_name="session_control.spa_router",
42+
function_name="make_atlas_jpg",
43+
token="token",
44+
session_id=1,
45+
data={"path": f"{tmp_path}/destination/{atlas_mrc.relative_to(tmp_path)}"},
46+
)
47+
48+
49+
@patch("murfey.client.contexts.atlas.capture_post")
50+
def test_atlas_context_xml(mock_capture_post, tmp_path):
51+
env = MurfeyInstanceEnvironment(
52+
url=urlparse("http://localhost:8000"),
53+
client_id=0,
54+
sources=[tmp_path / "cm12345-6"],
55+
default_destinations={
56+
tmp_path / "cm12345-6": f"{tmp_path}/destination/cm12345-6"
57+
},
58+
instrument_name="",
59+
visit="cm12345-6",
60+
murfey_session=1,
61+
)
62+
context = AtlasContext("tomo", tmp_path, "token")
63+
64+
atlas_pixel_size = 4.6
65+
atlas_xml = tmp_path / "cm12345-6/Supervisor_atlas/Sample2/Atlas/Atlas_1.xml"
66+
atlas_xml.parent.mkdir(parents=True)
67+
with open(atlas_xml, "w") as new_xml:
68+
new_xml.write(
69+
f"<MicroscopeImage><SpatialScale><pixelSize><x><numericValue>{atlas_pixel_size}"
70+
"</numericValue></x></pixelSize></SpatialScale></MicroscopeImage>"
71+
)
72+
73+
context.post_transfer(
74+
atlas_xml,
75+
environment=env,
76+
)
77+
dcg_data = {
78+
"experiment_type_id": 44, # Atlas
79+
"tag": str(atlas_xml.parent),
80+
"atlas": f"{tmp_path}/destination/{atlas_xml.relative_to(tmp_path).with_suffix('.jpg')}",
81+
"sample": 2,
82+
"atlas_pixel_size": atlas_pixel_size * 7.8,
83+
}
84+
mock_capture_post.assert_called_once_with(
85+
base_url="http://localhost:8000",
86+
router_name="workflow.router",
87+
function_name="register_dc_group",
88+
token="token",
89+
visit_name="cm12345-6",
90+
session_id=1,
91+
data=dcg_data,
92+
)

0 commit comments

Comments
 (0)