docstr: Workflow for tensor reconstruction and for computing DTI metrics.
  using Weighted Least-Squares.
  Performs a tensor reconstruction on the files by 'globing'
  ``input_files`` and saves the DTI 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 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.
  fit_method : string, optional
      can be one of the following:
      'WLS' for weighted least squares
      'LS' or 'OLS' for ordinary least squares
      'NLLS' for non-linear least-squares
      'RT' or 'restore' or 'RESTORE' for RESTORE robust tensor fitting
  b0_threshold : float, optional
      Threshold used to find b0 volumes.
  bvecs_tol : float, optional
      Threshold used to check that norm(bvec) = 1 +/- bvecs_tol
  sigma : float, optional
      An estimate of the variance. [5]_ 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)
      b-vectors are unit vectors.
  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_tensor : string, optional
      Name of the tensors volume to be saved.
      Per default, this will be saved following the nifti standard:
      with the tensor elements as Dxx, Dxy, Dyy, Dxz, Dyz, Dzz on the
      last (5th) dimension of the volume (shape: (i, j, k, 1, 6)). If
      `nifti_tensor` is False, this will be saved in an alternate format
      that is used by other software (e.g., FSL): a
      4-dimensional volume (shape (i, j, k, 6)) with Dxx, Dxy, Dxz, Dyy,
      Dyz, Dzz on the last dimension.
  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.
  nifti_tensor : bool, optional
      Whether the tensor is saved in the standard Nifti format or in an
      alternate format
      that is used by other software (e.g., FSL): a
      4-dimensional volume (shape (i, j, k, 6)) with
      Dxx, Dxy, Dxz, Dyy, Dyz, Dzz on the last dimension.
  
  References
  ----------
  .. [1] Basser, P.J., Mattiello, J., LeBihan, D., 1994. Estimation of
     the effective self-diffusion tensor from the NMR spin echo. J Magn
     Reson B 103, 247-254.
  
  .. [2] Basser, P., Pierpaoli, C., 1996. Microstructural and
     physiological features of tissues elucidated by quantitative
     diffusion-tensor MRI.  Journal of Magnetic Resonance 111, 209-219.
  
  .. [3] Lin-Ching C., Jones D.K., Pierpaoli, C. 2005. RESTORE: Robust
     estimation of tensors by outlier rejection. MRM 53: 1088-1095
  
  .. [4] hung, SW., Lu, Y., Henry, R.G., 2006. Comparison of bootstrap
     approaches for estimation of uncertainties of DTI parameters.
     NeuroImage 33, 531-541.
  
  .. [5] 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","tensor","reconstruction","and","for","computing","DTI","metrics",".","using","Weighted","Least-Squares",".","Performs","a","tensor","reconstruction","on","the","files","by","'globing'","``","input_files","``","and","saves","the","DTI","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","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",".","fit_method",":","string",",","optional","can","be","one","of","the","following",":","'WLS","'","for","weighted","least","squares","'LS","'","or","'OLS","'","for","ordinary","least","squares","'NLLS","'","for","non-linear","least-squares","'RT","'","or","'restore","'","or","'RESTORE","'","for","RESTORE","robust","tensor","fitting","b0_threshold",":","float",",","optional","Threshold","used","to","find","b0","volumes",".","bvecs_tol",":","float",",","optional","Threshold","used","to","check","that","norm","(","bvec",")","=","1","+\/-","bvecs_tol","sigma",":","float",",","optional","An","estimate","of","the","variance",".","[","5","]","_","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",")","b-vectors","are","unit","vectors",".","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_tensor",":","string",",","optional","Name","of","the","tensors","volume","to","be","saved",".","Per","default",",","this","will","be","saved","following","the","nifti","standard",":","with","the","tensor","elements","as","Dxx",",","Dxy",",","Dyy",",","Dxz",",","Dyz",",","Dzz","on","the","last","(","5th",")","dimension","of","the","volume","(","shape",":","(","i",",","j",",","k",",","1",",","6",")",")",".","If","`","nifti_tensor","`","is","False",",","this","will","be","saved","in","an","alternate","format","that","is","used","by","other","software","(","e.g.",",","FSL",")",":","a","4-dimensional","volume","(","shape","(","i",",","j",",","k",",","6",")",")","with","Dxx",",","Dxy",",","Dxz",",","Dyy",",","Dyz",",","Dzz","on","the","last","dimension",".","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",".","nifti_tensor",":","bool",",","optional","Whether","the","tensor","is","saved","in","the","standard","Nifti","format","or","in","an","alternate","format","that","is","used","by","other","software","(","e.g.",",","FSL",")",":","a","4-dimensional","volume","(","shape","(","i",",","j",",","k",",","6",")",")","with","Dxx",",","Dxy",",","Dxz",",","Dyy",",","Dyz",",","Dzz","on","the","last","dimension",".","References","--","--","--","--","--","..","[","1","]","Basser",",","P.J.",",","Mattiello",",","J.",",","LeBihan",",","D.",",","1994",".","Estimation","of","the","effective","self-diffusion","tensor","from","the","NMR","spin","echo",".","J","Magn","Reson","B","103",",","247-254",".","..","[","2","]","Basser",",","P.",",","Pierpaoli",",","C.",",","1996",".","Microstructural","and","physiological","features","of","tissues","elucidated","by","quantitative","diffusion-tensor","MRI",".","Journal","of","Magnetic","Resonance","111",",","209-219",".","..","[","3","]","Lin-Ching","C.",",","Jones","D.K.",",","Pierpaoli",",","C.","2005",".","RESTORE",":","Robust","estimation","of","tensors","by","outlier","rejection",".","MRM","53",":","1088-1095","..","[","4","]","hung",",","SW.",",","Lu",",","Y.",",","Henry",",","R.G.",",","2006",".","Comparison","of","bootstrap","approaches","for","estimation","of","uncertainties","of","DTI","parameters",".","NeuroImage","33",",","531-541",".","..","[","5","]","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,
      bvecs_tol=0.01,
      sigma=None,
      save_metrics=None,
      out_dir="",
      out_tensor="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",
      nifti_tensor=True,
  ):
      """Workflow for tensor reconstruction and for computing DTI metrics.
      using Weighted Least-Squares.
      Performs a tensor reconstruction on the files by 'globing'
      ``input_files`` and saves the DTI 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 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.
      fit_method : string, optional
          can be one of the following:
          'WLS' for weighted least squares
          'LS' or 'OLS' for ordinary least squares
          'NLLS' for non-linear least-squares
          'RT' or 'restore' or 'RESTORE' for RESTORE robust tensor fitting
      b0_threshold : float, optional
          Threshold used to find b0 volumes.
      bvecs_tol : float, optional
          Threshold used to check that norm(bvec) = 1 +/- bvecs_tol
      sigma : float, optional
          An estimate of the variance. [5]_ 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)
          b-vectors are unit vectors.
      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_tensor : string, optional
          Name of the tensors volume to be saved.
          Per default, this will be saved following the nifti standard:
          with the tensor elements as Dxx, Dxy, Dyy, Dxz, Dyz, Dzz on the
          last (5th) dimension of the volume (shape: (i, j, k, 1, 6)). If
          `nifti_tensor` is False, this will be saved in an alternate format
          that is used by other software (e.g., FSL): a
          4-dimensional volume (shape (i, j, k, 6)) with Dxx, Dxy, Dxz, Dyy,
          Dyz, Dzz on the last dimension.
      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.
      nifti_tensor : bool, optional
          Whether the tensor is saved in the standard Nifti format or in an
          alternate format
          that is used by other software (e.g., FSL): a
          4-dimensional volume (shape (i, j, k, 6)) with
          Dxx, Dxy, Dxz, Dyy, Dyz, Dzz on the last dimension.
  
      References
      ----------
      .. [1] Basser, P.J., Mattiello, J., LeBihan, D., 1994. Estimation of
         the effective self-diffusion tensor from the NMR spin echo. J Magn
         Reson B 103, 247-254.
  
      .. [2] Basser, P., Pierpaoli, C., 1996. Microstructural and
         physiological features of tissues elucidated by quantitative
         diffusion-tensor MRI.  Journal of Magnetic Resonance 111, 209-219.
  
      .. [3] Lin-Ching C., Jones D.K., Pierpaoli, C. 2005. RESTORE: Robust
         estimation of tensors by outlier rejection. MRM 53: 1088-1095
  
      .. [4] hung, SW., Lu, Y., Henry, R.G., 2006. Comparison of bootstrap
         approaches for estimation of uncertainties of DTI parameters.
         NeuroImage 33, 531-541.
  
      .. [5] 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,
      ) in io_it:
          logging.info("Computing DTI 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
  
          tenfit, _ = self.get_fitted_tensor(
              data,
              mask,
              bval,
              bvec,
              b0_threshold,
              bvecs_tol,
              fit_method,
              optional_args,
          )
  
          if not save_metrics:
              save_metrics = [
                  "fa",
                  "md",
                  "rd",
                  "ad",
                  "ga",
                  "rgb",
                  "mode",
                  "evec",
                  "eval",
                  "tensor",
              ]
  
          FA = fractional_anisotropy(tenfit.evals)
          FA[np.isnan(FA)] = 0
          FA = np.clip(FA, 0, 1)
  
          if "tensor" in save_metrics:
              tensor_vals = lower_triangular(tenfit.quadratic_form)
  
              if nifti_tensor:
                  ten_img = nifti1_symmat(tensor_vals, affine=affine)
              else:
                  alt_order = [0, 1, 3, 2, 4, 5]
                  ten_img = nib.Nifti1Image(
                      tensor_vals[..., alt_order].astype(np.float32),
                      affine,
                  )
  
              nib.save(ten_img, otensor)
  
          if "fa" in save_metrics:
              save_nifti(ofa, FA.astype(np.float32), affine)
  
          if "ga" in save_metrics:
              GA = geodesic_anisotropy(tenfit.evals)
              save_nifti(oga, GA.astype(np.float32), affine)
  
          if "rgb" in save_metrics:
              RGB = color_fa(FA, tenfit.evecs)
              save_nifti(orgb, np.array(255 * RGB, "uint8"), affine)
  
          if "md" in save_metrics:
              MD = mean_diffusivity(tenfit.evals)
              save_nifti(omd, MD.astype(np.float32), affine)
  
          if "ad" in save_metrics:
              AD = axial_diffusivity(tenfit.evals)
              save_nifti(oad, AD.astype(np.float32), affine)
  
          if "rd" in save_metrics:
              RD = radial_diffusivity(tenfit.evals)
              save_nifti(orad, RD.astype(np.float32), affine)
  
          if "mode" in save_metrics:
              MODE = get_mode(tenfit.quadratic_form)
              save_nifti(omode, MODE.astype(np.float32), affine)
  
          if "evec" in save_metrics:
              save_nifti(
                  oevecs, tenfit.evecs.astype(np.float32), affine
              )
  
          if "eval" in save_metrics:
              save_nifti(
                  oevals, tenfit.evals.astype(np.float32), affine
              )
  
          if save_metrics:
              logging.info("DTI metrics saved to")
              for metric in save_metrics:
                  logging.info(
                      self.last_generated_outputs["out_" + metric]
                  )
  
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 ReconstDtiFlow obj's underlying member method code with example usage: obj.run(input_files, bvalues_files, bvectors_files, mask_files, fit_method, b0_threshold, bvecs_tol, sigma, save_metrics, out_dir, out_tensor, out_fa, out_ga, out_rgb, out_md, out_ad, out_rd, out_mode, out_evec, out_eval, nifti_tensor) without return types