code: def plot_2d_diffeomorphic_map(
      mapping,
      delta=10,
      fname=None,
      direct_grid_shape=None,
      direct_grid2world=-1,
      inverse_grid_shape=None,
      inverse_grid2world=-1,
      show_figure=True,
      **fig_kwargs,
  ):
      r"""Draw the effect of warping a regular lattice by a diffeomorphic map.
  
      Draws a diffeomorphic map by showing the effect of the deformation on a
      regular grid. The resulting figure contains two images: the direct
      transformation is plotted to the left, and the inverse transformation is
      plotted to the right.
  
      Parameters
      ----------
      mapping : DiffeomorphicMap object
          the diffeomorphic map to be drawn
      delta : int, optional
          the size (in pixels) of the squares of the regular lattice to be used
          to plot the warping effects. Each square will be delta x delta pixels.
          By default, the size will be 10 pixels.
      fname : string, optional
          the name of the file the figure will be written to. If None (default),
          the figure will not be saved to disk.
      direct_grid_shape : tuple, shape (2,), optional
          the shape of the grid image after being deformed by the direct
          transformation. By default, the shape of the deformed grid is the
          same as the grid of the displacement field, which is by default
          equal to the shape of the fixed image. In other words, the resulting
          deformed grid (deformed by the direct transformation) will normally
          have the same shape as the fixed image.
      direct_grid2world : array, shape (3, 3), optional
          the affine transformation mapping the direct grid's coordinates to
          physical space. By default, this transformation will correspond to
          the image-to-world transformation corresponding to the default
          direct_grid_shape (in general, if users specify a direct_grid_shape,
          they should also specify direct_grid2world).
      inverse_grid_shape : tuple, shape (2,), optional
          the shape of the grid image after being deformed by the inverse
          transformation. By default, the shape of the deformed grid under the
          inverse transform is the same as the image used as "moving" when
          the diffeomorphic map was generated by a registration algorithm
          (so it corresponds to the effect of warping the static image towards
          the moving).
      inverse_grid2world : array, shape (3, 3), optional
          the affine transformation mapping inverse grid's coordinates to
          physical space. By default, this transformation will correspond to
          the image-to-world transformation corresponding to the default
          inverse_grid_shape (in general, if users specify an inverse_grid_shape,
          they should also specify inverse_grid2world).
      show_figure : bool, optional
          if True (default), the deformed grids will be plotted using matplotlib,
          else the grids are just returned
      fig_kwargs: extra parameters for saving figure, e.g. `dpi=300`.
  
      Returns
      -------
      warped_forward : array
          Image with the grid showing the effect of transforming the moving image to
          the static image.  The shape will be `direct_grid_shape` if specified,
          otherwise the shape of the static image.
      warped_backward : array
          Image with the grid showing the effect of transforming the static image to
          the moving image.  Shape will be `inverse_grid_shape` if specified,
          otherwise the shape of the moving image.
  
      Notes
      -----
      The default value for the affine transformation is "-1" to handle the case
      in which the user provides "None" as input meaning "identity". If we used
      None as default, we wouldn't know if the user specifically wants to use
      the identity (specifically passing None) or if it was left unspecified,
      meaning to use the appropriate default matrix.
      """
      if mapping.is_inverse:
          # By default, direct_grid_shape is the codomain grid
          if direct_grid_shape is None:
              direct_grid_shape = mapping.codomain_shape
          if direct_grid2world == -1:
              direct_grid2world = mapping.codomain_grid2world
  
          # By default, the inverse grid is the domain grid
          if inverse_grid_shape is None:
              inverse_grid_shape = mapping.domain_shape
          if inverse_grid2world == -1:
              inverse_grid2world = mapping.domain_grid2world
      else:
          # Now by default, direct_grid_shape is the mapping's input grid
          if direct_grid_shape is None:
              direct_grid_shape = mapping.domain_shape
          if direct_grid2world == -1:
              direct_grid2world = mapping.domain_grid2world
  
          # By default, the output grid is the mapping's domain grid
          if inverse_grid_shape is None:
              inverse_grid_shape = mapping.codomain_shape
          if inverse_grid2world == -1:
              inverse_grid2world = mapping.codomain_grid2world
  
      # The world-to-image (image = drawn lattice on the output grid)
      # transformation is the inverse of the output affine
      world_to_image = None
      if inverse_grid2world is not None:
          world_to_image = np.linalg.inv(inverse_grid2world)
  
      # Draw the squares on the output grid
      lattice_out = draw_lattice_2d(
          (inverse_grid_shape[0] + delta) // (delta + 1),
          (inverse_grid_shape[1] + delta) // (delta + 1),
          delta,
      )
      lattice_out = lattice_out[
          0 : inverse_grid_shape[0], 0 : inverse_grid_shape[1]
      ]
  
      # Warp in the forward direction (sampling it on the input grid)
      warped_forward = mapping.transform(
          lattice_out,
          "linear",
          world_to_image,
          direct_grid_shape,
          direct_grid2world,
      )
  
      # Now, the world-to-image (image = drawn lattice on the input grid)
      # transformation is the inverse of the input affine
      world_to_image = None
      if direct_grid2world is not None:
          world_to_image = np.linalg.inv(direct_grid2world)
  
      # Draw the squares on the input grid
      lattice_in = draw_lattice_2d(
          (direct_grid_shape[0] + delta) // (delta + 1),
          (direct_grid_shape[1] + delta) // (delta + 1),
          delta,
      )
      lattice_in = lattice_in[
          0 : direct_grid_shape[0], 0 : direct_grid_shape[1]
      ]
  
      # Warp in the backward direction (sampling it on the output grid)
      warped_backward = mapping.transform_inverse(
          lattice_in,
          "linear",
          world_to_image,
          inverse_grid_shape,
          inverse_grid2world,
      )
  
      # Now plot the grids
      if show_figure:
          plt.figure()
  
          plt.subplot(1, 2, 1).set_axis_off()
          plt.imshow(warped_forward, cmap=plt.cm.gray)
          plt.title("Direct transform")
  
          plt.subplot(1, 2, 2).set_axis_off()
          plt.imshow(warped_backward, cmap=plt.cm.gray)
          plt.title("Inverse transform")
  
          # Finally, save the figure to disk
          if fname is not None:
              plt.savefig(fname, bbox_inches="tight", **fig_kwargs)
  
      # Return the deformed grids
      return warped_forward, warped_backward
  
file_path: dipy/dipy/viz/regtools.py
imports: import numpy
  from dipy.utils.optpkg import optional_package
label_desc: Use image node_id 5 for calling a global function with example usage: plot_2d_diffeomorphic_map(mapping, delta, fname, direct_grid_shape, direct_grid2world, inverse_grid_shape, inverse_grid2world, show_figure) and returns: warped_forward,  warped_backward