code: def test_make_cmap():
      # Routine to put the guessing back into making coordinate maps
      d_names = list("ijklm")
      r_names = list("xyztu")
      domain_maker = CoordSysMaker(d_names, "voxels")
      range_maker = CoordSysMaker(r_names, "world")
      cmm = CoordMapMaker(domain_maker, range_maker)
      # Making with generic functions and with affines
      xform = lambda x: x + 1
      inv_xform = lambda x: x - 1
      diag_vals = list(range(2, 8))
      for i in range(1, 6):
          dcs = CS(d_names[:i], "voxels")
          rcs = CS(r_names[:i], "world")
          # Generic
          assert cmm.make_cmap(i, xform, inv_xform) == CoordinateMap(
              dcs, rcs, xform, inv_xform
          )
          assert cmm.make_cmap(i, xform) == CoordinateMap(
              dcs, rcs, xform
          )
          # Affines
          aff = np.diag(diag_vals[:i] + [1])
          assert cmm.make_affine(aff) == AffineTransform(dcs, rcs, aff)
          # Test that the call method selects what it got correctly
          assert cmm(i, xform, inv_xform) == CoordinateMap(
              dcs, rcs, xform, inv_xform
          )
          assert cmm(i, xform) == CoordinateMap(dcs, rcs, xform)
          assert cmm(aff) == AffineTransform(dcs, rcs, aff)
      # For affines, we can append dimensions by adding on the diagonal
      aff = np.diag([2, 3, 4, 1])
      dcs = CS(d_names[:4], "voxels")
      rcs = CS(r_names[:4], "world")
      assert cmm.make_affine(aff, 5) == AffineTransform(
          CS(d_names[:4], "voxels"),
          CS(r_names[:4], "world"),
          np.diag([2, 3, 4, 5, 1]),
      )
      assert cmm.make_affine(aff, [5, 6]) == AffineTransform(
          CS(d_names[:5], "voxels"),
          CS(r_names[:5], "world"),
          np.diag([2, 3, 4, 5, 6, 1]),
      )
      # we can add offsets too
      exp_aff = np.diag([2, 3, 4, 5, 6, 1])
      exp_aff[3:5, -1] = [7, 8]
      assert cmm.make_affine(aff, [5, 6], [7, 8]) == AffineTransform(
          CS(d_names[:5], "voxels"), CS(r_names[:5], "world"), exp_aff
      )
      # The zooms (diagonal elements) and offsets must match in length
      pytest.raises(CoordMapMakerError, cmm.make_affine, aff, [5, 6], 7)
      # Check non-square affines
      aff = np.array([[2, 0, 0], [0, 3, 0], [0, 0, 1], [0, 0, 1]])
      dcs = CS(d_names[:2], "voxels")
      rcs = CS(r_names[:3], "world")
      assert cmm.make_affine(aff) == AffineTransform(dcs, rcs, aff)
      dcs = CS(d_names[:3], "voxels")
      rcs = CS(r_names[:4], "world")
      exp_aff = np.array(
          [
              [2, 0, 0, 0],
              [0, 3, 0, 0],
              [0, 0, 0, 1],
              [0, 0, 4, 0],
              [0, 0, 0, 1],
          ]
      )
      assert cmm.make_affine(aff, 4) == AffineTransform(
          dcs, rcs, exp_aff
      )
  
imports: from copy import copy
  from types import SimpleNamespace
  import numpy
  from ..coordinate_map import AffineTransform, AxisError, CoordinateMap, CoordMapMaker, CoordMapMakerError, _as_coordinate_map, _fix0, append_io_dim, axmap, compose, drop_io_dim, equivalent, input_axis_index, io_axis_indices, orth_axes, product, shifted_domain_origin, shifted_range_origin
  from ..coordinate_system import CoordinateSystem, CoordinateSystemError, CoordSysMaker
  import pytest
  from numpy.testing import assert_almost_equal, assert_array_equal
label_desc: Use image node_id 39 for calling a global function with example usage: test_make_cmap() without return types