Skip to content

Commit d89f921

Browse files
committed
✅ Add checks for channel names and photometric options.
1 parent 9b840a5 commit d89f921

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

tests/test_utils.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
18781893
def 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

19201948
def 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

19511983
def 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+
)

tiatoolbox/utils/misc.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ def imwrite_ome_tiff(
185185
photometric (str):
186186
Color space of image for tifffile. Default is "minisblack".
187187
*MINISBLACK*: for bilevel and grayscale images, 0 is black.
188-
*RGB*: the image contains red, green and blue samples.
189188
See tifffile for more options.
190189
191190
Raises:
@@ -217,12 +216,18 @@ def imwrite_ome_tiff(
217216
msg = "Input 'img' must have 3 (YXC) dimensions."
218217
raise ValueError(msg)
219218

219+
if photometric.lower() != "minisblack":
220+
msg = (
221+
f"photometric = {photometric} is not supported. "
222+
f"This function currently supports photometric = 'minisblack'."
223+
)
224+
raise ValueError(msg)
225+
220226
if not channels:
221227
channels = [f"Channel{c + 1}" for c in range(img.shape[2])]
222228

223229
ome_metadata = {
224230
"axes": "CYX",
225-
"SignificantBits": 8,
226231
"PhysicalSizeX": mpp[1],
227232
"PhysicalSizeXUnit": "µm",
228233
"PhysicalSizeY": mpp[0],

0 commit comments

Comments
 (0)