| Total Complexity | 43 |
| Total Lines | 252 |
| Duplicated Lines | 13.89 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like tests.transforms.augmentation.test_random_labels_to_image often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | import numpy as np |
||
| 2 | from torchio.transforms import RandomLabelsToImage |
||
| 3 | from torchio import DATA |
||
| 4 | from ...utils import TorchioTestCase |
||
| 5 | from numpy.testing import assert_array_equal |
||
| 6 | |||
| 7 | |||
| 8 | class TestRandomLabelsToImage(TorchioTestCase): |
||
| 9 | """Tests for `RandomLabelsToImage`.""" |
||
| 10 | def test_random_simulation(self): |
||
| 11 | """The transform runs without error and an 'image_from_labels' key is |
||
| 12 | present in the transformed sample.""" |
||
| 13 | transform = RandomLabelsToImage(label_key='label') |
||
| 14 | transformed = transform(self.sample) |
||
| 15 | self.assertIn('image_from_labels', transformed) |
||
| 16 | |||
| 17 | View Code Duplication | def test_deterministic_simulation(self): |
|
|
|
|||
| 18 | """The transform creates an image where values are equal to given |
||
| 19 | mean if standard deviation is zero. |
||
| 20 | Using a label map.""" |
||
| 21 | transform = RandomLabelsToImage( |
||
| 22 | label_key='label', |
||
| 23 | mean=[0.5, 2], |
||
| 24 | std=[0, 0] |
||
| 25 | ) |
||
| 26 | transformed = transform(self.sample) |
||
| 27 | assert_array_equal( |
||
| 28 | transformed['image_from_labels'][DATA] == 0.5, |
||
| 29 | self.sample['label'][DATA] == 0 |
||
| 30 | ) |
||
| 31 | assert_array_equal( |
||
| 32 | transformed['image_from_labels'][DATA] == 2, |
||
| 33 | self.sample['label'][DATA] == 1 |
||
| 34 | ) |
||
| 35 | |||
| 36 | View Code Duplication | def test_deterministic_simulation_with_discretized_label_map(self): |
|
| 37 | """The transform creates an image where values are equal to given mean |
||
| 38 | if standard deviation is zero. |
||
| 39 | Using a discretized label map.""" |
||
| 40 | transform = RandomLabelsToImage( |
||
| 41 | label_key='label', |
||
| 42 | mean=[0.5, 2], |
||
| 43 | std=[0, 0], |
||
| 44 | discretize=True |
||
| 45 | ) |
||
| 46 | transformed = transform(self.sample) |
||
| 47 | assert_array_equal( |
||
| 48 | transformed['image_from_labels'][DATA] == 0.5, |
||
| 49 | self.sample['label'][DATA] == 0 |
||
| 50 | ) |
||
| 51 | assert_array_equal( |
||
| 52 | transformed['image_from_labels'][DATA] == 2, |
||
| 53 | self.sample['label'][DATA] == 1 |
||
| 54 | ) |
||
| 55 | |||
| 56 | def test_deterministic_simulation_with_pv_map(self): |
||
| 57 | """The transform creates an image where values are equal to given |
||
| 58 | mean weighted by partial-volume if standard deviation is zero.""" |
||
| 59 | sample = self.get_sample_with_partial_volume_label_map(components=2) |
||
| 60 | transform = RandomLabelsToImage( |
||
| 61 | label_key='label', |
||
| 62 | mean=[0.5, 1], |
||
| 63 | std=[0, 0] |
||
| 64 | ) |
||
| 65 | transformed = transform(sample) |
||
| 66 | assert_array_equal( |
||
| 67 | transformed['image_from_labels'][DATA][0], |
||
| 68 | sample['label'][DATA][0] * 0.5 + sample['label'][DATA][1] * 1 |
||
| 69 | ) |
||
| 70 | self.assertEqual( |
||
| 71 | transformed['image_from_labels'][DATA].shape, |
||
| 72 | (1, 10, 20, 30) |
||
| 73 | ) |
||
| 74 | |||
| 75 | def test_deterministic_simulation_with_discretized_pv_map(self): |
||
| 76 | """The transform creates an image where values are equal to given mean |
||
| 77 | if standard deviation is zero. |
||
| 78 | Using a discretized partial-volume label map.""" |
||
| 79 | sample = self.get_sample_with_partial_volume_label_map() |
||
| 80 | transform = RandomLabelsToImage( |
||
| 81 | label_key='label', |
||
| 82 | mean=[0.5], |
||
| 83 | std=[0], |
||
| 84 | discretize=True |
||
| 85 | ) |
||
| 86 | transformed = transform(sample) |
||
| 87 | assert_array_equal( |
||
| 88 | transformed['image_from_labels'][DATA], |
||
| 89 | (sample['label'][DATA] > 0) * 0.5 |
||
| 90 | ) |
||
| 91 | |||
| 92 | def test_filling(self): |
||
| 93 | """The transform can fill in the generated image with an already |
||
| 94 | existing image. |
||
| 95 | Using a label map.""" |
||
| 96 | transform = RandomLabelsToImage( |
||
| 97 | label_key='label', |
||
| 98 | image_key='t1', |
||
| 99 | used_labels=[1] |
||
| 100 | ) |
||
| 101 | t1_indices = self.sample['label'][DATA] == 0 |
||
| 102 | transformed = transform(self.sample) |
||
| 103 | assert_array_equal( |
||
| 104 | transformed['t1'][DATA][t1_indices], |
||
| 105 | self.sample['t1'][DATA][t1_indices] |
||
| 106 | ) |
||
| 107 | |||
| 108 | def test_filling_with_discretized_label_map(self): |
||
| 109 | """The transform can fill in the generated image with an already |
||
| 110 | existing image. |
||
| 111 | Using a discretized label map.""" |
||
| 112 | transform = RandomLabelsToImage( |
||
| 113 | label_key='label', |
||
| 114 | image_key='t1', |
||
| 115 | discretize=True, |
||
| 116 | used_labels=[1] |
||
| 117 | ) |
||
| 118 | t1_indices = self.sample['label'][DATA] < 0.5 |
||
| 119 | transformed = transform(self.sample) |
||
| 120 | assert_array_equal( |
||
| 121 | transformed['t1'][DATA][t1_indices], |
||
| 122 | self.sample['t1'][DATA][t1_indices] |
||
| 123 | ) |
||
| 124 | |||
| 125 | def test_filling_with_discretized_pv_label_map(self): |
||
| 126 | """The transform can fill in the generated image with an already |
||
| 127 | existing image. |
||
| 128 | Using a discretized partial-volume label map.""" |
||
| 129 | sample = self.get_sample_with_partial_volume_label_map(components=2) |
||
| 130 | transform = RandomLabelsToImage( |
||
| 131 | label_key='label', |
||
| 132 | image_key='t1', |
||
| 133 | discretize=True, |
||
| 134 | used_labels=[1] |
||
| 135 | ) |
||
| 136 | t1_indices = sample['label'][DATA].argmax(dim=0) == 0 |
||
| 137 | transformed = transform(sample) |
||
| 138 | assert_array_equal( |
||
| 139 | transformed['t1'][DATA][0][t1_indices], |
||
| 140 | sample['t1'][DATA][0][t1_indices] |
||
| 141 | ) |
||
| 142 | |||
| 143 | def test_filling_without_any_hole(self): |
||
| 144 | """The transform does not fill anything if there is no hole.""" |
||
| 145 | transform = RandomLabelsToImage( |
||
| 146 | label_key='label', |
||
| 147 | image_key='t1', |
||
| 148 | default_std=0., |
||
| 149 | default_mean=-1. |
||
| 150 | ) |
||
| 151 | original_t1 = self.sample.t1.data.clone() |
||
| 152 | transformed = transform(self.sample) |
||
| 153 | assert np.not_equal(original_t1, transformed.t1.data).all() |
||
| 154 | |||
| 155 | def test_missing_label_key(self): |
||
| 156 | """The transform raises an error if no label_key is given.""" |
||
| 157 | with self.assertRaises(TypeError): |
||
| 158 | RandomLabelsToImage() |
||
| 159 | |||
| 160 | def test_with_bad_default_mean_range(self): |
||
| 161 | """The transform raises an error if default_mean is not a |
||
| 162 | single value nor a tuple of two values.""" |
||
| 163 | with self.assertRaises(ValueError): |
||
| 164 | RandomLabelsToImage(label_key='label', default_mean=(0, 1, 2)) |
||
| 165 | |||
| 166 | def test_with_bad_default_mean_type(self): |
||
| 167 | """The transform raises an error if default_mean has the wrong type.""" |
||
| 168 | with self.assertRaises(ValueError): |
||
| 169 | RandomLabelsToImage(label_key='label', default_mean='wrong') |
||
| 170 | |||
| 171 | def test_with_bad_default_std_range(self): |
||
| 172 | """The transform raises an error if default_std is not a |
||
| 173 | single value nor a tuple of two values.""" |
||
| 174 | with self.assertRaises(ValueError): |
||
| 175 | RandomLabelsToImage(label_key='label', default_std=(0, 1, 2)) |
||
| 176 | |||
| 177 | def test_with_bad_default_std_type(self): |
||
| 178 | """The transform raises an error if default_std has the wrong type.""" |
||
| 179 | with self.assertRaises(ValueError): |
||
| 180 | RandomLabelsToImage(label_key='label', default_std='wrong') |
||
| 181 | |||
| 182 | def test_with_wrong_label_key_type(self): |
||
| 183 | """The transform raises an error if a wrong type is given for |
||
| 184 | label_key.""" |
||
| 185 | with self.assertRaises(TypeError): |
||
| 186 | RandomLabelsToImage(label_key=42) |
||
| 187 | |||
| 188 | def test_with_wrong_used_labels_type(self): |
||
| 189 | """The transform raises an error if a wrong type is given for |
||
| 190 | used_labels.""" |
||
| 191 | with self.assertRaises(TypeError): |
||
| 192 | RandomLabelsToImage(label_key='label', used_labels=42) |
||
| 193 | |||
| 194 | def test_with_wrong_used_labels_elements_type(self): |
||
| 195 | """The transform raises an error if wrong type are given for |
||
| 196 | used_labels elements.""" |
||
| 197 | with self.assertRaises(ValueError): |
||
| 198 | RandomLabelsToImage(label_key='label', used_labels=['wrong']) |
||
| 199 | |||
| 200 | def test_with_wrong_mean_type(self): |
||
| 201 | """The transform raises an error if wrong type is given for mean.""" |
||
| 202 | with self.assertRaises(TypeError): |
||
| 203 | RandomLabelsToImage(label_key='label', mean=42) |
||
| 204 | |||
| 205 | def test_with_wrong_mean_elements_type(self): |
||
| 206 | """The transform raises an error if wrong type are given for |
||
| 207 | mean elements.""" |
||
| 208 | with self.assertRaises(ValueError): |
||
| 209 | RandomLabelsToImage(label_key='label', mean=['wrong']) |
||
| 210 | |||
| 211 | def test_with_wrong_std_type(self): |
||
| 212 | """The transform raises an error if wrong type is given for std.""" |
||
| 213 | with self.assertRaises(TypeError): |
||
| 214 | RandomLabelsToImage(label_key='label', std=42) |
||
| 215 | |||
| 216 | def test_with_wrong_std_elements_type(self): |
||
| 217 | """The transform raises an error if wrong type are given for |
||
| 218 | std elements.""" |
||
| 219 | with self.assertRaises(ValueError): |
||
| 220 | RandomLabelsToImage(label_key='label', std=['wrong']) |
||
| 221 | |||
| 222 | def test_mean_and_std_len_not_matching(self): |
||
| 223 | """The transform raises an error if mean and std length don't match.""" |
||
| 224 | with self.assertRaises(AssertionError): |
||
| 225 | RandomLabelsToImage(label_key='label', mean=[0], std=[0, 1]) |
||
| 226 | |||
| 227 | def test_mean_and_used_labels_len_not_matching(self): |
||
| 228 | """The transform raises an error if mean and used_labels |
||
| 229 | length don't match.""" |
||
| 230 | with self.assertRaises(AssertionError): |
||
| 231 | RandomLabelsToImage(label_key='label', mean=[0], used_labels=[0, 1]) |
||
| 232 | |||
| 233 | def test_std_and_used_labels_len_not_matching(self): |
||
| 234 | """The transform raises an error if std and used_labels |
||
| 235 | length don't match.""" |
||
| 236 | with self.assertRaises(AssertionError): |
||
| 237 | RandomLabelsToImage(label_key='label', std=[0], used_labels=[0, 1]) |
||
| 238 | |||
| 239 | def test_mean_not_matching_number_of_labels(self): |
||
| 240 | """The transform raises an error at runtime if mean length |
||
| 241 | does not match label numbers.""" |
||
| 242 | transform = RandomLabelsToImage(label_key='label', mean=[0]) |
||
| 243 | with self.assertRaises(AssertionError): |
||
| 244 | transform(self.sample) |
||
| 245 | |||
| 246 | def test_std_not_matching_number_of_labels(self): |
||
| 247 | """The transform raises an error at runtime if std length |
||
| 248 | does not match label numbers.""" |
||
| 249 | transform = RandomLabelsToImage(label_key='label', std=[1, 2, 3]) |
||
| 250 | with self.assertRaises(AssertionError): |
||
| 251 | transform(self.sample) |
||
| 252 |