code: def test_simulate_raw_chpi():
      """Test simulation of raw data with cHPI."""
      raw = read_raw_fif(raw_chpi_fname, allow_maxshield="yes")
      drops = pick_types(raw.info, meg=True, eeg=True)[::4]  # for speed
      picks = np.setdiff1d(range(len(raw.ch_names)), drops)
      raw.pick(picks).load_data()
      raw.info.normalize_proj()
      sphere = make_sphere_model("auto", "auto", raw.info)
      # make sparse spherical source space
      sphere_vol = tuple(sphere["r0"]) + (sphere.radius,)
      src = setup_volume_source_space(
          sphere=sphere_vol, pos=70.0, sphere_units="m"
      )
      stcs = [_make_stc(raw, src)] * 15
      # simulate data with cHPI on
      raw_sim = simulate_raw(
          raw.info,
          stc=stcs,
          trans=None,
          src=src,
          bem=sphere,
          head_pos=pos_fname,
          interp="zero",
          first_samp=raw.first_samp,
      )
      # need to trim extra samples off this one
      raw_chpi = add_chpi(
          raw_sim.copy(), head_pos=pos_fname, interp="zero"
      )
      # test cHPI indication
      hpi_freqs, hpi_pick, hpi_ons = get_chpi_info(
          raw.info, on_missing="raise"
      )
      assert_allclose(raw_sim[hpi_pick][0], 0.0)
      assert_allclose(raw_chpi[hpi_pick][0], hpi_ons.sum())
      # test that the cHPI signals make some reasonable values
      picks_meg = pick_types(raw.info, meg=True)[:3]
      picks_eeg = pick_types(raw.info, eeg=True)[:3]
      for picks in (picks_meg, picks_eeg):
          psd_sim, freqs_sim = raw_sim.compute_psd(
              picks=picks
          ).get_data(return_freqs=True)
          psd_chpi, freqs_chpi = raw_chpi.compute_psd(
              picks=picks
          ).get_data(return_freqs=True)
          assert_array_equal(freqs_sim, freqs_chpi)
          # bins closest to cHPI freqs should have very high energy in MEG chans
          if picks is picks_meg:
              freq_idx = np.argmin(
                  np.abs(freqs_sim - hpi_freqs[:, np.newaxis]), axis=1
              )
              assert (
                  psd_chpi[:, freq_idx] > 100 * psd_sim[:, freq_idx]
              ).all()
          else:
              assert_allclose(psd_sim, psd_chpi, atol=1e-20)
  
      # test localization based on cHPI information
      chpi_amplitudes = compute_chpi_amplitudes(raw, t_step_min=10.0)
      coil_locs = compute_chpi_locs(raw.info, chpi_amplitudes)
      quats_sim = compute_head_pos(raw_chpi.info, coil_locs)
      quats = read_head_pos(pos_fname)
      _assert_quats(
          quats, quats_sim, dist_tol=5e-3, angle_tol=3.5, vel_atol=0.03
      )
  
imports: from copy import deepcopy
  from pathlib import Path
  import numpy
  import pytest
  from numpy.testing import assert_allclose, assert_array_equal
  from mne import Epochs, SourceEstimate, VolSourceEstimate, convert_forward_solution, create_info, find_events, fit_dipole, make_ad_hoc_cov, make_bem_solution, make_forward_solution, make_sphere_model, pick_types, read_bem_solution, read_cov, read_source_spaces, read_trans, setup_source_space, setup_volume_source_space, transform_surface_to
  from mne._fiff.constants import FIFF
  from mne.bem import _surfaces_to_bem
  from mne.chpi import compute_chpi_amplitudes, compute_chpi_locs, compute_head_pos, get_chpi_info, read_head_pos
  from mne.datasets import testing
  from mne.io import RawArray, read_raw_fif
  from mne.label import Label
  from mne.simulation import add_chpi, add_ecg, add_eog, add_noise, simulate_raw, simulate_sparse_stc
  from mne.simulation.source import SourceSimulator
  from mne.source_space._source_space import _compare_source_spaces
  from mne.surface import _get_ico_surface
  from mne.tests.test_chpi import _assert_quats
  from mne.utils import catch_logging
label_desc: Use image node_id 10 for calling a global function with example usage: test_simulate_raw_chpi() without return types