|
a |
|
b/tests/helpers.py |
|
|
1 |
from tests.data import HEMASK |
|
|
2 |
from PIL import Image |
|
|
3 |
import numpy |
|
|
4 |
from openslide import AbstractSlide |
|
|
5 |
|
|
|
6 |
|
|
|
7 |
class FakeSlide(AbstractSlide): |
|
|
8 |
|
|
|
9 |
""" |
|
|
10 |
A class to mimic an openslide.OpenSlide object. |
|
|
11 |
|
|
|
12 |
Args: |
|
|
13 |
staining: type of staining you wanna mimic. |
|
|
14 |
""" |
|
|
15 |
|
|
|
16 |
def __init__(self, name="fake_slide", staining="H&E", extension=".mrxs"): |
|
|
17 |
""" |
|
|
18 |
""" |
|
|
19 |
self._filename = name + extension |
|
|
20 |
self.tissue_color = [154, 120, 156, 255] |
|
|
21 |
|
|
|
22 |
@property |
|
|
23 |
def level_count(self): |
|
|
24 |
return 11 |
|
|
25 |
|
|
|
26 |
@property |
|
|
27 |
def level_dimensions(self): |
|
|
28 |
return ( |
|
|
29 |
(83968, 71680), |
|
|
30 |
(41984, 35840), |
|
|
31 |
(20992, 17920), |
|
|
32 |
(10496, 8960), |
|
|
33 |
(5248, 4480), |
|
|
34 |
(2624, 2240), |
|
|
35 |
(1312, 1120), |
|
|
36 |
(656, 560), |
|
|
37 |
(328, 280), |
|
|
38 |
(164, 140), |
|
|
39 |
(82, 70), |
|
|
40 |
) |
|
|
41 |
|
|
|
42 |
@property |
|
|
43 |
def level_downsamples(self): |
|
|
44 |
return ( |
|
|
45 |
1.0, |
|
|
46 |
2.0, |
|
|
47 |
4.0, |
|
|
48 |
8.0, |
|
|
49 |
16.0, |
|
|
50 |
32.0, |
|
|
51 |
64.0, |
|
|
52 |
128.0, |
|
|
53 |
256.0, |
|
|
54 |
512.0, |
|
|
55 |
1024.0, |
|
|
56 |
) |
|
|
57 |
|
|
|
58 |
@property |
|
|
59 |
def properties(self): |
|
|
60 |
""" |
|
|
61 |
""" |
|
|
62 |
return {} |
|
|
63 |
|
|
|
64 |
@property |
|
|
65 |
def associated_images(self): |
|
|
66 |
return {} |
|
|
67 |
|
|
|
68 |
def get_best_level_for_downsample(self, downsample): |
|
|
69 |
for k in range(1, self.level_count): |
|
|
70 |
if self.level_downsamples[k] > downsample: |
|
|
71 |
return k - 1 |
|
|
72 |
return self.level_count |
|
|
73 |
|
|
|
74 |
def read_region(self, location, level, size): |
|
|
75 |
""" |
|
|
76 |
""" |
|
|
77 |
# un pack request coordinates |
|
|
78 |
x, y = location |
|
|
79 |
dx, dy = size |
|
|
80 |
ds = self.level_downsamples[level] |
|
|
81 |
tds = self.level_downsamples[-1] |
|
|
82 |
dj, di = self.level_dimensions[-1] |
|
|
83 |
|
|
|
84 |
X_indices = numpy.full((dy, dx), x, dtype=float) |
|
|
85 |
Y_indices = numpy.full((dy, dx), y, dtype=float) |
|
|
86 |
# go through columns of X |
|
|
87 |
X_indices += (numpy.arange(dx) * ds)[None] |
|
|
88 |
Y_indices += (numpy.arange(dy) * ds)[:, None] |
|
|
89 |
# rescale x and y indices |
|
|
90 |
J_indices = numpy.floor(X_indices / tds).astype(int) |
|
|
91 |
I_indices = numpy.floor(Y_indices / tds).astype(int) |
|
|
92 |
# put a flag on out-of-bounds pixels |
|
|
93 |
J_indices[J_indices >= dj] = -1 |
|
|
94 |
I_indices[I_indices >= di] = -1 |
|
|
95 |
# compute 1D indices |
|
|
96 |
Indices = (I_indices * dj + J_indices) |
|
|
97 |
hemask = numpy.array(HEMASK) |
|
|
98 |
labels = numpy.where(Indices > 0, hemask[Indices], 0) |
|
|
99 |
numpy_img = numpy.zeros((dy, dx, 4), dtype=numpy.uint8) |
|
|
100 |
# out of bounds regions have val == 0 ^^ |
|
|
101 |
numpy_img[labels == 0] = (0, 0, 0, 0) |
|
|
102 |
numpy_img[labels == 1] = (255, 255, 255, 255) |
|
|
103 |
numpy_img[labels == 2] = self.tissue_color |
|
|
104 |
# get pil image |
|
|
105 |
return Image.fromarray(numpy_img, mode="RGBA") |