|
a |
|
b/tests/patches/test_functional_api.py |
|
|
1 |
from pathaia.patches import ( |
|
|
2 |
izi_filters, |
|
|
3 |
slide_filters, |
|
|
4 |
filter_image, |
|
|
5 |
apply_slide_filters, |
|
|
6 |
slide_rois, |
|
|
7 |
slide_rois_no_image, |
|
|
8 |
UnknownFilterError, |
|
|
9 |
Patch, |
|
|
10 |
) |
|
|
11 |
from tests.helpers import FakeSlide |
|
|
12 |
import numpy |
|
|
13 |
from pytest import raises |
|
|
14 |
|
|
|
15 |
|
|
|
16 |
def test_filter_image(): |
|
|
17 |
def always_true(x): |
|
|
18 |
return True |
|
|
19 |
|
|
|
20 |
def always_false(x): |
|
|
21 |
return False |
|
|
22 |
|
|
|
23 |
izi_filters["true"] = always_true |
|
|
24 |
izi_filters["false"] = always_false |
|
|
25 |
|
|
|
26 |
images = [ |
|
|
27 |
numpy.zeros((64, 64, 3), dtype=dtype) |
|
|
28 |
for dtype in (numpy.uint8, numpy.float32, numpy.float64) |
|
|
29 |
] |
|
|
30 |
for image in images: |
|
|
31 |
assert filter_image(image, [always_true]) |
|
|
32 |
assert not filter_image(image, [always_false]) |
|
|
33 |
assert filter_image(image, ["true"]) |
|
|
34 |
assert not filter_image(image, ["false"]) |
|
|
35 |
assert not filter_image(image, ["false", always_true]) |
|
|
36 |
assert not filter_image(image, ["true", always_false]) |
|
|
37 |
assert filter_image(image, ["true", always_true]) |
|
|
38 |
raises(UnknownFilterError, filter_image, image, ["this_filter_does_not_exist"]) |
|
|
39 |
raises(UnknownFilterError, filter_image, image, [0]) |
|
|
40 |
|
|
|
41 |
|
|
|
42 |
def test_apply_slide_filters(): |
|
|
43 |
def always_true(x): |
|
|
44 |
return numpy.ones(x.shape[:2], dtype=bool) |
|
|
45 |
|
|
|
46 |
def always_false(x): |
|
|
47 |
return numpy.zeros(x.shape[:2], dtype=bool) |
|
|
48 |
|
|
|
49 |
slide_filters["true"] = always_true |
|
|
50 |
slide_filters["false"] = always_false |
|
|
51 |
|
|
|
52 |
images = [ |
|
|
53 |
numpy.zeros((64, 64, 3), dtype=dtype) |
|
|
54 |
for dtype in (numpy.uint8, numpy.float32, numpy.float64) |
|
|
55 |
] |
|
|
56 |
for image in images: |
|
|
57 |
assert apply_slide_filters(image, [always_true]).all() |
|
|
58 |
assert not apply_slide_filters(image, [always_false]).any() |
|
|
59 |
assert apply_slide_filters(image, ["true"]).all() |
|
|
60 |
assert not apply_slide_filters(image, ["false"]).any() |
|
|
61 |
assert not apply_slide_filters(image, ["false", always_true]).any() |
|
|
62 |
assert not apply_slide_filters(image, ["true", always_false]).any() |
|
|
63 |
assert apply_slide_filters(image, ["true", always_true]).all() |
|
|
64 |
raises(UnknownFilterError, filter_image, image, ["this_filter_does_not_exist"]) |
|
|
65 |
|
|
|
66 |
|
|
|
67 |
def test_slide_rois(): |
|
|
68 |
slide = FakeSlide(name="fake_slide", staining="H&E", extension=".mrxs") |
|
|
69 |
level = 1 |
|
|
70 |
psize = 224 |
|
|
71 |
interval = (0, 0) |
|
|
72 |
dsr = slide.level_downsamples[level] |
|
|
73 |
patch1, _ = next(slide_rois(slide, level, psize, interval)) |
|
|
74 |
patch2 = next(slide_rois_no_image(slide, level, psize, interval)) |
|
|
75 |
expected = Patch( |
|
|
76 |
id="#1", |
|
|
77 |
slidename=slide._filename, |
|
|
78 |
position=(0, 0), |
|
|
79 |
level=1, |
|
|
80 |
size=(psize, psize), |
|
|
81 |
size_0=(int(psize * dsr), int(psize * dsr)), |
|
|
82 |
) |
|
|
83 |
assert patch1 == patch2 == expected |
|
|
84 |
|
|
|
85 |
ancestors = [expected] |
|
|
86 |
level -= 1 |
|
|
87 |
dsr = slide.level_downsamples[level] |
|
|
88 |
patch1, _ = next(slide_rois(slide, level, psize, interval, ancestors=ancestors)) |
|
|
89 |
patch2 = next( |
|
|
90 |
slide_rois_no_image(slide, level, psize, interval, ancestors=ancestors) |
|
|
91 |
) |
|
|
92 |
expected = Patch( |
|
|
93 |
id="#1#1", |
|
|
94 |
slidename=slide._filename, |
|
|
95 |
position=(0, 0), |
|
|
96 |
level=0, |
|
|
97 |
size=(psize, psize), |
|
|
98 |
size_0=(int(psize * dsr), int(psize * dsr)), |
|
|
99 |
parent=ancestors[0], |
|
|
100 |
) |
|
|
101 |
assert patch1 == patch2 == expected |