Skip to content

Commit 1010f5e

Browse files
authored
Analyser optimisations (#781)
* Updated Context names and removed deprecated logic for parsing XML metadata from the TomographyContext * Separate the bulk of the '_analyse' logic from the while loop it is in * Added brackets around the 'or' clauses in the 'if self._limited' block of logic * Disentangled the processing logic for the 4 SPA- and Tomography-related contexts * Replaced matching Contexts using 'str(self._context)' with 'self._context.name' * Replaced giant if-else blocks with match-case logic * Updated Analyser tests after rewrite
1 parent 0187914 commit 1010f5e

12 files changed

Lines changed: 545 additions & 317 deletions

File tree

src/murfey/client/analyser.py

Lines changed: 135 additions & 177 deletions
Large diffs are not rendered by default.

src/murfey/client/context.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
from importlib.metadata import entry_points
55
from pathlib import Path
6-
from typing import Any, List, NamedTuple
6+
from typing import Any, NamedTuple, OrderedDict
77

88
import xmltodict
99

@@ -209,12 +209,6 @@ def ensure_dcg_exists(
209209
return dcg_tag
210210

211211

212-
class ProcessingParameter(NamedTuple):
213-
name: str
214-
label: str
215-
default: Any = None
216-
217-
218212
def detect_acquisition_software(dir_for_transfer: Path) -> str:
219213
glob = dir_for_transfer.glob("*")
220214
for f in glob:
@@ -225,9 +219,15 @@ def detect_acquisition_software(dir_for_transfer: Path) -> str:
225219
return ""
226220

227221

222+
class ProcessingParameter(NamedTuple):
223+
name: str
224+
label: str
225+
default: Any = None
226+
227+
228228
class Context:
229-
user_params: List[ProcessingParameter] = []
230-
metadata_params: List[ProcessingParameter] = []
229+
user_params: list[ProcessingParameter] = []
230+
metadata_params: list[ProcessingParameter] = []
231231

232232
def __init__(self, name: str, acquisition_software: str, token: str):
233233
self._acquisition_software = acquisition_software
@@ -256,7 +256,7 @@ def post_first_transfer(
256256

257257
def gather_metadata(
258258
self, metadata_file: Path, environment: MurfeyInstanceEnvironment | None = None
259-
):
259+
) -> OrderedDict | None:
260260
raise NotImplementedError(
261261
f"gather_metadata must be declared in derived class to be used: {self}"
262262
)

src/murfey/client/contexts/atlas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def __init__(
2020
machine_config: dict,
2121
token: str,
2222
):
23-
super().__init__("Atlas", acquisition_software, token)
23+
super().__init__("AtlasContext", acquisition_software, token)
2424
self._basepath = basepath
2525
self._machine_config = machine_config
2626

src/murfey/client/contexts/clem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def __init__(
8787
machine_config: dict,
8888
token: str,
8989
):
90-
super().__init__("CLEM", acquisition_software, token)
90+
super().__init__("CLEMContext", acquisition_software, token)
9191
self._basepath = basepath
9292
self._machine_config = machine_config
9393
# CLEM contexts for "auto-save" acquisition mode

src/murfey/client/contexts/fib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def __init__(
8181
machine_config: dict,
8282
token: str,
8383
):
84-
super().__init__("FIB", acquisition_software, token)
84+
super().__init__("FIBContext", acquisition_software, token)
8585
self._basepath = basepath
8686
self._machine_config = machine_config
8787
self._milling: dict[int, list[MillingProgress]] = {}
@@ -189,7 +189,7 @@ def post_transfer(
189189
sites = metadata["AutoTEM"]["Project"]["Sites"]["Site"]
190190
for site in sites:
191191
number = _number_from_name(site["Name"])
192-
milling_angle = site["Workflow"]["Recipe"][0]["Activites"][
192+
milling_angle = site["Workflow"]["Recipe"][0]["Activities"][
193193
"MillingAngleActivity"
194194
].get("MillingAngle")
195195
if self._lamellae.get(number) and milling_angle:

src/murfey/client/contexts/spa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def __init__(
8282
machine_config: dict,
8383
token: str,
8484
):
85-
super().__init__("SPA", acquisition_software, token)
85+
super().__init__("SPAContext", acquisition_software, token)
8686
self._basepath = basepath
8787
self._machine_config = machine_config
8888
self._processing_job_stash: dict = {}

src/murfey/client/contexts/spa_metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def __init__(
8181
machine_config: dict,
8282
token: str,
8383
):
84-
super().__init__("SPA_metadata", acquisition_software, token)
84+
super().__init__("SPAMetadataContext", acquisition_software, token)
8585
self._basepath = basepath
8686
self._machine_config = machine_config
8787

src/murfey/client/contexts/sxt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(
2828
machine_config: dict,
2929
token: str,
3030
):
31-
super().__init__("SXT", acquisition_software, token)
31+
super().__init__("SXTContext", acquisition_software, token)
3232
self._basepath = basepath
3333
self._machine_config = machine_config
3434

src/murfey/client/contexts/tomo.py

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
from threading import RLock
66
from typing import Callable, Dict, List, OrderedDict
77

8-
import xmltodict
9-
108
import murfey.util.eer
119
from murfey.client.context import Context, ProcessingParameter, ensure_dcg_exists
1210
from murfey.client.instance_environment import (
@@ -84,7 +82,7 @@ def __init__(
8482
machine_config: dict,
8583
token: str,
8684
):
87-
super().__init__("Tomography", acquisition_software, token)
85+
super().__init__("TomographyContext", acquisition_software, token)
8886
self._basepath = basepath
8987
self._machine_config = machine_config
9088
self._tilt_series: Dict[str, List[Path]] = {}
@@ -550,46 +548,14 @@ def post_first_transfer(
550548
def gather_metadata(
551549
self, metadata_file: Path, environment: MurfeyInstanceEnvironment | None = None
552550
) -> OrderedDict:
553-
if metadata_file.suffix not in (".mdoc", ".xml"):
551+
if metadata_file.suffix != ".mdoc":
554552
raise ValueError(
555553
f"Tomography gather_metadata method expected xml or mdoc file not {metadata_file.name}"
556554
)
557555
try:
558556
if not metadata_file.is_file():
559557
logger.debug(f"Metadata file {metadata_file} not found")
560558
return OrderedDict({})
561-
if metadata_file.suffix == ".xml":
562-
with open(metadata_file, "r") as xml:
563-
try:
564-
for_parsing = xml.read()
565-
except Exception:
566-
logger.warning(
567-
f"Failed to parse file {metadata_file}", exc_info=True
568-
)
569-
return OrderedDict({})
570-
data = xmltodict.parse(for_parsing)
571-
try:
572-
metadata: OrderedDict = OrderedDict({})
573-
metadata["experiment_type"] = "tomography"
574-
metadata["voltage"] = 300
575-
metadata["image_size_x"] = data["Acquisition"]["Info"]["ImageSize"][
576-
"Width"
577-
]
578-
metadata["image_size_y"] = data["Acquisition"]["Info"]["ImageSize"][
579-
"Height"
580-
]
581-
metadata["pixel_size_on_image"] = float(
582-
data["Acquisition"]["Info"]["SensorPixelSize"]["Height"]
583-
)
584-
metadata["motion_corr_binning"] = 1
585-
metadata["gain_ref"] = None
586-
metadata["dose_per_frame"] = (
587-
environment.dose_per_frame if environment else None
588-
)
589-
metadata["source"] = str(self._basepath)
590-
except KeyError:
591-
return OrderedDict({})
592-
return metadata
593559
with open(metadata_file, "r") as md:
594560
mdoc_data = get_global_data(md)
595561
num_blocks = get_num_blocks(md)

src/murfey/client/contexts/tomo_metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def __init__(
2626
machine_config: dict,
2727
token: str,
2828
):
29-
super().__init__("Tomography_metadata", acquisition_software, token)
29+
super().__init__("TomographyMetadataContext", acquisition_software, token)
3030
self._basepath = basepath
3131
self._machine_config = machine_config
3232

0 commit comments

Comments
 (0)