code: def test_array_coord_map():
      # array coord map recreates the affine when you slice an image.  In
      # general, if you take an integer slice in some dimension, the
      # corresponding column of the affine will go, leaving a row for the
      # lost dimension, with all zeros, except for the translation in the
      # now-removed dimension, encoding the position of that particular
      # slice
      xz = 1.1
      yz = 2.3
      zz = 3.5
      xt = 10.0
      yt = 11
      zt = 12
      aff = np.diag([xz, yz, zz, 1])
      aff[:3, 3] = [xt, yt, zt]
      shape = (2, 3, 4)
      cmap = AffineTransform.from_params("ijk", "xyz", aff)
      acm = acs.ArrayCoordMap(cmap, shape)
      # slice the coordinate map for the first axis
      sacm = acm[1]
      # The affine has lost the first column, but has a remaining row (the
      # first) encoding the translation to get to this slice
      assert_array_almost_equal(
          sacm.coordmap.affine,
          np.array(
              [[0, 0, xz + xt], [yz, 0, yt], [0, zz, zt], [0, 0, 1]]
          ),
      )
      sacm = acm[:, 1]
      # lost second column, remaining second row with translation
      assert_array_almost_equal(
          sacm.coordmap.affine,
          np.array(
              [[xz, 0, xt], [0, 0, yz + yt], [0, zz, zt], [0, 0, 1]]
          ),
      )
      sacm = acm[:, :, 2]
      # ditto third column and row
      assert_array_almost_equal(
          sacm.coordmap.affine,
          np.array(
              [[xz, 0, xt], [0, yz, yt], [0, 0, 2 * zz + zt], [0, 0, 1]]
          ),
      )
      # check ellipsis slicing is the same as [:,: ...
      sacm = acm[..., 2]
      assert_array_almost_equal(
          sacm.coordmap.affine,
          np.array(
              [[xz, 0, xt], [0, yz, yt], [0, 0, 2 * zz + zt], [0, 0, 1]]
          ),
      )
      # that ellipsis can follow other slice types
      sacm = acm[:, ..., 2]
      assert_array_almost_equal(
          sacm.coordmap.affine,
          np.array(
              [[xz, 0, xt], [0, yz, yt], [0, 0, 2 * zz + zt], [0, 0, 1]]
          ),
      )
      # that there can be only one ellipsis
      pytest.raises(
          ValueError, acm.__getitem__, ((Ellipsis, Ellipsis, 2))
      )
      # that you can integer slice in all three dimensions, leaving only
      # the translation column
      sacm = acm[1, 0, 2]
      assert_array_almost_equal(
          sacm.coordmap.affine,
          np.array([[xz + xt], [yt], [2 * zz + zt], [1]]),
      )
      # that anything other than an int, slice or Ellipsis is an error
      pytest.raises(ValueError, acm.__getitem__, ([0, 2],))
      pytest.raises(ValueError, acm.__getitem__, (np.array([0, 2]),))
  
file_path: nipy/nipy/core/reference/tests/test_array_coords.py
imports: import numpy
  import pytest
  from numpy.testing import assert_array_almost_equal
  import nipy.core.reference.array_coords
  from nipy.core.api import AffineTransform, ArrayCoordMap, CoordinateMap, CoordinateSystem, Grid
label_desc: Use image node_id 1 for calling a global function with example usage: test_array_coord_map() without return types