docstr: Constrained spherical deconvolution
  
  Parameters
  ----------
  input_files : string
      Path to the input volumes. This path may contain wildcards to
      process multiple inputs at once.
  bvalues_files : string
      Path to the bvalues files. This path may contain wildcards to use
      multiple bvalues files at once.
  bvectors_files : string
      Path to the bvectors files. This path may contain wildcards to use
      multiple bvectors files at once.
  mask_files : string
      Path to the input masks. This path may contain wildcards to use
      multiple masks at once. (default: No mask used)
  b0_threshold : float, optional
      Threshold used to find b0 volumes.
  bvecs_tol : float, optional
      Bvecs should be unit vectors.
  roi_center : variable int, optional
      Center of ROI in data. If center is None, it is assumed that it is
      the center of the volume with shape `data.shape[:3]`.
  roi_radii : int or array-like, optional
      radii of cuboid ROI in voxels.
  fa_thr : float, optional
      FA threshold for calculating the response function.
  frf : variable float, optional
      Fiber response function can be for example inputted as 15 4 4
      (from the command line) or [15, 4, 4] from a Python script to be
      converted to float and multiplied by 10**-4 . If None
      the fiber response function will be computed automatically.
  extract_pam_values : bool, optional
      Save or not to save pam volumes as single nifti files.
  sh_order : int, optional
      Spherical harmonics order used in the CSA fit.
  odf_to_sh_order : int, optional
      Spherical harmonics order used for peak_from_model to compress
      the ODF to spherical harmonics coefficients.
  parallel : bool, optional
      Whether to use parallelization in peak-finding during the
      calibration procedure.
  num_processes : int, optional
      If `parallel` is True, the number of subprocesses to use
      (default multiprocessing.cpu_count()). If < 0 the maximal number
      of cores minus ``num_processes + 1`` is used (enter -1 to use as
      many cores as possible). 0 raises an error.
  out_dir : string, optional
      Output directory. (default current directory)
  out_pam : string, optional
      Name of the peaks volume to be saved.
  out_shm : string, optional
      Name of the spherical harmonics volume to be saved.
  out_peaks_dir : string, optional
      Name of the peaks directions volume to be saved.
  out_peaks_values : string, optional
      Name of the peaks values volume to be saved.
  out_peaks_indices : string, optional
      Name of the peaks indices volume to be saved.
  out_gfa : string, optional
      Name of the generalized FA volume to be saved.
  
  
  References
  ----------
  .. [1] Tournier, J.D., et al. NeuroImage 2007. Robust determination of
     the fibre orientation distribution in diffusion MRI: Non-negativity
     constrained super-resolved spherical deconvolution.
