-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathreader_test.py
315 lines (296 loc) · 9.67 KB
/
reader_test.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import json
import warnings
import polars as pl
import pytest
from PIL import Image
from histoprep import SlideReader
from histoprep.backend import (
CziBackend,
OpenSlideBackend,
PillowBackend,
TileCoordinates,
TissueMask,
TMASpotCoordinates,
)
from .utils import (
SLIDE_PATH_CZI,
SLIDE_PATH_JPEG,
SLIDE_PATH_TMA,
TMP_DIRECTORY,
clean_temporary_directory,
)
def test_tissue_mask() -> None:
reader = SlideReader(SLIDE_PATH_JPEG)
tissue_mask = reader.get_tissue_mask(level=1, sigma=0.5, threshold=200)
assert isinstance(tissue_mask, TissueMask)
assert tissue_mask.mask.shape == reader.level_dimensions[1]
assert tissue_mask.level == 1
assert tissue_mask.sigma == 0.5
assert tissue_mask.threshold == 200
assert tissue_mask.to_pil().size == reader.level_dimensions[1][::-1]
assert tissue_mask.read_region(xywh=(0, 0, 100, 80)).shape == (40, 50)
assert tissue_mask.read_region(xywh=(0, 0, 100, 80), shape=(80, 100)).shape == (
80,
100,
)
def test_tile_coords() -> None:
reader = SlideReader(SLIDE_PATH_JPEG)
tissue_mask = reader.get_tissue_mask(level=1, threshold=240)
tile_coords = reader.get_tile_coordinates(
tissue_mask, width=512, max_background=0.2
)
assert isinstance(tile_coords, TileCoordinates)
assert tile_coords.num_tiles == 4
assert tile_coords.width == 512
assert tile_coords.height == 512
assert tile_coords.coordinates == [
(1536, 0, 512, 512),
(512, 512, 512, 512),
(1024, 512, 512, 512),
(1536, 512, 512, 512),
]
assert tile_coords.max_background == 0.2
assert tile_coords.overlap == 0.0
assert isinstance(tile_coords.tissue_mask, TissueMask)
assert isinstance(tile_coords.thumbnail, Image.Image)
assert isinstance(tile_coords.thumbnail_tiles, Image.Image)
assert isinstance(tile_coords.thumbnail_tissue, Image.Image)
clean_temporary_directory()
tile_coords.save_thumbnails(TMP_DIRECTORY)
tile_coords.save_properties(
TMP_DIRECTORY, level=0, level_downsample=reader.level_downsamples[0]
)
assert (TMP_DIRECTORY / "thumbnail.jpeg").exists()
assert (TMP_DIRECTORY / "thumbnail_tiles.jpeg").exists()
assert (TMP_DIRECTORY / "thumbnail_tissue.jpeg").exists()
with (TMP_DIRECTORY / "properties.json").open() as f:
assert json.load(f) == {
"num_tiles": 4,
"level": 0,
"level_downsample": [1.0, 1.0],
"width": 512,
"height": 512,
"overlap": 0.0,
"max_background": 0.2,
"threshold": 240,
"sigma": 0.0,
}
clean_temporary_directory()
def test_spot_coordinates() -> None:
reader = SlideReader(SLIDE_PATH_TMA)
tissue_mask = reader.get_tissue_mask(level=-1, sigma=2.0, threshold=220)
with warnings.catch_warnings():
warnings.simplefilter("error")
spot_coords = reader.get_spot_coordinates(tissue_mask)
assert isinstance(spot_coords, TMASpotCoordinates)
assert spot_coords.num_spots == 38
assert len(spot_coords.names) == 38
assert len(spot_coords.coordinates) == 38
assert isinstance(spot_coords.tissue_mask, TissueMask)
assert isinstance(spot_coords.thumbnail, Image.Image)
assert isinstance(spot_coords.thumbnail_spots, Image.Image)
assert isinstance(spot_coords.thumbnail_tissue, Image.Image)
clean_temporary_directory()
spot_coords.save_thumbnails(TMP_DIRECTORY)
spot_coords.save_properties(
TMP_DIRECTORY, level=0, level_downsample=reader.level_downsamples[0]
)
assert (TMP_DIRECTORY / "thumbnail.jpeg").exists()
assert (TMP_DIRECTORY / "thumbnail_spots.jpeg").exists()
assert (TMP_DIRECTORY / "thumbnail_tissue.jpeg").exists()
with (TMP_DIRECTORY / "properties.json").open() as f:
assert json.load(f) == {
"num_spots": 38,
"level": 0,
"level_downsample": [1.0, 1.0],
"threshold": 220,
"sigma": 2.0,
}
clean_temporary_directory()
def test_save_tiles() -> None:
reader = SlideReader(SLIDE_PATH_JPEG)
tissue_mask = reader.get_tissue_mask(level=1, threshold=240)
tile_coords = reader.get_tile_coordinates(
tissue_mask, width=512, max_background=0.2
)
clean_temporary_directory()
metadata = reader.save_tiles(TMP_DIRECTORY, tile_coords, overwrite=False)
with pytest.raises(ValueError):
__ = reader.save_tiles(TMP_DIRECTORY, tile_coords, overwrite=False)
assert isinstance(metadata, pl.DataFrame)
assert metadata.columns == [
"path",
"x",
"y",
"w",
"h",
"background",
"black_pixels",
"white_pixels",
"laplacian_std",
"gray_mean",
"gray_std",
"red_mean",
"red_std",
"green_mean",
"green_std",
"blue_mean",
"blue_std",
"hue_mean",
"hue_std",
"saturation_mean",
"saturation_std",
"brightness_mean",
"brightness_std",
"gray_q5",
"gray_q10",
"gray_q25",
"gray_q50",
"gray_q75",
"gray_q90",
"gray_q95",
"red_q5",
"red_q10",
"red_q25",
"red_q50",
"red_q75",
"red_q90",
"red_q95",
"green_q5",
"green_q10",
"green_q25",
"green_q50",
"green_q75",
"green_q90",
"green_q95",
"blue_q5",
"blue_q10",
"blue_q25",
"blue_q50",
"blue_q75",
"blue_q90",
"blue_q95",
"hue_q5",
"hue_q10",
"hue_q25",
"hue_q50",
"hue_q75",
"hue_q90",
"hue_q95",
"saturation_q5",
"saturation_q10",
"saturation_q25",
"saturation_q50",
"saturation_q75",
"saturation_q90",
"saturation_q95",
"brightness_q5",
"brightness_q10",
"brightness_q25",
"brightness_q50",
"brightness_q75",
"brightness_q90",
"brightness_q95",
]
assert [f.name for f in (TMP_DIRECTORY / "slide").iterdir()] == [
"thumbnail.jpeg",
"thumbnail_tiles.jpeg",
"thumbnail_tissue.jpeg",
"properties.json",
"tiles",
"metadata.parquet",
]
assert [f.name for f in (TMP_DIRECTORY / "slide" / "tiles").iterdir()] == [
"x1536_y0_w512_h512.jpeg",
"x512_y512_w512_h512.jpeg",
"x1024_y512_w512_h512.jpeg",
"x1536_y512_w512_h512.jpeg",
]
metadata = reader.save_tiles(
TMP_DIRECTORY,
tile_coords,
overwrite=True,
save_metrics=False,
save_masks=True,
use_csv=True,
)
assert [f.name for f in (TMP_DIRECTORY / "slide").iterdir()] == [
"thumbnail.jpeg",
"thumbnail_tiles.jpeg",
"thumbnail_tissue.jpeg",
"properties.json",
"tiles",
"masks",
"metadata.csv",
]
assert [f.name for f in (TMP_DIRECTORY / "slide" / "masks").iterdir()] == [
"x1536_y0_w512_h512.png",
"x512_y512_w512_h512.png",
"x1024_y512_w512_h512.png",
"x1536_y512_w512_h512.png",
]
assert metadata.columns == ["path", "mask_path", "x", "y", "w", "h"]
clean_temporary_directory()
def test_save_spots() -> None:
# Uses the same implementation so most of the above tests cover it!
reader = SlideReader(SLIDE_PATH_TMA)
tissue_mask = reader.get_tissue_mask(level=-1, threshold=220, sigma=2)
spot_coords = reader.get_spot_coordinates(tissue_mask)
clean_temporary_directory()
metadata = reader.save_spots(TMP_DIRECTORY, coordinates=spot_coords, level=-1)
assert isinstance(metadata, pl.DataFrame)
assert metadata.columns == [
"name",
"path",
"x",
"y",
"w",
"h",
]
assert [f.name for f in (TMP_DIRECTORY / "slide_tma").iterdir()] == [
"thumbnail.jpeg",
"thumbnail_spots.jpeg",
"thumbnail_tissue.jpeg",
"properties.json",
"spots",
"metadata.parquet",
]
assert (
len([f.name for f in (TMP_DIRECTORY / "slide_tma" / "spots").iterdir()])
== spot_coords.num_spots
)
# Collect all filenames that should be present.
filenames = set()
for x, y, w, h in spot_coords.coordinates:
filenames.add(f"x{x}_y{y}_w{w}_h{h}.jpeg")
assert (
len(
[
f.name
for f in (TMP_DIRECTORY / "slide_tma" / "spots").iterdir()
if f.name not in filenames
]
)
== 0
)
clean_temporary_directory()
def test_yield_tiles() -> None:
reader = SlideReader(SLIDE_PATH_JPEG)
tissue_mask = reader.get_tissue_mask()
tile_coords = reader.get_tile_coordinates(tissue_mask, 512, max_background=1.0)
for i, (tile, xywh) in enumerate(reader.yield_tiles(tile_coords)):
assert tile.shape == (512, 512, 3)
assert tile_coords.coordinates[i] == xywh
for tile, __ in reader.yield_tiles(tile_coords, transform=lambda x: x[..., 0]):
assert tile.shape == (512, 512)
for tile, __ in reader.yield_tiles(tile_coords, level=1):
assert tile.shape == (256, 256, 3)
def test_estimate_mean_and_std() -> None:
reader = SlideReader(SLIDE_PATH_JPEG)
tissue_mask = reader.get_tissue_mask()
tile_coords = reader.get_tile_coordinates(tissue_mask, 128, max_background=1.0)
mean, std = reader.estimate_mean_and_std(tile_coords, max_samples=1000)
def test_reader_init_with_backend_class() -> None:
__ = SlideReader(SLIDE_PATH_JPEG, backend=PillowBackend)
__ = SlideReader(SLIDE_PATH_TMA, backend=OpenSlideBackend)
__ = SlideReader(SLIDE_PATH_CZI, backend=CziBackend)