MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / SpatialPad

Class SpatialPad

monai/transforms/croppad/array.py:182–237  ·  view source on GitHub ↗

Performs padding to the data, symmetric for all sides or all on one side for each dimension. This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic ` for more information. Args: spatial_size: the spatial size of output data after p

Source from the content-addressed store, hash-verified

180
181
182class SpatialPad(Pad):
183 """
184 Performs padding to the data, symmetric for all sides or all on one side for each dimension.
185
186 This transform is capable of lazy execution. See the :ref:`Lazy Resampling topic<lazy_resampling>`
187 for more information.
188
189 Args:
190 spatial_size: the spatial size of output data after padding, if a dimension of the input
191 data size is larger than the pad size, will not pad that dimension.
192 If its components have non-positive values, the corresponding size of input image will be used
193 (no padding). for example: if the spatial size of input data is [30, 30, 30] and
194 `spatial_size=[32, 25, -1]`, the spatial size of output data will be [32, 30, 30].
195 method: {``"symmetric"``, ``"end"``}
196 Pad image symmetrically on every side or only pad at the end sides. Defaults to ``"symmetric"``.
197 mode: available modes for numpy array:{``"constant"``, ``"edge"``, ``"linear_ramp"``, ``"maximum"``,
198 ``"mean"``, ``"median"``, ``"minimum"``, ``"reflect"``, ``"symmetric"``, ``"wrap"``, ``"empty"``}
199 available modes for PyTorch Tensor: {``"constant"``, ``"reflect"``, ``"replicate"``, ``"circular"``}.
200 One of the listed string values or a user supplied function. Defaults to ``"constant"``.
201 See also: https://numpy.org/doc/1.18/reference/generated/numpy.pad.html
202 https://pytorch.org/docs/stable/generated/torch.nn.functional.pad.html
203 lazy: a flag to indicate whether this transform should execute lazily or not. Defaults to False.
204 kwargs: other arguments for the `np.pad` or `torch.pad` function.
205 note that `np.pad` treats channel dimension as the first dimension.
206
207 """
208
209 def __init__(
210 self,
211 spatial_size: Sequence[int] | int | tuple[tuple[int, ...] | int, ...],
212 method: str = Method.SYMMETRIC,
213 mode: str = PytorchPadMode.CONSTANT,
214 lazy: bool = False,
215 **kwargs,
216 ) -> None:
217 self.spatial_size = spatial_size
218 self.method: Method = look_up_option(method, Method)
219 super().__init__(mode=mode, lazy=lazy, **kwargs)
220
221 def compute_pad_width(self, spatial_shape: Sequence[int]) -> tuple[tuple[int, int]]:
222 """
223 dynamically compute the pad width according to the spatial shape.
224
225 Args:
226 spatial_shape: spatial shape of the original image.
227
228 """
229 spatial_size = fall_back_tuple(self.spatial_size, spatial_shape)
230 if self.method == Method.SYMMETRIC:
231 pad_width = []
232 for i, sp_i in enumerate(spatial_size):
233 width = max(sp_i - spatial_shape[i], 0)
234 pad_width.append((int(width // 2), int(width - (width // 2))))
235 else:
236 pad_width = [(0, int(max(sp_i - spatial_shape[i], 0))) for i, sp_i in enumerate(spatial_size)]
237 return tuple([(0, 0)] + pad_width) # type: ignore
238
239

Callers 11

__call__Method · 0.90
pad_imagesFunction · 0.90
__init__Method · 0.90
__call__Method · 0.90
matshow3dFunction · 0.90
__init__Method · 0.90
__init__Method · 0.90
test_decollate.pyFile · 0.90
test_array_transformMethod · 0.90
compute_pad_widthMethod · 0.85
__init__Method · 0.85

Calls

no outgoing calls

Tested by 1

test_array_transformMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…