code: def test_annotations_from_events():
      """Test events to annotations conversion."""
      raw = read_raw_fif(fif_fname)
      events = mne.find_events(raw)
  
      # 1. Automatic event description
      # -------------------------------------------------------------------------
      annots = annotations_from_events(
          events,
          raw.info["sfreq"],
          first_samp=raw.first_samp,
          orig_time=None,
      )
      assert len(annots) == events.shape[0]
  
      # Convert back to events
      raw.set_annotations(annots)
      events_out, _ = events_from_annotations(raw, event_id=int)
      assert_array_equal(events, events_out)
  
      # 2. Explicit event mapping
      # -------------------------------------------------------------------------
      event_desc = {1: "one", 2: "two", 3: "three", 32: None}
      annots = annotations_from_events(
          events,
          sfreq=raw.info["sfreq"],
          event_desc=event_desc,
          first_samp=raw.first_samp,
          orig_time=None,
      )
  
      assert np.all(
          [a in ["one", "two", "three"] for a in annots.description]
      )
      assert len(annots) == events[events[:, 2] <= 3].shape[0]
  
      # 3. Pass list
      # -------------------------------------------------------------------------
      event_desc = [1, 2, 3]
      annots = annotations_from_events(
          events,
          sfreq=raw.info["sfreq"],
          event_desc=event_desc,
          first_samp=raw.first_samp,
          orig_time=None,
      )
  
      assert np.all([a in ["1", "2", "3"] for a in annots.description])
      assert len(annots) == events[events[:, 2] <= 3].shape[0]
  
      # 4. Try passing callable
      # -------------------------------------------------------------------------
      event_desc = lambda d: f"event{d}"  # noqa:E731
      annots = annotations_from_events(
          events,
          sfreq=raw.info["sfreq"],
          event_desc=event_desc,
          first_samp=raw.first_samp,
          orig_time=None,
      )
  
      assert np.all(["event" in a for a in annots.description])
      assert len(annots) == events.shape[0]
  
      # 5. Pass numpy array
      # -------------------------------------------------------------------------
      event_desc = np.array([[1, 2, 3], [1, 2, 3]])
      with pytest.raises(ValueError, match="event_desc must be 1D"):
          annots = annotations_from_events(
              events,
              sfreq=raw.info["sfreq"],
              event_desc=event_desc,
              first_samp=raw.first_samp,
              orig_time=None,
          )
  
      with pytest.raises(
          ValueError, match="Invalid type for event_desc"
      ):
          annots = annotations_from_events(
              events,
              sfreq=raw.info["sfreq"],
              event_desc=1,
              first_samp=raw.first_samp,
              orig_time=None,
          )
  
      event_desc = np.array([1, 2, 3])
      annots = annotations_from_events(
          events,
          sfreq=raw.info["sfreq"],
          event_desc=event_desc,
          first_samp=raw.first_samp,
          orig_time=None,
      )
      assert np.all([a in ["1", "2", "3"] for a in annots.description])
      assert len(annots) == events[events[:, 2] <= 3].shape[0]
  
imports: import sys
  from collections import OrderedDict
  from datetime import datetime, timedelta, timezone
  from itertools import repeat
  from pathlib import Path
  import numpy
  import pytest
  from numpy.testing import assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal
  from pytest import approx
  import mne
  from mne import Annotations, Epochs, annotations_from_events, count_annotations, create_info, events_from_annotations, read_annotations
  from mne.annotations import _handle_meas_date, _read_annotations_txt_parse_header, _sync_onset
  from mne.datasets import testing
  from mne.io import RawArray, concatenate_raws, read_raw_fif
  from mne.utils import _dt_to_stamp, _raw_annot, _record_warnings, _stamp_to_dt, assert_and_remove_boundary_annot, catch_logging, check_version
label_desc: Use image node_id 41 for calling a global function with example usage: test_annotations_from_events() without return types