code: def merge_events(events, ids, new_id, replace_events=True):
      """Merge a set of :term:`events`.
  
      Parameters
      ----------
      events : array, shape (n_events_in, 3)
          Events.
      ids : array of int
          The ids of events to merge.
      new_id : int
          The new id.
      replace_events : bool
          If True (default), old event ids are replaced. Otherwise,
          new events will be added to the old event list.
  
      Returns
      -------
      new_events : array, shape (n_events_out, 3)
          The new events.
  
      Notes
      -----
      Rather than merging events you can use hierarchical event_id
      in Epochs. For example, here::
  
          >>> event_id = {'auditory/left': 1, 'auditory/right': 2}
  
      And the condition 'auditory' would correspond to either 1 or 2.
  
      Examples
      --------
      Here is quick example of the behavior::
  
          >>> events = [[134, 0, 1], [341, 0, 2], [502, 0, 3]]
          >>> merge_events(events, [1, 2], 12, replace_events=True)
          array([[134,   0,  12],
                 [341,   0,  12],
                 [502,   0,   3]])
          >>> merge_events(events, [1, 2], 12, replace_events=False)
          array([[134,   0,   1],
                 [134,   0,  12],
                 [341,   0,   2],
                 [341,   0,  12],
                 [502,   0,   3]])
      """
      events = np.asarray(events)
      events_out = events.copy()
      idx_touched = (
          []
      )  # to keep track of the original events we can keep
      for col in [1, 2]:
          for i in ids:
              mask = events[:, col] == i
              events_out[mask, col] = new_id
              idx_touched.append(np.where(mask)[0])
      if not replace_events:
          idx_touched = np.unique(np.concatenate(idx_touched))
          events_out = np.concatenate(
              (events_out, events[idx_touched]), axis=0
          )
          # Now sort in lexical order
          events_out = events_out[np.lexsort(events_out.T[::-1])]
      return events_out
  
imports: from collections.abc import Sequence
  from pathlib import Path
  import numpy
  from ._fiff.constants import FIFF
  from ._fiff.open import fiff_open
  from ._fiff.pick import pick_channels
  from ._fiff.tag import read_tag
  from ._fiff.tree import dir_tree_find
  from ._fiff.write import end_block, start_and_end_file, start_block, write_int
  from .utils import _check_fname, _check_integer_or_list, _check_on_missing, _check_option, _get_stim_channel, _on_missing, _pl, _validate_type, check_fname, fill_doc, logger, verbose, warn
label_desc: Use image node_id 12 for calling a global function with example usage: merge_events(events, ids, new_id, replace_events) and returns: events_out