code: def sfm_design_matrix(gtab, sphere, response, mode="signal"):
      """
      Construct the SFM design matrix
  
      Parameters
      ----------
      gtab : GradientTable or Sphere
          Sets the rows of the matrix, if the mode is 'signal', this should be a
          GradientTable. If mode is 'odf' this should be a Sphere.
  
      sphere : Sphere
          Sets the columns of the matrix
  
      response : list of 3 elements
          The eigenvalues of a tensor which will serve as a kernel
          function.
  
      mode : str {'signal' | 'odf'}, optional
          Choose the (default) 'signal' for a design matrix containing predicted
          signal in the measurements defined by the gradient table for putative
          fascicles oriented along the vertices of the sphere. Otherwise, choose
          'odf' for an odf convolution matrix, with values of the odf calculated
          from a tensor with the provided response eigenvalues, evaluated at the
          b-vectors in the gradient table, for the tensors with principal
          diffusion directions along the vertices of the sphere.
  
      Returns
      -------
      mat : ndarray
          A design matrix that can be used for one of the following operations:
          when the 'signal' mode is used, each column contains the putative
          signal in each of the bvectors of the `gtab` if a fascicle is oriented
          in the direction encoded by the sphere vertex corresponding to this
          column. This is used for deconvolution with a measured DWI signal. If
          the 'odf' mode is chosen, each column instead contains the values of
          the tensor ODF for a tensor with a principal diffusion direction
          corresponding to this vertex. This is used to generate odfs from the
          fits of the SFM for the purpose of tracking.
  
      Examples
      --------
      >>> import dipy.data as dpd
      >>> data, gtab = dpd.dsi_voxels()
      >>> sphere = dpd.get_sphere()
      >>> from dipy.reconst.sfm import sfm_design_matrix
  
      A canonical tensor approximating corpus-callosum voxels [Rokem2014]_:
  
      >>> tensor_matrix = sfm_design_matrix(gtab, sphere,
      ...                                   [0.0015, 0.0005, 0.0005])
  
      A 'stick' function ([Behrens2007]_):
  
      >>> stick_matrix = sfm_design_matrix(gtab, sphere, [0.001, 0, 0])
  
      Notes
      -----
      .. [Rokem2015] Ariel Rokem, Jason D. Yeatman, Franco Pestilli, Kendrick
         N. Kay, Aviv Mezer, Stefan van der Walt, Brian A. Wandell
         (2015). Evaluating the accuracy of diffusion MRI models in white
         matter. PLoS ONE 10(4): e0123272. doi:10.1371/journal.pone.0123272
  
      .. [Rokem2014] Ariel Rokem, Kimberly L. Chan, Jason D. Yeatman, Franco
         Pestilli,  Brian A. Wandell (2014). Evaluating the accuracy of diffusion
         models at multiple b-values with cross-validation. ISMRM 2014.
  
      .. [Behrens2007] Behrens TEJ, Berg HJ, Jbabdi S, Rushworth MFS, Woolrich MW
         (2007): Probabilistic diffusion tractography with multiple fibre
         orientations: What can we gain? Neuroimage 34:144-55.
      """
      if mode == "signal":
          with warnings.catch_warnings():
              warnings.simplefilter("ignore")
              mat_gtab = grad.gradient_table(
                  gtab.bvals[~gtab.b0s_mask], gtab.bvecs[~gtab.b0s_mask]
              )
          # Preallocate:
          mat = np.empty(
              (np.sum(~gtab.b0s_mask), sphere.vertices.shape[0])
          )
      elif mode == "odf":
          mat = np.empty((gtab.x.shape[0], sphere.vertices.shape[0]))
  
      # Calculate column-wise:
      for ii, this_dir in enumerate(sphere.vertices):
          # Rotate the canonical tensor towards this vertex and calculate the
          # signal you would have gotten in the direction
          if mode == "signal":
              # For regressors based on the single tensor, remove $e^{-bD}$
              mat[:, ii] = sims.single_tensor(
                  mat_gtab,
                  evals=response,
                  evecs=sims.all_tensor_evecs(this_dir),
              ) - np.exp(-mat_gtab.bvals * np.mean(response))
  
          elif mode == "odf":
              # Stick function
              if response[1] == 0 or response[2] == 0:
                  mat[
                      sphere.find_closest(
                          sims.all_tensor_evecs(this_dir)[0]
                      ),
                      ii,
                  ] = 1
              else:
                  mat[:, ii] = sims.single_tensor_odf(
                      gtab.vertices,
                      evals=response,
                      evecs=sims.all_tensor_evecs(this_dir),
                  )
      return mat
  
imports: import warnings
  import numpy
  import gc
  from collections import OrderedDict
  from dipy.utils.optpkg import optional_package
  from dipy.utils.multiproc import determine_num_processes
  import dipy.core.gradients
  import dipy.core.optimize
  import dipy.sims.voxel
  import dipy.data
  from dipy.reconst.base import ReconstModel, ReconstFit
  from dipy.reconst.cache import Cache
  from dipy.core.onetime import auto_attr
label_desc: Use image node_id 2 for calling a global function with example usage: sfm_design_matrix(gtab, sphere, response, mode) and returns: mat