code: def test_xyz_affine(vars):
      # Getting an xyz affine from coordmaps
      aff3d = from_matvec(np.arange(9).reshape((3, 3)), [15, 16, 17])
      cmap3d = AffineTransform(vars["d_cs_r3"], vars["r_cs_r3"], aff3d)
      rzs = np.c_[np.arange(12).reshape((4, 3)), [0, 0, 0, 12]]
      aff4d = from_matvec(rzs, [15, 16, 17, 18])
      cmap4d = AffineTransform(vars["d_cs_r4"], vars["r_cs_r4"], aff4d)
      # Simplest case of 3D affine -> affine unchanged
      assert_array_equal(xyz_affine(cmap3d), aff3d)
      # 4D (5, 5) affine -> 3D equivalent
      assert_array_equal(xyz_affine(cmap4d), aff3d)
      # Any dimensions not spatial, AxesError
      r_cs = CS(("mni-x", "mni-y", "mni-q"), "mni")
      funny_cmap = AffineTransform(vars["d_cs_r3"], r_cs, aff3d)
      pytest.raises(AxesError, xyz_affine, funny_cmap)
      r_cs = CS(("mni-x", "mni-q", "mni-z"), "mni")
      funny_cmap = AffineTransform(vars["d_cs_r3"], r_cs, aff3d)
      pytest.raises(AxesError, xyz_affine, funny_cmap)
      # We insist that the coordmap is in output xyz order
      permutations = (
          (0, 2, 1),
          (1, 0, 2),
          (1, 2, 0),
          (2, 0, 1),
          (2, 1, 0),
      )
      for perm in permutations:
          pytest.raises(
              AxesError, xyz_affine, cmap3d.reordered_range(perm)
          )
      # The input order doesn't matter, as long as the xyz axes map to the first
      # three input axes
      for perm in permutations:
          assert_array_equal(
              xyz_affine(cmap3d.reordered_domain(perm)),
              aff3d[:, perm + (-1,)],
          )
      # But if the corresponding input axes not in the first three, an axis error
      wrong_inputs = cmap4d.reordered_domain([0, 1, 3, 2])
      pytest.raises(AxesError, xyz_affine, wrong_inputs)
      # xyzs must be orthogonal to dropped axis
      for i in range(3):
          aff = aff4d.copy()
          aff[i, 3] = 1
          cmap = AffineTransform(vars["d_cs_r4"], vars["r_cs_r4"], aff)
          pytest.raises(AffineError, xyz_affine, cmap)
          # And if reordered
          pytest.raises(
              AxesError, xyz_affine, cmap.reordered_range([2, 0, 1, 3])
          )
      # Non-square goes to square
      aff54 = np.array(
          [
              [0, 1, 2, 15],
              [3, 4, 5, 16],
              [6, 7, 8, 17],
              [0, 0, 0, 18],
              [0, 0, 0, 1],
          ]
      )
      cmap = AffineTransform(vars["d_cs_r3"], vars["r_cs_r4"], aff54)
      assert_array_equal(xyz_affine(cmap), aff3d)
      aff57 = np.array(
          [
              [0, 1, 2, 0, 0, 0, 15],
              [3, 4, 5, 0, 0, 0, 16],
              [6, 7, 8, 0, 0, 0, 17],
              [0, 0, 0, 0, 0, 0, 18],
              [0, 0, 0, 0, 0, 0, 1],
          ]
      )
      d_cs_r6 = CS("ijklmn", "voxels")
      cmap = AffineTransform(d_cs_r6, vars["r_cs_r4"], aff57)
      assert_array_equal(xyz_affine(cmap), aff3d)
      # Non-affine raises SpaceTypeError
      cmap_cmap = CoordinateMap(
          vars["d_cs_r4"], vars["r_cs_r4"], lambda x: x * 3
      )
      pytest.raises(SpaceTypeError, xyz_affine, cmap_cmap)
      # Not enough dimensions - SpaceTypeError
      d_cs_r2 = CS("ij", "voxels")
      r_cs_r2 = CS(vars["r_names"][:2], "mni")
      cmap = AffineTransform(
          d_cs_r2,
          r_cs_r2,
          np.array([[2, 0, 10], [0, 3, 11], [0, 0, 1]]),
      )
      pytest.raises(AxesError, xyz_affine, cmap)
      # Can pass in own validator
      my_valtor = {"blind": "x", "leading": "y", "ditch": "z"}
      r_cs = CS(("blind", "leading", "ditch"), "fall")
      cmap = AffineTransform(vars["d_cs_r3"], r_cs, aff3d)
      pytest.raises(AxesError, xyz_affine, cmap)
      assert_array_equal(xyz_affine(cmap, my_valtor), aff3d)
      # Slices in x, y, z coordmaps raise error because of missing spatial
      # dimensions
      arr = np.arange(120).reshape((2, 3, 4, 5))
      aff = np.diag([2, 3, 4, 5, 1])
      img = Image(arr, vox2mni(aff))
      pytest.raises(AxesError, xyz_affine, img[1].coordmap)
      pytest.raises(AxesError, xyz_affine, img[:, 1].coordmap)
      pytest.raises(AxesError, xyz_affine, img[:, :, 1].coordmap)
  
file_path: nipy/nipy/core/reference/tests/test_spaces.py
imports: import numpy
  import pytest
  from nibabel.affines import from_matvec
  from numpy.testing import assert_array_equal
  from ...image.image import Image
  from ..coordinate_map import AffineTransform, CoordinateMap
  from ..coordinate_system import CoordinateSystem
  from ..coordinate_system import CoordSysMakerError
  from ..spaces import AffineError, AxesError, SpaceError, SpaceTypeError, XYZSpace, get_world_cs, is_xyz_affable, is_xyz_space, known_space, known_spaces, mni_csm, mni_space, vox2aligned, vox2mni, vox2scanner, vox2talairach, vox2unknown, xyz_affine, xyz_order
label_desc: Use image node_id 8 for calling a global function with example usage: test_xyz_affine(vars) without return types