Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ ignore = [
]

[tool.ruff.lint.pydocstyle]
convention = "numpy"
convention = "numpy"
94 changes: 93 additions & 1 deletion src/imcflibs/imagej/bdv.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
FuseBigStitcherDatasetIntoOMETiffCommand,
)
from ij import IJ
from java.io import File, FileInputStream, InputStreamReader
from javax.xml.parsers import DocumentBuilderFactory
from org.xml.sax import InputSource

from .. import pathtools
from ..log import LOG as log


# internal template strings used in string formatting (note: the `"""@private"""`
# pseudo-decorator is there to instruct [pdoc] to omit those variables when generating
# API documentation):
Expand Down Expand Up @@ -1646,3 +1648,93 @@ def fuse_dataset_bdvp(
"compress_temp_files",
False,
)


def read_metadata_from_xml(xml_path):
"""Extract metadata from a Zeiss Lightsheet microscopy XML file.

Parse the XML document to retrieve the number of channels, illuminations,
and timepoints from the experiment metadata.

Parameters
----------
xml_path : str
Path to the XML metadata file.

Returns
-------
dict
A dictionary containing the following keys:
- 'channels_count': Number of channels in the dataset
- 'illuminations_count': Number of illumination directions
- 'timepoints_count': Number of timepoints in the dataset

Examples
--------
>>> metadata = read_metadata_from_xml("/path/to/experiment.xml")
>>> print(metadata["channels_count"])
... 2
>>> print(metadata["illuminations_count"])
... 4
>>> print(metadata["timepoints_count"])
... 1
"""
# Use our robust XML parsing function
dbf = DocumentBuilderFactory.newInstance()
db = dbf.newDocumentBuilder()

# Initialize default values
nbr_chnl = 1
nbr_ill = 1
nbr_tp = 1

reader = None
try:
# This is needed to fix some issues with the micron symbol in the xml file
reader = InputStreamReader(FileInputStream(File(xml_path)))
dom = db.parse(InputSource(reader))

# Extract channel and illumination counts
nodeList = dom.getElementsByTagName("Attributes")
for i in range(nodeList.getLength()):
name_attr = nodeList.item(i).getAttributes().getNamedItem("name")
if name_attr is None:
continue

node = name_attr.getNodeValue()
if node == "channel":
nbr_chnl = int(
nodeList.item(i).getElementsByTagName("Channel").getLength()
)
if node == "illumination":
nbr_ill = int(
nodeList.item(i).getElementsByTagName("Illumination").getLength()
)

# Get timepoints
timepoints_node = dom.getElementsByTagName("Timepoints")
if timepoints_node.getLength() > 0:
last_nodes = timepoints_node.item(0).getElementsByTagName("last")
if last_nodes.getLength() > 0:
nbr_tp = int(last_nodes.item(0).getTextContent()) + 1
except Exception as e:
# log.exception includes the traceback when available
try:
log.exception("Error extracting metadata from XML: %s", e)
except Exception:
log.error("Error extracting metadata from XML: %s", str(e))
finally:
# Ensure the Java reader is closed to free resources
try:
if reader is not None:
reader.close()
except Exception:
pass

xml_metadata = {
"channels_count": nbr_chnl,
"illuminations_count": nbr_ill,
"timepoints_count": nbr_tp,
}

return xml_metadata
Loading