class_docstr: Warp an image with given dense displacement field (DDF).
class_docstr_tok: ["Warp","an","image","with","given","dense","displacement","field","(","DDF",")","."]
docstr: Args:
      image: Tensor in shape (batch, num_channels, H, W[, D])
      ddf: Tensor in the same spatial size as image, in shape (batch, ``spatial_dims``, H, W[, D])
  
  Returns:
      warped_image in the same shape as image (batch, num_channels, H, W[, D])
docstr_tok: ["Args",":","image",":","Tensor","in","shape","(","batch",",","num_channels",",","H",",","W","[",",","D","]",")","ddf",":","Tensor","in","the","same","spatial","size","as","image",",","in","shape","(","batch",",","``","spatial_dims","``",",","H",",","W","[",",","D","]",")","Returns",":","warped_image","in","the","same","shape","as","image","(","batch",",","num_channels",",","H",",","W","[",",","D","]",")"]
code: def forward(self, image: torch.Tensor, ddf: torch.Tensor):
      """
      Args:
          image: Tensor in shape (batch, num_channels, H, W[, D])
          ddf: Tensor in the same spatial size as image, in shape (batch, ``spatial_dims``, H, W[, D])
  
      Returns:
          warped_image in the same shape as image (batch, num_channels, H, W[, D])
      """
      spatial_dims = len(image.shape) - 2
      if spatial_dims not in (2, 3):
          raise NotImplementedError(
              f"got unsupported spatial_dims={spatial_dims}, currently support 2 or 3."
          )
      ddf_shape = (image.shape[0], spatial_dims) + tuple(
          image.shape[2:]
      )
      if ddf.shape != ddf_shape:
          raise ValueError(
              f"Given input {spatial_dims}-d image shape {image.shape}, the input DDF shape must be {ddf_shape}, "
              f"Got {ddf.shape} instead."
          )
      grid = self.get_reference_grid(ddf, jitter=self.jitter) + ddf
      grid = grid.permute(
          [0] + list(range(2, 2 + spatial_dims)) + [1]
      )  # (batch, ..., spatial_dims)
  
      if not USE_COMPILED:  # pytorch native grid_sample
          for i, dim in enumerate(grid.shape[1:-1]):
              grid[..., i] = grid[..., i] * 2 / (dim - 1) - 1
          index_ordering: list[int] = list(
              range(spatial_dims - 1, -1, -1)
          )
          grid = grid[..., index_ordering]  # z, y, x -> x, y, z
          return F.grid_sample(
              image,
              grid,
              mode=self._interp_mode,
              padding_mode=f"{self._padding_mode}",
              align_corners=True,
          )
  
      # using csrc resampling
      return grid_pull(
          image,
          grid,
          bound=self._padding_mode,
          extrapolate=True,
          interpolation=self._interp_mode,
      )
  
file_path: MONAI/monai/networks/blocks/warp.py
imports: from __future__ import annotations
  import warnings
  import torch
  from torch import nn
  from torch.nn import functional
  from monai.config.deviceconfig import USE_COMPILED
  from monai.networks.layers.spatial_transforms import grid_pull
  from monai.networks.utils import meshgrid_ij
  from monai.utils import GridSampleMode, GridSamplePadMode, optional_import
label_desc: Use image node_id 3 for calling the Warp obj's underlying member method code with example usage: obj.forward(image, ddf) and returns: grid_pull, F