Skip to content

Commit 7eb60ca

Browse files
committed
Fix docstrings and linter errors for NonCentralChiNoise
Fixing the checks from the CI pipeline. Signed-off-by: karllandheer <karllandheer@gmail.com>
1 parent 33d8aa2 commit 7eb60ca

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

monai/transforms/intensity/array.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,32 @@ def __init__(
173173
prob: float = 0.1,
174174
mean: Sequence[float] | float = 0.0,
175175
std: Sequence[float] | float = 1.0,
176-
degrees_of_freedom: int = 64, #64 default because typical modern brain MRI is 32 quadrature coils
176+
degrees_of_freedom: int = 64, # 64 default because typical modern brain MRI is 32 quadrature coils
177177
channel_wise: bool = False,
178178
relative: bool = False,
179179
sample_std: bool = True,
180180
dtype: DtypeLike = np.float32,
181181
) -> None:
182+
"""
183+
Initializes the transform.
184+
185+
Args:
186+
prob: Probability to add noise.
187+
mean: Mean of the Gaussian noise distributions.
188+
std: Standard deviation (spread) of the Gaussian noise distributions.
189+
degrees_of_freedom: Number of Gaussian distributions (degrees of freedom).
190+
`degrees_of_freedom=2` is Rician noise. Defaults to 64 (32 quadrature coils).
191+
channel_wise: If True, treats each channel of the image separately.
192+
relative: If True, the spread of the sampled Gaussian distributions will
193+
be std times the standard deviation of the image or channel's intensity
194+
histogram.
195+
sample_std: If True, sample the spread of the Gaussian distributions
196+
uniformly from 0 to std.
197+
dtype: output data type, if None, same as input image. defaults to float32.
198+
199+
Raises:
200+
ValueError: If `degrees_of_freedom` is not an integer or is less than 1.
201+
"""
182202
RandomizableTransform.__init__(self, prob)
183203
self.prob = prob
184204
self.mean = mean
@@ -192,6 +212,23 @@ def __init__(
192212
self.dtype = dtype
193213

194214
def _add_noise(self, img: NdarrayOrTensor, mean: float, std: float, k: int):
215+
"""
216+
Applies non-central chi noise to a single image or channel.
217+
218+
This method generates `k` Gaussian noise arrays, adds the input `img`
219+
to the first one (as the non-centrality component), and then computes
220+
the square root of the sum of squares.
221+
222+
Args:
223+
img: Input image array.
224+
mean: Mean for the Gaussian noise distributions.
225+
std: Standard deviation for the Gaussian noise distributions.
226+
k: Degrees of freedom (number of noise arrays).
227+
228+
Returns:
229+
Image with non-central chi noise applied, with the same
230+
backend (Numpy/Torch) as the input.
231+
"""
195232
dtype_np = get_equivalent_dtype(img.dtype, np.ndarray)
196233
im_shape = img.shape
197234
_std = self.R.uniform(0, std) if self.sample_std else std

monai/transforms/intensity/dictionary.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
"RandGaussianNoised",
7272
"RandRicianNoised",
7373
"RandNonCentralChiNoised",
74+
"RandNonCentralChiNoiseD",
75+
"RandNonCentralChiNoiseDict",
7476
"ShiftIntensityd",
7577
"RandShiftIntensityd",
7678
"ScaleIntensityd",

tests/transforms/test_rand_noncentralchi_noised.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ class TestRandNonCentralChiNoised(NumpyImageTestCase2D):
3232
@parameterized.expand(TESTS)
3333
def test_correct_results(self, _, in_type, keys, mean, std):
3434
degrees_of_freedom = 64
35-
noise_fn = RandNonCentralChiNoised(keys=keys, prob=1.0, mean=mean, std=std, degrees_of_freedom=degrees_of_freedom, dtype=np.float64)
35+
noise_fn = RandNonCentralChiNoised(
36+
keys=keys,
37+
prob=1.0,
38+
mean=mean,
39+
std=std,
40+
degrees_of_freedom=degrees_of_freedom,
41+
dtype=np.float64,
42+
)
3643
noise_fn.set_random_state(seed)
3744
noised = noise_fn({k: in_type(self.imt) for k in keys})
3845
np.random.seed(seed)
@@ -53,7 +60,12 @@ def test_correct_results(self, _, in_type, keys, mean, std):
5360
def test_correct_results_k2(self, _, in_type, keys, mean, std):
5461
degrees_of_freedom = 2
5562
noise_fn = RandNonCentralChiNoised(
56-
keys=keys, prob=1.0, mean=mean, std=std, degrees_of_freedom=degrees_of_freedom, dtype=np.float64
63+
keys=keys,
64+
prob=1.0,
65+
mean=mean,
66+
std=std,
67+
degrees_of_freedom=degrees_of_freedom,
68+
dtype=np.float64,
5769
)
5870
noise_fn.set_random_state(seed)
5971
noised = noise_fn({k: in_type(self.imt) for k in keys})

0 commit comments

Comments
 (0)