code: def test_streamline_registration(rng):
      sl1 = [
          np.array([[0, 0, 0], [0, 0, 0.5], [0, 0, 1], [0, 0, 1.5]]),
          np.array([[0, 0, 0], [0, 0.5, 0.5], [0, 1, 1]]),
      ]
      affine_mat = np.eye(4)
      affine_mat[:3, 3] = rng.standard_normal(3)
      sl2 = list(transform_tracking_output(sl1, affine_mat))
      aligned, matrix = streamline_registration(sl2, sl1)
      npt.assert_almost_equal(matrix, np.linalg.inv(affine_mat))
      npt.assert_almost_equal(aligned[0], sl1[0])
      npt.assert_almost_equal(aligned[1], sl1[1])
  
      # We assume the two tracks come from the same space, but it might have
      # some affine associated with it:
      base_aff = np.eye(4) * rng.random()
      base_aff[:3, 3] = np.array([1, 2, 3])
      base_aff[3, 3] = 1
  
      with TemporaryDirectory() as tmpdir:
          for use_aff in [None, base_aff]:
              fname1 = op.join(tmpdir, "sl1.trk")
              fname2 = op.join(tmpdir, "sl2.trk")
              if use_aff is not None:
                  img = nib.Nifti1Image(np.zeros((2, 2, 2)), use_aff)
                  # Move the streamlines to this other space, and report it:
                  tgm1 = StatefulTractogram(
                      transform_tracking_output(
                          sl1, np.linalg.inv(use_aff)
                      ),
                      img,
                      Space.VOX,
                  )
  
                  save_trk(tgm1, fname1, bbox_valid_check=False)
  
                  tgm2 = StatefulTractogram(
                      transform_tracking_output(
                          sl2, np.linalg.inv(use_aff)
                      ),
                      img,
                      Space.VOX,
                  )
  
                  save_trk(tgm2, fname2, bbox_valid_check=False)
  
              else:
                  img = nib.Nifti1Image(np.zeros((2, 2, 2)), np.eye(4))
                  tgm1 = StatefulTractogram(sl1, img, Space.RASMM)
                  tgm2 = StatefulTractogram(sl2, img, Space.RASMM)
                  save_trk(tgm1, fname1, bbox_valid_check=False)
                  save_trk(tgm2, fname2, bbox_valid_check=False)
  
              aligned, matrix = streamline_registration(fname2, fname1)
              npt.assert_almost_equal(aligned[0], sl1[0], decimal=5)
              npt.assert_almost_equal(aligned[1], sl1[1], decimal=5)
  
imports: import os.path
  import pytest
  from tempfile import TemporaryDirectory
  import numpy
  import numpy.testing
  import nibabel
  import dipy.data
  import dipy.core.gradients
  from dipy.align import syn_registration, register_series, register_dwi_series, center_of_mass, translation, rigid_isoscaling, rigid_scaling, rigid, affine, motion_correction, affine_registration, streamline_registration, write_mapping, read_mapping, register_dwi_to_template
  from dipy.align.imwarp import DiffeomorphicMap
  from dipy.tracking.utils import transform_tracking_output
  from dipy.io.streamline import save_trk
  from dipy.io.stateful_tractogram import StatefulTractogram, Space
  from dipy.io.gradients import read_bvals_bvecs
  from dipy.io.image import load_nifti
  from dipy.testing.decorators import set_random_number_generator
label_desc: Use image node_id 8 for calling a global function with example usage: test_streamline_registration(rng) without return types