Total Complexity | 4 |
Total Lines | 34 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | from ....data.subject import Subject |
||
2 | from ...spatial_transform import SpatialTransform |
||
3 | from .to_orientation import ToOrientation |
||
4 | |||
5 | |||
6 | class Transpose(SpatialTransform): |
||
7 | """Swap the first and last spatial dimensions of the image, respecting metadata. |
||
8 | |||
9 | Example: |
||
10 | |||
11 | >>> import torchio as tio |
||
12 | >>> image = tio.datasets.FPG().t1 |
||
13 | >>> image |
||
14 | ScalarImage(shape: (1, 256, 256, 176); spacing: (1.00, 1.00, 1.00); orientation: PIR+; path: "/home/fernando/.cache/torchio/fpg/t1.nii.gz") |
||
15 | >>> transpose = tio.Transpose() |
||
16 | >>> transposed = transpose(image) |
||
17 | >>> transposed |
||
18 | ScalarImage(shape: (1, 176, 256, 256); spacing: (1.00, 1.00, 1.00); orientation: RIP+; dtype: torch.IntTensor; memory: 44.0 MiB) |
||
19 | """ |
||
20 | |||
21 | def apply_transform(self, subject: Subject) -> Subject: |
||
22 | for image in self.get_images(subject): |
||
23 | transform = ToOrientation(image.orientation_str[::-1]) |
||
24 | transposed = transform(image) |
||
25 | image.set_data(transposed.data) |
||
26 | image.affine = transposed.affine |
||
27 | return subject |
||
28 | |||
29 | def is_invertible(self): |
||
30 | return True |
||
31 | |||
32 | def inverse(self): |
||
33 | return self |
||
34 |