code: def test_rollimg():
      AT = AffineTransform
      data = np.random.standard_normal((3, 4, 7, 5))
      aff = np.diag([1, 2, 3, 4, 1])
      im = Image(data, AT("ijkl", "xyzt", aff))
      # No attempt to preserve the diagonal
      im1 = rollimg(im, 1)
      assert im1.coordmap == rollimg(im, "j").coordmap
      assert im1.coordmap == rollimg(im, "y").coordmap
      assert im1.coordmap == rollimg(im, -3).coordmap
      assert im1.coordmap == AT("jikl", "xyzt", aff[:, (1, 0, 2, 3, 4)])
      assert_array_equal(im1.get_fdata(), np.rollaxis(data, 1))
      im2 = rollimg(im, 2)
      assert im2.coordmap == rollimg(im, "k").coordmap
      assert im2.coordmap == rollimg(im, "z").coordmap
      assert im2.coordmap == rollimg(im, -2).coordmap
      assert im2.coordmap == AT("kijl", "xyzt", aff[:, (2, 0, 1, 3, 4)])
      assert_array_equal(im2.get_fdata(), np.rollaxis(data, 2))
      im3 = rollimg(im, 3)
      assert im3.coordmap == rollimg(im, "l").coordmap
      assert im3.coordmap == rollimg(im, "t").coordmap
      assert im3.coordmap == rollimg(im, -1).coordmap
      assert im3.coordmap == AT("lijk", "xyzt", aff[:, (3, 0, 1, 2, 4)])
      assert_array_equal(im3.get_fdata(), np.rollaxis(data, 3))
      # We can roll to before a specified axis
      im31 = rollimg(im, 3, 1)
      assert im31.coordmap == rollimg(im, "l", "j").coordmap
      assert im31.coordmap == rollimg(im, "t", "y").coordmap
      assert im31.coordmap == rollimg(im, "t", "j").coordmap
      assert im31.coordmap == rollimg(im, "l", "y").coordmap
      assert im31.coordmap == rollimg(im, -1, "y").coordmap
      assert im31.coordmap == rollimg(im, -1, -3).coordmap
      assert im31.coordmap == AT(
          "iljk", "xyzt", aff[:, (0, 3, 1, 2, 4)]
      )
      assert_array_equal(im31.get_fdata(), np.rollaxis(data, 3, 1))
      # Check that ambiguous axes raise an exception; 'l' appears both as an axis
      # and a reference coord name and in different places
      im_amb = Image(data, AT("ijkl", "xylt", np.diag([1, 2, 3, 4, 1])))
      pytest.raises(AxisError, rollimg, im_amb, "l")
      # But if it's unambiguous, then 'l' can appear both as an axis and a
      # reference coord name
      im_unamb = Image(
          data, AT("ijkl", "xyzl", np.diag([1, 2, 3, 4, 1]))
      )
      im_rolled = rollimg(im_unamb, "l")
      assert_array_equal(
          im_rolled.get_fdata(),
          im_unamb.get_fdata().transpose([3, 0, 1, 2]),
      )
      # Zero row / col means we can't find an axis mapping, when fix0 is false
      aff_z = np.diag([1, 2, 3, 0, 1])
      im_z = Image(data, AT("ijkl", "xyzt", aff_z))
      pytest.raises(AxisError, rollimg, im_z, "t", fix0=False)
      # But we can work it out if we turn on our zero detector
      assert rollimg(im_z, "t", fix0=True).coordmap == AT(
          "lijk", "xyzt", aff_z[:, (3, 0, 1, 2, 4)]
      )
      # That's the default
      assert rollimg(im_z, "t").coordmap == AT(
          "lijk", "xyzt", aff_z[:, (3, 0, 1, 2, 4)]
      )
      # Non square is OK
      aff_r = np.array(
          [
              [1, 0, 0, 10],
              [0, 2, 0, 11],
              [0, 0, 2, 12],
              [0, 0, 0, 13],
              [0, 0, 0, 1],
          ]
      )
      im_r = Image(data[:, :, :, 0], AT("ijk", "xyzt", aff_r))
      assert rollimg(im_r, "k").coordmap == AT(
          "kij", "xyzt", aff_r[:, (2, 0, 1, 3)]
      )
      # Unless you're trying to get at the dropped input dimension of course
      pytest.raises(AxisError, rollimg, im_r, "t")
      # Another check for integers, input names, output names, reversing
      for i, o, n in zip("ijkl", "xyzt", range(4)):
          im_i = rollimg(im, i)
          im_o = rollimg(im, o)
          im_n = rollimg(im, n)
          assert_array_equal(im_i.get_fdata(), im_o.get_fdata())
          assert_array_equal(im_i.affine, im_o.affine)
          assert_array_equal(im_n.get_fdata(), im_o.get_fdata())
          for _im in [im_n, im_o, im_i]:
              # We're rollimg back.  We want to roll the new axis 0 back to where
              # it started, which was position n
              im_n_inv = rollimg(_im, 0, n + 1)
              assert_array_equal(im_n_inv.affine, im.affine)
              assert_array_equal(im_n_inv.get_fdata(), im.get_fdata())
  
imports: import warnings
  from itertools import chain
  import nibabel
  import numpy
  import pytest
  from numpy.testing import assert_almost_equal, assert_array_almost_equal, assert_array_equal
  from ...api import data_generator, parcels, write_data
  from ...reference.coordinate_map import AffineTransform, AxisError
  from ...reference.coordinate_system import CoordinateSystem
  from ..None import image
  from ..image import Image, is_image, iter_axis, rollimg
label_desc: Use image node_id 22 for calling a global function with example usage: test_rollimg() without return types