| Total Complexity | 3 |
| Total Lines | 44 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 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 |