code: def test_ica_labels():
      """Test ICA labels."""
      # The CTF data are uniquely well suited to testing the ICA.find_bads_
      # methods
      raw = read_raw_ctf(ctf_fname, preload=True)
      raw.pick(raw.ch_names[:300:10] + raw.ch_names[300:])
  
      # set the appropriate EEG channels to EOG and ECG
      rename = {"EEG057": "eog", "EEG058": "eog", "EEG059": "ecg"}
      for key in rename:
          assert key in raw.ch_names
      raw.set_channel_types(rename)
      ica = ICA(
          n_components=4,
          max_iter=2,
          method="fastica",
          allow_ref_meg=True,
      )
      with pytest.warns(UserWarning, match="did not converge"):
          ica.fit(raw)
      _assert_ica_attributes(ica)
  
      ica.find_bads_eog(raw, l_freq=None, h_freq=None)
      picks = list(pick_types(raw.info, meg=False, eog=True))
      for idx, ch in enumerate(picks):
          assert (
              "{}/{}/{}".format("eog", idx, raw.ch_names[ch])
              in ica.labels_
          )
      assert "eog" in ica.labels_
      for key in ("ecg", "ref_meg", "ecg/ECG-MAG"):
          assert key not in ica.labels_
  
      ica.find_bads_ecg(
          raw,
          l_freq=None,
          h_freq=None,
          method="correlation",
          threshold="auto",
      )
      picks = list(pick_types(raw.info, meg=False, ecg=True))
      for idx, ch in enumerate(picks):
          assert (
              "{}/{}/{}".format("ecg", idx, raw.ch_names[ch])
              in ica.labels_
          )
      for key in ("ecg", "eog"):
          assert key in ica.labels_
      for key in ("ref_meg", "ecg/ECG-MAG"):
          assert key not in ica.labels_
  
      # derive reference ICA components and append them to raw
      ica_rf = ICA(n_components=2, max_iter=2, allow_ref_meg=True)
      with pytest.warns(UserWarning, match="did not converge"):
          ica_rf.fit(raw.copy().pick("ref_meg"))
      icacomps = ica_rf.get_sources(raw)
      # rename components so they are auto-detected by find_bads_ref
      icacomps.rename_channels(
          {c: "REF_" + c for c in icacomps.ch_names}
      )
      # and add them to raw
      raw.add_channels([icacomps])
      ica.find_bads_ref(
          raw, l_freq=None, h_freq=None, method="separate"
      )
      picks = pick_channels_regexp(raw.ch_names, "REF_ICA*")
      for idx, ch in enumerate(picks):
          assert (
              "{}/{}/{}".format("ref_meg", idx, raw.ch_names[ch])
              in ica.labels_
          )
      ica.find_bads_ref(
          raw, l_freq=None, h_freq=None, method="together"
      )
      assert "ref_meg" in ica.labels_
  
      for key in ("ecg", "eog", "ref_meg"):
          assert key in ica.labels_
      assert "ecg/ECG-MAG" not in ica.labels_
  
      ica.find_bads_ecg(raw, l_freq=None, h_freq=None, threshold="auto")
      for key in ("ecg", "eog", "ref_meg", "ecg/ECG-MAG"):
          assert key in ica.labels_
  
      labels, scores = ica.find_bads_muscle(raw, threshold=0.4)
      assert "muscle" in ica.labels_
      assert labels == [0]
      assert_allclose(
          scores, [0.5, 0.01, 0.02, 0.002], atol=0.001, rtol=0.1
      )
  
      events = np.array([[6000, 0, 0], [8000, 0, 0]])
      epochs = Epochs(raw, events=events, baseline=None, preload=True)
      # move up threhsold more noise because less data
      scores = ica.find_bads_muscle(epochs, threshold=0.8)[1]
      assert "muscle" in ica.labels_
      assert ica.labels_["muscle"] == [0]
      assert_allclose(scores, [0.81, 0.14, 0.37, 0.05], atol=0.03)
  
      ica = ICA(
          n_components=4,
          max_iter=2,
          method="fastica",
          allow_ref_meg=True,
      )
      with pytest.warns(UserWarning, match="did not converge"):
          ica.fit(raw, picks="eeg")
      ica.find_bads_muscle(raw)
      assert "muscle" in ica.labels_
  
imports: import os
  import shutil
  from contextlib import nullcontext
  from pathlib import Path
  import matplotlib.pyplot
  import numpy
  import pytest
  from numpy.testing import assert_allclose, assert_array_almost_equal, assert_array_equal, assert_equal
  from scipy import linalg, stats
  from scipy.io import loadmat, savemat
  from mne import Annotations, Epochs, EpochsArray, EvokedArray, Info, create_info, make_ad_hoc_cov, pick_channels_regexp, pick_types, read_events
  from mne._fiff.pick import _DATA_CH_TYPES_SPLIT, get_channel_type_constants
  from mne.cov import read_cov
  from mne.datasets import testing
  from mne.event import make_fixed_length_events
  from mne.io import RawArray, read_raw_ctf, read_raw_eeglab, read_raw_fif
  from mne.io.eeglab.eeglab import _check_load_mat
  from mne.preprocessing import ICA
  from mne.preprocessing import ica_find_ecg_events, ica_find_eog_events, read_ica
  from mne.preprocessing.ica import _ica_explained_variance, _sort_components, corrmap, get_score_funcs, read_ica_eeglab
  from mne.rank import _compute_rank_int
  from mne.utils import _record_warnings, catch_logging, check_version
label_desc: Use image node_id 25 for calling a global function with example usage: test_ica_labels() without return types