Skip to content

Add method to read metadata back from the xml #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ version = "0.0.0"
# - or: python = ">=3.10"

[tool.poetry.dependencies]
imcf-fiji-mocks = ">=0.10.0"
imcf-fiji-mocks = ">=0.11.0"
python = ">=2.7"
python-micrometa = "^15.2.2"
sjlogging = ">=0.5.2"
Expand Down
80 changes: 80 additions & 0 deletions src/imcflibs/imagej/bdv.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
)
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

Expand Down Expand Up @@ -1648,3 +1652,79 @@
"compress_temp_files",
False,
)

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

Parses 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()

Check warning on line 1687 in src/imcflibs/imagej/bdv.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/bdv.py#L1686-L1687

Added lines #L1686 - L1687 were not covered by tests
# 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))

Check warning on line 1690 in src/imcflibs/imagej/bdv.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/bdv.py#L1689-L1690

Added lines #L1689 - L1690 were not covered by tests

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

Check warning on line 1695 in src/imcflibs/imagej/bdv.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/bdv.py#L1693-L1695

Added lines #L1693 - L1695 were not covered by tests

try:

Check warning on line 1697 in src/imcflibs/imagej/bdv.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/bdv.py#L1697

Added line #L1697 was not covered by tests
# 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

Check warning on line 1703 in src/imcflibs/imagej/bdv.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/bdv.py#L1699-L1703

Added lines #L1699 - L1703 were not covered by tests

node = name_attr.getNodeValue()
if node == "channel":
nbr_chnl = int(

Check warning on line 1707 in src/imcflibs/imagej/bdv.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/bdv.py#L1705-L1707

Added lines #L1705 - L1707 were not covered by tests
nodeList.item(i).getElementsByTagName("Channel").getLength()
)
if node == "illumination":
nbr_ill = int(

Check warning on line 1711 in src/imcflibs/imagej/bdv.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/bdv.py#L1710-L1711

Added lines #L1710 - L1711 were not covered by tests
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.error("Error extracting metadata from XML: {0}".format(str(e)))

Check warning on line 1722 in src/imcflibs/imagej/bdv.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/bdv.py#L1716-L1722

Added lines #L1716 - L1722 were not covered by tests

xml_metadata = {

Check warning on line 1724 in src/imcflibs/imagej/bdv.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/bdv.py#L1724

Added line #L1724 was not covered by tests
"channels_count": nbr_chnl,
"illuminations_count": nbr_ill,
"timepoints_count": nbr_tp,
}

return xml_metadata

Check warning on line 1730 in src/imcflibs/imagej/bdv.py

View check run for this annotation

Codecov / codecov/patch

src/imcflibs/imagej/bdv.py#L1730

Added line #L1730 was not covered by tests
Loading