code: def near_roi(
      streamlines, affine, region_of_interest, tol=None, mode="any"
  ):
      """Provide filtering criteria for a set of streamlines based on whether
      they fall within a tolerance distance from an ROI.
  
      Parameters
      ----------
      streamlines : list or generator
          A sequence of streamlines. Each streamline should be a (N, 3) array,
          where N is the length of the streamline.
      affine : array (4, 4)
          The mapping between voxel indices and the point space for seeds.
          The voxel_to_rasmm matrix, typically from a NIFTI file.
      region_of_interest : ndarray
          A mask used as a target. Non-zero values are considered to be within
          the target region.
      tol : float
          Distance (in the units of the streamlines, usually mm). If any
          coordinate in the streamline is within this distance from the center
          of any voxel in the ROI, the filtering criterion is set to True for
          this streamline, otherwise False. Defaults to the distance between
          the center of each voxel and the corner of the voxel.
      mode : string, optional
          One of {"any", "all", "either_end", "both_end"}, where return True
          if:
  
          "any" : any point is within tol from ROI. Default.
  
          "all" : all points are within tol from ROI.
  
          "either_end" : either of the end-points is within tol from ROI
  
          "both_end" : both end points are within tol from ROI.
  
      Returns
      -------
      1D array of boolean dtype, shape (len(streamlines), )
  
      This contains `True` for indices corresponding to each streamline
      that passes within a tolerance distance from the target ROI, `False`
      otherwise.
  
      """
      dtc = dist_to_corner(affine)
      if tol is None:
          tol = dtc
      elif tol < dtc:
          w_s = "Tolerance input provided would create gaps in your"
          w_s += " inclusion ROI. Setting to: %s" % dtc
          warn(w_s)
          tol = dtc
  
      roi_coords = np.array(np.where(region_of_interest)).T
      x_roi_coords = apply_affine(affine, roi_coords)
  
      # If it's already a list, we can save time by pre-allocating the output
      if isinstance(streamlines, list):
          out = np.zeros(len(streamlines), dtype=bool)
          for ii, sl in enumerate(streamlines):
              out[ii] = streamline_near_roi(
                  sl, x_roi_coords, tol=tol, mode=mode
              )
          return out
      # If it's a generator, we'll need to generate the output into a list
      else:
          out = []
          for sl in streamlines:
              out.append(
                  streamline_near_roi(
                      sl, x_roi_coords, tol=tol, mode=mode
                  )
              )
  
          return np.array(out, dtype=bool)
  
imports: from collections import defaultdict, OrderedDict
  from functools import wraps
  from itertools import combinations, groupby
  from warnings import warn
  import numpy
  from nibabel.affines import apply_affine
  from scipy.spatial.distance import cdist
  from dipy.core.geometry import dist_to_corner
  from dipy.tracking import metrics
  from dipy.tracking.vox2track import _streamlines_in_mask
  from dipy.tracking._utils import _mapping_to_voxel, _to_voxel_coordinates
label_desc: Use image node_id 12 for calling a global function with example usage: near_roi(streamlines, affine, region_of_interest, tol, mode) and returns: out, np