|
4 | 4 |
|
5 | 5 | import json |
6 | 6 | import sqlite3 |
| 7 | +import tempfile |
7 | 8 | from pathlib import Path |
8 | 9 | from typing import TYPE_CHECKING, Callable |
| 10 | +from unittest import mock |
9 | 11 |
|
| 12 | +import dask.array as da |
10 | 13 | import numpy as np |
11 | 14 | import torch |
12 | 15 | import zarr |
|
15 | 18 | from tiatoolbox import cli |
16 | 19 | from tiatoolbox.annotation import SQLiteStore |
17 | 20 | from tiatoolbox.models.engine import semantic_segmentor |
18 | | -from tiatoolbox.models.engine.semantic_segmentor import SemanticSegmentor |
| 21 | +from tiatoolbox.models.engine.semantic_segmentor import ( |
| 22 | + SemanticSegmentor, |
| 23 | + merge_vertical_chunkwise, |
| 24 | +) |
19 | 25 | from tiatoolbox.utils import env_detection as toolbox_env |
20 | 26 | from tiatoolbox.utils.misc import imread |
21 | 27 |
|
@@ -275,6 +281,45 @@ def test_empty_blocks() -> None: |
275 | 281 | assert np.array_equal(count, np.zeros((2, 2, 1), dtype=np.uint8)) |
276 | 282 |
|
277 | 283 |
|
| 284 | +def test_merge_vertical_chunkwise_memory_threshold_triggered() -> None: |
| 285 | + """Test merge vertical chunkwise for memory threshold.""" |
| 286 | + # Create dummy canvas and count arrays with 3 vertical chunks |
| 287 | + data = np.ones((30, 10), dtype=np.uint8) |
| 288 | + canvas = da.from_array(data, chunks=(10, 10)) |
| 289 | + count = da.from_array(data, chunks=(10, 10)) |
| 290 | + |
| 291 | + # Output locations to simulate overlaps |
| 292 | + output_locs_y_ = np.array([[0, 10], [10, 20], [20, 30]]) |
| 293 | + |
| 294 | + # Temporary Zarr group |
| 295 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 296 | + save_path = Path(tmpdir) |
| 297 | + |
| 298 | + # Mock psutil to simulate low memory |
| 299 | + with mock.patch( |
| 300 | + "tiatoolbox.models.engine.semantic_segmentor.psutil.virtual_memory" |
| 301 | + ) as mock_vm: |
| 302 | + mock_vm.return_value.free = 1 # Very low free memory |
| 303 | + |
| 304 | + result = merge_vertical_chunkwise( |
| 305 | + canvas=canvas, |
| 306 | + count=count, |
| 307 | + output_locs_y_=output_locs_y_, |
| 308 | + zarr_group=None, |
| 309 | + save_path=save_path, |
| 310 | + memory_threshold=0.01, # Very low threshold to trigger the condition |
| 311 | + ) |
| 312 | + |
| 313 | + # Assertions |
| 314 | + assert isinstance(result, da.Array) |
| 315 | + assert hasattr(result, "name") |
| 316 | + assert result.name.startswith("from-zarr") |
| 317 | + assert np.all(result.compute() == data) |
| 318 | + |
| 319 | + zarr_group = zarr.open(tmpdir, mode="r") |
| 320 | + assert np.all(zarr_group["probabilities"][:] == data) |
| 321 | + |
| 322 | + |
278 | 323 | def test_wsi_segmentor_zarr( |
279 | 324 | remote_sample: Callable, |
280 | 325 | sample_svs: Path, |
|
0 commit comments