-
Notifications
You must be signed in to change notification settings - Fork 3
/
conftest.py
262 lines (224 loc) · 8.25 KB
/
conftest.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
from pathlib import Path
import numpy as np
import pytest
import torch
from cellseg_models_pytorch.utils import FileHandler
def pytest_addoption(parser):
parser.addoption(
"--cuda",
action="store_true",
default=False,
help="run gpu tests",
)
parser.addoption(
"--slow",
action="store_true",
default=False,
help="run slow tests",
)
parser.addoption(
"--optional",
action="store_true",
default=False,
help="run tests that require optional packages",
)
def pytest_configure(config):
config.addinivalue_line("markers", "cuda: mark test as a gpu test")
config.addinivalue_line("markers", "slow: mark test as a slow test")
config.addinivalue_line("markers", "optional: mark test as an optional test")
def pytest_collection_modifyitems(config, items):
only_cuda = pytest.mark.skip(reason="--cuda option runs only gpu tests")
if config.getoption("--cuda"):
for item in items:
if "cuda" not in item.keywords:
item.add_marker(only_cuda)
else:
skip_cuda = pytest.mark.skip(reason="need --cuda option to run")
for item in items:
if "cuda" in item.keywords:
item.add_marker(skip_cuda)
only_slow = pytest.mark.skip(reason="--slow option runs only slow tests")
if config.getoption("--slow"):
for item in items:
if "slow" not in item.keywords:
item.add_marker(only_slow)
else:
skip_slow = pytest.mark.skip(reason="need --slow option to run")
for item in items:
if "slow" in item.keywords:
item.add_marker(skip_slow)
only_opt = pytest.mark.skip(reason="--optional option runs only optional tests")
if config.getoption("--optional"):
for item in items:
if "optional" not in item.keywords:
item.add_marker(only_opt)
else:
skip_opt = pytest.mark.skip(reason="need --optional option to run")
for item in items:
if "optional" in item.keywords:
item.add_marker(skip_opt)
@pytest.fixture(scope="package")
def img_dir() -> Path:
"""Return a path to directory with a few test images."""
path = Path().resolve()
return path / "cellseg_models_pytorch/inference/tests/data"
@pytest.fixture(scope="package")
def img_patch_dir() -> Path:
"""Return a path to directory with a few test images."""
path = Path().resolve()
return path / "cellseg_models_pytorch/datasets/tests/data/imgs"
@pytest.fixture(scope="package")
def mask_patch_dir() -> Path:
"""Return a path to directory with a few test images."""
path = Path().resolve()
return path / "cellseg_models_pytorch/datasets/tests/data/masks"
@pytest.fixture(scope="package")
def type_map_tensor() -> torch.Tensor:
"""Return a dummy type map target tensor. Shape (8, 320, 320)."""
path = Path().resolve()
path = path / "cellseg_models_pytorch/training/tests/data/type_target_batch8.pt"
return torch.load(path.as_posix())
@pytest.fixture(scope="package")
def sem_map_tensor() -> torch.Tensor:
"""Return a dummy semantic map target tensor. Shape (8, 320, 320)."""
path = Path().resolve()
path = path / "cellseg_models_pytorch/training/tests/data/sem_target_batch8.pt"
return torch.load(path.as_posix())
@pytest.fixture(scope="package")
def img_sample() -> np.ndarray:
"""Read in test RGB img."""
path = Path().resolve()
return FileHandler.read_img(path / "cellseg_models_pytorch/utils/tests/data/HE.png")
@pytest.fixture(scope="package")
def hdf5db() -> Path:
"""Read in test RGB img."""
path = Path().resolve()
return path / "cellseg_models_pytorch/datasets/tests/data/tiny_test.h5"
@pytest.fixture(scope="package")
def inst_map() -> np.ndarray:
"""Return a dummy labelled segmentation mask."""
inst_map = np.array(
[
[2, 2, 2, 1, 1, 1, 1, 0, 5, 5],
[2, 2, 2, 1, 1, 1, 1, 0, 5, 5],
[2, 2, 0, 0, 1, 1, 1, 0, 5, 5],
[2, 0, 0, 0, 0, 0, 0, 4, 4, 0],
[0, 0, 3, 3, 3, 0, 4, 4, 4, 0],
[0, 3, 3, 3, 3, 0, 4, 4, 4, 0],
[0, 3, 3, 3, 0, 0, 4, 4, 4, 0],
[0, 3, 3, 0, 0, 0, 4, 4, 0, 0],
[0, 0, 0, 0, 0, 0, 9, 9, 0, 0],
[0, 8, 8, 8, 0, 0, 9, 9, 9, 0],
[0, 8, 8, 8, 0, 0, 9, 9, 9, 0],
],
dtype=int,
)
return inst_map
@pytest.fixture(scope="package")
def type_map() -> np.ndarray:
"""Return dummy (cell) type segmentation mask."""
type_map = np.array(
[
[2, 2, 2, 1, 1, 1, 1, 0, 3, 3],
[2, 2, 2, 1, 1, 1, 1, 0, 3, 3],
[2, 2, 0, 0, 1, 1, 1, 0, 3, 3],
[2, 0, 0, 0, 0, 0, 0, 1, 1, 0],
[0, 0, 3, 3, 3, 0, 1, 1, 1, 0],
[0, 3, 3, 3, 3, 0, 1, 1, 1, 0],
[0, 3, 3, 3, 0, 0, 1, 1, 1, 0],
[0, 3, 3, 0, 0, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 0, 0],
[0, 2, 2, 2, 0, 0, 1, 1, 1, 0],
[0, 2, 2, 2, 0, 0, 1, 1, 1, 0],
],
dtype=int,
)
return type_map
@pytest.fixture(scope="package")
def sem_map() -> np.ndarray:
"""Return a dummy semantic segmentation mask."""
sem_map = np.array(
[
[2, 2, 2, 1, 1, 1, 1, 3, 3, 3],
[2, 2, 2, 1, 1, 1, 1, 3, 3, 3],
[2, 1, 2, 2, 1, 1, 1, 3, 3, 3],
[2, 1, 0, 2, 3, 3, 3, 1, 1, 1],
[2, 0, 2, 2, 3, 3, 1, 1, 1, 0],
[2, 2, 2, 2, 3, 3, 1, 2, 1, 1],
[2, 2, 2, 2, 3, 3, 1, 1, 1, 1],
[3, 3, 3, 3, 3, 3, 1, 1, 0, 0],
[3, 3, 3, 0, 3, 3, 1, 1, 0, 0],
[3, 2, 2, 2, 3, 3, 1, 1, 1, 1],
[3, 2, 3, 2, 3, 3, 1, 1, 1, 1],
],
dtype=int,
)
return sem_map
@pytest.fixture(scope="package")
def true_sem() -> np.ndarray:
true = np.array(
[
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 0, 0, 2, 2, 2, 2, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 0, 0],
[1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 0, 0],
[0, 1, 1, 1, 1, 0, 0, 2, 2, 2, 2, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 4, 4, 0],
[0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 4, 4, 4, 4],
[0, 0, 0, 3, 3, 3, 3, 3, 3, 0, 4, 4, 4, 4],
[0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 0, 4, 4, 0],
[0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0],
],
dtype=int,
)
return true
@pytest.fixture(scope="package")
def pred_sem() -> np.ndarray:
pred = np.array(
[
[0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 0, 0, 2, 2, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 0, 0, 2, 2, 2, 2, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 0, 0],
[1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 0, 0],
[0, 1, 1, 1, 1, 0, 0, 2, 2, 2, 2, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 3, 4, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 3, 3, 4, 4, 0, 0, 4, 0, 0, 0],
[0, 0, 0, 3, 3, 3, 4, 4, 4, 4, 4, 0, 0, 0],
[0, 0, 3, 3, 3, 3, 4, 4, 4, 4, 0, 0, 0, 0],
[0, 3, 3, 3, 3, 3, 4, 4, 4, 4, 0, 0, 0, 0],
],
dtype=int,
)
return pred
@pytest.fixture(scope="package")
def tensor_sem_map() -> torch.LongTensor:
"""Return a dummy tensor of shape (2, 6, 6)."""
t = torch.LongTensor(
[
[
[0, 0, 0, 1, 1, 1],
[0, 0, 0, 1, 1, 1],
[0, 0, 0, 1, 1, 1],
[2, 2, 2, 3, 3, 3],
[2, 2, 2, 3, 3, 3],
[2, 2, 2, 3, 3, 3],
],
[
[2, 2, 2, 0, 0, 0],
[2, 2, 2, 0, 0, 0],
[2, 2, 2, 0, 0, 0],
[3, 3, 3, 1, 1, 1],
[3, 3, 3, 1, 1, 1],
[3, 3, 3, 1, 1, 1],
],
]
)
return t