code: def define_target_events(
      events,
      reference_id,
      target_id,
      sfreq,
      tmin,
      tmax,
      new_id=None,
      fill_na=None,
  ):
      """Define new events by co-occurrence of existing events.
  
      This function can be used to evaluate events depending on the
      temporal lag to another event. For example, this can be used to
      analyze evoked responses which were followed by a button press within
      a defined time window.
  
      Parameters
      ----------
      events : ndarray
          Array as returned by mne.find_events.
      reference_id : int
          The reference event. The event defining the epoch of interest.
      target_id : int
          The target event. The event co-occurring in within a certain time
          window around the reference event.
      sfreq : float
          The sampling frequency of the data.
      tmin : float
          The lower limit in seconds from the target event.
      tmax : float
          The upper limit border in seconds from the target event.
      new_id : int
          New ID for the new event.
      fill_na : int | None
          Fill event to be inserted if target is not available within the time
          window specified. If None, the 'null' events will be dropped.
  
      Returns
      -------
      new_events : ndarray
          The new defined events.
      lag : ndarray
          Time lag between reference and target in milliseconds.
      """
      if new_id is None:
          new_id = reference_id
  
      tsample = 1e3 / sfreq
      imin = int(tmin * sfreq)
      imax = int(tmax * sfreq)
  
      new_events = []
      lag = []
      for event in events.copy().astype(int):
          if event[2] == reference_id:
              lower = event[0] + imin
              upper = event[0] + imax
              res = events[
                  (events[:, 0] > lower)
                  & (events[:, 0] < upper)
                  & (events[:, 2] == target_id)
              ]
              if res.any():
                  lag += [event[0] - res[0][0]]
                  event[2] = new_id
                  new_events += [event]
              elif fill_na is not None:
                  event[2] = fill_na
                  new_events += [event]
                  lag.append(np.nan)
  
      new_events = np.array(new_events)
  
      with np.errstate(invalid="ignore"):  # casting nans
          lag = np.abs(lag, dtype="f8")
      if lag.any():
          lag *= tsample
      else:
          lag = np.array([])
  
      return new_events if new_events.any() else np.array([]), lag
  
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 2 for calling a global function with example usage: define_target_events(events, reference_id, target_id, sfreq, tmin, tmax, new_id, fill_na) and returns: unknown,  lag