code: def test_annotate_amplitude(meas_date, first_samp):
      """Test automatic annotation for segments based on peak-to-peak value."""
      n_ch, n_times = 11, 1000
      data = np.random.RandomState(0).randn(n_ch, n_times)
      assert not (
          np.diff(data, axis=-1) == 0
      ).any()  # nothing flat at first
      info = create_info(n_ch, 1000.0, "eeg")
      # from annotate_flat: test first_samp != for gh-6295
      raw = RawArray(data, info, first_samp=first_samp)
      raw.info["bads"] = [raw.ch_names[-1]]
      raw.set_meas_date(meas_date)
  
      # -- test bad channels spatial marking --
      for perc, dur in itertools.product(
          (5, 99.9, 100.0), (0.005, 0.95, 0.99)
      ):
          kwargs = dict(bad_percent=perc, min_duration=dur)
  
          # test entire channel flat
          raw_ = raw.copy()
          raw_._data[0] = 0.0
          annots, bads = annotate_amplitude(
              raw_, peak=None, flat=0.0, **kwargs
          )
          assert len(annots) == 0
          assert bads == ["0"]
  
          # test multiple channels flat
          raw_ = raw.copy()
          raw_._data[0] = 0.0
          raw_._data[2] = 0.0
          annots, bads = annotate_amplitude(
              raw_, peak=None, flat=0.0, **kwargs
          )
          assert len(annots) == 0
          assert bads == ["0", "2"]
  
          # test entire channel drifting
          raw_ = raw.copy()
          raw_._data[0] = np.arange(0, raw.times.size * 10, 10)
          annots, bads = annotate_amplitude(
              raw_, peak=5, flat=None, **kwargs
          )
          assert len(annots) == 0
          assert bads == ["0"]
  
          # test multiple channels drifting
          raw_ = raw.copy()
          raw_._data[0] = np.arange(0, raw.times.size * 10, 10)
          raw_._data[2] = np.arange(0, raw.times.size * 10, 10)
          annots, bads = annotate_amplitude(
              raw_, peak=5, flat=None, **kwargs
          )
          assert len(annots) == 0
          assert bads == ["0", "2"]
  
      # -- test bad channels temporal marking --
      # flat channel for the 20% last points
      n_good_times = int(round(0.8 * n_times))
      raw_ = raw.copy()
      raw_._data[0, n_good_times:] = 0.0
      for perc in (5, 20):
          annots, bads = annotate_amplitude(
              raw_, peak=None, flat=0.0, bad_percent=perc
          )
          assert len(annots) == 0
          assert bads == ["0"]
      annots, bads = annotate_amplitude(
          raw_, peak=None, flat=0.0, bad_percent=20.1
      )
      assert len(annots) == 1
      assert len(bads) == 0
      # check annotation instance
      assert annots[0]["description"] == "BAD_flat"
      _check_annotation(
          raw_, annots[0], meas_date, first_samp, n_good_times, -1
      )
  
      # test multiple channels flat and multiple channels drift
      raw_ = raw.copy()
      raw_._data[0, 800:] = 0.0
      raw_._data[1, 850:950] = 0.0
      raw_._data[2, :200] = np.arange(0, 200 * 10, 10)
      raw_._data[2, 200:] += raw_._data[
          2, 199
      ]  # add offset for next samples
      raw_._data[3, 50:150] = np.arange(0, 100 * 10, 10)
      raw_._data[3, 150:] += raw_._data[
          3, 149
      ]  # add offset for next samples
      for perc in (5, 10):
          annots, bads = annotate_amplitude(
              raw_, peak=5, flat=0.0, bad_percent=perc
          )
          assert len(annots) == 0
          assert bads == ["0", "1", "2", "3"]
      for perc in (10.1, 20):
          annots, bads = annotate_amplitude(
              raw_, peak=5, flat=0.0, bad_percent=perc
          )
          assert len(annots) == 2
          assert bads == ["0", "2"]
          # check annotation instance
          assert all(
              annot["description"] in ("BAD_flat", "BAD_peak")
              for annot in annots
          )
          for annot in annots:
              start_idx = (
                  50 if annot["description"] == "BAD_peak" else 850
              )
              stop_idx = (
                  149 if annot["description"] == "BAD_peak" else 949
              )
              _check_annotation(
                  raw_,
                  annot,
                  meas_date,
                  first_samp,
                  start_idx,
                  stop_idx,
              )
      annots, bads = annotate_amplitude(
          raw_, peak=5, flat=0.0, bad_percent=20.1
      )
      assert len(annots) == 2
      assert len(bads) == 0
      # check annotation instance
      assert all(
          annot["description"] in ("BAD_flat", "BAD_peak")
          for annot in annots
      )
      for annot in annots:
          start_idx = 0 if annot["description"] == "BAD_peak" else 800
          stop_idx = 199 if annot["description"] == "BAD_peak" else -1
          _check_annotation(
              raw_, annot, meas_date, first_samp, start_idx, stop_idx
          )
  
      # test flat on already marked bad channel
      raw_ = raw.copy()
      raw_._data[-1, :] = 0.0  # this channel is already in info['bads']
      annots, bads = annotate_amplitude(
          raw_, peak=None, flat=0.0, bad_percent=5
      )
      assert len(annots) == 0
      assert len(bads) == 0
  
      # test drift on already marked bad channel
      raw_ = raw.copy()
      raw_._data[-1, :] = np.arange(0, raw.times.size * 10, 10)
      annots, bads = annotate_amplitude(
          raw_, peak=5, flat=None, bad_percent=5
      )
      assert len(annots) == 0
      assert len(bads) == 0
  
file_path: mne-python/mne/preprocessing/tests/test_annotate_amplitude.py
imports: import datetime
  import itertools
  import re
  from pathlib import Path
  import numpy
  import pytest
  from mne import create_info
  from mne.annotations import Annotations
  from mne.datasets import testing
  from mne.io import RawArray, read_raw_fif
  from mne.preprocessing import annotate_amplitude
label_desc: Use image node_id 1 for calling a global function with example usage: test_annotate_amplitude(meas_date, first_samp) without return types