Skip to content

Commit

Permalink
Reading legacy FEI Helios NanoLab metadata correctly, mapping remains
Browse files Browse the repository at this point in the history
  • Loading branch information
atomprobe-tc committed Nov 3, 2024
1 parent b22f7b7 commit b13f917
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 43 deletions.
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
//"tests/data/image_zeiss/A-alloy800-0506_BSD.tif",
//"tests/data/image_zeiss/A-alloy800-05_grain_boundary22_InLens.tif",
//"tests/data/image_zeiss/A-alloy800-05GrainBoundary19_SE.tif",
"tests/data/image_fei_legacy/BF_02_40kx.tif",
//"tests/data/image_fei_legacy/BF_02_40kx.tif",
"tests/data/image_fei_legacy/SEM_Image_-_SliceImage_-_109.tif",
"--reader",
"em",
"--nxdl",
Expand Down
90 changes: 49 additions & 41 deletions src/pynxtools_em/parsers/image_tiff_fei_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,52 +81,60 @@ def check_if_tiff_fei_legacy(self):
# XML content suggestive of e.g. FEI Tecnai instruments
pos_s = s.find(bytes(f"<Root>", "utf8"))
pos_e = s.find(bytes(f"</Root>", "utf8"))
if pos_s != -1 and pos_e != -1:
if pos_e > pos_s:
pos_e += len(f"</Root>")
s.seek(pos_s)
# root = ET.fromstring(value)
tmp = flatten_xml_to_dict(
xmltodict.parse(s.read(pos_e - pos_s))
)
for key in tmp:
if key.endswith("Label"):
prefix = key[: len(key) - 5]
if all(
f"{prefix}{suffix}" in tmp
for suffix in ["Label", "Value", "Unit"]
):
try:
if -1 < pos_s < pos_e:
pos_e += len(f"</Root>")
s.seek(pos_s)
# root = ET.fromstring(value)
tmp = flatten_xml_to_dict(xmltodict.parse(s.read(pos_e - pos_s)))
for key in tmp:
if key.endswith("Label"):
prefix = key[: len(key) - 5]
if all(
f"{prefix}{suffix}" in tmp
for suffix in ["Label", "Value", "Unit"]
):
try:
self.flat_dict_meta[tmp[f"{prefix}Label"]] = (
ureg.Quantity(
f"""{tmp[f"{prefix}Value"]} {tmp[f"{prefix}Unit"]}"""
)
)
except UndefinedUnitError:
if tmp[f"{prefix}Value"] is not None:
self.flat_dict_meta[tmp[f"{prefix}Label"]] = (
ureg.Quantity(
f"""{tmp[f"{prefix}Value"]} {tmp[f"{prefix}Unit"]}"""
)
string_to_number(tmp[f"{prefix}Value"])
)
except UndefinedUnitError:
if tmp[f"{prefix}Value"] is not None:
self.flat_dict_meta[
tmp[f"{prefix}Label"]
] = string_to_number(tmp[f"{prefix}Value"])
if "Microscope" in self.flat_dict_meta:
if "Tecnai" in self.flat_dict_meta["Microscope"]:
for key, val in self.flat_dict_meta.items():
print(f"{key}, {val}, {type(val)}")
self.supported = FEI_LEGACY_TECNAI_TEM
return
if "Microscope" in self.flat_dict_meta:
if "Tecnai" in self.flat_dict_meta["Microscope"]:
for key, val in self.flat_dict_meta.items():
print(f"{key}, {val}, {type(val)}")
self.supported = FEI_LEGACY_TECNAI_TEM
return

pos_s = s.find(bytes(f"<Metadata", "utf8"))
pos_e = s.find(bytes(f"</Metadata>", "utf8"))
if pos_s != -1 and pos_e != -1:
if pos_e > pos_s:
pos_e += len(f"</Metadata>")
s.seek(0)
tmp = flatten_xml_to_dict(
xmltodict.parse(s.read(pos_e - pos_s))
)
# TODO::Implement mapping for FEI_LEGACY_HELIOS_SEM
for key, val in tmp.items():
print(f"{key}, {val}")
self.supported = FEI_LEGACY_UNKNOWN # FEI_LEGACY_HELIOS_SEM
if -1 < pos_s < pos_e:
pos_e += len(f"</Metadata>")
s.seek(pos_s)
tmp = flatten_xml_to_dict(xmltodict.parse(s.read(pos_e - pos_s)))
# TODO::Implement mapping for FEI_LEGACY_HELIOS_SEM
for key, val in tmp.items():
self.flat_dict_meta[key] = string_to_number(val)
if all(
val in self.flat_dict_meta
for val in [
"Metadata.Instrument.ControlSoftwareVersion",
"Metadata.Instrument.Manufacturer",
"Metadata.Instrument.InstrumentClass",
]
):
if self.flat_dict_meta[
"Metadata.Instrument.Manufacturer"
].startswith("FEI") and self.flat_dict_meta[
"Metadata.Instrument.InstrumentClass"
].startswith("Helios NanoLab"):
self.supported = FEI_LEGACY_HELIOS_SEM
return

except (FileNotFoundError, IOError):
print(f"{self.file_path} either FileNotFound or IOError !")
Expand Down
2 changes: 1 addition & 1 deletion src/pynxtools_em/parsers/image_tiff_tfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def get_metadata(self):
for term in get_fei_childs(parent):
s.seek(pos_s, 0)
pos = s.find(bytes(f"{term}=", "utf8"))
if pos < pos_e: # check if pos_e is None
if -1 < pos < pos_e: # check if pos_e is None
s.seek(pos, 0)
value = f"{s.readline().strip().decode('utf8').replace(f'{term}=', '')}"
self.flat_dict_meta[f"{parent}/{term}"] = None
Expand Down

0 comments on commit b13f917

Please sign in to comment.