code: def gaussian_weights(
      bundle, n_points=100, return_mahalnobis=False, stat=np.mean
  ):
      """
      Calculate weights for each streamline/node in a bundle, based on a
      Mahalanobis distance from the core the bundle, at that node (mean, per
      default).
  
      Parameters
      ----------
      bundle : Streamlines
          The streamlines to weight.
      n_points : int, optional
          The number of points to resample to. *If the `bundle` is an array, this
          input is ignored*. Default: 100.
      return_mahalanobis : bool, optional
          Whether to return the Mahalanobis distance instead of the weights.
          Default: False.
      stat : callable, optional.
          The statistic used to calculate the central tendency of streamlines in
          each node. Can be one of {`np.mean`, `np.median`} or other functions
          that have similar API. Default: `np.mean`
  
      Returns
      -------
      w : array of shape (n_streamlines, n_points)
          Weights for each node in each streamline, calculated as its relative
          inverse of the Mahalanobis distance, relative to the distribution of
          coordinates at that node position across streamlines.
  
      """
      # Resample to same length for each streamline:
      bundle = set_number_of_points(bundle, n_points)
  
      # This is the output
      w = np.zeros((len(bundle), n_points))
  
      # If there's only one fiber here, it gets the entire weighting:
      if len(bundle) == 1:
          if return_mahalnobis:
              return np.array([np.nan])
          else:
              return np.array([1])
  
      for node in range(n_points):
          # This should come back as a 3D covariance matrix with the spatial
          # variance covariance of this node across the different streamlines
          # This is a 3-by-3 array:
          node_coords = bundle._data[node::n_points]
          c = np.cov(node_coords.T, ddof=0)
          # Reorganize as an upper diagonal matrix for expected Mahalanobis
          # input:
          c = np.array(
              [
                  [c[0, 0], c[0, 1], c[0, 2]],
                  [0, c[1, 1], c[1, 2]],
                  [0, 0, c[2, 2]],
              ]
          )
          # Calculate the mean or median of this node as well
          # delta = node_coords - np.mean(node_coords, 0)
          m = stat(node_coords, 0)
          # Weights are the inverse of the Mahalanobis distance
          for fn in range(len(bundle)):
              # In the special case where all the streamlines have the exact same
              # coordinate in this node, the covariance matrix is all zeros, so
              # we can't calculate the Mahalanobis distance, we will instead give
              # each streamline an identical weight, equal to the number of
              # streamlines:
              if np.allclose(c, 0):
                  w[:, node] = len(bundle)
                  break
              # Otherwise, go ahead and calculate Mahalanobis for node on
              # fiber[fn]:
              w[fn, node] = mahalanobis(
                  node_coords[fn], m, np.linalg.inv(c)
              )
      if return_mahalnobis:
          return w
      # weighting is inverse to the distance (the further you are, the less you
      # should be weighted)
      w = 1 / w
      # Normalize before returning, so that the weights in each node sum to 1:
      return w / np.sum(w, 0)
  
file_path: dipy/dipy/stats/analysis.py
imports: import os
  import numpy
  from scipy.spatial import cKDTree
  from scipy.ndimage import map_coordinates
  from scipy.spatial.distance import mahalanobis
  from dipy.io.utils import save_buan_profiles_hdf5
  from dipy.segment.clustering import QuickBundles
  from dipy.segment.metricspeed import AveragePointwiseEuclideanMetric
  from dipy.tracking.streamline import set_number_of_points, values_from_volume, orient_by_streamline, Streamlines
label_desc: Use image node_id 4 for calling a global function with example usage: gaussian_weights(bundle, n_points, return_mahalnobis, stat) and returns: unknown, w, np, np