code: def test_em_3d_demons():
      r"""Test 3D SyN with EM metric, demons-like optimizer
  
      Register a volume created by stacking copies of a coronal slice from
      a T1w brain MRI before and after warping it under a synthetic
      invertible map. We verify that the final registration is of good
      quality.
      """
      fname = get_fnames("t1_coronal_slice")
      nslices = 21
      b = 0.1
      m = 4
  
      image = np.load(fname)
      moving, static = get_warped_stacked_image(image, nslices, b, m)
  
      # Create the EM metric
      smooth = 2.0
      inner_iter = 20
      step_length = 0.25
      q_levels = 256
      double_gradient = True
      iter_type = "demons"
      similarity_metric = metrics.EMMetric(
          3, smooth, inner_iter, q_levels, double_gradient, iter_type
      )
  
      # Create the optimizer
      level_iters = [20, 5]
      opt_tol = 1e-4
      inv_iter = 20
      inv_tol = 1e-3
      ss_sigma_factor = 1.0
      optimizer = imwarp.SymmetricDiffeomorphicRegistration(
          similarity_metric,
          level_iters,
          step_length,
          ss_sigma_factor,
          opt_tol,
          inv_iter,
          inv_tol,
      )
      optimizer.verbosity = VerbosityLevels.DEBUG
      mapping = optimizer.optimize(static, moving, None)
      m = optimizer.get_map()
      assert_equal(mapping, m)
  
      warped = mapping.transform(moving)
      starting_energy = np.sum((static - moving) ** 2)
      final_energy = np.sum((static - warped) ** 2)
      reduced = 1.0 - final_energy / starting_energy
  
      assert reduced > 0.9
  
imports: import numpy
  import nibabel.eulerangles
  from numpy.testing import assert_equal, assert_array_equal, assert_array_almost_equal, assert_raises
  from dipy.core.interpolation import interpolate_scalar_2d, interpolate_scalar_3d
  from dipy.data import get_fnames
  from dipy.align import floating
  from dipy.align import imwarp
  from dipy.align import metrics
  from dipy.align import vector_fields
  from dipy.align import VerbosityLevels
  from dipy.align.imwarp import DiffeomorphicMap
  from dipy.tracking.streamline import deform_streamlines
  from dipy.testing.decorators import set_random_number_generator
label_desc: Use image node_id 18 for calling a global function with example usage: test_em_3d_demons() without return types