nodes: 0, module; 1, function_definition; 2, function_name:merge_close; 3, parameters; 4, block; 5, identifier:events; 6, identifier:min_interval; 7, default_parameter; 8, expression_statement; 9, expression_statement; 10, expression_statement; 11, for_statement; 12, return_statement; 13, identifier:merge_to_longer; 14, False; 15, comment:"""Merge events that are separated by a less than a minimum interval.
  
      Parameters
      ----------
      events : list of dict
          events with 'start' and 'end' times, from one or several channels.
          **Events must be sorted by their start time.**
      min_interval : float
          minimum delay between consecutive events, in seconds
      merge_to_longer : bool (default: False)
          If True, info (chan, peak, etc.) from the longer of the 2 events is
          kept. Otherwise, info from the earlier onset spindle is kept.
  
      Returns
      -------
      list of dict
          original events list with close events merged.
      """; 16, assignment; 17, assignment; 18, identifier:higher; 19, identifier:events; 20, block; 21, identifier:merged; 22, identifier:half_iv; 23, binary_operator:min_interval / 2; 24, identifier:merged; 25, list; 26, if_statement; 27, identifier:min_interval; 28, integer:2; 29, not_operator; 30, block; 31, else_clause; 32, identifier:merged; 33, expression_statement; 34, block; 35, call; 36, expression_statement; 37, if_statement; 38, attribute; 39, argument_list; 40, assignment; 41, comparison_operator:higher['start'] - half_iv <= lower['end'] + half_iv; 42, block; 43, else_clause; 44, identifier:merged; 45, identifier:append; 46, identifier:higher; 47, identifier:lower; 48, subscript; 49, binary_operator:higher['start'] - half_iv; 50, binary_operator:lower['end'] + half_iv; 51, if_statement; 52, block; 53, identifier:merged; 54, unary_operator; 55, subscript; 56, identifier:half_iv; 57, subscript; 58, identifier:half_iv; 59, boolean_operator; 60, block; 61, else_clause; 62, expression_statement; 63, integer:1; 64, identifier:higher; 65, string; 66, identifier:lower; 67, string; 68, identifier:merge_to_longer; 69, parenthesized_expression; 70, expression_statement; 71, expression_statement; 72, expression_statement; 73, block; 74, call; 75, string_content:start; 76, string_content:end; 77, comparison_operator:higher['end'] - higher['start'] >
                  lower['end'] - lower['start']; 78, assignment; 79, call; 80, assignment; 81, expression_statement; 82, expression_statement; 83, attribute; 84, argument_list; 85, binary_operator:higher['end'] - higher['start']; 86, binary_operator:lower['end'] - lower['start']; 87, identifier:start; 88, call; 89, attribute; 90, argument_list; 91, subscript; 92, identifier:higher; 93, assignment; 94, call; 95, identifier:merged; 96, identifier:append; 97, identifier:higher; 98, subscript; 99, subscript; 100, subscript; 101, subscript; 102, identifier:min; 103, argument_list; 104, identifier:higher; 105, identifier:update; 106, dictionary; 107, identifier:merged; 108, unary_operator; 109, identifier:end; 110, call; 111, attribute; 112, argument_list; 113, identifier:higher; 114, string; 115, identifier:higher; 116, string; 117, identifier:lower; 118, string; 119, identifier:lower; 120, string; 121, subscript; 122, subscript; 123, pair; 124, integer:1; 125, identifier:max; 126, argument_list; 127, subscript; 128, identifier:update; 129, dictionary; 130, string_content:end; 131, string_content:start; 132, string_content:end; 133, string_content:start; 134, identifier:lower; 135, string; 136, identifier:higher; 137, string; 138, string; 139, identifier:start; 140, subscript; 141, subscript; 142, identifier:merged; 143, unary_operator; 144, pair; 145, string_content:start; 146, string_content:start; 147, string_content:start; 148, identifier:lower; 149, string; 150, identifier:higher; 151, string; 152, integer:1; 153, string; 154, identifier:end; 155, string_content:end; 156, string_content:end; 157, string_content:end
code: def merge_close(events, min_interval, merge_to_longer=False):
      """Merge events that are separated by a less than a minimum interval.
  
      Parameters
      ----------
      events : list of dict
          events with 'start' and 'end' times, from one or several channels.
          **Events must be sorted by their start time.**
      min_interval : float
          minimum delay between consecutive events, in seconds
      merge_to_longer : bool (default: False)
          If True, info (chan, peak, etc.) from the longer of the 2 events is
          kept. Otherwise, info from the earlier onset spindle is kept.
  
      Returns
      -------
      list of dict
          original events list with close events merged.
      """
      half_iv = min_interval / 2
      merged = []
  
      for higher in events:
  
          if not merged:
              merged.append(higher)
  
          else:
              lower = merged[-1]
  
              if higher['start'] - half_iv <= lower['end'] + half_iv:
  
                  if merge_to_longer and (higher['end'] - higher['start'] >
                  lower['end'] - lower['start']):
                      start = min(lower['start'], higher['start'])
                      higher.update({'start': start})
                      merged[-1] = higher
  
                  else:
                      end = max(lower['end'], higher['end'])
                      merged[-1].update({'end': end})
  
              else:
                  merged.append(higher)
  
      return merged