Passed
Pull Request — main (#1350)
by Fernando
01:27
created

torchio.transforms.preprocessing.spatial.transpose   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 16
dl 0
loc 34
rs 10
c 0
b 0
f 0

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 7 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, 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