code: def calc_intervals(events, fwd=False):
      """
      Calculate the intervals of all events to the previous/next event.
  
      Parameters
      ----------
      events : numpy array
          Beat sequence.
      fwd : bool, optional
          Calculate the intervals towards the next event (instead of previous).
  
      Returns
      -------
      numpy array
          Beat intervals.
  
      Notes
      -----
      The sequence must be ordered. The first (last) interval will be set to
      the same value as the second (second to last) interval (when used in
      `fwd` mode).
  
      """
      # at least 2 events must be given to calculate an interval
      if len(events) < 2:
          raise BeatIntervalError
      interval = np.zeros_like(events)
      if fwd:
          interval[:-1] = np.diff(events)
          # set the last interval to the same value as the second last
          interval[-1] = interval[-2]
      else:
          interval[1:] = np.diff(events)
          # set the first interval to the same value as the second
          interval[0] = interval[1]
      # return
      return 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 4 for calling a global function with example usage: calc_intervals(events, fwd) and returns: interval