|
1
|
|
|
from torchio.transforms import RandomAffine |
|
2
|
|
|
from ...utils import TorchioTestCase |
|
3
|
|
|
from numpy.testing import assert_array_equal |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class TestRandomAffine(TorchioTestCase): |
|
7
|
|
|
"""Tests for `RandomAffine`.""" |
|
8
|
|
|
def setUp(self): |
|
9
|
|
|
# Set image origin far from center |
|
10
|
|
|
super().setUp() |
|
11
|
|
|
affine = self.sample.t1.affine |
|
12
|
|
|
affine[:3, 3] = 1e5 |
|
13
|
|
|
|
|
14
|
|
|
def test_rotation_image(self): |
|
15
|
|
|
# Rotation around image center |
|
16
|
|
|
transform = RandomAffine( |
|
17
|
|
|
degrees=(90, 90), |
|
18
|
|
|
default_pad_value=0, |
|
19
|
|
|
center='image', |
|
20
|
|
|
) |
|
21
|
|
|
transformed = transform(self.sample) |
|
22
|
|
|
total = transformed.t1.data.sum() |
|
23
|
|
|
self.assertNotEqual(total, 0) |
|
24
|
|
|
|
|
25
|
|
|
def test_rotation_origin(self): |
|
26
|
|
|
# Rotation around far away point, image should be empty |
|
27
|
|
|
transform = RandomAffine( |
|
28
|
|
|
degrees=(90, 90), |
|
29
|
|
|
default_pad_value=0, |
|
30
|
|
|
center='origin', |
|
31
|
|
|
) |
|
32
|
|
|
transformed = transform(self.sample) |
|
33
|
|
|
total = transformed.t1.data.sum() |
|
34
|
|
|
self.assertEqual(total, 0) |
|
35
|
|
|
|
|
36
|
|
|
def test_no_rotation(self): |
|
37
|
|
|
transform = RandomAffine( |
|
38
|
|
|
scales=(1, 1), |
|
39
|
|
|
degrees=(0, 0), |
|
40
|
|
|
default_pad_value=0, |
|
41
|
|
|
center='image', |
|
42
|
|
|
) |
|
43
|
|
|
transformed = transform(self.sample) |
|
44
|
|
|
assert_array_equal(self.sample.t1.data, transformed.t1.data) |
|
45
|
|
|
|
|
46
|
|
|
transform = RandomAffine( |
|
47
|
|
|
scales=(1, 1), |
|
48
|
|
|
degrees=(180, 180), |
|
49
|
|
|
default_pad_value=0, |
|
50
|
|
|
center='image', |
|
51
|
|
|
) |
|
52
|
|
|
transformed = transform(self.sample) |
|
53
|
|
|
transformed = transform(transformed) |
|
54
|
|
|
assert_array_equal(self.sample.t1.data, transformed.t1.data) |
|
55
|
|
|
|
|
56
|
|
|
def test_translation(self): |
|
57
|
|
|
transform = RandomAffine( |
|
58
|
|
|
scales=(1, 1), |
|
59
|
|
|
degrees=0, |
|
60
|
|
|
translation=(5, 5) |
|
61
|
|
|
) |
|
62
|
|
|
transformed = transform(self.sample) |
|
63
|
|
|
|
|
64
|
|
|
# I think the right test should be the following one: |
|
65
|
|
|
# assert_array_equal( |
|
66
|
|
|
# self.sample.t1.data[:, :-5, :-5, :-5], |
|
67
|
|
|
# transformed.t1.data[:, 5:, 5:, 5:] |
|
68
|
|
|
# ) |
|
69
|
|
|
|
|
70
|
|
|
# However the passing test is this one: |
|
71
|
|
|
assert_array_equal( |
|
72
|
|
|
self.sample.t1.data[:, :-5, :-5, 5:], |
|
73
|
|
|
transformed.t1.data[:, 5:, 5:, :-5] |
|
74
|
|
|
) |
|
75
|
|
|
|
|
76
|
|
|
def test_negative_scales(self): |
|
77
|
|
|
with self.assertRaises(ValueError): |
|
78
|
|
|
RandomAffine(scales=-1.) |
|
79
|
|
|
|
|
80
|
|
|
def test_scales_range_with_negative_min(self): |
|
81
|
|
|
with self.assertRaises(ValueError): |
|
82
|
|
|
RandomAffine(scales=(-1., 4.)) |
|
83
|
|
|
|
|
84
|
|
|
def test_too_many_scales_values(self): |
|
85
|
|
|
with self.assertRaises(ValueError): |
|
86
|
|
|
RandomAffine(scales=(1., 4., 12.)) |
|
87
|
|
|
|
|
88
|
|
|
def test_wrong_scales_type(self): |
|
89
|
|
|
with self.assertRaises(ValueError): |
|
90
|
|
|
RandomAffine(scales='wrong') |
|
91
|
|
|
|
|
92
|
|
|
def test_too_many_degrees_values(self): |
|
93
|
|
|
with self.assertRaises(ValueError): |
|
94
|
|
|
RandomAffine(degrees=(-10., 4., 42.)) |
|
95
|
|
|
|
|
96
|
|
|
def test_wrong_degrees_type(self): |
|
97
|
|
|
with self.assertRaises(ValueError): |
|
98
|
|
|
RandomAffine(degrees='wrong') |
|
99
|
|
|
|
|
100
|
|
|
def test_too_many_translation_values(self): |
|
101
|
|
|
with self.assertRaises(ValueError): |
|
102
|
|
|
RandomAffine(translation=(-10., 4., 42.)) |
|
103
|
|
|
|
|
104
|
|
|
def test_wrong_translation_type(self): |
|
105
|
|
|
with self.assertRaises(ValueError): |
|
106
|
|
|
RandomAffine(translation='wrong') |
|
107
|
|
|
|
|
108
|
|
|
def test_wrong_center(self): |
|
109
|
|
|
with self.assertRaises(ValueError): |
|
110
|
|
|
RandomAffine(center=0) |
|
111
|
|
|
|
|
112
|
|
|
def test_wrong_default_pad_value(self): |
|
113
|
|
|
with self.assertRaises(ValueError): |
|
114
|
|
|
RandomAffine(default_pad_value='wrong') |
|
115
|
|
|
|
|
116
|
|
|
def test_wrong_image_interpolation_type(self): |
|
117
|
|
|
with self.assertRaises(TypeError): |
|
118
|
|
|
RandomAffine(image_interpolation=0) |
|
119
|
|
|
|
|
120
|
|
|
def test_wrong_image_interpolation_value(self): |
|
121
|
|
|
with self.assertRaises(AttributeError): |
|
122
|
|
|
RandomAffine(image_interpolation='wrong') |
|
123
|
|
|
|