|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
|
|
3
|
|
|
from copy import copy |
|
4
|
|
|
import torchio as tio |
|
5
|
|
|
from ...utils import TorchioTestCase |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class TestGridSampler(TorchioTestCase): |
|
9
|
|
|
"""Tests for `GridSampler`.""" |
|
10
|
|
|
|
|
11
|
|
|
def test_locations(self): |
|
12
|
|
|
patch_size = 5, 20, 20 |
|
13
|
|
|
patch_overlap = 2, 4, 6 |
|
14
|
|
|
sampler = tio.GridSampler( |
|
15
|
|
|
subject=self.sample_subject, |
|
16
|
|
|
patch_size=patch_size, |
|
17
|
|
|
patch_overlap=patch_overlap, |
|
18
|
|
|
) |
|
19
|
|
|
fixture = [ |
|
20
|
|
|
[0, 0, 0, 5, 20, 20], |
|
21
|
|
|
[0, 0, 10, 5, 20, 30], |
|
22
|
|
|
[3, 0, 0, 8, 20, 20], |
|
23
|
|
|
[3, 0, 10, 8, 20, 30], |
|
24
|
|
|
[5, 0, 0, 10, 20, 20], |
|
25
|
|
|
[5, 0, 10, 10, 20, 30], |
|
26
|
|
|
] |
|
27
|
|
|
locations = sampler.locations.tolist() |
|
28
|
|
|
self.assertEqual(locations, fixture) |
|
29
|
|
|
|
|
30
|
|
|
def test_large_patch(self): |
|
31
|
|
|
with self.assertRaises(ValueError): |
|
32
|
|
|
tio.GridSampler(self.sample_subject, (5, 21, 5), (0, 2, 0)) |
|
33
|
|
|
|
|
34
|
|
|
def test_large_overlap(self): |
|
35
|
|
|
with self.assertRaises(ValueError): |
|
36
|
|
|
tio.GridSampler(self.sample_subject, (5, 20, 5), (2, 4, 6)) |
|
37
|
|
|
|
|
38
|
|
|
def test_odd_overlap(self): |
|
39
|
|
|
with self.assertRaises(ValueError): |
|
40
|
|
|
tio.GridSampler(self.sample_subject, (5, 20, 5), (2, 4, 3)) |
|
41
|
|
|
|
|
42
|
|
|
def test_single_location(self): |
|
43
|
|
|
sampler = tio.GridSampler(self.sample_subject, (10, 20, 30), 0) |
|
44
|
|
|
fixture = [[0, 0, 0, 10, 20, 30]] |
|
45
|
|
|
self.assertEqual(sampler.locations.tolist(), fixture) |
|
46
|
|
|
|
|
47
|
|
|
def test_subject_shape(self): |
|
48
|
|
|
patch_size = 5, 20, 20 |
|
49
|
|
|
patch_overlap = 2, 4, 6 |
|
50
|
|
|
initial_shape = copy(self.sample_subject.shape) |
|
51
|
|
|
tio.GridSampler( |
|
52
|
|
|
self.sample_subject, |
|
53
|
|
|
patch_size, |
|
54
|
|
|
patch_overlap, |
|
55
|
|
|
padding_mode='reflect', |
|
56
|
|
|
) |
|
57
|
|
|
final_shape = self.sample_subject.shape |
|
58
|
|
|
self.assertEqual(initial_shape, final_shape) |
|
59
|
|
|
|
|
60
|
|
|
def test_bad_subject(self): |
|
61
|
|
|
with self.assertRaises(ValueError): |
|
62
|
|
|
patch_size = 88 |
|
63
|
|
|
tio.GridSampler(patch_size) |
|
64
|
|
|
|