code: def test_get_explained_variance_ratio(tmp_path, short_raw_epochs):
      """Test ICA.get_explained_variance_ratio()."""
      pytest.importorskip("sklearn")
      raw, epochs, _ = short_raw_epochs
      ica = ICA(max_iter=1)
  
      # Unfitted ICA should raise an exception
      with pytest.raises(ValueError, match="ICA must be fitted first"):
          ica.get_explained_variance_ratio(epochs)
  
      with pytest.warns(
          RuntimeWarning, match="were baseline-corrected"
      ):
          ica.fit(epochs)
  
      # components = int, ch_type = None
      explained_var_comp_0 = ica.get_explained_variance_ratio(
          epochs, components=0
      )
      # components = int, ch_type = str
      explained_var_comp_0_eeg = ica.get_explained_variance_ratio(
          epochs, components=0, ch_type="eeg"
      )
      # components = int, ch_type = list of str
      explained_var_comp_0_eeg_mag = ica.get_explained_variance_ratio(
          epochs, components=0, ch_type=["eeg", "mag"]
      )
      # components = list of int, single element, ch_type = None
      explained_var_comp_1 = ica.get_explained_variance_ratio(
          epochs, components=[1]
      )
      # components = list of int, multiple elements, ch_type = None
      explained_var_comps_01 = ica.get_explained_variance_ratio(
          epochs, components=[0, 1]
      )
      # components = None, i.e., all components, ch_type = None
      explained_var_comps_all = ica.get_explained_variance_ratio(
          epochs, components=None
      )
  
      assert "grad" in explained_var_comp_0
      assert "mag" in explained_var_comp_0
      assert "eeg" in explained_var_comp_0
  
      assert len(explained_var_comp_0_eeg) == 1
      assert "eeg" in explained_var_comp_0_eeg
  
      assert "mag" in explained_var_comp_0_eeg_mag
      assert "eeg" in explained_var_comp_0_eeg_mag
      assert "grad" not in explained_var_comp_0_eeg_mag
  
      assert round(explained_var_comp_0["grad"], 4) == 0.1784
      assert round(explained_var_comp_0["mag"], 4) == 0.0259
      assert round(explained_var_comp_0["eeg"], 4) == 0.0229
  
      assert np.isclose(
          explained_var_comp_0["eeg"], explained_var_comp_0_eeg["eeg"]
      )
      assert np.isclose(
          explained_var_comp_0["mag"],
          explained_var_comp_0_eeg_mag["mag"],
      )
      assert np.isclose(
          explained_var_comp_0["eeg"],
          explained_var_comp_0_eeg_mag["eeg"],
      )
  
      assert round(explained_var_comp_1["eeg"], 4) == 0.0231
      assert round(explained_var_comps_01["eeg"], 4) == 0.0459
      assert (
          explained_var_comps_all["grad"]
          == explained_var_comps_all["mag"]
          == explained_var_comps_all["eeg"]
          == 1
      )
  
      # Test Raw
      ica.get_explained_variance_ratio(raw)
      # Test Evoked
      evoked = epochs.average()
      ica.get_explained_variance_ratio(evoked)
      # Test Evoked without baseline correction
      evoked.baseline = None
      ica.get_explained_variance_ratio(evoked)
  
      # Test invalid ch_type
      with pytest.raises(
          ValueError, match="only the following channel types"
      ):
          ica.get_explained_variance_ratio(raw, ch_type="foobar")
  
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 15 for calling a global function with example usage: test_get_explained_variance_ratio(tmp_path, short_raw_epochs) without return types