Passed
Pull Request — master (#222)
by Fernando
03:12
created

tests.transforms.augmentation.test_random_labels_to_image   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 87
dl 0
loc 136
rs 10
c 0
b 0
f 0
wmc 25

16 Methods

Rating   Name   Duplication   Size   Complexity  
A TestRandomLabelsToImage.test_missing_label_key_and_pv_label_keys() 0 4 2
A TestRandomLabelsToImage.test_filling_with_binary_pv_label_map() 0 11 1
A TestRandomLabelsToImage.test_filling() 0 11 1
A TestRandomLabelsToImage.test_deterministic_simulation_with_pv_label_map() 0 9 1
A TestRandomLabelsToImage.test_random_simulation() 0 5 1
A TestRandomLabelsToImage.test_with_both_label_key_and_pv_label_keys() 0 4 2
A TestRandomLabelsToImage.test_filling_with_pv_label_map() 0 10 1
A TestRandomLabelsToImage.test_deterministic_simulation() 0 9 1
A TestRandomLabelsToImage.test_deterministic_simulation_with_binary_pv_label_map() 0 10 1
A TestRandomLabelsToImage.test_with_bad_default_gaussian_parameters_structure() 0 5 2
A TestRandomLabelsToImage.test_with_bad_gaussian_parameters_range() 0 4 2
A TestRandomLabelsToImage.test_with_wrong_label_key_type() 0 4 2
A TestRandomLabelsToImage.test_with_wrong_pv_label_keys_elements_type() 0 4 2
A TestRandomLabelsToImage.test_with_wrong_pv_label_keys_type() 0 4 2
A TestRandomLabelsToImage.test_with_inconsistent_pv_label_maps_affines() 0 10 2
A TestRandomLabelsToImage.test_with_inconsistent_pv_label_maps_shapes() 0 8 2
1
import torch
2
from torchio.transforms import RandomLabelsToImage
3
from torchio import DATA, AFFINE
4
from ...utils import TorchioTestCase
5
6
7
class TestRandomLabelsToImage(TorchioTestCase):
8
    """Tests for `RandomLabelsToImage`."""
9
    def test_random_simulation(self):
10
        """The transform runs without error and an 'image' key is present in the transformed sample."""
11
        transform = RandomLabelsToImage(label_key='label')
12
        transformed = transform(self.sample)
13
        self.assertIn('image', transformed)
14
15
    def test_deterministic_simulation(self):
16
        """The transform creates an image where values are equal to given mean if standard deviation is zero.
17
        Using a label map."""
18
        transform = RandomLabelsToImage(
19
            label_key='label',
20
            gaussian_parameters={1: {'mean': 0.5, 'std': 0}}
21
        )
22
        transformed = transform(self.sample)
23
        self.assertTrue(torch.eq(transformed['image'][DATA] == 0.5, self.sample['label'][DATA] == 1).all())
24
25
    def test_deterministic_simulation_with_pv_label_map(self):
26
        """The transform creates an image where values are equal to given mean if standard deviation is zero.
27
        Using a PV label map."""
28
        transform = RandomLabelsToImage(
29
            pv_label_keys=['label'],
30
            gaussian_parameters={'label': {'mean': 0.5, 'std': 0}}
31
        )
32
        transformed = transform(self.sample)
33
        self.assertTrue(torch.eq(transformed['image'][DATA] == 0.5, self.sample['label'][DATA] == 1).all())
34
35
    def test_deterministic_simulation_with_binary_pv_label_map(self):
36
        """The transform creates an image where values are equal to given mean if standard deviation is zero.
37
        Using a binarized PV label map."""
38
        transform = RandomLabelsToImage(
39
            pv_label_keys=['label'],
40
            gaussian_parameters={'label': {'mean': 0.5, 'std': 0}},
41
            binarize=True
42
        )
43
        transformed = transform(self.sample)
44
        self.assertTrue(torch.eq(transformed['image'][DATA] == 0.5, self.sample['label'][DATA] == 1).all())
45
46
    def test_filling(self):
47
        """The transform can fill in the generated image with an already existing image.
48
        Using a label map."""
49
        transform = RandomLabelsToImage(
50
            label_key='label',
51
            image_key='t1',
52
            gaussian_parameters={0: {'mean': 0.0, 'std': 0}}
53
        )
54
        t1_indices = self.sample['label'][DATA] == 0
55
        transformed = transform(self.sample)
56
        self.assertTrue(torch.eq(transformed['t1'][DATA][t1_indices], self.sample['t1'][DATA][t1_indices]).all())
57
58
    def test_filling_with_pv_label_map(self):
59
        """The transform can fill in the generated image with an already existing image.
60
        Using a PV label map."""
61
        transform = RandomLabelsToImage(
62
            pv_label_keys=['label'],
63
            image_key='t1'
64
        )
65
        t1_indices = self.sample['label'][DATA] == 0
66
        transformed = transform(self.sample)
67
        self.assertTrue(torch.eq(transformed['t1'][DATA][t1_indices], self.sample['t1'][DATA][t1_indices]).all())
68
69
    def test_filling_with_binary_pv_label_map(self):
70
        """The transform can fill in the generated image with an already existing image.
71
        Using a binarized PV label map."""
72
        transform = RandomLabelsToImage(
73
            pv_label_keys=['label'],
74
            image_key='t1',
75
            binarize=True
76
        )
77
        t1_indices = self.sample['label'][DATA] == 0
78
        transformed = transform(self.sample)
79
        self.assertTrue(torch.eq(transformed['t1'][DATA][t1_indices], self.sample['t1'][DATA][t1_indices]).all())
80
81
    def test_missing_label_key_and_pv_label_keys(self):
82
        """The transform raises an error if both label_key and pv_label_keys are None."""
83
        with self.assertRaises(ValueError):
84
            RandomLabelsToImage()
85
86
    def test_with_both_label_key_and_pv_label_keys(self):
87
        """The transform raises an error if both label_key and pv_label_keys are set."""
88
        with self.assertRaises(ValueError):
89
            RandomLabelsToImage(label_key='label', pv_label_keys=['label'])
90
91
    def test_with_bad_default_gaussian_parameters_structure(self):
92
        """The transform raises an error if gaussian parameters are not a dictionary
93
        with keys 'mean' and 'std'."""
94
        with self.assertRaises(KeyError):
95
            RandomLabelsToImage(label_key='label', default_gaussian_parameters={'wrong_key': 0})
96
97
    def test_with_bad_gaussian_parameters_range(self):
98
        """The transform raises an error if mean and std are not single values nor tuples of two values."""
99
        with self.assertRaises(ValueError):
100
            RandomLabelsToImage(label_key='label', default_gaussian_parameters={'mean': [0, 1, 2], 'std': 2})
101
102
    def test_with_wrong_label_key_type(self):
103
        """The transform raises an error if a wrong type is given for label_key."""
104
        with self.assertRaises(TypeError):
105
            RandomLabelsToImage(label_key=42)
106
107
    def test_with_wrong_pv_label_keys_type(self):
108
        """The transform raises an error if a wrong type is given for pv_label_keys."""
109
        with self.assertRaises(TypeError):
110
            RandomLabelsToImage(pv_label_keys=42)
111
112
    def test_with_wrong_pv_label_keys_elements_type(self):
113
        """The transform raises an error if wrong type are given for pv_label_keys elements."""
114
        with self.assertRaises(TypeError):
115
            RandomLabelsToImage(pv_label_keys=[42, 27])
116
117
    def test_with_inconsistent_pv_label_maps_shapes(self):
118
        """The transform raises an error if PV label maps have inconsistent shapes."""
119
        transform = RandomLabelsToImage(
120
            pv_label_keys=['label', 'label2'],
121
        )
122
        sample = self.get_inconsistent_sample()
123
        with self.assertRaises(RuntimeError):
124
            transform(sample)
125
126
    def test_with_inconsistent_pv_label_maps_affines(self):
127
        """The transform raises a warning if PV label maps have inconsistent affines."""
128
        transform = RandomLabelsToImage(
129
            pv_label_keys=['label', 'label2'],
130
        )
131
        sample = self.get_inconsistent_sample()
132
        sample['label2'][DATA] = sample['label'][DATA].clone()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable DATA does not seem to be defined.
Loading history...
133
        sample['label2'][AFFINE][0, 0] = -1
134
        with self.assertRaises(RuntimeWarning):
135
            transform(sample)
136