code: def xyz_affine(img, name2xyz=None):
      """Return xyz affine from image `img` if possible, or raise error
  
      Parameters
      ----------
      img : ``Image`` instance or nibabel image
          It has a ``coordmap`` or attribute ``affine`` or method ``get_affine``
      name2xyz : None or mapping
          Object such that ``name2xyz[ax_name]`` returns 'x', or 'y' or 'z' or
          raises a KeyError for a str ``ax_name``.  None means use module default.
          Not used for nibabel `img` input.
  
      Returns
      -------
      xyz_aff : (4,4) array
          voxel to X, Y, Z affine mapping
  
      Raises
      ------
      SpaceTypeError : if `img` does not have an affine coordinate map
      AxesError : if not all of x, y, z recognized in `img` ``coordmap`` range
      AffineError : if axes dropped from the affine contribute to x, y, z
      coordinates
  
      Examples
      --------
      >>> from nipy.core.api import vox2mni, Image
      >>> arr = np.arange(24).reshape((2,3,4,1)).astype(float)
      >>> img = Image(arr, vox2mni(np.diag([2,3,4,5,1])))
      >>> img.coordmap
      AffineTransform(
         function_domain=CoordinateSystem(coord_names=('i', 'j', 'k', 'l'), name='voxels', coord_dtype=float64),
         function_range=CoordinateSystem(coord_names=('mni-x=L->R', 'mni-y=P->A', 'mni-z=I->S', 't'), name='mni', coord_dtype=float64),
         affine=array([[ 2.,  0.,  0.,  0.,  0.],
                       [ 0.,  3.,  0.,  0.,  0.],
                       [ 0.,  0.,  4.,  0.,  0.],
                       [ 0.,  0.,  0.,  5.,  0.],
                       [ 0.,  0.,  0.,  0.,  1.]])
      )
      >>> xyz_affine(img)
      array([[ 2.,  0.,  0.,  0.],
             [ 0.,  3.,  0.,  0.],
             [ 0.,  0.,  4.,  0.],
             [ 0.,  0.,  0.,  1.]])
  
      Nibabel images always have xyz affines
  
      >>> import nibabel as nib
      >>> nimg = nib.Nifti1Image(arr, np.diag([2,3,4,1]))
      >>> xyz_affine(nimg)
      array([[ 2.,  0.,  0.,  0.],
             [ 0.,  3.,  0.,  0.],
             [ 0.,  0.,  4.,  0.],
             [ 0.,  0.,  0.,  1.]])
      """
      if hasattr(img, "coordmap"):  # nipy image
          return rsp.xyz_affine(img.coordmap, name2xyz)
      return nibcompat.get_affine(img)
  
file_path: nipy/nipy/core/image/image_spaces.py
imports: import sys
  import numpy
  from nibabel import io_orientation
  from ...io import nibcompat
  from ..image.image import Image
  from ..reference import spaces
  from ..reference.coordinate_map import AffineTransform
label_desc: Use image node_id 1 for calling a global function with example usage: xyz_affine(img, name2xyz) and returns: nibcompat, rsp