code: def test_cov_rank_estimation(rank_method, proj, meg):
      """Test cov rank estimation."""
      # Test that our rank estimation works properly on a simple case
      evoked = read_evokeds(
          ave_fname, condition=0, baseline=(None, 0), proj=False
      )
      cov = read_cov(cov_fname)
      ch_names = [
          ch
          for ch in evoked.info["ch_names"]
          if "053" not in ch and ch.startswith("EEG")
      ]
      cov = prepare_noise_cov(cov, evoked.info, ch_names, None)
      assert (
          cov["eig"][0] <= 1e-25
      )  # avg projector should set this to zero
      assert (cov["eig"][1:] > 1e-16).all()  # all else should be > 0
  
      # Now do some more comprehensive tests
      raw_sample = read_raw_fif(raw_fname)
      assert not _has_eeg_average_ref_proj(raw_sample.info)
  
      raw_sss = read_raw_fif(hp_fif_fname)
      assert not _has_eeg_average_ref_proj(raw_sss.info)
      raw_sss.add_proj(compute_proj_raw(raw_sss, meg=meg))
  
      cov_sample = compute_raw_covariance(raw_sample)
      cov_sample_proj = compute_raw_covariance(
          raw_sample.copy().apply_proj()
      )
  
      cov_sss = compute_raw_covariance(raw_sss)
      cov_sss_proj = compute_raw_covariance(raw_sss.copy().apply_proj())
  
      picks_all_sample = pick_types(raw_sample.info, meg=True, eeg=True)
      picks_all_sss = pick_types(raw_sss.info, meg=True, eeg=True)
  
      info_sample = pick_info(raw_sample.info, picks_all_sample)
      picks_stack_sample = [
          ("eeg", pick_types(info_sample, meg=False, eeg=True))
      ]
      picks_stack_sample += [("meg", pick_types(info_sample, meg=True))]
      picks_stack_sample += [
          ("all", pick_types(info_sample, meg=True, eeg=True))
      ]
  
      info_sss = pick_info(raw_sss.info, picks_all_sss)
      picks_stack_somato = [
          ("eeg", pick_types(info_sss, meg=False, eeg=True))
      ]
      picks_stack_somato += [("meg", pick_types(info_sss, meg=True))]
      picks_stack_somato += [
          ("all", pick_types(info_sss, meg=True, eeg=True))
      ]
  
      iter_tests = list(
          itertools.product(
              [
                  (cov_sample, picks_stack_sample, info_sample),
                  (cov_sample_proj, picks_stack_sample, info_sample),
                  (cov_sss, picks_stack_somato, info_sss),
                  (cov_sss_proj, picks_stack_somato, info_sss),
              ],  # sss
              [dict(mag=1e15, grad=1e13, eeg=1e6)],
          )
      )
  
      for (cov, picks_list, iter_info), scalings in iter_tests:
          rank = compute_rank(
              cov, rank_method, scalings, iter_info, proj=proj
          )
          rank["all"] = sum(rank.values())
          for ch_type, picks in picks_list:
              this_info = pick_info(iter_info, picks)
  
              # compute subset of projs, active and inactive
              n_projs_applied = sum(
                  proj["active"]
                  and len(
                      set(proj["data"]["col_names"])
                      & set(this_info["ch_names"])
                  )
                  > 0
                  for proj in cov["projs"]
              )
              n_projs_info = sum(
                  len(
                      set(proj["data"]["col_names"])
                      & set(this_info["ch_names"])
                  )
                  > 0
                  for proj in this_info["projs"]
              )
  
              # count channel types
              ch_types = this_info.get_channel_types()
              n_eeg, n_mag, n_grad = (
                  ch_types.count(k) for k in ["eeg", "mag", "grad"]
              )
              n_meg = n_mag + n_grad
              has_sss = n_meg > 0 and len(this_info["proc_history"]) > 0
              if has_sss:
                  n_meg = _get_rank_sss(this_info)
  
              expected_rank = n_meg + n_eeg
              if rank_method is None:
                  if meg == "combined" or not has_sss:
                      if proj:
                          expected_rank -= n_projs_info
                      else:
                          expected_rank -= n_projs_applied
              else:
                  # XXX for now it just uses the total count
                  assert rank_method == "info"
                  if proj:
                      expected_rank -= n_projs_info
  
              assert rank[ch_type] == expected_rank
  
file_path: mne-python/mne/tests/test_rank.py
imports: import itertools
  from pathlib import Path
  import numpy
  import pytest
  from numpy.testing import assert_array_equal
  from mne import compute_raw_covariance, make_fixed_length_epochs, pick_info, pick_types, read_cov, read_evokeds
  from mne._fiff.pick import _picks_by_type
  from mne._fiff.proj import _has_eeg_average_ref_proj
  from mne.cov import prepare_noise_cov
  from mne.datasets import testing
  from mne.io import read_raw_fif
  from mne.proj import compute_proj_raw
  from mne.rank import _compute_rank_int, _estimate_rank_raw, _get_rank_sss, compute_rank, estimate_rank
label_desc: Use image node_id 3 for calling a global function with example usage: test_cov_rank_estimation(rank_method, proj, meg) without return types