MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / RandScaleIntensity

Class RandScaleIntensity

monai/transforms/intensity/array.py:678–743  ·  view source on GitHub ↗

Randomly scale the intensity of input image by ``v = v * (1 + factor)`` where the `factor` is randomly picked.

Source from the content-addressed store, hash-verified

676
677
678class RandScaleIntensity(RandomizableTransform):
679 """
680 Randomly scale the intensity of input image by ``v = v * (1 + factor)`` where the `factor`
681 is randomly picked.
682 """
683
684 backend = ScaleIntensity.backend
685
686 def __init__(
687 self,
688 factors: tuple[float, float] | float,
689 prob: float = 0.1,
690 channel_wise: bool = False,
691 dtype: DtypeLike = np.float32,
692 ) -> None:
693 """
694 Args:
695 factors: factor range to randomly scale by ``v = v * (1 + factor)``.
696 if single number, factor value is picked from (-factors, factors).
697 prob: probability of scale.
698 channel_wise: if True, scale on each channel separately. Please ensure
699 that the first dimension represents the channel of the image if True.
700 dtype: output data type, if None, same as input image. defaults to float32.
701
702 """
703 RandomizableTransform.__init__(self, prob)
704 if isinstance(factors, (int, float)):
705 self.factors = (min(-factors, factors), max(-factors, factors))
706 elif len(factors) != 2:
707 raise ValueError(f"factors should be a number or pair of numbers, got {factors}.")
708 else:
709 self.factors = (min(factors), max(factors))
710 self.factor = self.factors[0]
711 self.channel_wise = channel_wise
712 self.dtype = dtype
713
714 def randomize(self, data: Any | None = None) -> None:
715 super().randomize(None)
716 if not self._do_transform:
717 return None
718 if self.channel_wise:
719 self.factor = [self.R.uniform(low=self.factors[0], high=self.factors[1]) for _ in range(data.shape[0])] # type: ignore
720 else:
721 self.factor = self.R.uniform(low=self.factors[0], high=self.factors[1])
722
723 def __call__(self, img: NdarrayOrTensor, randomize: bool = True) -> NdarrayOrTensor:
724 """
725 Apply the transform to `img`.
726 """
727 img = convert_to_tensor(img, track_meta=get_track_meta())
728 if randomize:
729 self.randomize(img)
730
731 if not self._do_transform:
732 return convert_data_type(img, dtype=self.dtype)[0]
733
734 ret: NdarrayOrTensor
735 if self.channel_wise:

Callers 4

__init__Method · 0.90
test_valueMethod · 0.90
test_channel_wiseMethod · 0.90

Calls

no outgoing calls

Tested by 3

test_valueMethod · 0.72
test_channel_wiseMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…