code: def test_shielding_factor(tmp_path):
      """Test Maxwell filter shielding factor using empty room."""
      raw_erm = read_crop(erm_fname).load_data().pick("meg")
      erm_power = raw_erm[pick_types(raw_erm.info, meg="mag")][0]
      erm_power = np.sqrt(np.sum(erm_power * erm_power))
      erm_power_grad = raw_erm[pick_types(raw_erm.info, meg="grad")][0]
      erm_power_grad = np.sqrt(np.sum(erm_power * erm_power))
  
      # Vanilla SSS (second value would be for meg=True instead of meg='mag')
      _assert_shielding(
          read_crop(sss_erm_std_fname), erm_power, 10
      )  # 1.5)
      raw_sss = maxwell_filter(
          raw_erm, coord_frame="meg", regularize=None
      )
      _assert_shielding(raw_sss, erm_power, 12, 13)  # 1.5)
      _assert_shielding(
          raw_sss, erm_power_grad, 0.45, 0.55, "grad"
      )  # 1.5)
  
      # No external basis
      raw_sss_0 = maxwell_filter(
          raw_erm, coord_frame="meg", regularize=None, ext_order=0
      )
      _assert_shielding(raw_sss_0, erm_power, 1.0, 1.1)
      del raw_sss_0
  
      # Regularization
      _assert_shielding(
          read_crop(sss_erm_std_fname), erm_power, 10
      )  # 1.5)
      raw_sss = maxwell_filter(raw_erm, coord_frame="meg")
      _assert_shielding(raw_sss, erm_power, 14.5, 15.5)
  
      #
      # Extended (eSSS)
      #
  
      # Show that using empty-room projectors increase shielding factor
      proj = mne.compute_proj_raw(
          raw_erm, meg="combined", verbose="error", n_mag=15, n_grad=15
      )
      raw_sss = maxwell_filter(
          raw_erm,
          coord_frame="meg",
          regularize=None,
          extended_proj=proj[:3],
      )
      _assert_shielding(raw_sss, erm_power, 38, 39)
      raw_sss = maxwell_filter(
          raw_erm,
          coord_frame="meg",
          regularize=None,
          extended_proj=proj,
      )
      _assert_shielding(raw_sss, erm_power, 49, 51)
      # Now with reg
      raw_sss = maxwell_filter(
          raw_erm, coord_frame="meg", extended_proj=proj[:3]
      )
      _assert_shielding(raw_sss, erm_power, 42, 44)
      raw_sss = maxwell_filter(
          raw_erm, coord_frame="meg", extended_proj=proj
      )
      _assert_shielding(raw_sss, erm_power, 59, 61)
  
      #
      # Different mag_scale values
      #
      raw_sss = maxwell_filter(
          raw_erm, coord_frame="meg", regularize=None, mag_scale="auto"
      )
      _assert_shielding(raw_sss, erm_power, 12, 13)
      _assert_shielding(raw_sss, erm_power_grad, 0.48, 0.58, "grad")
      raw_sss = maxwell_filter(
          raw_erm, coord_frame="meg", regularize=None, mag_scale=1.0
      )  # not a good choice
      _assert_shielding(raw_sss, erm_power, 7.3, 8.0)
      _assert_shielding(raw_sss, erm_power_grad, 0.2, 0.3, "grad")
      raw_sss = maxwell_filter(
          raw_erm,
          coord_frame="meg",
          regularize=None,
          mag_scale=1000.0,
          bad_condition="ignore",
      )
      _assert_shielding(raw_sss, erm_power, 4.0, 5.0)
      _assert_shielding(raw_sss, erm_power_grad, 0.1, 0.2, "grad")
  
      #
      # Fine cal
      #
      _assert_shielding(
          read_crop(sss_erm_fine_cal_fname), erm_power, 12
      )  # 2.0)
      raw_sss = maxwell_filter(
          raw_erm,
          coord_frame="meg",
          regularize=None,
          origin=mf_meg_origin,
          calibration=pathlib.Path(fine_cal_fname),
      )
      _assert_shielding(raw_sss, erm_power, 12, 13)  # 2.0)
  
      #
      # Crosstalk
      #
      _assert_shielding(
          read_crop(sss_erm_ctc_fname), erm_power, 12
      )  # 2.1)
      raw_sss = maxwell_filter(
          raw_erm,
          coord_frame="meg",
          regularize=None,
          origin=mf_meg_origin,
          cross_talk=ctc_fname,
      )
      _assert_shielding(raw_sss, erm_power, 12, 13)  # 2.1)
  
      # Fine cal + Crosstalk
      raw_sss = maxwell_filter(
          raw_erm,
          coord_frame="meg",
          regularize=None,
          calibration=fine_cal_fname,
          origin=mf_meg_origin,
          cross_talk=ctc_fname,
      )
      _assert_shielding(raw_sss, erm_power, 13, 14)  # 2.2)
      # Fine cal + Crosstalk + Extended
      raw_sss = maxwell_filter(
          raw_erm,
          coord_frame="meg",
          regularize=None,
          calibration=fine_cal_fname,
          origin=mf_meg_origin,
          cross_talk=ctc_fname,
          extended_proj=proj,
      )
      _assert_shielding(raw_sss, erm_power, 28, 30)
      raw_sss = maxwell_filter(
          raw_erm,
          coord_frame="meg",
          regularize=None,
          calibration=fine_cal_fname,
          origin=mf_meg_origin,
          cross_talk=ctc_fname,
          extended_proj=proj[:3],
      )
      _assert_shielding(raw_sss, erm_power, 25, 27)
  
      # tSSS
      _assert_shielding(
          read_crop(sss_erm_st_fname), erm_power, 37
      )  # 5.8)
      raw_sss = maxwell_filter(
          raw_erm,
          coord_frame="meg",
          regularize=None,
          origin=mf_meg_origin,
          st_duration=1.0,
      )
      _assert_shielding(raw_sss, erm_power, 37, 38)  # 5.8)
  
      # Crosstalk + tSSS
      with get_n_projected() as counts:
          raw_sss = maxwell_filter(
              raw_erm,
              coord_frame="meg",
              regularize=None,
              cross_talk=ctc_fname,
              origin=mf_meg_origin,
              st_duration=1.0,
          )
      _assert_shielding(raw_sss, erm_power, 38, 39)  # 5.91)
      assert counts[0] == 4
  
      # Fine cal + tSSS
      with get_n_projected() as counts:
          raw_sss = maxwell_filter(
              raw_erm,
              coord_frame="meg",
              regularize=None,
              calibration=fine_cal_fname,
              origin=mf_meg_origin,
              st_duration=1.0,
          )
      _assert_shielding(raw_sss, erm_power, 38, 39)  # 5.98)
      assert counts[0] == 4
  
      # Extended + tSSS
      with get_n_projected() as counts:
          raw_sss = maxwell_filter(
              raw_erm,
              coord_frame="meg",
              regularize=None,
              origin=mf_meg_origin,
              st_duration=1.0,
              extended_proj=proj,
          )
      _assert_shielding(raw_sss, erm_power, 40, 42)
      assert counts[0] == 0
      with get_n_projected() as counts:
          raw_sss = maxwell_filter(
              raw_erm,
              coord_frame="meg",
              regularize=None,
              origin=mf_meg_origin,
              st_duration=1.0,
              extended_proj=proj[:3],
          )
      _assert_shielding(raw_sss, erm_power, 35, 37)
      assert counts[0] == 1
  
      # Fine cal + Crosstalk + tSSS
      _assert_shielding(
          read_crop(sss_erm_st1FineCalCrossTalk_fname),
          erm_power,
          39,
          40,
      )  # 6.07)
      raw_sss = maxwell_filter(
          raw_erm,
          coord_frame="meg",
          regularize=None,
          calibration=fine_cal_fname,
          origin=mf_meg_origin,
          cross_talk=ctc_fname,
          st_duration=1.0,
      )
      _assert_shielding(raw_sss, erm_power, 39, 40)  # 6.05)
  
      # Fine cal + Crosstalk + tSSS + Extended (a bit worse)
      _assert_shielding(
          read_crop(sss_erm_st1FineCalCrossTalk_fname),
          erm_power,
          39,
          40,
      )  # 6.07)
      raw_sss = maxwell_filter(
          raw_erm,
          coord_frame="meg",
          regularize=None,
          calibration=fine_cal_fname,
          origin=mf_meg_origin,
          cross_talk=ctc_fname,
          st_duration=1.0,
          extended_proj=proj[:3],
      )
      _assert_shielding(raw_sss, erm_power, 34, 36)
  
      # Fine cal + Crosstalk + tSSS + Reg-in
      _assert_shielding(
          read_crop(sss_erm_st1FineCalCrossTalkRegIn_fname),
          erm_power,
          57,
          58,
      )  # 6.97)
      raw_sss = maxwell_filter(
          raw_erm,
          calibration=fine_cal_fname,
          cross_talk=ctc_fname,
          st_duration=1.0,
          origin=mf_meg_origin,
          coord_frame="meg",
          regularize="in",
      )
      _assert_shielding(raw_sss, erm_power, 53, 54)  # 6.64)
      with get_n_projected() as counts:
          raw_sss = maxwell_filter(
              raw_erm,
              calibration=fine_cal_fname,
              cross_talk=ctc_fname,
              st_duration=1.0,
              coord_frame="meg",
              regularize="in",
          )
      _assert_shielding(raw_sss, erm_power, 58, 59)  # 7.0)
      _assert_shielding(raw_sss, erm_power_grad, 1.6, 1.7, "grad")
      assert counts[0] == 4
      with get_n_projected() as counts:
          raw_sss = maxwell_filter(
              raw_erm,
              calibration=fine_cal_fname,
              cross_talk=ctc_fname,
              st_duration=1.0,
              coord_frame="meg",
              regularize="in",
              mag_scale="auto",
          )
      _assert_shielding(raw_sss, erm_power, 51, 52)
      _assert_shielding(raw_sss, erm_power_grad, 1.5, 1.6, "grad")
      assert counts[0] == 3
      with get_n_projected() as counts:
          with _record_warnings():  # SVD convergence on arm64
              raw_sss = maxwell_filter(
                  raw_erm,
                  calibration=fine_cal_fname_3d,
                  cross_talk=ctc_fname,
                  st_duration=1.0,
                  coord_frame="meg",
                  regularize="in",
              )
      # Our 3D cal has worse defaults for this ERM than the 1D file
      _assert_shielding(raw_sss, erm_power, 57, 58)
      assert counts[0] == 3
      # Show it by rewriting the 3D as 1D and testing it
      temp_fname = tmp_path / "test_cal.dat"
      with open(fine_cal_fname_3d) as fid:
          with open(temp_fname, "w") as fid_out:
              for line in fid:
                  fid_out.write(
                      " ".join(line.strip().split(" ")[:14]) + "\n"
                  )
      with get_n_projected() as counts:
          with _record_warnings():  # SVD convergence sometimes
              raw_sss = maxwell_filter(
                  raw_erm,
                  calibration=temp_fname,
                  cross_talk=ctc_fname,
                  st_duration=1.0,
                  coord_frame="meg",
                  regularize="in",
              )
      # Our 3D cal has worse defaults for this ERM than the 1D file
      _assert_shielding(raw_sss, erm_power, 44, 45)
      assert counts[0] == 3
  
      # Fine cal + Crosstalk + tSSS + Reg-in + Extended
      with get_n_projected() as counts:
          raw_sss = maxwell_filter(
              raw_erm,
              calibration=fine_cal_fname,
              cross_talk=ctc_fname,
              st_duration=1.0,
              coord_frame="meg",
              regularize="in",
              extended_proj=proj[:3],
          )
      _assert_shielding(raw_sss, erm_power, 48, 50)
      assert counts[0] == 1
  
