Skip to content
6 changes: 6 additions & 0 deletions docs/source/transforms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ Vanilla Transforms
Crop and Pad
^^^^^^^^^^^^

`Pad`
"""""
.. autoclass:: Pad
:members:
:special-members: __call__

`SpatialPad`
""""""""""""
.. autoclass:: SpatialPad
Expand Down
4 changes: 3 additions & 1 deletion monai/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
CenterSpatialCrop,
CropForeground,
DivisiblePad,
Pad,
RandCropByLabelClasses,
RandCropByPosNegLabel,
RandScaleCrop,
Expand Down Expand Up @@ -48,7 +49,7 @@
DivisiblePadd,
DivisiblePadD,
DivisiblePadDict,
NumpyPadModeSequence,
PadModeSequence,
RandCropByLabelClassesd,
RandCropByLabelClassesD,
RandCropByLabelClassesDict,
Expand Down Expand Up @@ -490,6 +491,7 @@
allow_missing_keys_mode,
compute_divisible_spatial_size,
convert_inverse_interp_mode,
convert_pad_mode,
copypaste_arrays,
create_control_grid,
create_grid,
Expand Down
173 changes: 107 additions & 66 deletions monai/transforms/croppad/array.py

Large diffs are not rendered by default.

53 changes: 30 additions & 23 deletions monai/transforms/croppad/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
weighted_patch_samples,
)
from monai.utils import ImageMetaKey as Key
from monai.utils import Method, NumpyPadMode, ensure_tuple, ensure_tuple_rep, fall_back_tuple
from monai.utils import Method, NumpyPadMode, PytorchPadMode, ensure_tuple, ensure_tuple_rep, fall_back_tuple
from monai.utils.enums import InverseKeys

__all__ = [
"NumpyPadModeSequence",
"PadModeSequence",
"SpatialPadd",
"BorderPadd",
"DivisiblePadd",
Expand Down Expand Up @@ -99,6 +99,7 @@
]

NumpyPadModeSequence = Union[Sequence[Union[NumpyPadMode, str]], NumpyPadMode, str]
PadModeSequence = Union[Sequence[Union[NumpyPadMode, PytorchPadMode, str]], NumpyPadMode, PytorchPadMode, str]


class SpatialPadd(MapTransform, InvertibleTransform):
Expand All @@ -114,9 +115,9 @@ def __init__(
keys: KeysCollection,
spatial_size: Union[Sequence[int], int],
method: Union[Method, str] = Method.SYMMETRIC,
mode: NumpyPadModeSequence = NumpyPadMode.CONSTANT,
mode: PadModeSequence = NumpyPadMode.CONSTANT,
allow_missing_keys: bool = False,
**np_kwargs,
**kwargs,
) -> None:
"""
Args:
Expand All @@ -129,19 +130,21 @@ def __init__(
the spatial size of output data will be [32, 30, 30].
method: {``"symmetric"``, ``"end"``}
Pad image symmetrically on every side or only pad at the end sides. Defaults to ``"symmetric"``.
mode: {``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, ``"mean"``,
``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,
``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
available modes for PyTorch Tensor: {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}.
One of the listed string values or a user supplied function. Defaults to ``"constant"``.
See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
It also can be a sequence of string, each element corresponds to a key in ``keys``.
allow_missing_keys: don't raise exception if key is missing.
np_kwargs: other args for `np.pad` API, note that `np.pad` treats channel dimension as the first dimension.
more details: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
kwargs: other arguments for the `np.pad` or `torch.pad` function.
note that `np.pad` treats channel dimension as the first dimension.

"""
super().__init__(keys, allow_missing_keys)
self.mode = ensure_tuple_rep(mode, len(self.keys))
self.padder = SpatialPad(spatial_size, method, **np_kwargs)
self.padder = SpatialPad(spatial_size, method, **kwargs)

def __call__(self, data: Mapping[Hashable, NdarrayTensor]) -> Dict[Hashable, NdarrayTensor]:
d = dict(data)
Expand Down Expand Up @@ -183,9 +186,9 @@ def __init__(
self,
keys: KeysCollection,
spatial_border: Union[Sequence[int], int],
mode: NumpyPadModeSequence = NumpyPadMode.CONSTANT,
mode: PadModeSequence = NumpyPadMode.CONSTANT,
allow_missing_keys: bool = False,
**np_kwargs,
**kwargs,
) -> None:
"""
Args:
Expand All @@ -202,19 +205,21 @@ def __init__(
pad bottom of H dim with 2, pad left of W dim with 3, pad right of W dim with 4.
the result shape is [1, 7, 11].

mode: {``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, ``"mean"``,
``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,
``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
available modes for PyTorch Tensor: {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}.
One of the listed string values or a user supplied function. Defaults to ``"constant"``.
See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
It also can be a sequence of string, each element corresponds to a key in ``keys``.
allow_missing_keys: don't raise exception if key is missing.
np_kwargs: other args for `np.pad` API, note that `np.pad` treats channel dimension as the first dimension.
more details: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
kwargs: other arguments for the `np.pad` or `torch.pad` function.
note that `np.pad` treats channel dimension as the first dimension.

"""
super().__init__(keys, allow_missing_keys)
self.mode = ensure_tuple_rep(mode, len(self.keys))
self.padder = BorderPad(spatial_border=spatial_border, **np_kwargs)
self.padder = BorderPad(spatial_border=spatial_border, **kwargs)

def __call__(self, data: Mapping[Hashable, NdarrayTensor]) -> Dict[Hashable, NdarrayTensor]:
d = dict(data)
Expand Down Expand Up @@ -260,10 +265,10 @@ def __init__(
self,
keys: KeysCollection,
k: Union[Sequence[int], int],
mode: NumpyPadModeSequence = NumpyPadMode.CONSTANT,
mode: PadModeSequence = NumpyPadMode.CONSTANT,
method: Union[Method, str] = Method.SYMMETRIC,
allow_missing_keys: bool = False,
**np_kwargs,
**kwargs,
) -> None:
"""
Args:
Expand All @@ -272,23 +277,25 @@ def __init__(
k: the target k for each spatial dimension.
if `k` is negative or 0, the original size is preserved.
if `k` is an int, the same `k` be applied to all the input spatial dimensions.
mode: {``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``, ``"mean"``,
``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,
``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
available modes for PyTorch Tensor: {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}.
One of the listed string values or a user supplied function. Defaults to ``"constant"``.
See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
It also can be a sequence of string, each element corresponds to a key in ``keys``.
method: {``"symmetric"``, ``"end"``}
Pad image symmetrically on every side or only pad at the end sides. Defaults to ``"symmetric"``.
allow_missing_keys: don't raise exception if key is missing.
np_kwargs: other args for `np.pad` API, note that `np.pad` treats channel dimension as the first dimension.
more details: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
kwargs: other arguments for the `np.pad` or `torch.pad` function.
note that `np.pad` treats channel dimension as the first dimension.

See also :py:class:`monai.transforms.SpatialPad`

"""
super().__init__(keys, allow_missing_keys)
self.mode = ensure_tuple_rep(mode, len(self.keys))
self.padder = DivisiblePad(k=k, method=method, **np_kwargs)
self.padder = DivisiblePad(k=k, method=method, **kwargs)

def __call__(self, data: Mapping[Hashable, NdarrayTensor]) -> Dict[Hashable, NdarrayTensor]:
d = dict(data)
Expand Down
Loading