code: def nls_fit_tensor(
      gtab,
      data,
      mask=None,
      Diso=3e-3,
      mdreg=2.7e-3,
      min_signal=1.0e-6,
      f_transform=True,
      cholesky=False,
      jac=False,
      weighting=None,
      sigma=None,
  ):
      """
      Fit the water elimination tensor model using the non-linear least-squares.
  
      Parameters
      ----------
      gtab : a GradientTable class instance
          The gradient table containing diffusion acquisition parameters.
      data : ndarray ([X, Y, Z, ...], g)
          Data or response variables holding the data. Note that the last
          dimension should contain the data. It makes no copies of data.
      mask : array, optional
          A boolean array used to mark the coordinates in the data that should
          be analyzed that has the shape data.shape[:-1]
      Diso : float, optional
          Value of the free water isotropic diffusion. Default is set to 3e-3
          $mm^{2}.s^{-1}$. Please adjust this value if you are assuming different
          units of diffusion.
      mdreg : float, optimal
          DTI's mean diffusivity regularization threshold. If standard DTI
          diffusion tensor's mean diffusivity is almost near the free water
          diffusion value, the diffusion signal is assumed to be only free water
          diffusion (i.e. volume fraction will be set to 1 and tissue's diffusion
          parameters are set to zero). Default md_reg is 2.7e-3 $mm^{2}.s^{-1}$
          (corresponding to 90% of the free water diffusion value).
      min_signal : float
          The minimum signal value. Needs to be a strictly positive
          number. Default: 1.0e-6.
      f_transform : bool, optional
          If true, the water volume fractions is converted during the convergence
          procedure to ft = arcsin(2*f - 1) + pi/2, insuring f estimates between
          0 and 1.
          Default: True
      cholesky : bool, optional
          If true it uses Cholesky decomposition to insure that diffusion tensor
          is positive define.
          Default: False
      jac : bool
          Use the Jacobian? Default: False
      weighting: str, optional
          the weighting scheme to use in considering the
          squared-error. Default behavior is to use uniform weighting. Other
          options: 'sigma' 'gmm'
      sigma: float, optional
          If the 'sigma' weighting scheme is used, a value of sigma needs to be
          provided here. According to [Chang2005]_, a good value to use is
          1.5267 * std(background_noise), where background_noise is estimated
          from some part of the image known to contain no signal (only noise).
  
      Returns
      -------
      fw_params : ndarray (x, y, z, 13)
          Matrix containing in the dimension the free water model parameters in
          the following order:
              1) Three diffusion tensor's eigenvalues
              2) Three lines of the eigenvector matrix each containing the
                 first, second and third coordinates of the eigenvector
              3) The volume fraction of the free water compartment
      """
      fw_params = np.zeros(data.shape[:-1] + (13,))
      W = design_matrix(gtab)
  
      # Prepare mask
      if mask is None:
          mask = np.ones(data.shape[:-1], dtype=bool)
      else:
          if mask.shape != data.shape[:-1]:
              raise ValueError("Mask is not the same shape as data.")
          mask = np.array(mask, dtype=bool, copy=False)
  
      # Prepare S0
      S0 = np.mean(data[:, :, :, gtab.b0s_mask], axis=-1)
  
      index = ndindex(mask.shape)
      for v in index:
          if mask[v]:
              params = nls_iter(
                  W,
                  data[v],
                  S0[v],
                  Diso=Diso,
                  mdreg=mdreg,
                  min_signal=min_signal,
                  f_transform=f_transform,
                  cholesky=cholesky,
                  jac=jac,
                  weighting=weighting,
                  sigma=sigma,
              )
              fw_params[v] = params
  
      return fw_params
  
imports: import warnings
  import numpy
  import scipy.optimize
  from dipy.reconst.base import ReconstModel
  from dipy.reconst.dti import TensorFit, design_matrix, decompose_tensor, _decompose_tensor_nan, from_lower_triangular, lower_triangular
  from dipy.reconst.dki import _positive_evals
  from dipy.reconst.vec_val_sum import vec_val_vect
  from dipy.core.ndindex import ndindex
  from dipy.core.gradients import check_multi_b
  from dipy.reconst.multi_voxel import multi_voxel_fit
label_desc: Use image node_id 7 for calling a global function with example usage: nls_fit_tensor(gtab, data, mask, Diso, mdreg, min_signal, f_transform, cholesky, jac, weighting, sigma) and returns: fw_params