-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathdictionary.py
752 lines (641 loc) · 29.7 KB
/
dictionary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
# Copyright 2020 - 2021 MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
A collection of dictionary-based wrappers around the "vanilla" transforms for intensity adjustment
defined in :py:class:`monai.transforms.intensity.array`.
Class names are ended with 'd' to denote dictionary-based transforms.
"""
from collections.abc import Iterable
from typing import Any, Dict, Hashable, Mapping, Optional, Sequence, Tuple, Union
import numpy as np
import torch
from monai.config import KeysCollection
from monai.transforms.compose import MapTransform, Randomizable
from monai.transforms.intensity.array import (
AdjustContrast,
GaussianSharpen,
GaussianSmooth,
MaskIntensity,
NormalizeIntensity,
ScaleIntensity,
ScaleIntensityRange,
ScaleIntensityRangePercentiles,
ShiftIntensity,
ThresholdIntensity,
)
from monai.utils import dtype_torch_to_numpy, ensure_tuple_size
__all__ = [
"RandGaussianNoised",
"ShiftIntensityd",
"RandShiftIntensityd",
"ScaleIntensityd",
"RandScaleIntensityd",
"NormalizeIntensityd",
"ThresholdIntensityd",
"ScaleIntensityRanged",
"AdjustContrastd",
"RandAdjustContrastd",
"ScaleIntensityRangePercentilesd",
"MaskIntensityd",
"GaussianSmoothd",
"RandGaussianSmoothd",
"GaussianSharpend",
"RandGaussianSharpend",
"RandHistogramShiftd",
"RandGaussianNoiseD",
"RandGaussianNoiseDict",
"ShiftIntensityD",
"ShiftIntensityDict",
"RandShiftIntensityD",
"RandShiftIntensityDict",
"ScaleIntensityD",
"ScaleIntensityDict",
"RandScaleIntensityD",
"RandScaleIntensityDict",
"NormalizeIntensityD",
"NormalizeIntensityDict",
"ThresholdIntensityD",
"ThresholdIntensityDict",
"ScaleIntensityRangeD",
"ScaleIntensityRangeDict",
"AdjustContrastD",
"AdjustContrastDict",
"RandAdjustContrastD",
"RandAdjustContrastDict",
"ScaleIntensityRangePercentilesD",
"ScaleIntensityRangePercentilesDict",
"MaskIntensityD",
"MaskIntensityDict",
"GaussianSmoothD",
"GaussianSmoothDict",
"RandGaussianSmoothD",
"RandGaussianSmoothDict",
"GaussianSharpenD",
"GaussianSharpenDict",
"RandGaussianSharpenD",
"RandGaussianSharpenDict",
"RandHistogramShiftD",
"RandHistogramShiftDict",
]
class RandGaussianNoised(Randomizable, MapTransform):
"""
Dictionary-based version :py:class:`monai.transforms.RandGaussianNoise`.
Add Gaussian noise to image. This transform assumes all the expected fields have same shape.
Args:
keys: keys of the corresponding items to be transformed.
See also: :py:class:`monai.transforms.compose.MapTransform`
prob: Probability to add Gaussian noise.
mean: Mean or “centre” of the distribution.
std: Standard deviation (spread) of distribution.
"""
def __init__(
self, keys: KeysCollection, prob: float = 0.1, mean: Union[Sequence[float], float] = 0.0, std: float = 0.1
) -> None:
super().__init__(keys)
self.prob = prob
self.mean = ensure_tuple_size(mean, len(self.keys))
self.std = std
self._do_transform = False
self._noise: Optional[np.ndarray] = None
def randomize(self, im_shape: Sequence[int]) -> None:
self._do_transform = self.R.random() < self.prob
self._noise = self.R.normal(self.mean, self.R.uniform(0, self.std), size=im_shape)
def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
image_shape = d[self.keys[0]].shape # image shape from the first data key
self.randomize(image_shape)
if self._noise is None:
raise AssertionError
if not self._do_transform:
return d
for key in self.keys:
dtype = dtype_torch_to_numpy(d[key].dtype) if isinstance(d[key], torch.Tensor) else d[key].dtype
d[key] = d[key] + self._noise.astype(dtype)
return d
class ShiftIntensityd(MapTransform):
"""
Dictionary-based wrapper of :py:class:`monai.transforms.ShiftIntensity`.
"""
def __init__(self, keys: KeysCollection, offset: float) -> None:
"""
Args:
keys: keys of the corresponding items to be transformed.
See also: :py:class:`monai.transforms.compose.MapTransform`
offset: offset value to shift the intensity of image.
"""
super().__init__(keys)
self.shifter = ShiftIntensity(offset)
def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
for key in self.keys:
d[key] = self.shifter(d[key])
return d
class RandShiftIntensityd(Randomizable, MapTransform):
"""
Dictionary-based version :py:class:`monai.transforms.RandShiftIntensity`.
"""
def __init__(self, keys: KeysCollection, offsets: Union[Tuple[float, float], float], prob: float = 0.1) -> None:
"""
Args:
keys: keys of the corresponding items to be transformed.
See also: :py:class:`monai.transforms.compose.MapTransform`
offsets: offset range to randomly shift.
if single number, offset value is picked from (-offsets, offsets).
prob: probability of rotating.
(Default 0.1, with 10% probability it returns a rotated array.)
"""
super().__init__(keys)
if isinstance(offsets, (int, float)):
self.offsets = (min(-offsets, offsets), max(-offsets, offsets))
else:
if len(offsets) != 2:
raise AssertionError("offsets should be a number or pair of numbers.")
self.offsets = (min(offsets), max(offsets))
self.prob = prob
self._do_transform = False
def randomize(self, data: Optional[Any] = None) -> None:
self._offset = self.R.uniform(low=self.offsets[0], high=self.offsets[1])
self._do_transform = self.R.random() < self.prob
def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
self.randomize()
if not self._do_transform:
return d
shifter = ShiftIntensity(self._offset)
for key in self.keys:
d[key] = shifter(d[key])
return d
class ScaleIntensityd(MapTransform):
"""
Dictionary-based wrapper of :py:class:`monai.transforms.ScaleIntensity`.
Scale the intensity of input image to the given value range (minv, maxv).
If `minv` and `maxv` not provided, use `factor` to scale image by ``v = v * (1 + factor)``.
"""
def __init__(
self, keys: KeysCollection, minv: float = 0.0, maxv: float = 1.0, factor: Optional[float] = None
) -> None:
"""
Args:
keys: keys of the corresponding items to be transformed.
See also: :py:class:`monai.transforms.compose.MapTransform`
minv: minimum value of output data.
maxv: maximum value of output data.
factor: factor scale by ``v = v * (1 + factor)``.
"""
super().__init__(keys)
self.scaler = ScaleIntensity(minv, maxv, factor)
def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
for key in self.keys:
d[key] = self.scaler(d[key])
return d
class RandScaleIntensityd(Randomizable, MapTransform):
"""
Dictionary-based version :py:class:`monai.transforms.RandScaleIntensity`.
"""
def __init__(self, keys: KeysCollection, factors: Union[Tuple[float, float], float], prob: float = 0.1) -> None:
"""
Args:
keys: keys of the corresponding items to be transformed.
See also: :py:class:`monai.transforms.compose.MapTransform`
factors: factor range to randomly scale by ``v = v * (1 + factor)``.
if single number, factor value is picked from (-factors, factors).
prob: probability of rotating.
(Default 0.1, with 10% probability it returns a rotated array.)
"""
super().__init__(keys)
if isinstance(factors, (int, float)):
self.factors = (min(-factors, factors), max(-factors, factors))
else:
if len(factors) != 2:
raise AssertionError("factors should be a number or pair of numbers.")
self.factors = (min(factors), max(factors))
self.prob = prob
self._do_transform = False
def randomize(self, data: Optional[Any] = None) -> None:
self.factor = self.R.uniform(low=self.factors[0], high=self.factors[1])
self._do_transform = self.R.random() < self.prob
def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
self.randomize()
if not self._do_transform:
return d
scaler = ScaleIntensity(minv=None, maxv=None, factor=self.factor)
for key in self.keys:
d[key] = scaler(d[key])
return d
class NormalizeIntensityd(MapTransform):
"""
Dictionary-based wrapper of :py:class:`monai.transforms.NormalizeIntensity`.
This transform can normalize only non-zero values or entire image, and can also calculate
mean and std on each channel separately.
Args:
keys: keys of the corresponding items to be transformed.
See also: monai.transforms.MapTransform
subtrahend: the amount to subtract by (usually the mean)
divisor: the amount to divide by (usually the standard deviation)
nonzero: whether only normalize non-zero values.
channel_wise: if using calculated mean and std, calculate on each channel separately
or calculate on the entire image directly.
dtype: output data type, defaut to float32.
"""
def __init__(
self,
keys: KeysCollection,
subtrahend: Optional[np.ndarray] = None,
divisor: Optional[np.ndarray] = None,
nonzero: bool = False,
channel_wise: bool = False,
dtype: np.dtype = np.float32,
) -> None:
super().__init__(keys)
self.normalizer = NormalizeIntensity(subtrahend, divisor, nonzero, channel_wise, dtype)
def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
for key in self.keys:
d[key] = self.normalizer(d[key])
return d
class ThresholdIntensityd(MapTransform):
"""
Dictionary-based wrapper of :py:class:`monai.transforms.ThresholdIntensity`.
Args:
keys: keys of the corresponding items to be transformed.
See also: monai.transforms.MapTransform
threshold: the threshold to filter intensity values.
above: filter values above the threshold or below the threshold, default is True.
cval: value to fill the remaining parts of the image, default is 0.
"""
def __init__(self, keys: KeysCollection, threshold: float, above: bool = True, cval: float = 0.0) -> None:
super().__init__(keys)
self.filter = ThresholdIntensity(threshold, above, cval)
def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
for key in self.keys:
d[key] = self.filter(d[key])
return d
class ScaleIntensityRanged(MapTransform):
"""
Dictionary-based wrapper of :py:class:`monai.transforms.ScaleIntensityRange`.
Args:
keys: keys of the corresponding items to be transformed.
See also: monai.transforms.MapTransform
a_min: intensity original range min.
a_max: intensity original range max.
b_min: intensity target range min.
b_max: intensity target range max.
clip: whether to perform clip after scaling.
"""
def __init__(
self, keys: KeysCollection, a_min: float, a_max: float, b_min: float, b_max: float, clip: bool = False
) -> None:
super().__init__(keys)
self.scaler = ScaleIntensityRange(a_min, a_max, b_min, b_max, clip)
def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
for key in self.keys:
d[key] = self.scaler(d[key])
return d
class AdjustContrastd(MapTransform):
"""
Dictionary-based wrapper of :py:class:`monai.transforms.AdjustContrast`.
Changes image intensity by gamma. Each pixel/voxel intensity is updated as:
`x = ((x - min) / intensity_range) ^ gamma * intensity_range + min`
Args:
keys: keys of the corresponding items to be transformed.
See also: monai.transforms.MapTransform
gamma: gamma value to adjust the contrast as function.
"""
def __init__(self, keys: KeysCollection, gamma: float) -> None:
super().__init__(keys)
self.adjuster = AdjustContrast(gamma)
def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
for key in self.keys:
d[key] = self.adjuster(d[key])
return d
class RandAdjustContrastd(Randomizable, MapTransform):
"""
Dictionary-based version :py:class:`monai.transforms.RandAdjustContrast`.
Randomly changes image intensity by gamma. Each pixel/voxel intensity is updated as:
`x = ((x - min) / intensity_range) ^ gamma * intensity_range + min`
Args:
keys: keys of the corresponding items to be transformed.
See also: monai.transforms.MapTransform
prob: Probability of adjustment.
gamma: Range of gamma values.
If single number, value is picked from (0.5, gamma), default is (0.5, 4.5).
"""
def __init__(
self, keys: KeysCollection, prob: float = 0.1, gamma: Union[Tuple[float, float], float] = (0.5, 4.5)
) -> None:
super().__init__(keys)
self.prob: float = prob
if isinstance(gamma, (int, float)):
if gamma <= 0.5:
raise AssertionError(
"if gamma is single number, must greater than 0.5 and value is picked from (0.5, gamma)"
)
self.gamma = (0.5, gamma)
else:
if len(gamma) != 2:
raise AssertionError("gamma should be a number or pair of numbers.")
self.gamma = (min(gamma), max(gamma))
self._do_transform = False
self.gamma_value: Optional[float] = None
def randomize(self, data: Optional[Any] = None) -> None:
self._do_transform = self.R.random_sample() < self.prob
self.gamma_value = self.R.uniform(low=self.gamma[0], high=self.gamma[1])
def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
self.randomize()
if self.gamma_value is None:
raise AssertionError
if not self._do_transform:
return d
adjuster = AdjustContrast(self.gamma_value)
for key in self.keys:
d[key] = adjuster(d[key])
return d
class ScaleIntensityRangePercentilesd(MapTransform):
"""
Dictionary-based wrapper of :py:class:`monai.transforms.ScaleIntensityRangePercentiles`.
Args:
keys: keys of the corresponding items to be transformed.
See also: monai.transforms.MapTransform
lower: lower percentile.
upper: upper percentile.
b_min: intensity target range min.
b_max: intensity target range max.
clip: whether to perform clip after scaling.
relative: whether to scale to the corresponding percentiles of [b_min, b_max]
"""
def __init__(
self,
keys: KeysCollection,
lower: float,
upper: float,
b_min: float,
b_max: float,
clip: bool = False,
relative: bool = False,
) -> None:
super().__init__(keys)
self.scaler = ScaleIntensityRangePercentiles(lower, upper, b_min, b_max, clip, relative)
def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
for key in self.keys:
d[key] = self.scaler(d[key])
return d
class MaskIntensityd(MapTransform):
"""
Dictionary-based wrapper of :py:class:`monai.transforms.MaskIntensity`.
Args:
keys: keys of the corresponding items to be transformed.
See also: :py:class:`monai.transforms.compose.MapTransform`
mask_data: if mask data is single channel, apply to evey channel
of input image. if multiple channels, the channel number must
match input data. mask_data will be converted to `bool` values
by `mask_data > 0` before applying transform to input image.
"""
def __init__(self, keys: KeysCollection, mask_data: np.ndarray) -> None:
super().__init__(keys)
self.converter = MaskIntensity(mask_data)
def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
for key in self.keys:
d[key] = self.converter(d[key])
return d
class GaussianSmoothd(MapTransform):
"""
Dictionary-based wrapper of :py:class:`monai.transforms.GaussianSmooth`.
Args:
keys: keys of the corresponding items to be transformed.
See also: :py:class:`monai.transforms.compose.MapTransform`
sigma: if a list of values, must match the count of spatial dimensions of input data,
and apply every value in the list to 1 spatial dimension. if only 1 value provided,
use it for all spatial dimensions.
approx: discrete Gaussian kernel type, available options are "erf", "sampled", and "scalespace".
see also :py:meth:`monai.networks.layers.GaussianFilter`.
"""
def __init__(self, keys: KeysCollection, sigma: Union[Sequence[float], float], approx: str = "erf") -> None:
super().__init__(keys)
self.converter = GaussianSmooth(sigma, approx=approx)
def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
for key in self.keys:
d[key] = self.converter(d[key])
return d
class RandGaussianSmoothd(Randomizable, MapTransform):
"""
Dictionary-based wrapper of :py:class:`monai.transforms.GaussianSmooth`.
Args:
keys: keys of the corresponding items to be transformed.
See also: :py:class:`monai.transforms.compose.MapTransform`
sigma_x: randomly select sigma value for the first spatial dimension.
sigma_y: randomly select sigma value for the second spatial dimension if have.
sigma_z: randomly select sigma value for the third spatial dimension if have.
approx: discrete Gaussian kernel type, available options are "erf", "sampled", and "scalespace".
see also :py:meth:`monai.networks.layers.GaussianFilter`.
prob: probability of Gaussian smooth.
"""
def __init__(
self,
keys: KeysCollection,
sigma_x: Tuple[float, float] = (0.25, 1.5),
sigma_y: Tuple[float, float] = (0.25, 1.5),
sigma_z: Tuple[float, float] = (0.25, 1.5),
approx: str = "erf",
prob: float = 0.1,
) -> None:
super().__init__(keys)
self.sigma_x = sigma_x
self.sigma_y = sigma_y
self.sigma_z = sigma_z
self.approx = approx
self.prob = prob
self._do_transform = False
def randomize(self, data: Optional[Any] = None) -> None:
self._do_transform = self.R.random_sample() < self.prob
self.x = self.R.uniform(low=self.sigma_x[0], high=self.sigma_x[1])
self.y = self.R.uniform(low=self.sigma_y[0], high=self.sigma_y[1])
self.z = self.R.uniform(low=self.sigma_z[0], high=self.sigma_z[1])
def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
self.randomize()
if not self._do_transform:
return d
for key in self.keys:
sigma = ensure_tuple_size(tup=(self.x, self.y, self.z), dim=d[key].ndim - 1)
d[key] = GaussianSmooth(sigma=sigma, approx=self.approx)(d[key])
return d
class GaussianSharpend(MapTransform):
"""
Dictionary-based wrapper of :py:class:`monai.transforms.GaussianSharpen`.
Args:
keys: keys of the corresponding items to be transformed.
See also: :py:class:`monai.transforms.compose.MapTransform`
sigma1: sigma parameter for the first gaussian kernel. if a list of values, must match the count
of spatial dimensions of input data, and apply every value in the list to 1 spatial dimension.
if only 1 value provided, use it for all spatial dimensions.
sigma2: sigma parameter for the second gaussian kernel. if a list of values, must match the count
of spatial dimensions of input data, and apply every value in the list to 1 spatial dimension.
if only 1 value provided, use it for all spatial dimensions.
alpha: weight parameter to compute the final result.
approx: discrete Gaussian kernel type, available options are "erf", "sampled", and "scalespace".
see also :py:meth:`monai.networks.layers.GaussianFilter`.
"""
def __init__(
self,
keys: KeysCollection,
sigma1: Union[Sequence[float], float] = 3.0,
sigma2: Union[Sequence[float], float] = 1.0,
alpha: float = 30.0,
approx: str = "erf",
) -> None:
super().__init__(keys)
self.converter = GaussianSharpen(sigma1, sigma2, alpha, approx=approx)
def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
for key in self.keys:
d[key] = self.converter(d[key])
return d
class RandGaussianSharpend(Randomizable, MapTransform):
"""
Dictionary-based wrapper of :py:class:`monai.transforms.GaussianSharpen`.
Args:
keys: keys of the corresponding items to be transformed.
See also: :py:class:`monai.transforms.compose.MapTransform`
sigma1_x: randomly select sigma value for the first spatial dimension of first gaussian kernel.
sigma1_y: randomly select sigma value for the second spatial dimension(if have) of first gaussian kernel.
sigma1_z: randomly select sigma value for the third spatial dimension(if have) of first gaussian kernel.
sigma2_x: randomly select sigma value for the first spatial dimension of second gaussian kernel.
if only 1 value `X` provided, it must be smaller than `sigma1_x` and randomly select from [X, sigma1_x].
sigma2_y: randomly select sigma value for the second spatial dimension(if have) of second gaussian kernel.
if only 1 value `Y` provided, it must be smaller than `sigma1_y` and randomly select from [Y, sigma1_y].
sigma2_z: randomly select sigma value for the third spatial dimension(if have) of second gaussian kernel.
if only 1 value `Z` provided, it must be smaller than `sigma1_z` and randomly select from [Z, sigma1_z].
alpha: randomly select weight parameter to compute the final result.
approx: discrete Gaussian kernel type, available options are "erf", "sampled", and "scalespace".
see also :py:meth:`monai.networks.layers.GaussianFilter`.
prob: probability of Gaussian sharpen.
"""
def __init__(
self,
keys: KeysCollection,
sigma1_x: Tuple[float, float] = (0.5, 1.0),
sigma1_y: Tuple[float, float] = (0.5, 1.0),
sigma1_z: Tuple[float, float] = (0.5, 1.0),
sigma2_x: Union[Tuple[float, float], float] = 0.5,
sigma2_y: Union[Tuple[float, float], float] = 0.5,
sigma2_z: Union[Tuple[float, float], float] = 0.5,
alpha: Tuple[float, float] = (10.0, 30.0),
approx: str = "erf",
prob: float = 0.1,
):
super().__init__(keys)
self.sigma1_x = sigma1_x
self.sigma1_y = sigma1_y
self.sigma1_z = sigma1_z
self.sigma2_x = sigma2_x
self.sigma2_y = sigma2_y
self.sigma2_z = sigma2_z
self.alpha = alpha
self.approx = approx
self.prob = prob
self._do_transform = False
def randomize(self, data: Optional[Any] = None) -> None:
self._do_transform = self.R.random_sample() < self.prob
self.x1 = self.R.uniform(low=self.sigma1_x[0], high=self.sigma1_x[1])
self.y1 = self.R.uniform(low=self.sigma1_y[0], high=self.sigma1_y[1])
self.z1 = self.R.uniform(low=self.sigma1_z[0], high=self.sigma1_z[1])
sigma2_x = (self.sigma2_x, self.x1) if not isinstance(self.sigma2_x, Iterable) else self.sigma2_x
sigma2_y = (self.sigma2_y, self.y1) if not isinstance(self.sigma2_y, Iterable) else self.sigma2_y
sigma2_z = (self.sigma2_z, self.z1) if not isinstance(self.sigma2_z, Iterable) else self.sigma2_z
self.x2 = self.R.uniform(low=sigma2_x[0], high=sigma2_x[1])
self.y2 = self.R.uniform(low=sigma2_y[0], high=sigma2_y[1])
self.z2 = self.R.uniform(low=sigma2_z[0], high=sigma2_z[1])
self.a = self.R.uniform(low=self.alpha[0], high=self.alpha[1])
def __call__(self, data):
d = dict(data)
self.randomize()
if not self._do_transform:
return d
for key in self.keys:
sigma1 = ensure_tuple_size(tup=(self.x1, self.y1, self.z1), dim=d[key].ndim - 1)
sigma2 = ensure_tuple_size(tup=(self.x2, self.y2, self.z2), dim=d[key].ndim - 1)
d[key] = GaussianSharpen(sigma1=sigma1, sigma2=sigma2, alpha=self.a, approx=self.approx)(d[key])
return d
class RandHistogramShiftd(Randomizable, MapTransform):
"""
Dictionary-based version :py:class:`monai.transforms.RandHistogramShift`.
Apply random nonlinear transform the the image's intensity histogram.
Args:
keys: keys of the corresponding items to be transformed.
See also: monai.transforms.MapTransform
num_control_points: number of control points governing the nonlinear intensity mapping.
a smaller number of control points allows for larger intensity shifts. if two values provided, number of
control points selecting from range (min_value, max_value).
prob: probability of histogram shift.
"""
def __init__(
self, keys: KeysCollection, num_control_points: Union[Tuple[int, int], int] = 10, prob: float = 0.1
) -> None:
super().__init__(keys)
if isinstance(num_control_points, int):
if num_control_points <= 2:
raise AssertionError("num_control_points should be greater than or equal to 3")
self.num_control_points = (num_control_points, num_control_points)
else:
if len(num_control_points) != 2:
raise AssertionError("num_control points should be a number or a pair of numbers")
if min(num_control_points) <= 2:
raise AssertionError("num_control_points should be greater than or equal to 3")
self.num_control_points = (min(num_control_points), max(num_control_points))
self.prob = prob
self._do_transform = False
def randomize(self, data: Optional[Any] = None) -> None:
self._do_transform = self.R.random() < self.prob
num_control_point = self.R.randint(self.num_control_points[0], self.num_control_points[1] + 1)
self.reference_control_points = np.linspace(0, 1, num_control_point)
self.floating_control_points = np.copy(self.reference_control_points)
for i in range(1, num_control_point - 1):
self.floating_control_points[i] = self.R.uniform(
self.floating_control_points[i - 1], self.floating_control_points[i + 1]
)
def __call__(self, data: Mapping[Hashable, np.ndarray]) -> Dict[Hashable, np.ndarray]:
d = dict(data)
self.randomize()
if not self._do_transform:
return d
for key in self.keys:
img_min, img_max = d[key].min(), d[key].max()
reference_control_points_scaled = self.reference_control_points * (img_max - img_min) + img_min
floating_control_points_scaled = self.floating_control_points * (img_max - img_min) + img_min
dtype = d[key].dtype
d[key] = np.interp(d[key], reference_control_points_scaled, floating_control_points_scaled).astype(dtype)
return d
RandGaussianNoiseD = RandGaussianNoiseDict = RandGaussianNoised
ShiftIntensityD = ShiftIntensityDict = ShiftIntensityd
RandShiftIntensityD = RandShiftIntensityDict = RandShiftIntensityd
ScaleIntensityD = ScaleIntensityDict = ScaleIntensityd
RandScaleIntensityD = RandScaleIntensityDict = RandScaleIntensityd
NormalizeIntensityD = NormalizeIntensityDict = NormalizeIntensityd
ThresholdIntensityD = ThresholdIntensityDict = ThresholdIntensityd
ScaleIntensityRangeD = ScaleIntensityRangeDict = ScaleIntensityRanged
AdjustContrastD = AdjustContrastDict = AdjustContrastd
RandAdjustContrastD = RandAdjustContrastDict = RandAdjustContrastd
ScaleIntensityRangePercentilesD = ScaleIntensityRangePercentilesDict = ScaleIntensityRangePercentilesd
MaskIntensityD = MaskIntensityDict = MaskIntensityd
GaussianSmoothD = GaussianSmoothDict = GaussianSmoothd
RandGaussianSmoothD = RandGaussianSmoothDict = RandGaussianSmoothd
GaussianSharpenD = GaussianSharpenDict = GaussianSharpend
RandGaussianSharpenD = RandGaussianSharpenDict = RandGaussianSharpend
RandHistogramShiftD = RandHistogramShiftDict = RandHistogramShiftd