Skip to content

Commit 070a490

Browse files
committed
ADD: Add unit test for tiffreader
1 parent 5f1c6a2 commit 070a490

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

tests/test_tiffreader.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,38 @@ def __init__(
138138

139139
reader = wsireader.WSIReader.try_tiff(dummy_file, ".tiff", None, None, None)
140140
assert isinstance(reader, wsireader.VirtualWSIReader)
141+
142+
143+
def test_parse_filtercolor_metadata_with_filter_pair() -> None:
144+
"""Test full parsing including filter pair matching from XML metadata."""
145+
# We can't possibly test on all the different types of tiff files, so simulate them.
146+
xml_str = """
147+
<root>
148+
<FilterColors>
149+
<FilterColors-k>EM123_EX456</FilterColors-k>
150+
<FilterColors-v>255,128,0</FilterColors-v>
151+
</FilterColors>
152+
<ScanBands-i>
153+
<Bands-i>
154+
<Name>Channel1</Name>
155+
</Bands-i>
156+
<FilterPair>
157+
<EmissionFilter>
158+
<FixedFilter>
159+
<PartNumber>EM123</PartNumber>
160+
</FixedFilter>
161+
</EmissionFilter>
162+
<ExcitationFilter>
163+
<FixedFilter>
164+
<PartNumber>EX456</PartNumber>
165+
</FixedFilter>
166+
</ExcitationFilter>
167+
</FilterPair>
168+
</ScanBands-i>
169+
</root>
170+
"""
171+
root = ElementTree.fromstring(xml_str)
172+
result = wsireader.TIFFWSIReader._parse_filtercolor_metadata(root)
173+
assert result is not None
174+
assert "Channel1" in result
175+
assert result["Channel1"] == (1.0, 128 / 255, 0.0)

tests/test_wsireader.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
from tiatoolbox import cli, utils
3030
from tiatoolbox.annotation import SQLiteStore
31-
from tiatoolbox.utils import imread, tiff_to_fsspec
31+
from tiatoolbox.utils import imread, postproc_defs, tiff_to_fsspec
3232
from tiatoolbox.utils.exceptions import FileNotSupportedError
3333
from tiatoolbox.utils.magic import is_sqlite3
3434
from tiatoolbox.utils.transforms import imresize, locsize2bounds
@@ -2934,6 +2934,29 @@ def test_visualise_multi_channel(sample_qptiff: Path) -> None:
29342934
# Was 7 channels. Not sure if this is correct. Check this!
29352935

29362936

2937+
def test_get_post_proc_variants() -> None:
2938+
"""Test different branches of get_post_proc method."""
2939+
reader = wsireader.VirtualWSIReader(np.zeros((10, 10, 3)))
2940+
2941+
assert callable(reader.get_post_proc(lambda x: x))
2942+
assert reader.get_post_proc(None) is None
2943+
assert isinstance(reader.get_post_proc("auto"), postproc_defs.MultichannelToRGB)
2944+
assert isinstance(
2945+
reader.get_post_proc("MultichannelToRGB"), postproc_defs.MultichannelToRGB
2946+
)
2947+
2948+
with pytest.raises(ValueError, match="Invalid post-processing function"):
2949+
reader.get_post_proc("invalid_proc")
2950+
2951+
2952+
def test_post_proc_applied() -> None:
2953+
"""Test that post_proc is applied to image region."""
2954+
reader = wsireader.VirtualWSIReader(np.ones((100, 100, 3), dtype=np.uint8))
2955+
reader.post_proc = lambda x: x * 0
2956+
region = reader.read_rect((0, 0), (50, 50))
2957+
assert np.all(region == 0)
2958+
2959+
29372960
def test_fsspec_json_wsi_reader_instantiation() -> None:
29382961
"""Test if FsspecJsonWSIReader is instantiated.
29392962

0 commit comments

Comments
 (0)