-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathatlas.py
More file actions
137 lines (125 loc) · 5.24 KB
/
Copy pathatlas.py
File metadata and controls
137 lines (125 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import logging
from pathlib import Path
from typing import Optional
import xmltodict
from murfey.client.context import Context, _atlas_destination
from murfey.client.contexts.spa import _get_source
from murfey.client.instance_environment import MurfeyInstanceEnvironment
from murfey.util.client import capture_post
logger = logging.getLogger("murfey.client.contexts.atlas")
class AtlasContext(Context):
def __init__(self, acquisition_software: str, basepath: Path, token: str):
super().__init__("Atlas", acquisition_software, token)
self._basepath = basepath
def post_transfer(
self,
transferred_file: Path,
environment: Optional[MurfeyInstanceEnvironment] = None,
**kwargs,
):
super().post_transfer(
transferred_file=transferred_file,
environment=environment,
**kwargs,
)
if self._acquisition_software == "serialem":
self.post_transfer_serialem(
transferred_file, environment=environment, **kwargs
)
else:
self.post_transfer_epu(transferred_file, environment=environment, **kwargs)
def post_transfer_serialem(
self,
transferred_file: Path,
environment: Optional[MurfeyInstanceEnvironment] = None,
**kwargs,
):
if environment and transferred_file.suffix == ".mrc":
source = _get_source(transferred_file, environment)
if source:
capture_post(
base_url=str(environment.url.geturl()),
router_name="session_control.spa_router",
function_name="register_atlas",
token=self._token,
session_id=environment.murfey_session,
data={
"name": transferred_file.stem,
"acquisition_uuid": environment.acquisition_uuid,
},
)
def post_transfer_epu(
self,
transferred_file: Path,
environment: Optional[MurfeyInstanceEnvironment] = None,
**kwargs,
):
if (
environment
and "Atlas_" in transferred_file.stem
and transferred_file.suffix == ".mrc"
):
source = _get_source(transferred_file, environment)
if source:
transferred_atlas_name = _atlas_destination(
environment, source, self._token
) / transferred_file.relative_to(source.parent)
capture_post(
base_url=str(environment.url.geturl()),
router_name="session_control.spa_router",
function_name="make_atlas_jpg",
token=self._token,
session_id=environment.murfey_session,
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, 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}"
)