Passed
Pull Request — master (#622)
by
unknown
01:21
created

torchio.transforms.preprocessing.intensity.clamp   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 25
dl 0
loc 44
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A ClampIntensity.clamp() 0 6 1
A ClampIntensity.__init__() 0 8 1
A ClampIntensity.apply_normalization() 0 8 1
1
from typing import Optional
2
import torch
3
from ....data.subject import Subject
4
from .normalization_transform import NormalizationTransform
5
6
7
class ClampIntensity(NormalizationTransform):
8
    """Clamp intensity values to a certain range.
9
    Args:
10
        out_min: See :func:`~torch.clamp`.
11
        out_max: See :func:`~torch.clamp`.
12
13
    Example:
14
        >>> import torchio as tio
15
        >>> ct = tio.ScalarImage('ct_scan.nii.gz')
16
        >>> ct_air, ct_bone = -1000, 1000
17
        >>> clamp = tio.ClampIntensity(out_min=ct_air, out_max=ct_bone)
18
        >>> ct_clamped = clamp(ct)
19
    """
20
    def __init__(
21
            self,
22
            out_min: Optional[float] = None,
23
            out_max: Optional[float] = None,
24
            **kwargs
25
            ):
26
        super().__init__(**kwargs)
27
        self.out_min, self.out_max = out_min, out_max
28
29
    def apply_normalization(
30
            self,
31
            subject: Subject,
32
            image_name: str,
33
            mask: torch.Tensor,
34
            ) -> None:
35
        image = subject[image_name]
36
        image.set_data(self.clamp(image.data, image_name))
37
38
    def clamp(
39
            self,
40
            tensor: torch.Tensor,
41
            image_name: str,
42
            ) -> torch.Tensor:
43
        return tensor.clamp(self.out_min, self.out_max)
44