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

Method resample_if_needed

monai/data/image_writer.py:208–281  ·  view source on GitHub ↗

Convert the ``data_array`` into the coordinate system specified by ``target_affine``, from the current coordinate definition of ``affine``. If the transform between ``affine`` and ``target_affine`` could be achieved by simply transposing and flipping ``data_array``,

(
        cls,
        data_array: NdarrayOrTensor,
        affine: NdarrayOrTensor | None = None,
        target_affine: NdarrayOrTensor | None = None,
        output_spatial_shape: Sequence[int] | int | None = None,
        mode: str = GridSampleMode.BILINEAR,
        padding_mode: str = GridSamplePadMode.BORDER,
        align_corners: bool = False,
        dtype: DtypeLike = np.float64,
    )

Source from the content-addressed store, hash-verified

206
207 @classmethod
208 def resample_if_needed(
209 cls,
210 data_array: NdarrayOrTensor,
211 affine: NdarrayOrTensor | None = None,
212 target_affine: NdarrayOrTensor | None = None,
213 output_spatial_shape: Sequence[int] | int | None = None,
214 mode: str = GridSampleMode.BILINEAR,
215 padding_mode: str = GridSamplePadMode.BORDER,
216 align_corners: bool = False,
217 dtype: DtypeLike = np.float64,
218 ):
219 """
220 Convert the ``data_array`` into the coordinate system specified by
221 ``target_affine``, from the current coordinate definition of ``affine``.
222
223 If the transform between ``affine`` and ``target_affine`` could be
224 achieved by simply transposing and flipping ``data_array``, no resampling
225 will happen. Otherwise, this function resamples ``data_array`` using the
226 transformation computed from ``affine`` and ``target_affine``.
227
228 This function assumes the NIfTI dimension notations. Spatially it
229 supports up to three dimensions, that is, H, HW, HWD for 1D, 2D, 3D
230 respectively. When saving multiple time steps or multiple channels,
231 time and/or modality axes should be appended after the first three
232 dimensions. For example, shape of 2D eight-class segmentation
233 probabilities to be saved could be `(64, 64, 1, 8)`. Also, data in
234 shape `(64, 64, 8)` or `(64, 64, 8, 1)` will be considered as a
235 single-channel 3D image. The ``convert_to_channel_last`` method can be
236 used to convert the data to the format described here.
237
238 Note that the shape of the resampled ``data_array`` may subject to some
239 rounding errors. For example, resampling a 20x20 pixel image from pixel
240 size (1.5, 1.5)-mm to (3.0, 3.0)-mm space will return a 10x10-pixel
241 image. However, resampling a 20x20-pixel image from pixel size (2.0,
242 2.0)-mm to (3.0, 3.0)-mm space will output a 14x14-pixel image, where
243 the image shape is rounded from 13.333x13.333 pixels. In this case
244 ``output_spatial_shape`` could be specified so that this function
245 writes image data to a designated shape.
246
247 Args:
248 data_array: input data array to be converted.
249 affine: the current affine of ``data_array``. Defaults to identity
250 target_affine: the designated affine of ``data_array``.
251 The actual output affine might be different from this value due to precision changes.
252 output_spatial_shape: spatial shape of the output image.
253 This option is used when resampling is needed.
254 mode: available options are {``"bilinear"``, ``"nearest"``, ``"bicubic"``}.
255 This option is used when resampling is needed.
256 Interpolation mode to calculate output values. Defaults to ``"bilinear"``.
257 See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample
258 padding_mode: available options are {``"zeros"``, ``"border"``, ``"reflection"``}.
259 This option is used when resampling is needed.
260 Padding mode for outside grid values. Defaults to ``"border"``.
261 See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample
262 align_corners: boolean option of ``grid_sample`` to handle the corner convention.
263 See also: https://pytorch.org/docs/stable/nn.functional.html#grid-sample
264 dtype: data type for resampling computation. Defaults to
265 ``np.float64`` for best precision. If ``None``, use the data type of input data.

Callers 2

set_metadataMethod · 0.80
set_metadataMethod · 0.80

Calls 3

convert_to_tensorFunction · 0.90
SpatialResampleClass · 0.90
convert_data_typeFunction · 0.90

Tested by

no test coverage detected