Passed
Pull Request — master (#346)
by Fernando
05:34 queued 04:10
created

TestRandomAffine.test_parse_degrees()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
from torchio.transforms import RandomAffine
2
from ...utils import TorchioTestCase
3
4
5
class TestRandomAffine(TorchioTestCase):
6
    """Tests for `RandomAffine`."""
7
    def setUp(self):
8
        # Set image origin far from center
9
        super().setUp()
10
        affine = self.sample_subject.t1.affine
11
        affine[:3, 3] = 1e5
12
13
    def test_rotation_image(self):
14
        # Rotation around image center
15
        transform = RandomAffine(
16
            degrees=(90, 90),
17
            default_pad_value=0,
18
            center='image',
19
        )
20
        transformed = transform(self.sample_subject)
21
        total = transformed.t1.data.sum()
22
        self.assertNotEqual(total, 0)
23
24
    def test_rotation_origin(self):
25
        # Rotation around far away point, image should be empty
26
        transform = RandomAffine(
27
            degrees=(90, 90),
28
            default_pad_value=0,
29
            center='origin',
30
        )
31
        transformed = transform(self.sample_subject)
32
        total = transformed.t1.data.sum()
33
        self.assertEqual(total, 0)
34
35
    def test_no_rotation(self):
36
        transform = RandomAffine(
37
            scales=(1, 1),
38
            degrees=(0, 0),
39
            default_pad_value=0,
40
            center='image',
41
        )
42
        transformed = transform(self.sample_subject)
43
        self.assertTensorAlmostEqual(self.sample_subject.t1.data, transformed.t1.data)
44
45
        transform = RandomAffine(
46
            scales=(1, 1),
47
            degrees=(180, 180),
48
            default_pad_value=0,
49
            center='image',
50
        )
51
        transformed = transform(self.sample_subject)
52
        transformed = transform(transformed)
53
        self.assertTensorAlmostEqual(self.sample_subject.t1.data, transformed.t1.data)
54
55
    def test_translation(self):
56
        transform = RandomAffine(
57
            scales=(1, 1),
58
            degrees=0,
59
            translation=(5, 5)
60
        )
61
        transformed = transform(self.sample_subject)
62
63
        # I think the right test should be the following one:
64
        # self.assertTensorAlmostEqual(
65
        #     self.sample_subject.t1.data[:, :-5, :-5, :-5],
66
        #     transformed.t1.data[:, 5:, 5:, 5:]
67
        # )
68
69
        # However the passing test is this one:
70
        self.assertTensorAlmostEqual(
71
            self.sample_subject.t1.data[:, :-5, :-5, 5:],
72
            transformed.t1.data[:, 5:, 5:, :-5]
73
        )
74
75
    def test_negative_scales(self):
76
        with self.assertRaises(ValueError):
77
            RandomAffine(scales=-1.)
78
79
    def test_scale_too_large(self):
80
        with self.assertRaises(ValueError):
81
            RandomAffine(scales=1.5)
82
83
    def test_scales_range_with_negative_min(self):
84
        with self.assertRaises(ValueError):
85
            RandomAffine(scales=(-1., 4.))
86
87
    def test_wrong_scales_type(self):
88
        with self.assertRaises(ValueError):
89
            RandomAffine(scales='wrong')
90
91
    def test_wrong_degrees_type(self):
92
        with self.assertRaises(ValueError):
93
            RandomAffine(degrees='wrong')
94
95
    def test_too_many_translation_values(self):
96
        with self.assertRaises(ValueError):
97
            RandomAffine(translation=(-10., 4., 42.))
98
99
    def test_wrong_translation_type(self):
100
        with self.assertRaises(ValueError):
101
            RandomAffine(translation='wrong')
102
103
    def test_wrong_center(self):
104
        with self.assertRaises(ValueError):
105
            RandomAffine(center=0)
106
107
    def test_wrong_default_pad_value(self):
108
        with self.assertRaises(ValueError):
109
            RandomAffine(default_pad_value='wrong')
110
111
    def test_wrong_image_interpolation_type(self):
112
        with self.assertRaises(TypeError):
113
            RandomAffine(image_interpolation=0)
114
115
    def test_wrong_image_interpolation_value(self):
116
        with self.assertRaises(AttributeError):
117
            RandomAffine(image_interpolation='wrong')
118
119
    def test_incompatible_args_isotropic(self):
120
        with self.assertRaises(ValueError):
121
            RandomAffine((0.8, 0.5, 0.1), isotropic=True)
122
123
    def test_parse_scales(self):
124
        def do_assert(transform):
125
            self.assertEqual(transform.scales, 3 * (0.9, 1.1))
126
        do_assert(RandomAffine(scales=0.1))
127
        do_assert(RandomAffine(scales=(0.9, 1.1)))
128
        do_assert(RandomAffine(scales=3 * (0.1,)))
129
        do_assert(RandomAffine(scales=3 * [0.9, 1.1]))
130
131
    def test_parse_degrees(self):
132
        def do_assert(transform):
133
            self.assertEqual(transform.degrees, 3 * (-10, 10))
134
        do_assert(RandomAffine(degrees=10))
135
        do_assert(RandomAffine(degrees=(-10, 10)))
136
        do_assert(RandomAffine(degrees=3 * (10,)))
137
        do_assert(RandomAffine(degrees=3 * [-10, 10]))
138
139
    def test_parse_translation(self):
140
        def do_assert(transform):
141
            self.assertEqual(transform.translation, 3 * (-10, 10))
142
        do_assert(RandomAffine(translation=10))
143
        do_assert(RandomAffine(translation=(-10, 10)))
144
        do_assert(RandomAffine(translation=3 * (10,)))
145
        do_assert(RandomAffine(translation=3 * [-10, 10]))
146