MCPcopy
hub / github.com/mne-tools/mne-python / test_simulate_raw_chpi

Function test_simulate_raw_chpi

mne/simulation/tests/test_raw.py:525–579  ·  view source on GitHub ↗

Test simulation of raw data with cHPI.

()

Source from the content-addressed store, hash-verified

523@pytest.mark.slowtest
524@testing.requires_testing_data
525def test_simulate_raw_chpi():
526 """Test simulation of raw data with cHPI."""
527 raw = read_raw_fif(raw_chpi_fname, allow_maxshield="yes")
528 drops = pick_types(raw.info, meg=True, eeg=True)[::4] # for speed
529 picks = np.setdiff1d(range(len(raw.ch_names)), drops)
530 raw.pick(picks).load_data()
531 raw.info.normalize_proj()
532 sphere = make_sphere_model("auto", "auto", raw.info)
533 # make sparse spherical source space
534 sphere_vol = tuple(sphere["r0"]) + (sphere.radius,)
535 src = setup_volume_source_space(sphere=sphere_vol, pos=70.0, sphere_units="m")
536 stcs = [_make_stc(raw, src)] * 15
537 # simulate data with cHPI on
538 raw_sim = simulate_raw(
539 raw.info,
540 stc=stcs,
541 trans=None,
542 src=src,
543 bem=sphere,
544 head_pos=pos_fname,
545 interp="zero",
546 first_samp=raw.first_samp,
547 )
548 # need to trim extra samples off this one
549 raw_chpi = add_chpi(raw_sim.copy(), head_pos=pos_fname, interp="zero")
550 # test cHPI indication
551 hpi_freqs, hpi_pick, hpi_ons = get_chpi_info(raw.info, on_missing="raise")
552 assert_allclose(raw_sim[hpi_pick][0], 0.0)
553 assert_allclose(raw_chpi[hpi_pick][0], hpi_ons.sum())
554 # test that the cHPI signals make some reasonable values
555 picks_meg = pick_types(raw.info, meg=True)[:3]
556 picks_eeg = pick_types(raw.info, eeg=True)[:3]
557 for picks in (picks_meg, picks_eeg):
558 psd_sim, freqs_sim = raw_sim.compute_psd(picks=picks).get_data(
559 return_freqs=True
560 )
561 psd_chpi, freqs_chpi = raw_chpi.compute_psd(picks=picks).get_data(
562 return_freqs=True
563 )
564 assert_array_equal(freqs_sim, freqs_chpi)
565 # bins closest to cHPI freqs should have very high energy in MEG chans
566 if picks is picks_meg:
567 freq_idx = np.argmin(np.abs(freqs_sim - hpi_freqs[:, np.newaxis]), axis=1)
568 assert (psd_chpi[:, freq_idx] > 100 * psd_sim[:, freq_idx]).all()
569 else:
570 assert_allclose(psd_sim, psd_chpi, atol=1e-20)
571
572 # test localization based on cHPI information
573 chpi_amplitudes = compute_chpi_amplitudes(raw, t_step_min=10.0)
574 coil_locs = compute_chpi_locs(raw.info, chpi_amplitudes)
575 quats_sim = compute_head_pos(raw_chpi.info, coil_locs)
576 quats = read_head_pos(pos_fname)
577 _assert_quats(
578 quats, quats_sim, dist_tol=5e-3, angle_tol=3.5, vel_atol=0.03
579 ) # velicity huge because of t_step_min above
580
581
582@testing.requires_testing_data

Callers

nothing calls this directly

Calls 15

read_raw_fifFunction · 0.90
pick_typesFunction · 0.90
make_sphere_modelFunction · 0.90
simulate_rawFunction · 0.90
add_chpiFunction · 0.90
get_chpi_infoFunction · 0.90
compute_chpi_amplitudesFunction · 0.90
compute_chpi_locsFunction · 0.90
compute_head_posFunction · 0.90
read_head_posFunction · 0.90
_assert_quatsFunction · 0.90

Tested by

no test coverage detected