Passed
Push — main ( e43adb...aa4098 )
by Fernando
01:29
created

torchio.transforms.preprocessing.spatial.transpose   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Transpose.inverse() 0 2 1
A Transpose.is_invertible() 0 2 1
A Transpose.apply_transform() 0 9 2
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.
8
9
    The spatial metadata is updated accordingly, so the world coordinates of
10
    all voxels in the input and output spaces match.
11
12
    Example:
13
14
    >>> import torchio as tio
15
    >>> image = tio.datasets.FPG().t1
16
    >>> image
17
    ScalarImage(shape: (1, 256, 256, 176); spacing: (1.00, 1.00, 1.00); orientation: PIR+; path: "/home/fernando/.cache/torchio/fpg/t1.nii.gz")
18
    >>> transpose = tio.Transpose()
19
    >>> transposed = transpose(image)
20
    >>> transposed
21
    ScalarImage(shape: (1, 176, 256, 256); spacing: (1.00, 1.00, 1.00); orientation: RIP+; dtype: torch.IntTensor; memory: 44.0 MiB)
22
    """
23
24
    def apply_transform(self, subject: Subject) -> Subject:
25
        for image in self.get_images(subject):
26
            old_orientation = image.orientation_str
27
            new_orientation = old_orientation[::-1]
28
            transform = ToOrientation(new_orientation)
29
            transposed = transform(image)
30
            image.set_data(transposed.data)
31
            image.affine = transposed.affine
32
        return subject
33
34
    def is_invertible(self):
35
        return True
36
37
    def inverse(self):
38
        return self
39