code: def test_movement_compensation(tmp_path):
      """Test movement compensation."""
      lims = (0, 4)
      raw = read_crop(raw_fname, lims).load_data()
      head_pos = read_head_pos(pos_fname)
  
      #
      # Movement compensation, no regularization, no tSSS
      #
      _assert_mag_coil_type(raw.info, FIFF.FIFFV_COIL_VV_MAG_T3)
      assert_allclose(raw.info["chs"][2]["cal"], 4.14e-11, rtol=1e-6)
      raw.info["chs"][2]["coil_type"] = FIFF.FIFFV_COIL_VV_MAG_T2
      raw_sss = maxwell_filter(
          raw,
          head_pos=head_pos,
          origin=mf_head_origin,
          regularize=None,
          bad_condition="ignore",
      )
      _assert_mag_coil_type(raw_sss.info, FIFF.FIFFV_COIL_VV_MAG_T3)
      assert_meg_snr(
          raw_sss,
          read_crop(sss_movecomp_fname, lims),
          4.6,
          12.4,
          chpi_med_tol=58,
      )
      # IO
      temp_fname = tmp_path / "test_raw_sss.fif"
      raw_sss.save(temp_fname)
      raw_sss = read_crop(temp_fname)
      assert_meg_snr(
          raw_sss,
          read_crop(sss_movecomp_fname, lims),
          4.6,
          12.4,
          chpi_med_tol=58,
      )
  
      #
      # Movement compensation,    regularization, no tSSS
      #
      raw_sss = maxwell_filter(
          raw, head_pos=head_pos, origin=mf_head_origin
      )
      assert_meg_snr(
          raw_sss,
          read_crop(sss_movecomp_reg_in_fname, lims),
          0.5,
          1.9,
          chpi_med_tol=121,
      )
  
      #
      # Movement compensation,    regularization,    tSSS at the end
      #
      raw_nohpi = filter_chpi(raw.copy(), t_window=0.2)
      with pytest.warns(RuntimeWarning, match="untested"):
          raw_sss_mv = maxwell_filter(
              raw_nohpi,
              head_pos=head_pos,
              st_duration=4.0,
              origin=mf_head_origin,
              st_fixed=False,
          )
      # Neither match is particularly good because our algorithm actually differs
      assert_meg_snr(
          raw_sss_mv,
          read_crop(sss_movecomp_reg_in_st4s_fname, lims),
          0.6,
          1.3,
      )
      tSSS_fname = sss_path / "test_move_anon_st4s_raw_sss.fif"
      assert_meg_snr(
          raw_sss_mv,
          read_crop(tSSS_fname, lims),
          0.6,
          1.0,
          chpi_med_tol=None,
      )
      assert_meg_snr(
          read_crop(sss_movecomp_reg_in_st4s_fname),
          read_crop(tSSS_fname),
          0.8,
          1.0,
          chpi_med_tol=None,
      )
  
      #
      # Movement compensation,    regularization,    tSSS at the beginning
      #
      raw_sss_mc = maxwell_filter(
          raw_nohpi,
          head_pos=head_pos,
          st_duration=4.0,
          origin=mf_head_origin,
      )
      assert_meg_snr(
          raw_sss_mc,
          read_crop(tSSS_fname, lims),
          0.6,
          1.0,
          chpi_med_tol=None,
      )
      assert_meg_snr(raw_sss_mc, raw_sss_mv, 0.6, 1.4)
  
      # some degenerate cases
      raw_erm = read_crop(erm_fname)
      with pytest.raises(
          ValueError, match="positions can only be used"
      ):
          maxwell_filter(raw_erm, coord_frame="meg", head_pos=head_pos)
      with pytest.raises(ValueError, match=r"of shape \(N, 10\)"):
          maxwell_filter(raw, head_pos=head_pos[:, :9])
      with pytest.raises(TypeError, match="instance of ndarray"):
          maxwell_filter(raw, head_pos="foo")
      with pytest.raises(ValueError, match="ascending"):
          maxwell_filter(raw, head_pos=head_pos[::-1])
      head_pos_bad = head_pos.copy()
      head_pos_bad[0, 0] = raw._first_time - 1e-2
      with pytest.raises(ValueError, match="greater than"):
          maxwell_filter(raw, head_pos=head_pos_bad)
  
      head_pos_bad = head_pos.copy()
      head_pos_bad[0, 4] = 1.0  # off by more than 1 m
      with pytest.warns(RuntimeWarning, match="greater than 1 m"):
          maxwell_filter(
              raw.copy().crop(0, 0.1),
              head_pos=head_pos_bad,
              bad_condition="ignore",
          )
  
      # make sure numerical error doesn't screw it up, though
      head_pos_bad = head_pos.copy()
      head_pos_bad[0, 0] = raw._first_time - 5e-4
      raw_sss_tweak = maxwell_filter(
          raw.copy().crop(0, 0.05),
          head_pos=head_pos_bad,
          origin=mf_head_origin,
      )
      assert_meg_snr(
          raw_sss_tweak,
          raw_sss.copy().crop(0, 0.05),
          1.4,
          8.0,
          chpi_med_tol=5,
      )
  
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 4 for calling a global function with example usage: test_movement_compensation(tmp_path) without return types