-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtest_data.py
74 lines (58 loc) · 2.39 KB
/
test_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Test for handling toolbox remote data."""
from pathlib import Path
import numpy as np
import pytest
from tiatoolbox.data import _fetch_remote_sample, small_svs, stain_norm_target
from tiatoolbox.wsicore.wsireader import WSIReader
def test_fetch_sample(tmp_path: Path) -> None:
"""Test for fetching sample via code name."""
# Load a dictionary of sample files data (names and urls)
# code name retrieved from TOOLBOX_ROOT/data/remote_samples.yaml
tmp_path = Path(tmp_path)
path = _fetch_remote_sample("stainnorm-source")
assert Path.exists(path)
# Test if corrupted
WSIReader.open(path)
path = _fetch_remote_sample("stainnorm-source", tmp_path)
# Assuming Path has no trailing '/'
assert Path.exists(path)
assert str(tmp_path) in str(path)
# Test not directory path
test_path = Path(f"{tmp_path}/dummy.npy")
np.save(str(test_path), np.zeros([3, 3, 3]))
with pytest.raises(ValueError, match=r".*tmp_path must be a directory.*"):
_ = _fetch_remote_sample("wsi1_8k_8k_svs", test_path)
# Very tiny so temporary hook here also
arr = stain_norm_target()
assert isinstance(arr, np.ndarray)
def test_fetch_sample_skip(tmp_path: Path) -> None:
"""Test skipping fetching sample via code name if already downloaded."""
# Fetch the remote file twice
tmp_path = Path(tmp_path)
path_a = _fetch_remote_sample("wsi1_8k_8k_svs")
path_b = _fetch_remote_sample("wsi1_8k_8k_svs")
assert Path.exists(path_a)
assert path_a == path_b
# Test if corrupted
WSIReader.open(path_a)
path = _fetch_remote_sample("wsi1_8k_8k_svs", tmp_path)
# Assuming Path has no trailing '/'
assert Path.exists(path)
assert str(tmp_path) in str(path)
# Test not directory path
test_path = Path(f"{tmp_path}/dummy.npy")
np.save(str(test_path), np.zeros([3, 3, 3]))
with pytest.raises(ValueError, match=r".*tmp_path must be a directory.*"):
_ = _fetch_remote_sample("wsi1_8k_8k_svs", test_path)
# Very tiny so temporary hook here also
arr = stain_norm_target()
assert isinstance(arr, np.ndarray)
_ = _fetch_remote_sample("stainnorm-source", tmp_path)
def test_small_svs() -> None:
"""Test for fetching small SVS (CMU-1-Small-Region) sample image."""
path = small_svs()
# Check it exists
assert path.exists()
assert path.is_file()
# Test if corrupted
WSIReader.open(path)