@@ -1875,6 +1875,21 @@ def get_ome_metadata(tiff_path: Path) -> str | None:
18751875 return None
18761876
18771877
1878+ def assert_channel_names_from_ome_metadata (
1879+ ome_metadata : str , expected_channel_names : list [str ]
1880+ ) -> None :
1881+ """Asserts channel naems from OME metadata."""
1882+ root = ET .fromstring (ome_metadata )
1883+ namespace = "{http://www.openmicroscopy.org/Schemas/OME/2016-06}"
1884+ channel_elements = root .findall (
1885+ f".//{ namespace } Image/{ namespace } Pixels/{ namespace } Channel"
1886+ )
1887+ channel_names = [
1888+ channel .get ("Name" ) for channel in channel_elements if channel .get ("Name" )
1889+ ]
1890+ assert channel_names == expected_channel_names
1891+
1892+
18781893def assert_ome_metadata_value (
18791894 ome_xml : ET .Element , tag : str , expected_value : str
18801895) -> None :
@@ -1916,6 +1931,19 @@ def test_imwrite_ome_tiff_errors(tmp_path: Path) -> None:
19161931 img = img ,
19171932 )
19181933
1934+ img = np .zeros (shape = (256 , 256 , 3 ))
1935+
1936+ # Input image must be a NumPy array or a Zarr array.
1937+ with pytest .raises (
1938+ ValueError ,
1939+ match = r".*This function currently supports photometric = 'minisblack'.*" ,
1940+ ):
1941+ misc .imwrite_ome_tiff (
1942+ image_path = tmp_path / "failed_test.tif" ,
1943+ img = img ,
1944+ photometric = "rgb" ,
1945+ )
1946+
19191947
19201948def test_save_numpy_array (tmp_path : Path , source_image : Path ) -> None :
19211949 """Tests saving a basic NumPy array."""
@@ -1927,7 +1955,7 @@ def test_save_numpy_array(tmp_path: Path, source_image: Path) -> None:
19271955 tile_size = (10 , 15 ),
19281956 channels = ["Red" , "Green" , "Blue" ],
19291957 mpp = (0.5 , 0.5 ),
1930- photometric = "rgb " ,
1958+ photometric = "minisblack " ,
19311959 )
19321960 assert image_path .is_file ()
19331961 saved_img = tifffile .imread (image_path )
@@ -1946,6 +1974,10 @@ def test_save_numpy_array(tmp_path: Path, source_image: Path) -> None:
19461974 assert_ome_metadata_value (ome_xml , "PhysicalSizeY" , "0.5" )
19471975 assert_ome_metadata_value (ome_xml , "PhysicalSizeXUnit" , "µm" )
19481976 assert_ome_metadata_value (ome_xml , "PhysicalSizeYUnit" , "µm" )
1977+ assert_channel_names_from_ome_metadata (
1978+ ome_metadata = get_ome_metadata (image_path ),
1979+ expected_channel_names = ["Red" , "Green" , "Blue" ],
1980+ )
19491981
19501982
19511983def test_save_zarr_array (tmp_path : Path , source_image : Path ) -> None :
@@ -1974,3 +2006,14 @@ def test_save_zarr_array(tmp_path: Path, source_image: Path) -> None:
19742006 assert_ome_metadata_value (ome_xml , "PhysicalSizeY" , "0.25" )
19752007 assert_ome_metadata_value (ome_xml , "PhysicalSizeXUnit" , "µm" )
19762008 assert_ome_metadata_value (ome_xml , "PhysicalSizeYUnit" , "µm" )
2009+ assert_channel_names_from_ome_metadata (
2010+ ome_metadata = get_ome_metadata (image_path ),
2011+ expected_channel_names = [
2012+ "Channel1" ,
2013+ "Channel2" ,
2014+ "Channel3" ,
2015+ "Channel4" ,
2016+ "Channel5" ,
2017+ "Channel6" ,
2018+ ],
2019+ )
0 commit comments