docstr_tok: ["Constrained","spherical","deconvolution","Parameters","--","--","--","--","--","input_files",":","string","Path","to","the","input","volumes",".","This","path","may","contain","wildcards","to","process","multiple","inputs","at","once",".","bvalues_files",":","string","Path","to","the","bvalues","files",".","This","path","may","contain","wildcards","to","use","multiple","bvalues","files","at","once",".","bvectors_files",":","string","Path","to","the","bvectors","files",".","This","path","may","contain","wildcards","to","use","multiple","bvectors","files","at","once",".","mask_files",":","string","Path","to","the","input","masks",".","This","path","may","contain","wildcards","to","use","multiple","masks","at","once",".","(","default",":","No","mask","used",")","b0_threshold",":","float",",","optional","Threshold","used","to","find","b0","volumes",".","bvecs_tol",":","float",",","optional","Bvecs","should","be","unit","vectors",".","roi_center",":","variable","int",",","optional","Center","of","ROI","in","data",".","If","center","is","None",",","it","is","assumed","that","it","is","the","center","of","the","volume","with","shape","`","data.shape","[",":3","]","`",".","roi_radii",":","int","or","array-like",",","optional","radii","of","cuboid","ROI","in","voxels",".","fa_thr",":","float",",","optional","FA","threshold","for","calculating","the","response","function",".","frf",":","variable","float",",","optional","Fiber","response","function","can","be","for","example","inputted","as","15","4","4","(","from","the","command","line",")","or","[","15",",","4",",","4","]","from","a","Python","script","to","be","converted","to","float","and","multiplied","by","10","*","*","-4",".","If","None","the","fiber","response","function","will","be","computed","automatically",".","extract_pam_values",":","bool",",","optional","Save","or","not","to","save","pam","volumes","as","single","nifti","files",".","sh_order",":","int",",","optional","Spherical","harmonics","order","used","in","the","CSA","fit",".","odf_to_sh_order",":","int",",","optional","Spherical","harmonics","order","used","for","peak_from_model","to","compress","the","ODF","to","spherical","harmonics","coefficients",".","parallel",":","bool",",","optional","Whether","to","use","parallelization","in","peak-finding","during","the","calibration","procedure",".","num_processes",":","int",",","optional","If","`","parallel","`","is","True",",","the","number","of","subprocesses","to","use","(","default","multiprocessing.cpu_count","(",")",")",".","If","<","0","the","maximal","number","of","cores","minus","``","num_processes","+","1","``","is","used","(","enter","-1","to","use","as","many","cores","as","possible",")",".","0","raises","an","error",".","out_dir",":","string",",","optional","Output","directory",".","(","default","current","directory",")","out_pam",":","string",",","optional","Name","of","the","peaks","volume","to","be","saved",".","out_shm",":","string",",","optional","Name","of","the","spherical","harmonics","volume","to","be","saved",".","out_peaks_dir",":","string",",","optional","Name","of","the","peaks","directions","volume","to","be","saved",".","out_peaks_values",":","string",",","optional","Name","of","the","peaks","values","volume","to","be","saved",".","out_peaks_indices",":","string",",","optional","Name","of","the","peaks","indices","volume","to","be","saved",".","out_gfa",":","string",",","optional","Name","of","the","generalized","FA","volume","to","be","saved",".","References","--","--","--","--","--","..","[","1","]","Tournier",",","J.D.",",","et","al",".","NeuroImage","2007",".","Robust","determination","of","the","fibre","orientation","distribution","in","diffusion","MRI",":","Non-negativity","constrained","super-resolved","spherical","deconvolution","."]
code: def run(
      self,
      input_files,
      bvalues_files,
      bvectors_files,
      mask_files,
      b0_threshold=50.0,
      bvecs_tol=0.01,
      roi_center=None,
      roi_radii=10,
      fa_thr=0.7,
      frf=None,
      extract_pam_values=False,
      sh_order=8,
      odf_to_sh_order=8,
      parallel=False,
      num_processes=None,
      out_dir="",
      out_pam="peaks.pam5",
      out_shm="shm.nii.gz",
      out_peaks_dir="peaks_dirs.nii.gz",
      out_peaks_values="peaks_values.nii.gz",
      out_peaks_indices="peaks_indices.nii.gz",
      out_gfa="gfa.nii.gz",
  ):
      """Constrained spherical deconvolution
  
      Parameters
      ----------
      input_files : string
          Path to the input volumes. This path may contain wildcards to
          process multiple inputs at once.
      bvalues_files : string
          Path to the bvalues files. This path may contain wildcards to use
          multiple bvalues files at once.
      bvectors_files : string
          Path to the bvectors files. This path may contain wildcards to use
          multiple bvectors files at once.
      mask_files : string
          Path to the input masks. This path may contain wildcards to use
          multiple masks at once. (default: No mask used)
      b0_threshold : float, optional
          Threshold used to find b0 volumes.
      bvecs_tol : float, optional
          Bvecs should be unit vectors.
      roi_center : variable int, optional
          Center of ROI in data. If center is None, it is assumed that it is
          the center of the volume with shape `data.shape[:3]`.
      roi_radii : int or array-like, optional
          radii of cuboid ROI in voxels.
      fa_thr : float, optional
          FA threshold for calculating the response function.
      frf : variable float, optional
          Fiber response function can be for example inputted as 15 4 4
          (from the command line) or [15, 4, 4] from a Python script to be
          converted to float and multiplied by 10**-4 . If None
          the fiber response function will be computed automatically.
      extract_pam_values : bool, optional
          Save or not to save pam volumes as single nifti files.
      sh_order : int, optional
          Spherical harmonics order used in the CSA fit.
      odf_to_sh_order : int, optional
          Spherical harmonics order used for peak_from_model to compress
          the ODF to spherical harmonics coefficients.
      parallel : bool, optional
          Whether to use parallelization in peak-finding during the
          calibration procedure.
      num_processes : int, optional
          If `parallel` is True, the number of subprocesses to use
          (default multiprocessing.cpu_count()). If < 0 the maximal number
          of cores minus ``num_processes + 1`` is used (enter -1 to use as
          many cores as possible). 0 raises an error.
      out_dir : string, optional
          Output directory. (default current directory)
      out_pam : string, optional
          Name of the peaks volume to be saved.
      out_shm : string, optional
          Name of the spherical harmonics volume to be saved.
      out_peaks_dir : string, optional
          Name of the peaks directions volume to be saved.
      out_peaks_values : string, optional
          Name of the peaks values volume to be saved.
      out_peaks_indices : string, optional
          Name of the peaks indices volume to be saved.
      out_gfa : string, optional
          Name of the generalized FA volume to be saved.
  
  
      References
      ----------
      .. [1] Tournier, J.D., et al. NeuroImage 2007. Robust determination of
         the fibre orientation distribution in diffusion MRI: Non-negativity
         constrained super-resolved spherical deconvolution.
      """
      io_it = self.get_io_iterator()
  
      for (
          dwi,
          bval,
          bvec,
          maskfile,
          opam,
          oshm,
          opeaks_dir,
          opeaks_values,
          opeaks_indices,
          ogfa,
      ) in io_it:
          logging.info("Loading {0}".format(dwi))
          data, affine = load_nifti(dwi)
  
          bvals, bvecs = read_bvals_bvecs(bval, bvec)
  
          # If all b-values are smaller or equal to the b0 threshold, it is
          # assumed that no thresholding is requested
          if any(mask_non_weighted_bvals(bvals, b0_threshold)):
              if b0_threshold < bvals.min():
                  warn(
                      "b0_threshold (value: {0}) is too low, increase your "
                      "b0_threshold. It should be higher than the first b0 "
                      "value ({1}).".format(b0_threshold, bvals.min())
                  )
          gtab = gradient_table(
              bvals, bvecs, b0_threshold=b0_threshold, atol=bvecs_tol
          )
          mask_vol = load_nifti_data(maskfile).astype(bool)
  
          n_params = ((sh_order + 1) * (sh_order + 2)) / 2
          if data.shape[-1] < n_params:
              raise ValueError(
                  "You need at least {0} unique DWI volumes to "
                  "compute fiber odfs. You currently have: {1}"
                  " DWI volumes.".format(n_params, data.shape[-1])
              )
  
          if frf is None:
              logging.info("Computing response function")
              if roi_center is not None:
                  logging.info(
                      "Response ROI center:\n{0}".format(roi_center)
                  )
                  logging.info(
                      "Response ROI radii:\n{0}".format(roi_radii)
                  )
              response, ratio = auto_response_ssst(
                  gtab,
                  data,
                  roi_center=roi_center,
                  roi_radii=roi_radii,
                  fa_thr=fa_thr,
              )
              response = list(response)
  
          else:
              logging.info("Using response function")
              if isinstance(frf, str):
                  l01 = np.array(literal_eval(frf), dtype=np.float64)
              else:
                  l01 = np.array(frf, dtype=np.float64)
  
              l01 *= 10**-4
              response = np.array([l01[0], l01[1], l01[1]])
              ratio = l01[1] / l01[0]
              response = (response, ratio)
  
          logging.info(
              "Eigenvalues for the frf of the input"
              " data are :{0}".format(response[0])
          )
          logging.info(
              "Ratio for smallest to largest eigen value is {0}".format(
                  ratio
              )
          )
  
          peaks_sphere = default_sphere
  
          logging.info("CSD computation started.")
          csd_model = ConstrainedSphericalDeconvModel(
              gtab, response, sh_order=sh_order
          )
  
          peaks_csd = peaks_from_model(
              model=csd_model,
              data=data,
              sphere=peaks_sphere,
              relative_peak_threshold=0.5,
              min_separation_angle=25,
              mask=mask_vol,
              return_sh=True,
              sh_order=sh_order,
              normalize_peaks=True,
              parallel=parallel,
              num_processes=num_processes,
          )
          peaks_csd.affine = affine
  
          save_peaks(opam, peaks_csd)
  
          logging.info("CSD computation completed.")
  
          if extract_pam_values:
              peaks_to_niftis(
                  peaks_csd,
                  oshm,
                  opeaks_dir,
                  opeaks_values,
                  opeaks_indices,
                  ogfa,
                  reshape_dirs=True,
              )
  
          dname_ = os.path.dirname(opam)
          if dname_ == "":
              logging.info("Pam5 file saved in current directory")
          else:
              logging.info("Pam5 file saved in {0}".format(dname_))
  
          return io_it
  
