Dictionary-based wrapper of :py:class:`monai.transforms.Lambda`. For example: .. code-block:: python :emphasize-lines: 2 input_data={'image': np.zeros((10, 2, 2)), 'label': np.ones((10, 2, 2))} lambd = Lambdad(keys='label', func=lambda x: x[:4, :, :])
| 1021 | |
| 1022 | |
| 1023 | class Lambdad(MapTransform, InvertibleTransform): |
| 1024 | """ |
| 1025 | Dictionary-based wrapper of :py:class:`monai.transforms.Lambda`. |
| 1026 | |
| 1027 | For example: |
| 1028 | |
| 1029 | .. code-block:: python |
| 1030 | :emphasize-lines: 2 |
| 1031 | |
| 1032 | input_data={'image': np.zeros((10, 2, 2)), 'label': np.ones((10, 2, 2))} |
| 1033 | lambd = Lambdad(keys='label', func=lambda x: x[:4, :, :]) |
| 1034 | print(lambd(input_data)['label'].shape) |
| 1035 | (4, 2, 2) |
| 1036 | |
| 1037 | |
| 1038 | Args: |
| 1039 | keys: keys of the corresponding items to be transformed. |
| 1040 | See also: :py:class:`monai.transforms.compose.MapTransform` |
| 1041 | func: Lambda/function to be applied. It also can be a sequence of Callable, |
| 1042 | each element corresponds to a key in ``keys``. |
| 1043 | inv_func: Lambda/function of inverse operation if want to invert transforms, default to `lambda x: x`. |
| 1044 | It also can be a sequence of Callable, each element corresponds to a key in ``keys``. |
| 1045 | track_meta: If `False`, then standard data objects will be returned (e.g., torch.Tensor` and `np.ndarray`) |
| 1046 | as opposed to MONAI's enhanced objects. By default, this is `True`. |
| 1047 | overwrite: whether to overwrite the original data in the input dictionary with lambda function output. it |
| 1048 | can be bool or str, when setting to str, it will create a new key for the output and keep the value of |
| 1049 | key intact. default to True. it also can be a sequence of bool or str, each element corresponds to a key |
| 1050 | in ``keys``. |
| 1051 | allow_missing_keys: don't raise exception if key is missing. |
| 1052 | |
| 1053 | Note: The inverse operation doesn't allow to define `extra_info` or access other information, such as the |
| 1054 | image's original size. If need these complicated information, please write a new InvertibleTransform directly. |
| 1055 | |
| 1056 | """ |
| 1057 | |
| 1058 | backend = Lambda.backend |
| 1059 | |
| 1060 | def __init__( |
| 1061 | self, |
| 1062 | keys: KeysCollection, |
| 1063 | func: Sequence[Callable] | Callable, |
| 1064 | inv_func: Sequence[Callable] | Callable = no_collation, |
| 1065 | track_meta: bool = True, |
| 1066 | overwrite: Sequence[bool] | bool | Sequence[str] | str = True, |
| 1067 | allow_missing_keys: bool = False, |
| 1068 | ) -> None: |
| 1069 | super().__init__(keys, allow_missing_keys) |
| 1070 | self.func = ensure_tuple_rep(func, len(self.keys)) |
| 1071 | self.inv_func = ensure_tuple_rep(inv_func, len(self.keys)) |
| 1072 | self.overwrite = ensure_tuple_rep(overwrite, len(self.keys)) |
| 1073 | self._lambd = Lambda(track_meta=track_meta) |
| 1074 | |
| 1075 | def __call__(self, data: Mapping[Hashable, torch.Tensor]) -> dict[Hashable, torch.Tensor]: |
| 1076 | d = dict(data) |
| 1077 | for key, func, overwrite in self.key_iterator(d, self.func, self.overwrite): |
| 1078 | ret = self._lambd(img=d[key], func=func) |
| 1079 | if overwrite and isinstance(overwrite, bool): |
| 1080 | d[key] = ret |
no outgoing calls
searching dependent graphs…