-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfib.py
More file actions
337 lines (305 loc) · 12.9 KB
/
fib.py
File metadata and controls
337 lines (305 loc) · 12.9 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
328
329
330
331
332
333
334
335
336
337
from __future__ import annotations
import logging
import re
import threading
from datetime import datetime
from pathlib import Path
from typing import NamedTuple
from xml.etree import ElementTree as ET
import xmltodict
from murfey.client.context import Context
from murfey.client.instance_environment import MurfeyInstanceEnvironment
from murfey.util.client import capture_post
logger = logging.getLogger("murfey.client.contexts.fib")
lock = threading.Lock()
class Lamella(NamedTuple):
name: str
number: int
angle: float | None = None
class MillingProgress(NamedTuple):
file: Path
timestamp: float
class ElectronSnapshotMetadata(NamedTuple):
slot_num: int | None # Which slot in the FIB-SEM it is from
image_num: int
image_dir: str # Partial path from EMproject.emxml parent to the image
status: str
x_len: float | None
y_len: float | None
z_len: float | None
x_center: float | None
y_center: float | None
z_center: float | None
extent: tuple[float, float, float, float] | None
rotation_angle: float | None
def _number_from_name(name: str) -> int:
"""
In the AutoTEM and Maps workflows for the FIB, the sites and images are
auto-incremented with parenthesised numbers (e.g. "Lamella (2)"), with
the first site/image typically not having a number.
This function extracts the number from the file name, and returns 1 if
no such number is found.
"""
return (
int(match.group(1))
if (match := re.search(r"^[\w\s]+\((\d+)\)$", name)) is not None
else 1
)
def _get_source(file_path: Path, environment: MurfeyInstanceEnvironment) -> Path | None:
"""
Returns the Path of the file on the client PC.
"""
for s in environment.sources:
if file_path.is_relative_to(s):
return s
return None
def _file_transferred_to(
environment: MurfeyInstanceEnvironment,
source: Path,
file_path: Path,
rsync_basepath: Path,
) -> Path | None:
"""
Returns the Path of the transferred file on the DLS file system.
"""
# Construct destination path
base_destination = rsync_basepath / Path(environment.default_destinations[source])
# Add visit number to the path if it's not present in default destination
if environment.visit not in environment.default_destinations[source]:
base_destination = base_destination / environment.visit
destination = base_destination / file_path.relative_to(source)
return destination
def _parse_electron_snapshot_metadata(xml_file: Path):
metadata_dict = {}
root = ET.parse(xml_file).getroot()
datasets = root.findall(".//Datasets/Dataset")
for dataset in datasets:
# Extract all string-based values
name, image_dir, status = [
node.text
if ((node := dataset.find(node_path)) is not None and node.text is not None)
else ""
for node_path in (
".//Name",
".//FinalImages",
".//Status",
)
]
# Extract all float values
cx, cy, cz, x_len, y_len, z_len, rotation_angle = [
float(node.text)
if ((node := dataset.find(node_path)) is not None and node.text is not None)
else None
for node_path in (
".//BoxCenter/CenterX",
".//BoxCenter/CenterY",
".//BoxCenter/CenterZ",
".//BoxSize/SizeX",
".//BoxSize/SizeY",
".//BoxSize/SizeZ",
".//RotationAngle",
)
]
# Calculate the extent of the image
extent = None
if (
cx is not None
and cy is not None
and x_len is not None
and y_len is not None
):
extent = (
x_len - (cx / 2),
x_len + (cx / 2),
y_len - (cy / 2),
y_len - (cy / 2),
)
# Append metadata for current site to dict
metadata_dict[name] = ElectronSnapshotMetadata(
slot_num=None if cx is None else (1 if cx < 0 else 2),
image_num=_number_from_name(name),
status=status,
image_dir=image_dir,
x_len=x_len,
y_len=y_len,
z_len=z_len,
x_center=cx,
y_center=cy,
z_center=cz,
extent=extent,
rotation_angle=rotation_angle,
)
return metadata_dict
class FIBContext(Context):
def __init__(
self,
acquisition_software: str,
basepath: Path,
machine_config: dict,
token: str,
):
super().__init__("FIB", acquisition_software, token)
self._basepath = basepath
self._machine_config = machine_config
self._milling: dict[int, list[MillingProgress]] = {}
self._lamellae: dict[int, Lamella] = {}
self._electron_snapshots: dict[str, Path] = {}
self._electron_snapshot_metadata: dict[str, ElectronSnapshotMetadata] = {}
self._electron_snapshots_submitted: set[str] = set()
def post_transfer(
self,
transferred_file: Path,
environment: MurfeyInstanceEnvironment | None = None,
**kwargs,
):
super().post_transfer(transferred_file, environment=environment, **kwargs)
if environment is None:
logger.warning("No environment passed in")
return
# -----------------------------------------------------------------------------
# AutoTEM
# -----------------------------------------------------------------------------
if self._acquisition_software == "autotem":
parts = transferred_file.parts
if "DCImages" in parts and transferred_file.suffix == ".png":
lamella_name = parts[parts.index("Sites") + 1]
lamella_number = _number_from_name(lamella_name)
time_from_name = transferred_file.name.split("-")[:6]
timestamp = datetime.timestamp(
datetime(
year=int(time_from_name[0]),
month=int(time_from_name[1]),
day=int(time_from_name[2]),
hour=int(time_from_name[3]),
minute=int(time_from_name[4]),
second=int(time_from_name[5]),
)
)
if not self._lamellae.get(lamella_number):
self._lamellae[lamella_number] = Lamella(
name=lamella_name,
number=lamella_number,
)
if not self._milling.get(lamella_number):
self._milling[lamella_number] = [
MillingProgress(
timestamp=timestamp,
file=transferred_file,
)
]
else:
self._milling[lamella_number].append(
MillingProgress(
timestamp=timestamp,
file=transferred_file,
)
)
gif_list = [
l.file
for l in sorted(
self._milling[lamella_number], key=lambda x: x.timestamp
)
]
if environment:
raw_directory = Path(
environment.default_destinations[self._basepath]
).name
# post gif list to gif making API call
capture_post(
base_url=str(environment.url.geturl()),
router_name="workflow.correlative_router",
function_name="make_gif",
token=self._token,
instrument_name=environment.instrument_name,
year=datetime.now().year,
visit_name=environment.visit,
session_id=environment.murfey_session,
data={
"lamella_number": lamella_number,
"images": gif_list,
"raw_directory": raw_directory,
},
)
elif transferred_file.name == "ProjectData.dat":
with open(transferred_file, "r") as dat:
try:
for_parsing = dat.read()
except Exception:
logger.warning(f"Failed to parse file {transferred_file}")
return
metadata = xmltodict.parse(for_parsing)
sites = metadata["AutoTEM"]["Project"]["Sites"]["Site"]
for site in sites:
number = _number_from_name(site["Name"])
milling_angle = site["Workflow"]["Recipe"][0]["Activites"][
"MillingAngleActivity"
].get("MillingAngle")
if self._lamellae.get(number) and milling_angle:
self._lamellae[number]._replace(
angle=float(milling_angle.split(" ")[0])
)
# -----------------------------------------------------------------------------
# Maps
# -----------------------------------------------------------------------------
elif self._acquisition_software == "maps":
# Electron snapshot metadata file
if transferred_file.name == "EMproject.emxml":
# Extract all "Electron Snapshot" metadata and store it
self._electron_snapshot_metadata = _parse_electron_snapshot_metadata(
transferred_file
)
# If dataset hasn't been transferred, register it
for dataset_name in list(self._electron_snapshot_metadata.keys()):
if dataset_name not in self._electron_snapshots_submitted:
if dataset_name in self._electron_snapshots:
logger.info(f"Registering {dataset_name!r}")
## Workflow to trigger goes here
# Clear old entry after triggering workflow
self._electron_snapshots_submitted.add(dataset_name)
with lock:
self._electron_snapshots.pop(dataset_name, None)
self._electron_snapshot_metadata.pop(dataset_name, None)
else:
logger.debug(f"Waiting for image for {dataset_name}")
# Electron snapshot image
elif (
"Electron Snapshot" in transferred_file.name
and transferred_file.suffix in (".tif", ".tiff")
):
# Store file in Context memory
dataset_name = transferred_file.stem
if not (source := _get_source(transferred_file, environment)):
logger.warning(f"No source found for file {transferred_file}")
return
if not (
destination_file := _file_transferred_to(
environment=environment,
source=source,
file_path=transferred_file,
rsync_basepath=Path(
self._machine_config.get("rsync_basepath", "")
),
)
):
logger.warning(
f"File {transferred_file.name!r} not found on storage system"
)
return
self._electron_snapshots[dataset_name] = destination_file
if dataset_name not in self._electron_snapshots_submitted:
# If the metadata and image are both present, register dataset
if dataset_name in list(self._electron_snapshot_metadata.keys()):
logger.info(f"Registering {dataset_name!r}")
## Workflow to trigger goes here
# Clear old entry after triggering workflow
self._electron_snapshots_submitted.add(dataset_name)
with lock:
self._electron_snapshots.pop(dataset_name, None)
self._electron_snapshot_metadata.pop(dataset_name, None)
else:
logger.debug(f"Waiting for metadata for {dataset_name}")
# -----------------------------------------------------------------------------
# Meteor
# -----------------------------------------------------------------------------
elif self._acquisition_software == "meteor":
pass