imports: import logging
  import numpy
  import os.path
  from ast import literal_eval
  from warnings import warn
  import nibabel
  from dipy.core.gradients import mask_non_weighted_bvals, gradient_table
  from dipy.data import default_sphere, get_sphere
  from dipy.io.gradients import read_bvals_bvecs
  from dipy.io.peaks import save_peaks, peaks_to_niftis
  from dipy.io.image import load_nifti, save_nifti, load_nifti_data
  from dipy.io.utils import nifti1_symmat
  from dipy.reconst.csdeconv import ConstrainedSphericalDeconvModel, auto_response_ssst
  from dipy.reconst.dti import TensorModel, color_fa, fractional_anisotropy, geodesic_anisotropy, mean_diffusivity, axial_diffusivity, radial_diffusivity, lower_triangular, mode
  from dipy.direction.peaks import peaks_from_model
  from dipy.reconst.shm import CsaOdfModel
  from dipy.workflows.workflow import Workflow
  from dipy.reconst.dki import DiffusionKurtosisModel, split_dki_param
  from dipy.reconst.ivim import IvimModel
  from dipy.reconst.rumba import RumbaSDModel
  from dipy.reconst import mapmri
  from dipy.utils.deprecator import deprecated_params
label_desc: Use image node_id 2 for calling the ReconstCSDFlow obj's underlying member method code with example usage: obj.run(input_files, bvalues_files, bvectors_files, mask_files, b0_threshold, bvecs_tol, roi_center, roi_radii, fa_thr, frf, extract_pam_values, sh_order, odf_to_sh_order, parallel, num_processes, out_dir, out_pam, out_shm, out_peaks_dir, out_peaks_values, out_peaks_indices, out_gfa) and returns: io_it