1818)
1919from ij import IJ
2020
21+ from java .io import File , FileInputStream , InputStreamReader
22+ from javax .xml .parsers import DocumentBuilderFactory
23+ from org .xml .sax import InputSource
24+
2125from .. import pathtools
2226from ..log import LOG as log
2327
@@ -1648,3 +1652,79 @@ def fuse_dataset_bdvp(
16481652 "compress_temp_files" ,
16491653 False ,
16501654 )
1655+
1656+ def read_metadata_from_xml (xml_path ):
1657+ """Extract metadata from a Zeiss Lightsheet microscopy XML file.
1658+
1659+ Parses the XML document to retrieve the number of channels, illuminations,
1660+ and timepoints from the experiment metadata.
1661+
1662+ Parameters
1663+ ----------
1664+ xml_path : str
1665+ Path to the XML metadata file.
1666+
1667+ Returns
1668+ -------
1669+ dict
1670+ A dictionary containing the following keys:
1671+ - 'channels_count': Number of channels in the dataset
1672+ - 'illuminations_count': Number of illumination directions
1673+ - 'timepoints_count': Number of timepoints in the dataset
1674+
1675+ Examples
1676+ --------
1677+ >>> metadata = read_metadata_from_xml("/path/to/experiment.xml")
1678+ >>> print(metadata["channels_count"])
1679+ 2
1680+ >>> print(metadata["illuminations_count"])
1681+ 4
1682+ >>> print(metadata["timepoints_count"])
1683+ 1
1684+ """
1685+ # Use our robust XML parsing function
1686+ dbf = DocumentBuilderFactory .newInstance ()
1687+ db = dbf .newDocumentBuilder ()
1688+ # This is needed to fix some issues with `µm` in the xml file
1689+ reader = InputStreamReader (FileInputStream (File (xml_path )))
1690+ dom = db .parse (InputSource (reader ))
1691+
1692+ # Initialize default values
1693+ nbr_chnl = 1
1694+ nbr_ill = 1
1695+ nbr_tp = 1
1696+
1697+ try :
1698+ # Extract channel and illumination counts
1699+ nodeList = dom .getElementsByTagName ("Attributes" )
1700+ for i in range (nodeList .getLength ()):
1701+ name_attr = nodeList .item (i ).getAttributes ().getNamedItem ("name" )
1702+ if name_attr is None :
1703+ continue
1704+
1705+ node = name_attr .getNodeValue ()
1706+ if node == "channel" :
1707+ nbr_chnl = int (
1708+ nodeList .item (i ).getElementsByTagName ("Channel" ).getLength ()
1709+ )
1710+ if node == "illumination" :
1711+ nbr_ill = int (
1712+ nodeList .item (i ).getElementsByTagName ("Illumination" ).getLength ()
1713+ )
1714+
1715+ # Get timepoints
1716+ timepoints_node = dom .getElementsByTagName ("Timepoints" )
1717+ if timepoints_node .getLength () > 0 :
1718+ last_nodes = timepoints_node .item (0 ).getElementsByTagName ("last" )
1719+ if last_nodes .getLength () > 0 :
1720+ nbr_tp = int (last_nodes .item (0 ).getTextContent ()) + 1
1721+ except Exception as e :
1722+ log .error ("Error extracting metadata from XML: {0}" .format (str (e )))
1723+
1724+ xml_metadata = {
1725+ "channels_count" : nbr_chnl ,
1726+ "illuminations_count" : nbr_ill ,
1727+ "timepoints_count" : nbr_tp ,
1728+ }
1729+
1730+ return xml_metadata
0 commit comments