Repository: Project-MONAI/MONAI
File: monai/transforms/utility/dictionary.py (Python)

Code before:
    """
    Dictionary-based wrapper of :py:class:`monai.transforms.TorchIO` for transforms.
    All transforms in TorchIO can be applied randomly with probability p by specifying the `p=` argument.
    """

    backend = TorchIO.backend

    def __init__(self, keys: KeysCollection, name: str, allow_missing_keys: bool = False, *args, **kwargs) -> None:
        """
        Args:
            keys: keys of the corresponding items to be transformed.
                See also: :py:class:`monai.transforms.compose.MapTransform`
            name: The transform name in TorchIO package.
            allow_missing_keys: don't raise exception if key is missing.
            args: parameters for the TorchIO transform.
            kwargs: parameters for the TorchIO transform.

        """
        super().__init__(keys, allow_missing_keys)
        self.name = name
        self.trans = TorchIO(name, *args, **kwargs)

    def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> dict[Hashable, NdarrayOrTensor]:
        d = dict(data)
        for key in self.key_iterator(d):
            d[key] = self.trans(d[key])
        return d


class MapLabelValued(MapTransform):
    """
    Dictionary-based wrapper of :py:class:`monai.transforms.MapLabelValue`.
    """

    backend = MapLabelValue.backend

    def __init__(
        self,
        keys: KeysCollection,
        orig_labels: Sequence,
        target_labels: Sequence,
        dtype: DtypeLike = np.float32,
        allow_missing_keys: bool = False,
    ) -> None:
        """
        Args:
            keys: keys of the corresponding items to be transformed.
                See also: :py:class:`monai.transforms.compose.MapTransform`
            orig_labels: original labels that map to others.
            target_labels: expected label values, 1: 1 map to the `orig_labels`.
            dtype: convert the output data to dtype, default to float32.

Review comment (suggestion):
Now that I think of it: The randomness applied here would be different for every key, right? This is especially relevant for spatial transforms (e.g. affine) that need to do the same to an image and it's label. Therefore the dictionary variant should either emit a warning (plus it being mentioned in the docstring) or should only allow a single key.

Code after:
        allow_missing_keys: don't raise exception if key is missing.
        args: parameters for the TorchVision transform.
        kwargs: parameters for the TorchVision transform.

    Note:

        - As most of the TorchVision transforms only work for PIL image and PyTorch Tensor, this transform expects input
          data to be dict of PyTorch Tensors. Users should call `ToTensord` transform first to convert Numpy to Tensor.
        - This class inherits the ``Randomizable`` purely to prevent any dataset caching to skip the transform
          computation. If the random factor of the underlying torchvision transform is not derived from `self.R`,
          the results may not be deterministic. See Also: :py:class:`monai.transforms.Randomizable`.

    """

    backend = TorchVision.backend

    def __init__(self, keys: KeysCollection, name: str, allow_missing_keys: bool = False, *args, **kwargs) -> None:
        MapTransform.__init__(self, keys, allow_missing_keys)
        self.name = name
        self.trans = TorchVision(name, *args, **kwargs)

    def __call__(self, data: Mapping[Hashable, NdarrayOrTensor]) -> dict[Hashable, NdarrayOrTensor]:
        d = dict(data)
        for key in self.key_iterator(d):
            d[key] = self.trans(d[key])
        return d


class TorchIOd(MapTransform):
    """
    Dictionary-based wrapper of :py:class:`monai.transforms.TorchIO` for non-randomized transforms.
    For randomized transforms of TorchIO use :py:class:`monai.transforms.RandTorchIOd`.
    """

    backend = TorchIO.backend

    def __init__(self, keys: KeysCollection, name: str, allow_missing_keys: bool = False, *args, **kwargs) -> None:
        """
        Args:
            keys: keys of the corresponding items to be transformed.
                See also: :py:class:`monai.transforms.compose.MapTransform`
            name: The transform name in TorchIO package.
            allow_missing_keys: don't raise exception if key is missing.
            args: parameters for the TorchIO transform.
            kwargs: parameters for the TorchIO transform.

        """
        super().__init__(keys, allow_missing_keys)
        self.name = name
        kwargs["include"] = self.keys