code: def xyz_affine(coordmap, name2xyz=None):
      """Return (4, 4) affine mapping voxel coordinates to XYZ from `coordmap`
  
      If no (4, 4) affine "makes sense"(TM) for this `coordmap` then raise errors
      listed below.  A (4, 4) affine makes sense if the first three output axes
      are recognizably X, Y, and Z in that order AND they there are corresponding
      input dimensions, AND the corresponding input dimensions are the first three
      input dimension (in any order).  Thus the input axes have to be 3D.
  
      Parameters
      ----------
      coordmap : ``CoordinateMap`` instance
      name2xyz : None or mapping, optional
          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.
  
      Returns
      -------
      xyz_aff : (4,4) array
          voxel to X, Y, Z affine mapping
  
      Raises
      ------
      SpaceTypeError : if this is not an affine coordinate map
      AxesError : if not all of x, y, z recognized in `coordmap` output, or they
      are in the wrong order, or the x, y, z axes do not correspond to the first
      three input axes.
      AffineError : if axes dropped from the affine contribute to x, y, z
      coordinates.
  
      Notes
      -----
      We could also try and "make sense" (TM) of a coordmap that had X, Y and Z
      outputs, but not in that order, nor all in the first three axes.  In that
      case we could just permute the affine to get the output order we need.  But,
      that could become confusing if the returned affine has different output
      coordinates than the passed `coordmap`.  And it's more complicated.  So,
      let's not do that for now.
  
      Examples
      --------
      >>> cmap = vox2mni(np.diag([2,3,4,5,1]))
      >>> cmap
      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(cmap)
      array([[ 2.,  0.,  0.,  0.],
             [ 0.,  3.,  0.,  0.],
             [ 0.,  0.,  4.,  0.],
             [ 0.,  0.,  0.,  1.]])
      """
      if name2xyz is None:
          name2xyz = known_names
      try:
          affine = coordmap.affine
      except AttributeError:
          raise SpaceTypeError("Need affine coordinate map")
      order = xyz_order(coordmap.function_range, name2xyz)
      if order[:3] != [0, 1, 2]:
          raise AxesError("First 3 output axes must be X, Y, Z")
      # Check equivalent input axes
      ornt = io_orientation(affine)
      if set(ornt[:3, 0]) != {0, 1, 2}:
          raise AxesError(
              "First 3 input axes must correspond to X, Y, Z"
          )
      # Check that dropped dimensions don't provide xyz coordinate info
      extra_cols = affine[:3, 3:-1]
      if not np.allclose(extra_cols, 0):
          raise AffineError("Dropped dimensions not orthogonal to xyz")
      return from_matvec(affine[:3, :3], affine[:3, -1])
  
imports: import numpy
  from nibabel import io_orientation
  from nibabel.affines import from_matvec
  from .coordinate_map import CoordMapMaker
  from .coordinate_system import CoordSysMaker, is_coordsys, is_coordsys_maker
label_desc: Use image node_id 3 for calling a global function with example usage: xyz_affine(coordmap, name2xyz) and returns: from_matvec