38
38
compute_head_pos ,
39
39
get_chpi_info ,
40
40
read_head_pos ,
41
+ write_head_pos ,
41
42
)
42
43
from mne .datasets import testing
43
44
from mne .io import RawArray , read_raw_fif
54
55
from mne .source_space ._source_space import _compare_source_spaces
55
56
from mne .surface import _get_ico_surface
56
57
from mne .tests .test_chpi import _assert_quats
58
+ from mne .transforms import _affine_to_quat
57
59
from mne .utils import catch_logging
58
60
59
61
raw_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):
251
253
252
254
# head pos
253
255
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 )
254
261
255
262
#
256
263
# Test raw simulation with basic parameters
@@ -259,11 +266,9 @@ def test_simulate_raw_sphere(raw_data, tmp_path):
259
266
cov = read_cov (cov_fname )
260
267
cov ["projs" ] = raw .info ["projs" ]
261
268
raw .info ["bads" ] = raw .ch_names [:1 ]
262
- sphere_norad = make_sphere_model ("auto" , None , raw .info )
263
269
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 ]
267
272
# Test IO on processed data
268
273
test_outname = tmp_path / "sim_test_raw.fif"
269
274
raw_sim .save (test_outname )
@@ -307,12 +312,14 @@ def test_simulate_raw_sphere(raw_data, tmp_path):
307
312
)
308
313
del raw_sim , raw_sim_2
309
314
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
311
317
raw_sim = simulate_raw (
312
318
raw .info , stc , trans , src , sphere , head_pos = head_pos_sim , interp = "linear"
313
319
)
320
+ assert_allclose (raw_sim .get_data ("meg" ), raw_data , rtol = 0.02 )
314
321
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"
316
323
)
317
324
assert_allclose (raw_sim [:][0 ], raw_sim_hann [:][0 ], rtol = 1e-1 , atol = 1e-14 )
318
325
del raw_sim_hann
@@ -391,7 +398,7 @@ def test_simulate_raw_bem(raw_data):
391
398
cov = make_ad_hoc_cov (raw .info )
392
399
# The tolerance for the BEM is surprisingly high but I get the same
393
400
# 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 )):
395
402
events = find_events (use_raw , "STI 014" )
396
403
assert len (locs ) == 6
397
404
evoked = Epochs (use_raw , events , 1 , 0 , tmax , baseline = None ).average ()
@@ -425,6 +432,18 @@ def test_simulate_raw_bem(raw_data):
425
432
assert_allclose (amp0 / amp1 , wf_sim [0 ] / wf_sim [1 ], rtol = 1e-5 )
426
433
assert amp2 == 0
427
434
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 )
428
447
429
448
430
449
@pytest .mark .slowtest # slow on Windows Azure
0 commit comments