imports: import pathlib
  import re
  from contextlib import contextmanager
  from pathlib import Path
  import numpy
  import pytest
  from numpy.testing import assert_allclose, assert_array_equal
  from scipy import sparse
  from scipy.special import sph_harm
  import mne
  from mne import compute_raw_covariance, concatenate_raws, pick_info, pick_types
  from mne._fiff.constants import FIFF
  from mne.annotations import _annotations_starts_stops
  from mne.chpi import filter_chpi, read_head_pos
  from mne.datasets import testing
  from mne.forward import _prep_meg_channels, use_coil_def
  from mne.io import BaseRaw, read_info, read_raw_bti, read_raw_ctf, read_raw_fif, read_raw_kit
  from mne.preprocessing import annotate_amplitude, annotate_movement, compute_maxwell_basis, find_bad_channels_maxwell, maxwell_filter, maxwell_filter_prepare_emptyroom
  from mne.preprocessing.maxwell import _bases_complex_to_real, _bases_real_to_complex, _get_n_moments, _prep_mf_coils, _sh_complex_to_real, _sh_negate, _sh_real_to_complex, _sss_basis_basic, _trans_sss_basis
  from mne.rank import _compute_rank_int, _get_rank_sss, compute_rank
  from mne.utils import _record_warnings, assert_meg_snr, buggy_mkl_svd, catch_logging, object_diff, use_log_level
label_desc: Use image node_id 21 for calling a global function with example usage: test_shielding_factor(tmp_path) without return types