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

torchio.transforms.augmentation.intensity.random_biasfield_denoise   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

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

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