docstr: Radius Search using Fast Streamline Search
  
  For each given streamlines, return all reference streamlines
  within the given radius. See [StOnge2022]_ for further details.
  
  Parameters
  ----------
  streamlines : Streamlines
      Streamlines to generate the tree structure.
  radius : float
      Search radius (with MDF / average L2 distance)
      must be smaller than max_radius when FFS was initialized.
  use_negative : bool, optional
      When used with bidirectional,
      negative values are returned for reversed order neighbors.
  
  
  Returns
  -------
  res : scipy COOrdinates sparse matrix (nb_slines x nb_slines_ref)
      Adjacency matrix containing all neighbors within the given radius
  
  Notes
  -----
  Given streamlines should be already aligned with ref streamlines.
  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: ["Radius","Search","using","Fast","Streamline","Search","For","each","given","streamlines",",","return","all","reference","streamlines","within","the","given","radius",".","See","[","StOnge2022","]","_","for","further","details",".","Parameters","--","--","--","--","--","streamlines",":","Streamlines","Streamlines","to","generate","the","tree","structure",".","radius",":","float","Search","radius","(","with","MDF","\/","average","L2","distance",")","must","be","smaller","than","max_radius","when","FFS","was","initialized",".","use_negative",":","bool",",","optional","When","used","with","bidirectional",",","negative","values","are","returned","for","reversed","order","neighbors",".","Returns","--","--","--","-","res",":","scipy","COOrdinates","sparse","matrix","(","nb_slines","x","nb_slines_ref",")","Adjacency","matrix","containing","all","neighbors","within","the","given","radius","Notes","--","--","-","Given","streamlines","should","be","already","aligned","with","ref","streamlines",".","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 radius_search(self, streamlines, radius, use_negative=True):
      """Radius Search using Fast Streamline Search
  
      For each given streamlines, return all reference streamlines
      within the given radius. See [StOnge2022]_ for further details.
  
      Parameters
      ----------
      streamlines : Streamlines
          Streamlines to generate the tree structure.
      radius : float
          Search radius (with MDF / average L2 distance)
          must be smaller than max_radius when FFS was initialized.
      use_negative : bool, optional
          When used with bidirectional,
          negative values are returned for reversed order neighbors.
  
  
      Returns
      -------
      res : scipy COOrdinates sparse matrix (nb_slines x nb_slines_ref)
          Adjacency matrix containing all neighbors within the given radius
  
      Notes
      -----
      Given streamlines should be already aligned with ref streamlines.
      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 radius > self.max_radius:
          raise ValueError(
              "radius should be smaller or equal to the given"
              "\n 'max_radius' in FastStreamlineSearch init"
          )
  
      if isinstance(streamlines, StatefulTractogram):
          streamlines = streamlines.streamlines
  
      # Resample query streamlines
      q_slines = self._resample(streamlines)
      q_nb_slines = len(q_slines)
  
      # Compute streamlines barycenter
      q_baryc = self._slines_barycenters(q_slines)
  
      # Verify if each barycenter are inside the min max box
      u_bin, binned_slines_ids = self._barycenters_binning(q_baryc)
  
      # Adapting radius for L1 query: sqrt(3) = 1.73205080756887729..
      # Rounded up for float32 precision to avoid error / false negative
      l1_sum_dist = 1.73205081 * radius * self.nb_mpts
  
      # Search for all similar streamlines
      list_id = []
      list_id_ref = []
      list_dist = []
      for i, bin_id in enumerate(u_bin):
          if bin_id in self.bin_dict:
              slines_id_ref, ref_tree = self.bin_dict[bin_id]
              slines_id = binned_slines_ids[i]
  
              mpts = self._slines_mean_points(q_slines[slines_id])
  
              # Compute Tree L1 Query with mean-points
              res = ref_tree.query_ball_point(mpts, l1_sum_dist, p=1)
  
              # Refine distance with the complete
              for s, ref_ids in enumerate(res):
                  if ref_ids:
                      s_id = slines_id[s]
                      rs_ids = slines_id_ref[ref_ids]
                      d = mean_euclidean_distance(
                          q_slines[s_id], self.ref_slines[rs_ids]
                      )
  
                      # Return all pairs within the radius
                      in_dist_max = d < radius
                      id_ref = rs_ids[in_dist_max]
                      id_s = np.full_like(id_ref, s_id)
  
                      list_id.append(id_s)
                      list_id_ref.append(id_ref)
                      list_dist.append(d[in_dist_max])
  
      # Combine all results in a coup sparse matrix
      if len(list_id) > 0:
          ids_in = np.hstack(list_id)
          ids_ref = np.hstack(list_id_ref)
          dist = np.hstack(list_dist)
  
          if self.bidirectional:
              flipped = ids_ref >= self.ref_nb_slines
              ids_ref[flipped] -= self.ref_nb_slines
              if use_negative:
                  dist[flipped] *= -1.0
  
          return coo_matrix(
              (dist, (ids_in, ids_ref)),
              shape=(q_nb_slines, self.ref_nb_slines),
          )
  
      # No results, return an empty sparse matrix
      return coo_matrix((q_nb_slines, self.ref_nb_slines))
  
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 2 for calling the FastStreamlineSearch obj's underlying member method code with example usage: obj.radius_search(streamlines, radius, use_negative) and returns: coo_matrix, coo_matrix