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

Class RandLambda

monai/transforms/utility/array.py:847–891  ·  view source on GitHub ↗

Randomizable version :py:class:`monai.transforms.Lambda`, the input `func` may contain random logic, or randomly execute the function based on `prob`. Args: func: Lambda/function to be applied. prob: probability of executing the random function, default to 1.0, with 100

Source from the content-addressed store, hash-verified

845
846
847class RandLambda(Lambda, RandomizableTransform):
848 """
849 Randomizable version :py:class:`monai.transforms.Lambda`, the input `func` may contain random logic,
850 or randomly execute the function based on `prob`.
851
852 Args:
853 func: Lambda/function to be applied.
854 prob: probability of executing the random function, default to 1.0, with 100% probability to execute.
855 inv_func: Lambda/function of inverse operation, default to `lambda x: x`.
856 track_meta: If `False`, then standard data objects will be returned (e.g., torch.Tensor` and `np.ndarray`)
857 as opposed to MONAI's enhanced objects. By default, this is `True`.
858
859 For more details, please check :py:class:`monai.transforms.Lambda`.
860 """
861
862 backend = Lambda.backend
863
864 def __init__(
865 self,
866 func: Callable | None = None,
867 prob: float = 1.0,
868 inv_func: Callable = no_collation,
869 track_meta: bool = True,
870 ) -> None:
871 Lambda.__init__(self=self, func=func, inv_func=inv_func, track_meta=track_meta)
872 RandomizableTransform.__init__(self=self, prob=prob)
873
874 def __call__(self, img: NdarrayOrTensor, func: Callable | None = None):
875 self.randomize(img)
876 out = deepcopy(super().__call__(img, func) if self._do_transform else img)
877 # convert to MetaTensor if necessary
878 if not isinstance(out, MetaTensor) and self.track_meta:
879 out = MetaTensor(out)
880 if isinstance(out, MetaTensor):
881 lambda_info = self.pop_transform(out) if self._do_transform else {}
882 self.push_transform(out, extra_info=lambda_info)
883 return out
884
885 def inverse(self, data: torch.Tensor):
886 do_transform = self.get_most_recent_transform(data).pop(TraceKeys.DO_TRANSFORM)
887 if do_transform:
888 data = super().inverse(data)
889 else:
890 self.pop_transform(data)
891 return data
892
893
894class LabelToMask(Transform):

Callers 3

test_set_dataMethod · 0.90
test_set_dataMethod · 0.90

Calls

no outgoing calls

Tested by 3

test_set_dataMethod · 0.72
test_set_dataMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…