Skip to content

Commit 4df59a2

Browse files
committed
fix: handle missing keys and alternate time formats in DM extractor
Fixes KeyError when DM3/DM4 files lack a 'Name' key in ImageTags. Fixes time parsing for 24-hour format timestamps. Fixes KeyError in utils._set_eels_processing() for TagGroups (e.g. "Summing") that don't have an "Operation" key.
1 parent f96d29b commit 4df59a2

3 files changed

Lines changed: 20 additions & 6 deletions

File tree

docs/changes/98.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed `KeyError` exceptions in the DM3/DM4 extractor for files missing a `Name` key, 24-hour timestamps, and EELS TagGroups without an `Operation` key.

nexusLIMS/extractors/plugins/digital_micrograph.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def extract(
129129
return metadata_list
130130

131131

132-
def get_dm3_metadata(filename: Path, instrument=None):
132+
def get_dm3_metadata(filename: Path, instrument=None): # noqa: PLR0912
133133
"""
134134
Get metadata from a dm3 or dm4 file.
135135
@@ -232,8 +232,10 @@ def get_dm3_metadata(filename: Path, instrument=None):
232232
# tg_name should be 'TagGroup0', 'TagGroup1', etc.
233233
keys = tag.keys()
234234
# We want to keep 'ImageTags' and 'Name', so remove from list
235-
keys.remove("ImageTags")
236-
keys.remove("Name")
235+
# (not all dm3/dm4 files have a 'Name' key)
236+
for keep in ("ImageTags", "Name"):
237+
if keep in keys:
238+
keys.remove(keep)
237239
for k in keys:
238240
# k should be in ['ImageData', 'UniqueID']
239241
m_tree = remove_dtb_element( # noqa: PLW2901
@@ -917,7 +919,7 @@ def parse_dm3_eds_info(mdict):
917919
return mdict
918920

919921

920-
def parse_dm3_spectrum_image_info(mdict):
922+
def parse_dm3_spectrum_image_info(mdict): # noqa: PLR0912
921923
"""
922924
Parse "spectrum image" information from the metadata.
923925
@@ -1002,8 +1004,16 @@ def parse_dm3_spectrum_image_info(mdict):
10021004
start_val = try_getting_dict_value(mdict, [*base, "Acquisition", "Start time"])
10031005
end_val = try_getting_dict_value(mdict, [*base, "Acquisition", "End time"])
10041006
if start_val is not None and end_val is not None:
1005-
start_dt = dt.strptime(start_val, "%I:%M:%S %p").replace(tzinfo=UTC)
1006-
end_dt = dt.strptime(end_val, "%I:%M:%S %p").replace(tzinfo=UTC)
1007+
for fmt in ("%I:%M:%S %p", "%H:%M:%S"):
1008+
try:
1009+
start_dt = dt.strptime(start_val, fmt).replace(tzinfo=UTC)
1010+
end_dt = dt.strptime(end_val, fmt).replace(tzinfo=UTC)
1011+
break
1012+
except ValueError:
1013+
continue
1014+
else:
1015+
start_dt = end_dt = None
1016+
if start_val is not None and end_val is not None and start_dt is not None:
10071017
duration = (end_dt - start_dt).seconds # Calculate acquisition duration
10081018
with contextlib.suppress(ValueError, TypeError):
10091019
duration = ureg.Quantity(duration, "second")

nexusLIMS/extractors/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ def _set_eels_processing(mdict, pre_path):
154154
# v will be dictionaries specifying the process step
155155
# AlignSIByPeak, DataPicker, SpectrumCalibrate,
156156
# Compute Thickness, Background Removal, Signal Integration
157+
# Some TagGroups (e.g. Summing) don't have an Operation key
158+
if "Operation" not in v:
159+
continue
157160
operation = v["Operation"]
158161
param = v["Parameters"]
159162
if operation == "AlignSIByPeak":

0 commit comments

Comments
 (0)