3838 compute_head_pos ,
3939 get_chpi_info ,
4040 read_head_pos ,
41+ write_head_pos ,
4142)
4243from mne .datasets import testing
4344from mne .io import RawArray , read_raw_fif
5455from mne .source_space ._source_space import _compare_source_spaces
5556from mne .surface import _get_ico_surface
5657from mne .tests .test_chpi import _assert_quats
58+ from mne .transforms import _affine_to_quat
5759from mne .utils import catch_logging
5860
5961raw_fname_short = Path (__file__ ).parents [2 ] / "io" / "tests" / "data" / "test_raw.fif"
@@ -251,6 +253,11 @@ def test_simulate_raw_sphere(raw_data, tmp_path):
251253
252254 # head pos
253255 head_pos_sim = _get_head_pos_sim (raw )
256+ head_pos_sim_2 = np .zeros ((len (head_pos_sim ), 10 ))
257+ for ii , (t , mat ) in enumerate (head_pos_sim .items ()):
258+ head_pos_sim_2 [ii , :7 ] = [t ] + list (_affine_to_quat (mat ))
259+ head_pos_sim_3 = tmp_path / "head_pos.txt"
260+ write_head_pos (head_pos_sim_3 , head_pos_sim_2 )
254261
255262 #
256263 # Test raw simulation with basic parameters
@@ -259,11 +266,9 @@ def test_simulate_raw_sphere(raw_data, tmp_path):
259266 cov = read_cov (cov_fname )
260267 cov ["projs" ] = raw .info ["projs" ]
261268 raw .info ["bads" ] = raw .ch_names [:1 ]
262- sphere_norad = make_sphere_model ("auto" , None , raw .info )
263269 raw_meg = raw .copy ().pick ("meg" )
264- raw_sim = simulate_raw (
265- raw_meg .info , stc , trans , src , sphere_norad , head_pos = head_pos_sim
266- )
270+ raw_sim = simulate_raw (raw_meg .info , stc , trans , src , sphere , head_pos = head_pos_sim )
271+ raw_data = raw_sim [:][0 ]
267272 # Test IO on processed data
268273 test_outname = tmp_path / "sim_test_raw.fif"
269274 raw_sim .save (test_outname )
@@ -307,12 +312,14 @@ def test_simulate_raw_sphere(raw_data, tmp_path):
307312 )
308313 del raw_sim , raw_sim_2
309314
310- # check that different interpolations are similar given small movements
315+ # check that different interpolations are similar given small movements,
316+ # using different input forms of head_pos
311317 raw_sim = simulate_raw (
312318 raw .info , stc , trans , src , sphere , head_pos = head_pos_sim , interp = "linear"
313319 )
320+ assert_allclose (raw_sim .get_data ("meg" ), raw_data , rtol = 0.02 )
314321 raw_sim_hann = simulate_raw (
315- raw .info , stc , trans , src , sphere , head_pos = head_pos_sim , interp = "hann"
322+ raw .info , stc , trans , src , sphere , head_pos = head_pos_sim_3 , interp = "hann"
316323 )
317324 assert_allclose (raw_sim [:][0 ], raw_sim_hann [:][0 ], rtol = 1e-1 , atol = 1e-14 )
318325 del raw_sim_hann
@@ -391,7 +398,7 @@ def test_simulate_raw_bem(raw_data):
391398 cov = make_ad_hoc_cov (raw .info )
392399 # The tolerance for the BEM is surprisingly high but I get the same
393400 # result when using MNE-C and Xfit, even when using a proper 5120 BEM :(
394- for use_raw , bem , tol in ((raw_sim_sph , sphere , 4 ), (raw_sim_bem , bem_fname , 31 )):
401+ for use_raw , bem , tol in ((raw_sim_sph , sphere , 6 ), (raw_sim_bem , bem_fname , 31 )):
395402 events = find_events (use_raw , "STI 014" )
396403 assert len (locs ) == 6
397404 evoked = Epochs (use_raw , events , 1 , 0 , tmax , baseline = None ).average ()
@@ -425,6 +432,18 @@ def test_simulate_raw_bem(raw_data):
425432 assert_allclose (amp0 / amp1 , wf_sim [0 ] / wf_sim [1 ], rtol = 1e-5 )
426433 assert amp2 == 0
427434 assert raw_sim .n_times == ss .n_times
435+ # smoke test that different head positions can be used as well
436+ head_pos_sim = {1.0 : raw .info ["dev_head_t" ]["trans" ]}
437+ raw_sim_2 = simulate_raw (
438+ raw .info ,
439+ ss ,
440+ src = src_ss ,
441+ bem = bem_fname ,
442+ first_samp = first_samp ,
443+ head_pos = head_pos_sim ,
444+ )
445+ data_2 = raw_sim_2 .get_data ()
446+ assert_allclose (data , data_2 , rtol = 1e-7 )
428447
429448
430449@pytest .mark .slowtest # slow on Windows Azure
0 commit comments