Passed
Pull Request — main (#1393)
by
unknown
01:28
created

torchio.transforms.augmentation.intensity.random_biasfield_denoise   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A RandomBiasFieldDenoise.__init__() 0 3 1
A RandomBiasFieldDenoise.__repr__() 0 2 1
A RandomBiasFieldDenoise.apply_transform() 0 13 2
1
import torch
2
from ...transform import Transform
3
from torchio.data.subject import Subject
4
5
class RandomBiasFieldDenoise(Transform):
6
    """
7
    Simple placeholder transform that simulates denoising after bias field
8
    correction by blending voxel intensities toward the mean value.
9
    
10
    Parameters:
11
        noise_reduction_factor (float): Strength of denoising (0-1).
12
    """
13
14
    def __init__(self, noise_reduction_factor: float = 0.1, **kwargs):
15
        super().__init__(**kwargs)
16
        self.noise_reduction_factor = noise_reduction_factor
17
18
    def apply_transform(self, subject: Subject) -> Subject:
19
        for _, image in subject.get_images_dict(intensity_only=True).items():
20
            tensor = image.data.float()
21
22
            # Basic denoising by shifting toward mean intensity
23
            mean_val = tensor.mean()
24
            tensor = (tensor * (1 - self.noise_reduction_factor)) + (
25
                mean_val * self.noise_reduction_factor
26
            )
27
28
            image.set_data(tensor)
29
30
        return subject
31
32
    def __repr__(self):
33
        return f"{self.__class__.__name__}(noise_reduction_factor={self.noise_reduction_factor})"
34