diff --git a/poetry.lock b/poetry.lock index 833588f..bcf0955 100644 --- a/poetry.lock +++ b/poetry.lock @@ -105,13 +105,13 @@ test = ["pytest (>=6)"] [[package]] name = "imcf-fiji-mocks" -version = "0.10.0" +version = "0.11.0" description = "Mocks collection for Fiji-Python. Zero functional code." optional = false python-versions = ">=2.7" files = [ - {file = "imcf_fiji_mocks-0.10.0-py2.py3-none-any.whl", hash = "sha256:476927d82fa0e93b0b0b738f82cab60e180cf0da5b3dd09dc6a5336b08e18d2d"}, - {file = "imcf_fiji_mocks-0.10.0.tar.gz", hash = "sha256:d1f3302031cad5f1d15388bf337025bbfb59037a04e79a102de59093e643a5f5"}, + {file = "imcf_fiji_mocks-0.11.0-py2.py3-none-any.whl", hash = "sha256:8695be371eb8b811e63996af04f76588e97f35c46eb5fa6f8cee0237f949b984"}, + {file = "imcf_fiji_mocks-0.11.0.tar.gz", hash = "sha256:d7f0fb592f72afc55e1ac3ea82f6a4f36e22482412e54a21fd1d91db4392bb47"}, ] [[package]] @@ -271,4 +271,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = ">=3.10" -content-hash = "6b4a1828157bbddc15f61de0427a3d7970a61da1750f2cbf9e160f1b7546d7c9" +content-hash = "53a8a3d96664d128652b6a94163691dc764dd114f25e3daf373a4baf41ce3799" diff --git a/pyproject.toml b/pyproject.toml index c722202..d8c9839 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/src/imcflibs/imagej/bdv.py b/src/imcflibs/imagej/bdv.py index 5cc7b41..2bf48df 100644 --- a/src/imcflibs/imagej/bdv.py +++ b/src/imcflibs/imagej/bdv.py @@ -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 @@ -1648,3 +1652,79 @@ def fuse_dataset_bdvp( "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() + # 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)) + + # Initialize default values + nbr_chnl = 1 + nbr_ill = 1 + nbr_tp = 1 + + try: + # 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.error("Error extracting metadata from XML: {0}".format(str(e))) + + xml_metadata = { + "channels_count": nbr_chnl, + "illuminations_count": nbr_ill, + "timepoints_count": nbr_tp, + } + + return xml_metadata \ No newline at end of file