docstr: Workflow for Diffusion Kurtosis reconstruction and for computing
  DKI metrics. Performs a DKI reconstruction on the files by 'globing'
  ``input_files`` and saves the DKI metrics in a directory specified by
  ``out_dir``.
  
  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 bvalues files. This path may contain wildcards to use
      multiple bvalues 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)
  fit_method : string, optional
      can be one of the following:
      'OLS' or 'ULLS' for ordinary least squares
      'WLS' or 'UWLLS' for weighted ordinary least squares
  b0_threshold : float, optional
      Threshold used to find b0 volumes.
  sigma : float, optional
      An estimate of the variance. [3]_ recommend to use
      1.5267 * std(background_noise), where background_noise is estimated
      from some part of the image known to contain no signal (only noise)
  save_metrics : variable string, optional
      List of metrics to save.
      Possible values: fa, ga, rgb, md, ad, rd, mode, tensor, evec, eval
  out_dir : string, optional
      Output directory. (default current directory)
  out_dt_tensor : string, optional
      Name of the tensors volume to be saved.
  out_dk_tensor : string, optional
      Name of the tensors volume to be saved.
  out_fa : string, optional
      Name of the fractional anisotropy volume to be saved.
  out_ga : string, optional
      Name of the geodesic anisotropy volume to be saved.
  out_rgb : string, optional
      Name of the color fa volume to be saved.
  out_md : string, optional
      Name of the mean diffusivity volume to be saved.
  out_ad : string, optional
      Name of the axial diffusivity volume to be saved.
  out_rd : string, optional
      Name of the radial diffusivity volume to be saved.
  out_mode : string, optional
      Name of the mode volume to be saved.
  out_evec : string, optional
      Name of the eigenvectors volume to be saved.
  out_eval : string, optional
      Name of the eigenvalues to be saved.
  out_mk : string, optional
      Name of the mean kurtosis to be saved.
  out_ak : string, optional
      Name of the axial kurtosis to be saved.
  out_rk : string, optional
      Name of the radial kurtosis to be saved.
  
  References
  ----------
  .. [1] Tabesh, A., Jensen, J.H., Ardekani, B.A., Helpern, J.A., 2011.
     Estimation of tensors and tensor-derived measures in diffusional
     kurtosis imaging. Magn Reson Med. 65(3), 823-836
  
  .. [2] Jensen, Jens H., Joseph A. Helpern, Anita Ramani, Hanzhang Lu,
     and Kyle Kaczynski. 2005. Diffusional Kurtosis Imaging: The
     Quantification of Non-Gaussian Water Diffusion by Means of Magnetic
     Resonance Imaging. MRM 53 (6):1432-40.
  
  .. [3] Chang, L-C, Jones, DK and Pierpaoli, C (2005). RESTORE: robust
     estimation of tensors by outlier rejection. MRM, 53: 1088-95.
