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

Code before:
        MapTransform.__init__(self, keys, allow_missing_keys)
        RandomizableTransform.__init__(self, prob)
        self.filter = ImageFilter(kernel, kernel_size, **kwargs)

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


class ApplyTransformToPointsd(MapTransform, InvertibleTransform):
    """
    Dictionary-based wrapper of :py:class:`monai.transforms.ApplyTransformToPoints`.
    The input coordinates are assumed to be in the shape (C, N, 2 or 3),
    where C represents the number of channels and N denotes the number of points.
    The output has the same shape as the input.

    Args:
        keys: keys of the corresponding items to be transformed.
            See also: monai.transforms.MapTransform
        refer_keys: The key of the reference item used for transformation.
            It can directly refer to an affine or an image from which the affine can be derived. It can also be a
            sequence of keys.
        dtype: The desired data type for the output.
        affine: A 3x3 or 4x4 affine transformation matrix applied to points. This matrix typically originates
            from the image. For 2D points, a 3x3 matrix can be provided, avoiding the need to add an unnecessary
            Z dimension. While a 4x4 matrix is required for 3D transformations, it's important to note that when
            applying a 4x4 matrix to 2D points, the additional dimensions are handled accordingly.
            The matrix is always converted to float64 for computation, which can be computationally
            expensive when applied to a large number of points.
            If None, will try to use the affine matrix from the refer data.
        invert_affine: Whether to invert the affine transformation matrix applied to the points. Defaults to ``True``.
            Typically, the affine matrix is derived from the image, while the points are in world coordinates.
            If you want to align the points with the image, set this to ``True``. Otherwise, set it to ``False``.
        affine_lps_to_ras: Defaults to ``False``. Set to `True` if your point data is in the RAS coordinate system
            or you're using `ITKReader` with `affine_lps_to_ras=True`.
            This ensures the correct application of the affine transformation between LPS (left-posterior-superior)
            and RAS (right-anterior-superior) coordinate systems. This argument ensures the points and the affine
            matrix are in the same coordinate system.
        allow_missing_keys: Don't raise exception if key is missing.
    """

    def __init__(
        self,
        keys: KeysCollection,
        refer_keys: KeysCollection | None = None,
        dtype: DtypeLike | torch.dtype = torch.float64,
        affine: torch.Tensor | None = None,

Review comment (suggestion):
```suggestion
            It can directly refer to an affine or an image from which the affine can be derived. It can also be a
            sequence of keys, in which case each refers to the affine applied to the matching points in `keys`.
```

Code after:
        MapTransform.__init__(self, keys, allow_missing_keys)
        RandomizableTransform.__init__(self, prob)
        self.filter = ImageFilter(kernel, kernel_size, **kwargs)

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


class ApplyTransformToPointsd(MapTransform, InvertibleTransform):
    """
    Dictionary-based wrapper of :py:class:`monai.transforms.ApplyTransformToPoints`.
    The input coordinates are assumed to be in the shape (C, N, 2 or 3),
    where C represents the number of channels and N denotes the number of points.
    The output has the same shape as the input.

    Args:
        keys: keys of the corresponding items to be transformed.
            See also: monai.transforms.MapTransform
        refer_keys: The key of the reference item used for transformation.
            It can directly refer to an affine or an image from which the affine can be derived. It can also be a
            sequence of keys, in which case each refers to the affine applied to the matching points in `keys`.
        dtype: The desired data type for the output.
        affine: A 3x3 or 4x4 affine transformation matrix applied to points. This matrix typically originates
            from the image. For 2D points, a 3x3 matrix can be provided, avoiding the need to add an unnecessary
            Z dimension. While a 4x4 matrix is required for 3D transformations, it's important to note that when
            applying a 4x4 matrix to 2D points, the additional dimensions are handled accordingly.
            The matrix is always converted to float64 for computation, which can be computationally
            expensive when applied to a large number of points.
            If None, will try to use the affine matrix from the refer data.
        invert_affine: Whether to invert the affine transformation matrix applied to the points. Defaults to ``True``.
            Typically, the affine matrix is derived from the image, while the points are in world coordinates.
            If you want to align the points with the image, set this to ``True``. Otherwise, set it to ``False``.
        affine_lps_to_ras: Defaults to ``False``. Set to `True` if your point data is in the RAS coordinate system
            or you're using `ITKReader` with `affine_lps_to_ras=True`.
            This ensures the correct application of the affine transformation between LPS (left-posterior-superior)
            and RAS (right-anterior-superior) coordinate systems. This argument ensures the points and the affine
            matrix are in the same coordinate system.
        allow_missing_keys: Don't raise exception if key is missing.
    """

    def __init__(
        self,
        keys: KeysCollection,
        refer_keys: KeysCollection | None = None,
        dtype: DtypeLike | torch.dtype = torch.float64,
        affine: torch.Tensor | None = None,