Skip to content

Commit 480025b

Browse files
committed
⚡ Use tile based approach for faster writing.
- Convert the architecture to save per class heatmaps.
1 parent d89f921 commit 480025b

File tree

2 files changed

+156
-177
lines changed

2 files changed

+156
-177
lines changed

tests/test_utils.py

Lines changed: 43 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,21 +1875,6 @@ 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-
18931878
def assert_ome_metadata_value(
18941879
ome_xml: ET.Element, tag: str, expected_value: str
18951880
) -> None:
@@ -1910,110 +1895,90 @@ def assert_ome_metadata_value(
19101895
pytest.fail(f"Attribute '{tag}' not found in OME metadata.")
19111896

19121897

1913-
def test_imwrite_ome_tiff_errors(tmp_path: Path) -> None:
1914-
"""Test expected errors in `imwrite_ome_tiff`."""
1915-
img = np.zeros(shape=(256, 256))
1898+
def test_iwrite_probability_heatmap_as_ome_tiff_errors(tmp_path: Path) -> None:
1899+
"""Test expected errors in `write_probability_heatmap_as_ome_tiff`."""
1900+
probability = np.zeros(shape=(256, 256, 3))
19161901

1917-
# Input image must have 3 (CYX) dimensions.
1918-
with pytest.raises(ValueError, match=r".*must have 3 \(YXC\).*"):
1919-
misc.imwrite_ome_tiff(
1902+
# Input image must have 2 (CY) dimensions.
1903+
with pytest.raises(ValueError, match=r".*must have 2 \(YX\).*"):
1904+
misc.write_probability_heatmap_as_ome_tiff(
19201905
image_path=tmp_path / "failed_test.tif",
1921-
img=img,
1906+
probability=probability,
19221907
)
19231908

1924-
img = np.zeros(shape=(256, 256, 3))
1925-
img = torch.from_numpy(img)
1909+
probability = np.zeros(shape=(256, 256, 3))
1910+
probability = torch.from_numpy(probability)
19261911

19271912
# Input image must be a NumPy array or a Zarr array.
19281913
with pytest.raises(TypeError, match=r".*must be a NumPy array or a Zarr.*"):
1929-
misc.imwrite_ome_tiff(
1914+
misc.write_probability_heatmap_as_ome_tiff(
19301915
image_path=tmp_path / "failed_test.tif",
1931-
img=img,
1916+
probability=probability,
19321917
)
19331918

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-
)
19461919

1947-
1948-
def test_save_numpy_array(tmp_path: Path, source_image: Path) -> None:
1920+
def test_save_numpy_array_proability_ome_tiff(
1921+
tmp_path: Path, source_image: Path
1922+
) -> None:
19491923
"""Tests saving a basic NumPy array."""
19501924
image_path = tmp_path / "numpy_image.ome.tif"
1951-
img = utils.imread(source_image)
1952-
misc.imwrite_ome_tiff(
1925+
probability = utils.imread(source_image)
1926+
probability_0 = probability[:, :, 0]
1927+
misc.write_probability_heatmap_as_ome_tiff(
19531928
image_path=image_path,
1954-
img=img,
1955-
tile_size=(10, 15),
1956-
channels=["Red", "Green", "Blue"],
1929+
probability=probability_0,
1930+
tile_size=(64, 64),
19571931
mpp=(0.5, 0.5),
1958-
photometric="minisblack",
1932+
levels=2,
1933+
colormap=cv2.COLORMAP_JET,
19591934
)
19601935
assert image_path.is_file()
19611936
saved_img = tifffile.imread(image_path)
1962-
saved_img = np.transpose(saved_img, (1, 2, 0))
1963-
assert np.all(saved_img == img)
1964-
assert img.shape == saved_img.shape
1965-
assert img.dtype == saved_img.dtype
1937+
assert probability.shape == saved_img.shape
1938+
assert probability.dtype == saved_img.dtype
19661939
ome_xml = ET.fromstring(get_ome_metadata(image_path))
19671940
assert ome_xml is not None
19681941

1969-
assert_ome_metadata_value(ome_xml, "SizeY", str(img.shape[0]))
1970-
assert_ome_metadata_value(ome_xml, "SizeX", str(img.shape[1]))
1971-
assert_ome_metadata_value(ome_xml, "SizeC", str(img.shape[2]))
1942+
assert_ome_metadata_value(ome_xml, "SizeY", str(probability.shape[0]))
1943+
assert_ome_metadata_value(ome_xml, "SizeX", str(probability.shape[1]))
1944+
assert_ome_metadata_value(ome_xml, "SizeC", str(3))
19721945
assert_ome_metadata_value(ome_xml, "DimensionOrder", "XYCZT")
19731946
assert_ome_metadata_value(ome_xml, "PhysicalSizeX", "0.5")
19741947
assert_ome_metadata_value(ome_xml, "PhysicalSizeY", "0.5")
19751948
assert_ome_metadata_value(ome_xml, "PhysicalSizeXUnit", "µm")
19761949
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-
)
19811950

19821951

1983-
def test_save_zarr_array(tmp_path: Path, source_image: Path) -> None:
1952+
def test_save_zarr_array_probability_ome_tiff(
1953+
tmp_path: Path, source_image: Path
1954+
) -> None:
19841955
"""Tests saving a Zarr array with uint8 dtype."""
19851956
image_path = tmp_path / "zarr_uint8_image.ome.tif"
19861957

19871958
img = utils.imread(source_image)
1988-
img = img[:, 0:200, :]
1989-
img = np.concatenate((img, img), axis=2)
1990-
img_zarr = zarr.zeros(shape=img.shape, dtype=np.uint8)
1991-
img_zarr[:] = img
1992-
1993-
misc.imwrite_ome_tiff(image_path, img_zarr, tile_size=(10, 20))
1959+
probability = img[:, 0:200, 0]
1960+
img_zarr = zarr.zeros(shape=probability.shape, dtype=np.uint8)
1961+
img_zarr[:] = probability
1962+
1963+
misc.write_probability_heatmap_as_ome_tiff(
1964+
image_path,
1965+
img_zarr,
1966+
tile_size=(32, 32),
1967+
levels=2,
1968+
colormap=cv2.COLORMAP_INFERNO,
1969+
)
19941970
assert image_path.is_file()
19951971
saved_img = tifffile.imread(image_path, squeeze=True)
1996-
saved_img = np.transpose(saved_img, (1, 2, 0))
1997-
assert np.all(saved_img == img_zarr)
1972+
assert img_zarr.shape == saved_img.shape[0:2]
1973+
assert img_zarr.dtype == saved_img.dtype
19981974
ome_xml = ET.fromstring(get_ome_metadata(image_path))
19991975
assert ome_xml is not None
20001976

20011977
assert_ome_metadata_value(ome_xml, "SizeY", str(img_zarr.shape[0]))
20021978
assert_ome_metadata_value(ome_xml, "SizeX", str(img_zarr.shape[1]))
2003-
assert_ome_metadata_value(ome_xml, "SizeC", str(img_zarr.shape[2]))
1979+
assert_ome_metadata_value(ome_xml, "SizeC", str(3))
20041980
assert_ome_metadata_value(ome_xml, "DimensionOrder", "XYCZT")
20051981
assert_ome_metadata_value(ome_xml, "PhysicalSizeX", "0.25")
20061982
assert_ome_metadata_value(ome_xml, "PhysicalSizeY", "0.25")
20071983
assert_ome_metadata_value(ome_xml, "PhysicalSizeXUnit", "µm")
20081984
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

Comments
 (0)