code: def test_find_events():
      """Test find events in raw file."""
      events = read_events(fname)
      raw = read_raw_fif(raw_fname, preload=True)
      # let's test the defaulting behavior while we're at it
      extra_ends = ["", "_1"]
      orig_envs = [
          os.getenv("MNE_STIM_CHANNEL%s" % s) for s in extra_ends
      ]
      os.environ["MNE_STIM_CHANNEL"] = "STI 014"
      if "MNE_STIM_CHANNEL_1" in os.environ:
          del os.environ["MNE_STIM_CHANNEL_1"]
      events2 = find_events(raw)
      assert_array_almost_equal(events, events2)
      # now test with mask
      events11 = find_events(raw, mask=3, mask_type="not_and")
      with pytest.warns(RuntimeWarning, match="events masked"):
          events22 = read_events(fname, mask=3, mask_type="not_and")
      assert_array_equal(events11, events22)
  
      # Reset some data for ease of comparison
      raw._cropped_samp = 0
      with raw.info._unlock():
          raw.info["sfreq"] = 1000
  
      stim_channel = "STI 014"
      stim_channel_idx = pick_channels(
          raw.info["ch_names"], include=[stim_channel]
      )
  
      # test digital masking
      raw._data[stim_channel_idx, :5] = np.arange(5)
      raw._data[stim_channel_idx, 5:] = 0
      # 1 == '0b1', 2 == '0b10', 3 == '0b11', 4 == '0b100'
  
      pytest.raises(
          TypeError, find_events, raw, mask="0", mask_type="and"
      )
      pytest.raises(
          ValueError, find_events, raw, mask=0, mask_type="blah"
      )
      # testing mask_type. default = 'not_and'
      assert_array_equal(
          find_events(
              raw, shortest_event=1, mask=1, mask_type="not_and"
          ),
          [[2, 0, 2], [4, 2, 4]],
      )
      assert_array_equal(
          find_events(
              raw, shortest_event=1, mask=2, mask_type="not_and"
          ),
          [[1, 0, 1], [3, 0, 1], [4, 1, 4]],
      )
      assert_array_equal(
          find_events(
              raw, shortest_event=1, mask=3, mask_type="not_and"
          ),
          [[4, 0, 4]],
      )
      assert_array_equal(
          find_events(
              raw, shortest_event=1, mask=4, mask_type="not_and"
          ),
          [[1, 0, 1], [2, 1, 2], [3, 2, 3]],
      )
      # testing with mask_type = 'and'
      assert_array_equal(
          find_events(raw, shortest_event=1, mask=1, mask_type="and"),
          [[1, 0, 1], [3, 0, 1]],
      )
      assert_array_equal(
          find_events(raw, shortest_event=1, mask=2, mask_type="and"),
          [[2, 0, 2]],
      )
      assert_array_equal(
          find_events(raw, shortest_event=1, mask=3, mask_type="and"),
          [[1, 0, 1], [2, 1, 2], [3, 2, 3]],
      )
      assert_array_equal(
          find_events(raw, shortest_event=1, mask=4, mask_type="and"),
          [[4, 0, 4]],
      )
  
      # test empty events channel
      raw._data[stim_channel_idx, :] = 0
      assert_array_equal(
          find_events(raw), np.empty((0, 3), dtype="int32")
      )
  
      raw._data[stim_channel_idx, :4] = 1
      assert_array_equal(
          find_events(raw), np.empty((0, 3), dtype="int32")
      )
  
      raw._data[stim_channel_idx, -1:] = 9
      assert_array_equal(find_events(raw), [[14399, 0, 9]])
  
      # Test that we can handle consecutive events with no gap
      raw._data[stim_channel_idx, 10:20] = 5
      raw._data[stim_channel_idx, 20:30] = 6
      raw._data[stim_channel_idx, 30:32] = 5
      raw._data[stim_channel_idx, 40] = 6
  
      assert_array_equal(
          find_events(raw, consecutive=False),
          [[10, 0, 5], [40, 0, 6], [14399, 0, 9]],
      )
      assert_array_equal(
          find_events(raw, consecutive=True),
          [
              [10, 0, 5],
              [20, 5, 6],
              [30, 6, 5],
              [40, 0, 6],
              [14399, 0, 9],
          ],
      )
      assert_array_equal(
          find_events(raw),
          [[10, 0, 5], [20, 5, 6], [40, 0, 6], [14399, 0, 9]],
      )
      assert_array_equal(
          find_events(raw, output="offset", consecutive=False),
          [[31, 0, 5], [40, 0, 6], [14399, 0, 9]],
      )
      assert_array_equal(
          find_events(raw, output="offset", consecutive=True),
          [
              [19, 6, 5],
              [29, 5, 6],
              [31, 0, 5],
              [40, 0, 6],
              [14399, 0, 9],
          ],
      )
      pytest.raises(
          ValueError, find_events, raw, output="step", consecutive=True
      )
      assert_array_equal(
          find_events(
              raw, output="step", consecutive=True, shortest_event=1
          ),
          [
              [10, 0, 5],
              [20, 5, 6],
              [30, 6, 5],
              [32, 5, 0],
              [40, 0, 6],
              [41, 6, 0],
              [14399, 0, 9],
              [14400, 9, 0],
          ],
      )
      assert_array_equal(
          find_events(raw, output="offset"),
          [[19, 6, 5], [31, 0, 6], [40, 0, 6], [14399, 0, 9]],
      )
      assert_array_equal(
          find_events(raw, consecutive=False, min_duration=0.002),
          [[10, 0, 5]],
      )
      assert_array_equal(
          find_events(raw, consecutive=True, min_duration=0.002),
          [[10, 0, 5], [20, 5, 6], [30, 6, 5]],
      )
      assert_array_equal(
          find_events(
              raw,
              output="offset",
              consecutive=False,
              min_duration=0.002,
          ),
          [[31, 0, 5]],
      )
      assert_array_equal(
          find_events(
              raw, output="offset", consecutive=True, min_duration=0.002
          ),
          [[19, 6, 5], [29, 5, 6], [31, 0, 5]],
      )
      assert_array_equal(
          find_events(raw, consecutive=True, min_duration=0.003),
          [[10, 0, 5], [20, 5, 6]],
      )
  
      # test find_stim_steps merge parameter
      raw._data[stim_channel_idx, :] = 0
      raw._data[stim_channel_idx, 0] = 1
      raw._data[stim_channel_idx, 10] = 4
      raw._data[stim_channel_idx, 11:20] = 5
      assert_array_equal(
          find_stim_steps(
              raw, pad_start=0, merge=0, stim_channel=stim_channel
          ),
          [[0, 0, 1], [1, 1, 0], [10, 0, 4], [11, 4, 5], [20, 5, 0]],
      )
      assert_array_equal(
          find_stim_steps(raw, merge=-1, stim_channel=stim_channel),
          [[1, 1, 0], [10, 0, 5], [20, 5, 0]],
      )
      assert_array_equal(
          find_stim_steps(raw, merge=1, stim_channel=stim_channel),
          [[1, 1, 0], [11, 0, 5], [20, 5, 0]],
      )
  
      # put back the env vars we trampled on
      for s, o in zip(extra_ends, orig_envs):
          if o is not None:
              os.environ["MNE_STIM_CHANNEL%s" % s] = o
  
      # Test with list of stim channels
      raw._data[stim_channel_idx, 1:101] = np.zeros(100)
      raw._data[stim_channel_idx, 10:11] = 1
      raw._data[stim_channel_idx, 30:31] = 3
      stim_channel2 = "STI 015"
      stim_channel2_idx = pick_channels(
          raw.info["ch_names"], include=[stim_channel2]
      )
      raw._data[stim_channel2_idx, :] = 0
      raw._data[stim_channel2_idx, :100] = raw._data[
          stim_channel_idx, 5:105
      ]
      events1 = find_events(raw, stim_channel="STI 014")
      events2 = events1.copy()
      events2[:, 0] -= 5
      events = find_events(raw, stim_channel=["STI 014", stim_channel2])
      assert_array_equal(events[::2], events2)
      assert_array_equal(events[1::2], events1)
  
      # test initial_event argument
      info = create_info(["MYSTI"], 1000, "stim")
      data = np.zeros((1, 1000))
      raw = RawArray(data, info, first_samp=7)
      data[0, :10] = 100
      data[0, 30:40] = 200
      assert_array_equal(find_events(raw, "MYSTI"), [[37, 0, 200]])
      assert_array_equal(
          find_events(raw, "MYSTI", initial_event=True),
          [[7, 0, 100], [37, 0, 200]],
      )
  
      # test error message for raw without stim channels
      raw = read_raw_fif(raw_fname, preload=True)
      raw.pick(picks="meg")
      # raw does not have annotations
      with pytest.raises(ValueError, match="'stim_channel'"):
          find_events(raw)
      # if raw has annotations, we show a different error message
      raw.set_annotations(Annotations(0, 2, "test"))
      with pytest.raises(
          ValueError, match="mne.events_from_annotations"
      ):
          find_events(raw)
  
imports: import os
  from pathlib import Path
  import numpy
  import pytest
  from numpy.testing import assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal
  from mne import Annotations, Epochs, compute_raw_covariance, count_events, create_info, find_events, find_stim_steps, make_fixed_length_events, pick_channels, pick_events, read_events, read_evokeds, write_events
  from mne.datasets import testing
  from mne.event import AcqParserFIF, define_target_events, match_event_names, merge_events, shift_time_events
  from mne.io import RawArray, read_raw_fif
label_desc: Use image node_id 6 for calling a global function with example usage: test_find_events() without return types