[docs]classLambda(Transform):"""Applies a user-defined function as transform. Args: function: Callable that receives and returns a 4D :class:`torch.Tensor`. types_to_apply: List of strings corresponding to the image types to which this transform should be applied. If ``None``, the transform will be applied to all images in the subject. **kwargs: See :class:`~torchio.transforms.Transform` for additional keyword arguments. Example: >>> import torchio as tio >>> invert_intensity = tio.Lambda(lambda x: -x, types_to_apply=[tio.INTENSITY]) >>> invert_mask = tio.Lambda(lambda x: 1 - x, types_to_apply=[tio.LABEL]) >>> def double(x): ... return 2 * x >>> double_transform = tio.Lambda(double) """# noqa: B950def__init__(self,function:TypeCallable,types_to_apply:Optional[Sequence[str]]=None,**kwargs,):super().__init__(**kwargs)self.function=functionself.types_to_apply=types_to_applyself.args_names=['function','types_to_apply']defapply_transform(self,subject:Subject)->Subject:images=subject.get_images(intensity_only=False,include=self.include,exclude=self.exclude,)forimageinimages:image_type=image[TYPE]ifself.types_to_applyisnotNone:ifimage_typenotinself.types_to_apply:continuefunction_arg=image.dataresult=self.function(function_arg)ifnotisinstance(result,torch.Tensor):message=('The returned value from the callable argument must be'f' of type {torch.Tensor}, not {type(result)}')raiseValueError(message)ifresult.ndim!=function_arg.ndim:message=('The number of dimensions of the returned value must'f' be {function_arg.ndim}, not {result.ndim}')raiseValueError(message)image.set_data(result)returnsubject