code: def find_closest_intervals(detections, annotations, matches=None):
      """
      Find the closest annotated interval to each beat detection.
  
      Parameters
      ----------
      detections : list or numpy array
          Detected beats.
      annotations : list or numpy array
          Annotated beats.
      matches : list or numpy array
          Indices of the closest beats.
  
      Returns
      -------
      numpy array
          Closest annotated beat intervals.
  
      Notes
      -----
      The sequences must be ordered. To speed up the calculation, a list of
      pre-computed indices of the closest matches can be used.
  
      The function does NOT test if each detection has a surrounding interval,
      it always returns the closest interval.
  
      """
      # if no detection are given, return an empty interval array
      if len(detections) == 0:
          return np.zeros(0, dtype=float)
      # at least annotations must be given
      if len(annotations) < 2:
          raise BeatIntervalError
      # init array
      closest_interval = np.ones_like(detections)
      # intervals
      # Note: it is faster if we combine the forward and backward intervals,
      #       but we need to take care of the sizes; intervals to the next
      #       annotation are always the same as those at the next index
      intervals = np.zeros(len(annotations) + 1)
      # intervals to previous annotation
      intervals[1:-1] = np.diff(annotations)
      # interval of the first annotation to the left is the same as to the right
      intervals[0] = intervals[1]
      # interval of the last annotation to the right is the same as to the left
      intervals[-1] = intervals[-2]
      # determine the closest annotations
      if matches is None:
          matches = find_closest_matches(detections, annotations)
      # calculate the absolute errors
      errors = calc_errors(detections, annotations, matches)
      # if the errors are positive, the detection is after the annotation
      # thus use the interval towards the next annotation
      closest_interval[errors > 0] = intervals[matches[errors > 0] + 1]
      # if the errors are 0 or negative, the detection is before the annotation
      # or at the same position; thus use the interval to previous annotation
      closest_interval[errors <= 0] = intervals[matches[errors <= 0]]
      # return the closest interval
      return closest_interval
  
imports: from __future__ import absolute_import, division, print_function
  import warnings
  from functools import wraps
  import numpy
  from .None import MeanEvaluation, calc_absolute_errors, calc_errors, evaluation_io, find_closest_matches
  from .onsets import OnsetEvaluation
label_desc: Use image node_id 5 for calling a global function with example usage: find_closest_intervals(detections, annotations, matches) and returns: closest_interval, np