Skip to content

Commit eca8fc8

Browse files
committed
✅ Add tests for improved coverage
1 parent 96676c8 commit eca8fc8

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

tests/engines/test_semantic_segmentor.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from tiatoolbox import cli
1616
from tiatoolbox.annotation import SQLiteStore
17+
from tiatoolbox.models.engine import semantic_segmentor
1718
from tiatoolbox.models.engine.semantic_segmentor import SemanticSegmentor
1819
from tiatoolbox.utils import env_detection as toolbox_env
1920
from tiatoolbox.utils.misc import imread
@@ -221,6 +222,59 @@ def test_save_annotation_store_nparray(
221222
_test_store_output_patch(output[1])
222223

223224

225+
def test_non_overlapping_blocks() -> None:
226+
"""Test for non-overlapping merge to canvas."""
227+
blocks = np.array([np.ones((2, 2, 1)), np.ones((2, 2, 1)) * 2])
228+
output_locations = np.array([[0, 0, 2, 2], [2, 0, 4, 2]])
229+
merged_shape = (2, 4, 1)
230+
canvas, count = semantic_segmentor.merge_batch_to_canvas(
231+
blocks, output_locations, merged_shape
232+
)
233+
assert np.array_equal(canvas[:, :2, :], np.ones((2, 2, 1)))
234+
assert np.array_equal(canvas[:, 2:, :], np.ones((2, 2, 1)) * 2)
235+
assert np.array_equal(count, np.ones((2, 4, 1)))
236+
237+
238+
def test_overlapping_blocks() -> None:
239+
"""Test for overlapping merge to canvas."""
240+
blocks = np.array([np.ones((2, 2, 1)), np.ones((2, 2, 1)) * 3])
241+
output_locations = np.array([[0, 0, 2, 2], [1, 0, 3, 2]])
242+
merged_shape = (2, 3, 1)
243+
canvas, count = semantic_segmentor.merge_batch_to_canvas(
244+
blocks, output_locations, merged_shape
245+
)
246+
expected_canvas = np.array([[[1], [4], [3]], [[1], [4], [3]]])
247+
expected_count = np.array([[[1], [2], [1]], [[1], [2], [1]]])
248+
assert np.array_equal(canvas, expected_canvas)
249+
assert np.array_equal(count, expected_count)
250+
251+
252+
def test_zero_block() -> None:
253+
"""Test for zero merge to canvas."""
254+
blocks = np.array([np.zeros((2, 2, 1)), np.ones((2, 2, 1))])
255+
output_locations = np.array([[0, 0, 2, 2], [2, 0, 4, 2]])
256+
merged_shape = (2, 4, 1)
257+
canvas, count = semantic_segmentor.merge_batch_to_canvas(
258+
blocks, output_locations, merged_shape
259+
)
260+
assert np.array_equal(canvas[:, :2, :], np.zeros((2, 2, 1)))
261+
assert np.array_equal(canvas[:, 2:, :], np.ones((2, 2, 1)))
262+
assert np.array_equal(count[:, :2, :], np.zeros((2, 2, 1)))
263+
assert np.array_equal(count[:, 2:, :], np.ones((2, 2, 1)))
264+
265+
266+
def test_empty_blocks() -> None:
267+
"""Test for empty merge to canvas."""
268+
blocks = np.empty((0, 2, 2, 1))
269+
output_locations = np.empty((0, 4))
270+
merged_shape = (2, 2, 1)
271+
canvas, count = semantic_segmentor.merge_batch_to_canvas(
272+
blocks, output_locations, merged_shape
273+
)
274+
assert np.array_equal(canvas, np.zeros((2, 2, 1)))
275+
assert np.array_equal(count, np.zeros((2, 2, 1), dtype=np.uint8))
276+
277+
224278
def test_wsi_segmentor_zarr(
225279
remote_sample: Callable,
226280
sample_svs: Path,

0 commit comments

Comments
 (0)