code: def test_reordering():
      """Test the xyz_ordered method of the VolumeImg."""
      # We need to test on a square array, as rotation does not change
      # shape, whereas reordering does.
      shape = (5, 5, 5, 2, 2)
      data = np.random.random(shape)
      affine = np.eye(4)
      affine[:3, -1] = 0.5 * np.array(shape[:3])
      ref_im = VolumeImg(data, affine, "mine")
      # Test with purely positive matrices and compare to a rotation
      for theta, phi in np.random.randint(4, size=(5, 2)):
          rot = rotation(theta * np.pi / 2, phi * np.pi / 2)
          rot[np.abs(rot) < 0.001] = 0
          rot[rot > 0.9] = 1
          rot[rot < -0.9] = 1
          b = 0.5 * np.array(shape[:3])
          new_affine = from_matrix_vector(rot, b)
          rot_im = ref_im.as_volume_img(affine=new_affine)
          assert_array_equal(rot_im.affine, new_affine)
          assert_array_equal(rot_im.get_fdata().shape, shape)
          reordered_im = rot_im.xyz_ordered()
          assert_array_equal(reordered_im.affine[:3, :3], np.eye(3))
          assert_almost_equal(reordered_im.get_fdata(), data)
  
      # Check that we cannot swap axes for non spatial axis:
      pytest.raises(ValueError, ref_im._swapaxes, 4, 5)
  
      # Create a non-diagonal affine, and check that we raise a sensible
      # exception
      affine[1, 0] = 0.1
      ref_im = VolumeImg(data, affine, "mine")
      pytest.raises(CompositionError, ref_im.xyz_ordered)
  
      # Test flipping an axis
      data = np.random.random(shape)
      for i in (0, 1, 2):
          # Make a diagonal affine with a negative axis, and check that
          # can be reordered, also vary the shape
          shape = (i + 1, i + 2, 3 - i)
          affine = np.eye(4)
          affine[i, i] *= -1
          img = VolumeImg(data, affine, "mine")
          orig_img = copy.copy(img)
          x, y, z = img.get_world_coords()
          sample = img.values_in_world(x, y, z)
          img2 = img.xyz_ordered()
          # Check that img has not been changed
          assert img == orig_img
          x_, y_, z_ = img.get_world_coords()
          assert_array_equal(np.unique(x), np.unique(x_))
          assert_array_equal(np.unique(y), np.unique(y_))
          assert_array_equal(np.unique(z), np.unique(z_))
          sample2 = img.values_in_world(x, y, z)
          assert_array_equal(sample, sample2)
  
imports: import copy
  import numpy
  import pytest
  from numpy.testing import assert_almost_equal, assert_array_equal
  from ...transforms.affine_transform import AffineTransform
  from ...transforms.affine_utils import from_matrix_vector
  from ...transforms.transform import Transform
  from ..volume_img import CompositionError, VolumeImg
label_desc: Use image node_id 7 for calling a global function with example usage: test_reordering() without return types