docstr: Fast Streamline Search (FFS)
  
  Generate the Binned K-D Tree structure with reference streamlines,
  using streamlines barycenter and mean-points.
  See [StOnge2022]_ for further details.
  
  Parameters
  ----------
  ref_streamlines : Streamlines
      Streamlines (ref) to generate the tree structure.
  max_radius : float
      The maximum radius (distance) for subsequent streamline search.
      Used to compute the overlap in-between bins.
  nb_mpts : int, optional
      Number of means points to improve computation speed.
      (this only changes computation time)
  bin_size : float, optional
      The bin size to separate streamlines in groups.
      (this only changes computation time)
  resampling : int, optional
      Number of points used to reshape each streamline.
  bidirectional : bool, optional
      Compute the smallest distance with and without flip.
  
  Notes
  -----
  Make sure that streamlines are aligned in the same space.
  Preferably in millimeter space (voxmm or rasmm).
  
  References
  ----------
  .. [StOnge2022] St-Onge E. et al. Fast Streamline Search:
                  An Exact Technique for Diffusion MRI Tractography.
                  Neuroinformatics, 2022.
docstr_tok: ["Fast","Streamline","Search","(","FFS",")","Generate","the","Binned","K-D","Tree","structure","with","reference","streamlines",",","using","streamlines","barycenter","and","mean-points",".","See","[","StOnge2022","]","_","for","further","details",".","Parameters","--","--","--","--","--","ref_streamlines",":","Streamlines","Streamlines","(","ref",")","to","generate","the","tree","structure",".","max_radius",":","float","The","maximum","radius","(","distance",")","for","subsequent","streamline","search",".","Used","to","compute","the","overlap","in-between","bins",".","nb_mpts",":","int",",","optional","Number","of","means","points","to","improve","computation","speed",".","(","this","only","changes","computation","time",")","bin_size",":","float",",","optional","The","bin","size","to","separate","streamlines","in","groups",".","(","this","only","changes","computation","time",")","resampling",":","int",",","optional","Number","of","points","used","to","reshape","each","streamline",".","bidirectional",":","bool",",","optional","Compute","the","smallest","distance","with","and","without","flip",".","Notes","--","--","-","Make","sure","that","streamlines","are","aligned","in","the","same","space",".","Preferably","in","millimeter","space","(","voxmm","or","rasmm",")",".","References","--","--","--","--","--","..","[","StOnge2022","]","St-Onge","E.","et","al",".","Fast","Streamline","Search",":","An","Exact","Technique","for","Diffusion","MRI","Tractography",".","Neuroinformatics",",","2022","."]
code: def __init__(
      self,
      ref_streamlines,
      max_radius,
      nb_mpts=4,
      bin_size=20.0,
      resampling=24,
      bidirectional=True,
  ):
      """Fast Streamline Search (FFS)
  
      Generate the Binned K-D Tree structure with reference streamlines,
      using streamlines barycenter and mean-points.
      See [StOnge2022]_ for further details.
  
      Parameters
      ----------
      ref_streamlines : Streamlines
          Streamlines (ref) to generate the tree structure.
      max_radius : float
          The maximum radius (distance) for subsequent streamline search.
          Used to compute the overlap in-between bins.
      nb_mpts : int, optional
          Number of means points to improve computation speed.
          (this only changes computation time)
      bin_size : float, optional
          The bin size to separate streamlines in groups.
          (this only changes computation time)
      resampling : int, optional
          Number of points used to reshape each streamline.
      bidirectional : bool, optional
          Compute the smallest distance with and without flip.
  
      Notes
      -----
      Make sure that streamlines are aligned in the same space.
      Preferably in millimeter space (voxmm or rasmm).
  
      References
      ----------
      .. [StOnge2022] St-Onge E. et al. Fast Streamline Search:
                      An Exact Technique for Diffusion MRI Tractography.
                      Neuroinformatics, 2022.
      """
      if max_radius <= 0.0:
          raise ValueError("max_radius needs to be a positive value")
  
      if resampling < 20:
          warnings.warn(
              "For accurate results, resampling should be"
              " at least >= 10 and preferably >= 20"
          )
  
      if resampling % nb_mpts != 0:
          raise ValueError("nb_mpts needs to be a factor of resampling")
  
      if isinstance(ref_streamlines, StatefulTractogram):
          ref_streamlines = ref_streamlines.streamlines
  
      self.nb_mpts = nb_mpts
      self.bin_size = bin_size
      self.bidirectional = bidirectional
      self.resampling = resampling
      self.max_radius = max_radius
  
      # Resample streamlines
      self.ref_slines = self._resample(ref_streamlines)
      self.ref_nb_slines = len(self.ref_slines)
  
      if self.bidirectional:
          self.ref_slines = np.concatenate(
              [self.ref_slines, np.flip(self.ref_slines, axis=1)]
          )
  
      # Compute streamlines barycenter
      barycenters = self._slines_barycenters(self.ref_slines)
  
      # Compute bin shape (min, max, shape)
      bin_overlap = max_radius
      self.min_box = np.min(barycenters, axis=0) - bin_overlap
      self.max_box = np.max(barycenters, axis=0) + bin_overlap
  
      box_length = self.max_box - self.min_box
      self.bin_shape = (box_length // bin_size).astype(int) + 1
  
      # Compute the center of each bin
      bin_list = np.arange(np.prod(self.bin_shape))
      all_bins = np.vstack(np.unravel_index(bin_list, self.bin_shape)).T
      bins_center = all_bins * bin_size + self.min_box + bin_size / 2.0
  
      # Assign a list of streamlines to each bin
      baryc_tree = cKDTree(barycenters)
      center_dist = bin_size / 2.0 + bin_overlap
      baryc_bins = baryc_tree.query_ball_point(
          bins_center, center_dist, p=np.inf
      )
  
      # Compute streamlines mean-points
      meanpts = self._slines_mean_points(self.ref_slines)
  
      # Compute bin indices, streamlines + mean-points tree
      self.bin_dict = {}
      for i, baryc_b in enumerate(baryc_bins):
          if baryc_b:
              slines_id = np.asarray(baryc_b)
              self.bin_dict[i] = (
                  slines_id,
                  cKDTree(meanpts[slines_id]),
              )
  
file_path: dipy/dipy/segment/fss.py
imports: import warnings
  import numpy
  from scipy.sparse import coo_matrix
  from scipy.spatial import cKDTree
  from dipy.tracking.streamline import set_number_of_points
  from dipy.segment.metric import mean_euclidean_distance
  from dipy.io.stateful_tractogram import StatefulTractogram
label_desc: Use image node_id 1 to create a new FastStreamlineSearch object  with example: obj = FastStreamlineSearch(ref_streamlines, max_radius, nb_mpts, bin_size, resampling, bidirectional)