code: def itk_warp(img, ddf):
      """
      warping with python itk
      Args:
          img: numpy array of shape (D, H, W)
          ddf: numpy array of shape (3, D, H, W)
  
      Returns:
          warped_img: numpy arrap of shape (D, H, W)
      """
      import itk
  
      # 3, D, H, W -> D, H, W, 3
      ddf = ddf.transpose((1, 2, 3, 0))
      # x, y, z -> z, x, y
      ddf = ddf[..., ::-1]
  
      dimension = 3
  
      # initialise image
      pixel_type = itk.F  # float32
      image_type = itk.Image[pixel_type, dimension]
      itk_img = itk.PyBuffer[image_type].GetImageFromArray(
          img.astype(np.float32), is_vector=None
      )
  
      # initialise displacement field
      vector_component_type = itk.F
      vector_pixel_type = itk.Vector[vector_component_type, dimension]
      displacement_field_type = itk.Image[vector_pixel_type, dimension]
      displacement_field = itk.PyBuffer[
          displacement_field_type
      ].GetImageFromArray(ddf.astype(np.float32), is_vector=True)
  
      # initialise warp_filter
      warp_filter = itk.WarpImageFilter[
          image_type, image_type, displacement_field_type
      ].New()
      interpolator = itk.LinearInterpolateImageFunction[
          image_type, itk.D
      ].New()
      warp_filter.SetInterpolator(interpolator)
      warp_filter.SetOutputSpacing(itk_img.GetSpacing())
      warp_filter.SetOutputOrigin(itk_img.GetOrigin())
      warp_filter.SetOutputDirection(itk_img.GetDirection())
  
      # warp
      warp_filter.SetDisplacementField(displacement_field)
      warp_filter.SetInput(itk_img)
      warped_img = warp_filter.GetOutput()
      warped_img = np.asarray(warped_img)
  
      return warped_img
  
imports: from __future__ import annotations
  import os
  import unittest
  import numpy
  import torch
  from parameterized import parameterized
  from torch.autograd import gradcheck
  from monai.config.deviceconfig import USE_COMPILED
  from monai.networks.blocks.warp import Warp
  from monai.transforms import LoadImaged
  from monai.utils import GridSampleMode, GridSamplePadMode
  from tests.utils import SkipIfBeforePyTorchVersion, SkipIfNoModule, download_url_or_skip_test, skip_if_quick, testing_data_config
label_desc: Use image node_id 2 for calling a global function with example usage: itk_warp(img, ddf) and returns: warped_img