docstr_tok: ["Workflow","for","Diffusion","Kurtosis","reconstruction","and","for","computing","DKI","metrics",".","Performs","a","DKI","reconstruction","on","the","files","by","'globing'","``","input_files","``","and","saves","the","DKI","metrics","in","a","directory","specified","by","``","out_dir","``",".","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","bvalues","files",".","This","path","may","contain","wildcards","to","use","multiple","bvalues","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",")","fit_method",":","string",",","optional","can","be","one","of","the","following",":","'OLS","'","or","'ULLS","'","for","ordinary","least","squares","'WLS","'","or","'UWLLS","'","for","weighted","ordinary","least","squares","b0_threshold",":","float",",","optional","Threshold","used","to","find","b0","volumes",".","sigma",":","float",",","optional","An","estimate","of","the","variance",".","[","3","]","_","recommend","to","use","1.5267","*","std","(","background_noise",")",",","where","background_noise","is","estimated","from","some","part","of","the","image","known","to","contain","no","signal","(","only","noise",")","save_metrics",":","variable","string",",","optional","List","of","metrics","to","save",".","Possible","values",":","fa",",","ga",",","rgb",",","md",",","ad",",","rd",",","mode",",","tensor",",","evec",",","eval","out_dir",":","string",",","optional","Output","directory",".","(","default","current","directory",")","out_dt_tensor",":","string",",","optional","Name","of","the","tensors","volume","to","be","saved",".","out_dk_tensor",":","string",",","optional","Name","of","the","tensors","volume","to","be","saved",".","out_fa",":","string",",","optional","Name","of","the","fractional","anisotropy","volume","to","be","saved",".","out_ga",":","string",",","optional","Name","of","the","geodesic","anisotropy","volume","to","be","saved",".","out_rgb",":","string",",","optional","Name","of","the","color","fa","volume","to","be","saved",".","out_md",":","string",",","optional","Name","of","the","mean","diffusivity","volume","to","be","saved",".","out_ad",":","string",",","optional","Name","of","the","axial","diffusivity","volume","to","be","saved",".","out_rd",":","string",",","optional","Name","of","the","radial","diffusivity","volume","to","be","saved",".","out_mode",":","string",",","optional","Name","of","the","mode","volume","to","be","saved",".","out_evec",":","string",",","optional","Name","of","the","eigenvectors","volume","to","be","saved",".","out_eval",":","string",",","optional","Name","of","the","eigenvalues","to","be","saved",".","out_mk",":","string",",","optional","Name","of","the","mean","kurtosis","to","be","saved",".","out_ak",":","string",",","optional","Name","of","the","axial","kurtosis","to","be","saved",".","out_rk",":","string",",","optional","Name","of","the","radial","kurtosis","to","be","saved",".","References","--","--","--","--","--","..","[","1","]","Tabesh",",","A.",",","Jensen",",","J.H.",",","Ardekani",",","B.A.",",","Helpern",",","J.A.",",","2011",".","Estimation","of","tensors","and","tensor-derived","measures","in","diffusional","kurtosis","imaging",".","Magn","Reson","Med",".","65","(","3",")",",","823-836","..","[","2","]","Jensen",",","Jens","H.",",","Joseph","A.","Helpern",",","Anita","Ramani",",","Hanzhang","Lu",",","and","Kyle","Kaczynski",".","2005",".","Diffusional","Kurtosis","Imaging",":","The","Quantification","of","Non-Gaussian","Water","Diffusion","by","Means","of","Magnetic","Resonance","Imaging",".","MRM","53","(","6",")",":1432-40",".","..","[","3","]","Chang",",","L-C",",","Jones",",","DK","and","Pierpaoli",",","C","(","2005",")",".","RESTORE",":","robust","estimation","of","tensors","by","outlier","rejection",".","MRM",",","53",":","1088-95","."]
code: def run(
      self,
      input_files,
      bvalues_files,
      bvectors_files,
      mask_files,
      fit_method="WLS",
      b0_threshold=50.0,
      sigma=None,
      save_metrics=None,
      out_dir="",
      out_dt_tensor="dti_tensors.nii.gz",
      out_fa="fa.nii.gz",
      out_ga="ga.nii.gz",
      out_rgb="rgb.nii.gz",
      out_md="md.nii.gz",
      out_ad="ad.nii.gz",
      out_rd="rd.nii.gz",
      out_mode="mode.nii.gz",
      out_evec="evecs.nii.gz",
      out_eval="evals.nii.gz",
      out_dk_tensor="dki_tensors.nii.gz",
      out_mk="mk.nii.gz",
      out_ak="ak.nii.gz",
      out_rk="rk.nii.gz",
  ):
      """Workflow for Diffusion Kurtosis reconstruction and for computing
      DKI metrics. Performs a DKI reconstruction on the files by 'globing'
      ``input_files`` and saves the DKI metrics in a directory specified by
      ``out_dir``.
  
      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 bvalues files. This path may contain wildcards to use
          multiple bvalues 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)
      fit_method : string, optional
          can be one of the following:
          'OLS' or 'ULLS' for ordinary least squares
          'WLS' or 'UWLLS' for weighted ordinary least squares
      b0_threshold : float, optional
          Threshold used to find b0 volumes.
      sigma : float, optional
          An estimate of the variance. [3]_ recommend to use
          1.5267 * std(background_noise), where background_noise is estimated
          from some part of the image known to contain no signal (only noise)
      save_metrics : variable string, optional
          List of metrics to save.
          Possible values: fa, ga, rgb, md, ad, rd, mode, tensor, evec, eval
      out_dir : string, optional
          Output directory. (default current directory)
      out_dt_tensor : string, optional
          Name of the tensors volume to be saved.
      out_dk_tensor : string, optional
          Name of the tensors volume to be saved.
      out_fa : string, optional
          Name of the fractional anisotropy volume to be saved.
      out_ga : string, optional
          Name of the geodesic anisotropy volume to be saved.
      out_rgb : string, optional
          Name of the color fa volume to be saved.
      out_md : string, optional
          Name of the mean diffusivity volume to be saved.
      out_ad : string, optional
          Name of the axial diffusivity volume to be saved.
      out_rd : string, optional
          Name of the radial diffusivity volume to be saved.
      out_mode : string, optional
          Name of the mode volume to be saved.
      out_evec : string, optional
          Name of the eigenvectors volume to be saved.
      out_eval : string, optional
          Name of the eigenvalues to be saved.
      out_mk : string, optional
          Name of the mean kurtosis to be saved.
      out_ak : string, optional
          Name of the axial kurtosis to be saved.
      out_rk : string, optional
          Name of the radial kurtosis to be saved.
  
      References
      ----------
      .. [1] Tabesh, A., Jensen, J.H., Ardekani, B.A., Helpern, J.A., 2011.
         Estimation of tensors and tensor-derived measures in diffusional
         kurtosis imaging. Magn Reson Med. 65(3), 823-836
  
      .. [2] Jensen, Jens H., Joseph A. Helpern, Anita Ramani, Hanzhang Lu,
         and Kyle Kaczynski. 2005. Diffusional Kurtosis Imaging: The
         Quantification of Non-Gaussian Water Diffusion by Means of Magnetic
         Resonance Imaging. MRM 53 (6):1432-40.
  
      .. [3] Chang, L-C, Jones, DK and Pierpaoli, C (2005). RESTORE: robust
         estimation of tensors by outlier rejection. MRM, 53: 1088-95.
  
      """
      save_metrics = save_metrics or []
  
      io_it = self.get_io_iterator()
  
      for (
          dwi,
          bval,
          bvec,
          mask,
          otensor,
          ofa,
          oga,
          orgb,
          omd,
          oad,
          orad,
          omode,
          oevecs,
          oevals,
          odk_tensor,
          omk,
          oak,
          ork,
      ) in io_it:
          logging.info("Computing DKI metrics for {0}".format(dwi))
          data, affine = load_nifti(dwi)
  
          if mask is not None:
              mask = load_nifti_data(mask).astype(bool)
  
          optional_args = {}
          if fit_method in ["RT", "restore", "RESTORE", "NLLS"]:
              optional_args["sigma"] = sigma
  
          dkfit, _ = self.get_fitted_tensor(
              data,
              mask,
              bval,
              bvec,
              b0_threshold,
              fit_method,
              optional_args=optional_args,
          )
  
          if not save_metrics:
              save_metrics = [
                  "mk",
                  "rk",
                  "ak",
                  "fa",
                  "md",
                  "rd",
                  "ad",
                  "ga",
                  "rgb",
                  "mode",
                  "evec",
                  "eval",
                  "dt_tensor",
                  "dk_tensor",
              ]
  
          evals, evecs, kt = split_dki_param(dkfit.model_params)
          FA = fractional_anisotropy(evals)
          FA[np.isnan(FA)] = 0
          FA = np.clip(FA, 0, 1)
  
          if "dt_tensor" in save_metrics:
              tensor_vals = lower_triangular(dkfit.quadratic_form)
              correct_order = [0, 1, 3, 2, 4, 5]
              tensor_vals_reordered = tensor_vals[..., correct_order]
              save_nifti(
                  otensor,
                  tensor_vals_reordered.astype(np.float32),
                  affine,
              )
  
          if "dk_tensor" in save_metrics:
              save_nifti(
                  odk_tensor, dkfit.kt.astype(np.float32), affine
              )
  
          if "fa" in save_metrics:
              save_nifti(ofa, FA.astype(np.float32), affine)
  
          if "ga" in save_metrics:
              GA = geodesic_anisotropy(dkfit.evals)
              save_nifti(oga, GA.astype(np.float32), affine)
  
          if "rgb" in save_metrics:
              RGB = color_fa(FA, dkfit.evecs)
              save_nifti(orgb, np.array(255 * RGB, "uint8"), affine)
  
          if "md" in save_metrics:
              MD = mean_diffusivity(dkfit.evals)
              save_nifti(omd, MD.astype(np.float32), affine)
  
          if "ad" in save_metrics:
              AD = axial_diffusivity(dkfit.evals)
              save_nifti(oad, AD.astype(np.float32), affine)
  
          if "rd" in save_metrics:
              RD = radial_diffusivity(dkfit.evals)
              save_nifti(orad, RD.astype(np.float32), affine)
  
          if "mode" in save_metrics:
              MODE = get_mode(dkfit.quadratic_form)
              save_nifti(omode, MODE.astype(np.float32), affine)
  
          if "evec" in save_metrics:
              save_nifti(oevecs, dkfit.evecs.astype(np.float32), affine)
  
          if "eval" in save_metrics:
              save_nifti(oevals, dkfit.evals.astype(np.float32), affine)
  
          if "mk" in save_metrics:
              save_nifti(omk, dkfit.mk().astype(np.float32), affine)
  
          if "ak" in save_metrics:
              save_nifti(oak, dkfit.ak().astype(np.float32), affine)
  
          if "rk" in save_metrics:
              save_nifti(ork, dkfit.rk().astype(np.float32), affine)
  
          logging.info(
              "DKI metrics saved in {0}".format(os.path.dirname(oevals))
          )
  
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 ReconstDkiFlow obj's underlying member method code with example usage: obj.run(input_files, bvalues_files, bvectors_files, mask_files, fit_method, b0_threshold, sigma, save_metrics, out_dir, out_dt_tensor, out_fa, out_ga, out_rgb, out_md, out_ad, out_rd, out_mode, out_evec, out_eval, out_dk_tensor, out_mk, out_ak, out_rk) without return types