code: def test_realign4d():
      """
      This tests whether realign4d yields the same results depending on
      whether the slice order is input explicitly or as
      slice_times='ascending'.
  
      Due to the very small size of the image used for testing (only 3
      slices), optimization is numerically unstable. It seems to make
      the default optimizer, namely scipy.fmin.fmin_ncg, adopt a random
      behavior. To work around the resulting inconsistency in results,
      we use nipy.optimize.fmin_steepest as the optimizer, although it's
      generally not recommended in practice.
      """
      runs = [IM, IM]
      orient = io_orientation(IM.affine)
      slice_axis = int(np.where(orient[:, 0] == 2)[0])
      R1 = SpaceTimeRealign(
          runs, tr=2.0, slice_times="ascending", slice_info=slice_axis
      )
      R1.estimate(
          refscan=None, loops=1, between_loops=1, optimizer="steepest"
      )
      nslices = IM.shape[slice_axis]
      slice_times = (2.0 / float(nslices)) * np.arange(nslices)
      R2 = SpaceTimeRealign(
          runs, tr=2.0, slice_times=slice_times, slice_info=slice_axis
      )
      R2.estimate(
          refscan=None, loops=1, between_loops=1, optimizer="steepest"
      )
      for r in range(2):
          for i in range(IM.shape[3]):
              assert_array_almost_equal(
                  R1._transforms[r][i].translation,
                  R2._transforms[r][i].translation,
              )
              assert_array_almost_equal(
                  R1._transforms[r][i].rotation,
                  R2._transforms[r][i].rotation,
              )
      for i in range(IM.shape[3]):
          assert_array_almost_equal(
              R1._mean_transforms[r].translation,
              R2._mean_transforms[r].translation,
          )
          assert_array_almost_equal(
              R1._mean_transforms[r].rotation,
              R2._mean_transforms[r].rotation,
          )
  
imports: import warnings
  import nibabel
  import numpy
  import pytest
  from nibabel import io_orientation
  from numpy.testing import assert_array_almost_equal, assert_array_equal
  from ....None import load_image
  from ....core.image.image_spaces import make_xyz_image, xyz_affine
  from ....io.nibcompat import get_header
  from ....testing import funcfile
  from ...slicetiming.timefuncs import st_02413, st_42031, st_43210
  from ..affine import Rigid
  from ..groupwise_registration import FmriRealign4d, Image4d, Realign4d, Realign4dAlgorithm, SpaceRealign, SpaceTimeRealign, make_grid, resample4d
label_desc: Use image node_id 12 for calling a global function with example usage: test_realign4d() without return types