Skip to content

Commit 1a64a85

Browse files
committed
Added logic to try searching for XML metadata in other tag keys found in FIB atlas file if it's not found under the default key
1 parent 0d96dfe commit 1a64a85

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/murfey/workflows/fib/register_atlas.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,26 @@ def _parse_metadata(file: Path, visit_name: str):
9292
Parses through the atlas image's tags to extract the relevant metadata
9393
"""
9494

95-
# Metadata is stored in the TIFF file under tag number 34683
95+
# Search for the XML metadata in the tags (34683 is the default key)
9696
img = PIL.Image.open(file)
9797
tags = dict(img.tag_v2)
98-
xml_metadata = ET.fromstring(str(tags.get(34683)))
98+
xml_metadata = None
99+
if (
100+
isinstance((tag_contents := tags.get(34683)), str)
101+
and "xml version" in tag_contents
102+
):
103+
xml_metadata = ET.fromstring(tag_contents)
104+
else:
105+
logger.warning(
106+
"Could not find metadata under tag key 34683, iterating through tags"
107+
)
108+
for key, value in tags.items():
109+
if key == 34683: # Already inspected
110+
continue
111+
if isinstance(value, str) and "xml version" in value:
112+
xml_metadata = ET.fromstring(value)
113+
if xml_metadata is None:
114+
raise ValueError(f"Could not find required metadata in file {file}")
99115

100116
# Extract key values from metadata
101117
return FIBAtlasMetadata(

tests/workflows/fib/test_register_atlas.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ def create_electron_snapshot_metadata(
134134
"test_params",
135135
(
136136
(
137+
34683, # Tag key
137138
"Electron Snapshot",
138139
"some_project",
139140
2000, # Voltage
@@ -153,6 +154,7 @@ def create_electron_snapshot_metadata(
153154
1e-6, # Y
154155
),
155156
(
157+
34682, # Tag key
156158
"Electron Snapshot (2)",
157159
"another_project",
158160
2000, # Voltage
@@ -176,6 +178,7 @@ def create_electron_snapshot_metadata(
176178
def test_parse_metadata(
177179
mocker: MockerFixture,
178180
test_params: tuple[
181+
int,
179182
str,
180183
str,
181184
float,
@@ -198,6 +201,7 @@ def test_parse_metadata(
198201
):
199202
# Unpack test params
200203
(
204+
tag_key,
201205
image_name,
202206
project_name,
203207
voltage,
@@ -245,7 +249,9 @@ def test_parse_metadata(
245249
pixel_size_y,
246250
)
247251
mock_image = MagicMock()
248-
mock_image.tag_v2 = {34683: xml_string}
252+
tags = dict.fromkeys([*list(range(100)), *list(range(34600, 34700))], 0)
253+
tags[tag_key] = xml_string
254+
mock_image.tag_v2 = tags
249255
mocker.patch(
250256
"murfey.workflows.fib.register_atlas.PIL.Image.open",
251257
return_value=mock_image,

0 commit comments

Comments
 (0)