-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtomo_metadata.py
More file actions
327 lines (300 loc) · 13.1 KB
/
tomo_metadata.py
File metadata and controls
327 lines (300 loc) · 13.1 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import logging
import shutil
from datetime import datetime
from pathlib import Path
from typing import Optional
import xmltodict
from murfey.client.context import (
Context,
_file_transferred_to,
_get_source,
ensure_dcg_exists,
)
from murfey.client.instance_environment import MurfeyInstanceEnvironment
from murfey.util.client import capture_post
logger = logging.getLogger("murfey.client.contexts.tomo_metadata")
class TomographyMetadataContext(Context):
def __init__(
self,
acquisition_software: str,
basepath: Path,
machine_config: dict,
token: str,
):
super().__init__("Tomography_metadata", acquisition_software, token)
self._basepath = basepath
self._machine_config = machine_config
def post_transfer(
self,
transferred_file: Path,
environment: Optional[MurfeyInstanceEnvironment] = None,
**kwargs,
):
super().post_transfer(
transferred_file=transferred_file,
environment=environment,
**kwargs,
)
if environment is None:
logger.warning("No environment set")
return
metadata_source = _get_source(transferred_file, environment=environment)
if not metadata_source:
logger.warning(f"No source found for {str(transferred_file)}")
return
if transferred_file.name == "Session.dm":
logger.info("Tomography session metadata found")
ensure_dcg_exists(
collection_type="tomo",
metadata_source=metadata_source,
environment=environment,
machine_config=self._machine_config,
token=self._token,
)
elif transferred_file.name == "SearchMap.xml":
logger.info("Tomography session search map xml found")
dcg_tag = ensure_dcg_exists(
collection_type="tomo",
metadata_source=metadata_source,
environment=environment,
machine_config=self._machine_config,
token=self._token,
)
with open(transferred_file, "r") as sm_xml:
sm_data = xmltodict.parse(sm_xml.read())
# This bit gets SearchMap location on Atlas
sm_pixel_size = float(
sm_data["MicroscopeImage"]["SpatialScale"]["pixelSize"]["x"][
"numericValue"
]
)
stage_position = sm_data["MicroscopeImage"]["microscopeData"]["stage"][
"Position"
]
sm_binning = float(
sm_data["MicroscopeImage"]["microscopeData"]["acquisition"]["camera"][
"Binning"
]["a:x"]
)
# Get the stage transformation
sm_transformations = sm_data["MicroscopeImage"]["CustomData"][
"a:KeyValueOfstringanyType"
]
stage_matrix: dict[str, float] = {}
image_matrix: dict[str, float] = {}
for key_val in sm_transformations:
if key_val["a:Key"] == "ReferenceCorrectionForStage":
stage_matrix = {
"m11": float(key_val["a:Value"]["b:_m11"]),
"m12": float(key_val["a:Value"]["b:_m12"]),
"m21": float(key_val["a:Value"]["b:_m21"]),
"m22": float(key_val["a:Value"]["b:_m22"]),
}
elif key_val["a:Key"] == "ReferenceCorrectionForImageShift":
image_matrix = {
"m11": float(key_val["a:Value"]["b:_m11"]),
"m12": float(key_val["a:Value"]["b:_m12"]),
"m21": float(key_val["a:Value"]["b:_m21"]),
"m22": float(key_val["a:Value"]["b:_m22"]),
}
if not stage_matrix or not image_matrix:
logger.error(
f"No stage or image shift matrix found for {transferred_file}"
)
ref_matrix = {
"m11": float(
sm_data["MicroscopeImage"]["ReferenceTransformation"]["matrix"][
"a:_m11"
]
),
"m12": float(
sm_data["MicroscopeImage"]["ReferenceTransformation"]["matrix"][
"a:_m12"
]
),
"m21": float(
sm_data["MicroscopeImage"]["ReferenceTransformation"]["matrix"][
"a:_m21"
]
),
"m22": float(
sm_data["MicroscopeImage"]["ReferenceTransformation"]["matrix"][
"a:_m22"
]
),
}
source = _get_source(transferred_file, environment=environment)
image_path = (
_file_transferred_to(
environment,
source,
transferred_file.parent / "SearchMap.jpg",
Path(self._machine_config.get("rsync_basepath", "")),
)
if source
else ""
)
capture_post(
base_url=str(environment.url.geturl()),
router_name="session_control.tomo_router",
function_name="register_search_map",
token=self._token,
instrument_name=environment.instrument_name,
session_id=environment.murfey_session,
sm_name=transferred_file.parent.name,
data={
"tag": dcg_tag,
"x_stage_position": float(stage_position["X"]),
"y_stage_position": float(stage_position["Y"]),
"pixel_size": sm_pixel_size,
"image": str(image_path),
"binning": sm_binning,
"reference_matrix": ref_matrix,
"stage_correction": stage_matrix,
"image_shift_correction": image_matrix,
},
)
elif transferred_file.name == "SearchMap.dm":
logger.info("Tomography session search map dm found")
dcg_tag = ensure_dcg_exists(
collection_type="tomo",
metadata_source=metadata_source,
environment=environment,
machine_config=self._machine_config,
token=self._token,
)
with open(transferred_file, "r") as sm_xml:
sm_data = xmltodict.parse(sm_xml.read())
# This bit gets SearchMap size
try:
sm_width = int(sm_data["TileSetXml"]["ImageSize"]["a:width"])
sm_height = int(sm_data["TileSetXml"]["ImageSize"]["a:height"])
except KeyError:
logger.warning(f"Unable to find size for SearchMap {transferred_file}")
readout_width = int(
sm_data["TileSetXml"]["AcquisitionSettings"]["a:camera"][
"a:ReadoutArea"
]["b:width"]
)
readout_height = int(
sm_data["TileSetXml"]["AcquisitionSettings"]["a:camera"][
"a:ReadoutArea"
]["b:height"]
)
sm_width = int(
8005 * readout_width / max(readout_height, readout_width)
)
sm_height = int(
8005 * readout_height / max(readout_height, readout_width)
)
logger.warning(
f"Inserting incorrect width {sm_width}, height {sm_height} for SearchMap display"
)
capture_post(
base_url=str(environment.url.geturl()),
router_name="session_control.tomo_router",
function_name="register_search_map",
token=self._token,
instrument_name=environment.instrument_name,
session_id=environment.murfey_session,
sm_name=transferred_file.parent.name,
data={
"tag": dcg_tag,
"height": sm_height,
"width": sm_width,
},
)
elif transferred_file.name == "BatchPositionsList.xml":
logger.info("Tomography session batch positions list found")
shutil.copy(
transferred_file,
transferred_file.parent
/ f"{transferred_file.stem}-{datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}{transferred_file.suffix}",
)
dcg_tag = ensure_dcg_exists(
collection_type="tomo",
metadata_source=metadata_source,
environment=environment,
machine_config=self._machine_config,
token=self._token,
)
with open(transferred_file) as xml:
for_parsing = xml.read()
batch_xml = xmltodict.parse(for_parsing)
batch_positions_from_xml = batch_xml["BatchPositionsList"]["BatchPositions"]
if not batch_positions_from_xml:
logger.info("No batch positions yet")
return
batch_positions_list = batch_positions_from_xml["BatchPositionParameters"]
if isinstance(batch_positions_list, dict):
# Case of a single batch
batch_positions_list = [batch_positions_list]
for batch_position in batch_positions_list:
batch_name = batch_position["Name"]
search_map_name = batch_position["PositionOnTileSet"]["TileSetName"]
batch_stage_location_x = float(
batch_position["PositionOnTileSet"]["StagePositionX"]
)
batch_stage_location_y = float(
batch_position["PositionOnTileSet"]["StagePositionY"]
)
# Always need search map before batch position
capture_post(
base_url=str(environment.url.geturl()),
router_name="session_control.tomo_router",
function_name="register_search_map",
token=self._token,
instrument_name=environment.instrument_name,
session_id=environment.murfey_session,
sm_name=search_map_name,
data={
"tag": dcg_tag,
},
)
# Then register batch position
capture_post(
base_url=str(environment.url.geturl()),
router_name="session_control.tomo_router",
function_name="register_batch_position",
token=self._token,
instrument_name=environment.instrument_name,
session_id=environment.murfey_session,
batch_name=batch_name,
data={
"tag": dcg_tag,
"x_stage_position": batch_stage_location_x,
"y_stage_position": batch_stage_location_y,
"x_beamshift": 0,
"y_beamshift": 0,
"search_map_name": search_map_name,
},
)
# Beamshifts
if batch_position.get("AdditionalExposureTemplateAreas"):
beamshifts = batch_position["AdditionalExposureTemplateAreas"][
"ExposureTemplateAreaParameters"
]
if type(beamshifts) is dict:
beamshifts = [beamshifts]
for beamshift in beamshifts:
beamshift_name = beamshift["Name"]
beamshift_position_x = float(beamshift["PositionX"])
beamshift_position_y = float(beamshift["PositionY"])
# Registration of beamshifted position
capture_post(
base_url=str(environment.url.geturl()),
router_name="session_control.tomo_router",
function_name="register_batch_position",
token=self._token,
instrument_name=environment.instrument_name,
session_id=environment.murfey_session,
batch_name=beamshift_name,
data={
"tag": dcg_tag,
"x_stage_position": batch_stage_location_x,
"y_stage_position": batch_stage_location_y,
"x_beamshift": beamshift_position_x,
"y_beamshift": beamshift_position_y,
"search_map_name": search_map_name,
},
)