code: def test_other_systems():
      """Test Maxwell filtering on KIT, BTI, and CTF files."""
      # KIT
      kit_dir = io_dir / "kit" / "tests" / "data"
      sqd_path = kit_dir / "test.sqd"
      mrk_path = kit_dir / "test_mrk.sqd"
      elp_path = kit_dir / "test_elp.txt"
      hsp_path = kit_dir / "test_hsp.txt"
      raw_kit = read_raw_kit(
          sqd_path, str(mrk_path), str(elp_path), str(hsp_path)
      )
      with pytest.warns(RuntimeWarning, match="fit"):
          pytest.raises(RuntimeError, maxwell_filter, raw_kit)
      with catch_logging() as log:
          raw_sss = maxwell_filter(
              raw_kit,
              origin=(0.0, 0.0, 0.04),
              ignore_ref=True,
              verbose=True,
          )
      assert "12/15 out" in log.getvalue()  # homogeneous fields removed
      _assert_n_free(raw_sss, 65, 65)
      raw_sss_auto = maxwell_filter(
          raw_kit,
          origin=(0.0, 0.0, 0.04),
          ignore_ref=True,
          mag_scale="auto",
      )
      assert_allclose(raw_sss._data, raw_sss_auto._data)
      # The KIT origin fit is terrible
      with pytest.warns(RuntimeWarning, match="more than 20 mm"):
          with catch_logging() as log:
              pytest.raises(
                  RuntimeError,
                  maxwell_filter,
                  raw_kit,
                  ignore_ref=True,
                  regularize=None,
              )  # bad condition
              raw_sss = maxwell_filter(
                  raw_kit,
                  origin="auto",
                  ignore_ref=True,
                  bad_condition="info",
                  verbose=True,
              )
      log = log.getvalue()
      assert "badly conditioned" in log
      assert "more than 20 mm from" in log
      # fits can differ slightly based on scipy version, so be lenient here
      _assert_n_free(raw_sss, 28, 34)  # bad origin == brutal reg
      # Let's set the origin
      with catch_logging() as log:
          raw_sss = maxwell_filter(
              raw_kit,
              origin=(0.0, 0.0, 0.04),
              ignore_ref=True,
              bad_condition="info",
              regularize=None,
              verbose=True,
          )
      log = log.getvalue()
      assert "badly conditioned" in log
      assert "80/80 in, 12/15 out" in log
      _assert_n_free(raw_sss, 80)
      # Now with reg
      with catch_logging() as log:
          raw_sss = maxwell_filter(
              raw_kit,
              origin=(0.0, 0.0, 0.04),
              ignore_ref=True,
              verbose=True,
          )
      log = log.getvalue()
      assert "badly conditioned" not in log
      assert "12/15 out" in log
      _assert_n_free(raw_sss, 65)
  
      # BTi
      bti_dir = io_dir / "bti" / "tests" / "data"
      bti_pdf = bti_dir / "test_pdf_linux"
      bti_config = bti_dir / "test_config_linux"
      bti_hs = bti_dir / "test_hs_linux"
      raw_bti = read_raw_bti(bti_pdf, bti_config, bti_hs, preload=False)
      picks = pick_types(raw_bti.info, meg="mag", exclude=())
      power = np.sqrt(np.sum(raw_bti[picks][0] ** 2))
      raw_sss = maxwell_filter(raw_bti)
      _assert_n_free(raw_sss, 70)
      _assert_shielding(raw_sss, power, 0.5)
      raw_sss_auto = maxwell_filter(
          raw_bti, mag_scale="auto", verbose=True
      )
      _assert_shielding(raw_sss_auto, power, 0.7)
  
      # CTF
      raw_ctf_3 = read_crop(fname_ctf_raw)
      assert raw_ctf_3.compensation_grade == 3
      raw_ctf_0 = raw_ctf_3.copy().apply_gradient_compensation(0)
      assert raw_ctf_0.compensation_grade == 0
      # 3rd-order gradient compensation works really well (better than MF here)
      _assert_shielding(raw_ctf_3, raw_ctf_0, 20, 21)
      origin = (0.0, 0.0, 0.04)
      raw_sss_3 = maxwell_filter(raw_ctf_3, origin=origin, verbose=True)
      _assert_n_free(raw_sss_3, 70)
      _assert_shielding(raw_sss_3, raw_ctf_3, 0.12, 0.14)
      _assert_shielding(raw_sss_3, raw_ctf_0, 2.63, 2.66)
      assert raw_sss_3.compensation_grade == 3
      raw_sss_3.apply_gradient_compensation(0)
      assert raw_sss_3.compensation_grade == 0
      _assert_shielding(raw_sss_3, raw_ctf_3, 0.15, 0.17)
      _assert_shielding(raw_sss_3, raw_ctf_0, 3.18, 3.20)
      with pytest.raises(ValueError, match="digitization points"):
          maxwell_filter(raw_ctf_0)
      raw_sss_0 = maxwell_filter(raw_ctf_0, origin=origin, verbose=True)
      _assert_n_free(raw_sss_0, 68)
      _assert_shielding(raw_sss_0, raw_ctf_3, 0.07, 0.09)
      _assert_shielding(raw_sss_0, raw_ctf_0, 1.8, 1.9)
      raw_sss_0.apply_gradient_compensation(3)
      _assert_shielding(raw_sss_0, raw_ctf_3, 0.07, 0.09)
      _assert_shielding(raw_sss_0, raw_ctf_0, 1.63, 1.67)
      with pytest.raises(RuntimeError, match="ignore_ref"):
          maxwell_filter(raw_ctf_3, ignore_ref=True)
      # ignoring ref outperforms including it in maxwell filtering
      with catch_logging() as log:
          raw_sss = maxwell_filter(
              raw_ctf_0, origin=origin, ignore_ref=True, verbose=True
          )
      assert (
          ", 12/15 out" in log.getvalue()
      )  # homogeneous fields removed
      _assert_n_free(raw_sss, 70)
      _assert_shielding(raw_sss, raw_ctf_0, 12, 13)
      # if ignore_ref=True, we remove compensators because they will not
      # work the way people expect (it puts noise back in the data!)
      with pytest.raises(
          ValueError, match="Desired compensation.*not found"
      ):
          raw_sss.copy().apply_gradient_compensation(3)
      raw_sss_auto = maxwell_filter(
          raw_ctf_0, origin=origin, ignore_ref=True, mag_scale="auto"
      )
      assert_allclose(raw_sss._data, raw_sss_auto._data)
      with catch_logging() as log:
          maxwell_filter(
              raw_ctf_0,
              origin=origin,
              regularize=None,
              ignore_ref=True,
              verbose=True,
          )
      assert "80/80 in, 12/15 out" in log.getvalue()
  
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 5 for calling a global function with example usage: test_other_systems() without return types