Passed
Push — master ( b06930...3ddbe5 )
by Fernando
03:55
created

TestGridSampler.test_subject_shape()   A

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python
2
3
from copy import copy
4
from torchio.data import GridSampler
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 = GridSampler(self.sample_subject, patch_size, patch_overlap)
15
        fixture = [
16
            [0, 0, 0, 5, 20, 20],
17
            [0, 0, 10, 5, 20, 30],
18
            [3, 0, 0, 8, 20, 20],
19
            [3, 0, 10, 8, 20, 30],
20
            [5, 0, 0, 10, 20, 20],
21
            [5, 0, 10, 10, 20, 30],
22
        ]
23
        locations = sampler.locations.tolist()
24
        self.assertEqual(locations, fixture)
25
26
    def test_large_patch(self):
27
        with self.assertRaises(ValueError):
28
            GridSampler(self.sample_subject, (5, 21, 5), (0, 2, 0))
29
30
    def test_large_overlap(self):
31
        with self.assertRaises(ValueError):
32
            GridSampler(self.sample_subject, (5, 20, 5), (2, 4, 6))
33
34
    def test_odd_overlap(self):
35
        with self.assertRaises(ValueError):
36
            GridSampler(self.sample_subject, (5, 20, 5), (2, 4, 3))
37
38
    def test_single_location(self):
39
        sampler = GridSampler(self.sample_subject, (10, 20, 30), 0)
40
        fixture = [[0, 0, 0, 10, 20, 30]]
41
        self.assertEqual(sampler.locations.tolist(), fixture)
42
43
    def test_subject_shape(self):
44
        patch_size = 5, 20, 20
45
        patch_overlap = 2, 4, 6
46
        initial_shape = copy(self.sample_subject.shape)
47
        GridSampler(
48
            self.sample_subject, patch_size, patch_overlap, padding_mode='reflect')
49
        final_shape = self.sample_subject.shape
50
        self.assertEqual(initial_shape, final_shape)
51