#NEWLINE# @ testing . requires_testing_data #NEWLINE# def test_simulate_raw_sphere( ) : #NEWLINE# #INDENT# 'Test simulation of raw data with sphere model' #NEWLINE# seed = 42 #NEWLINE# ( raw , src , stc , trans , sphere ) = _get_data ( ) #NEWLINE# assert_true ( ( len ( pick_types ( raw . info , meg = False , ecg = True ) ) == 1 ) ) #NEWLINE# head_pos_sim = dict ( ) #NEWLINE# shifts = [ [ 0.001 , 0.0 , ( - 0.001 ) ] , [ ( - 0.001 ) , 0.001 , 0.0 ] ] #NEWLINE# for ( time_key , shift ) in enumerate ( shifts ) : #NEWLINE# #INDENT# temp_trans = deepcopy ( raw . info [ 'dev_head_t' ] ) #NEWLINE# temp_trans [ 'trans' ] [ : 3 , 3 ] += shift #NEWLINE# head_pos_sim [ ( time_key + 1.0 ) ] = temp_trans [ 'trans' ] #NEWLINE# #UNINDENT# raw_sim = simulate_raw ( raw , stc , trans , src , sphere , read_cov ( cov_fname ) , head_pos = head_pos_sim , blink = True , ecg = True , random_state = seed ) #NEWLINE# raw_sim_2 = simulate_raw ( raw , stc , trans_fname , src_fname , sphere , cov_fname , head_pos = head_pos_sim , blink = True , ecg = True , random_state = seed ) #NEWLINE# assert_array_equal ( time_key [ : ] [ 0 ] , raw_sim [ : ] [ 0 ] ) #NEWLINE# tempdir = _TempDir ( ) #NEWLINE# test_outname = op . join ( tempdir , 'sim_test_raw.fif' ) #NEWLINE# raw_sim . save ( test_outname ) #NEWLINE# raw_sim_loaded = Raw ( test_outname , preload = True , proj = False ) #NEWLINE# assert_allclose ( raw_sim_loaded [ : ] [ 0 ] , raw_sim [ : ] [ 0 ] , rtol = 1e-06 , atol = 1e-20 ) #NEWLINE# del raw_sim , raw_sim_2 #NEWLINE# for ( ecg , eog ) in ( ( True , False ) , ( False , True ) , ( True , True ) ) : #NEWLINE# #INDENT# raw_sim_3 = simulate_raw ( raw , stc , trans , src , sphere , cov = None , head_pos = head_pos_sim , blink = eog , ecg = ecg , random_state = seed ) #NEWLINE# raw_sim_4 = simulate_raw ( raw , stc , trans , src , sphere , cov = None , head_pos = head_pos_sim , blink = False , ecg = False , random_state = seed ) #NEWLINE# picks = np . arange ( len ( raw . ch_names ) ) #NEWLINE# diff_picks = pick_types ( raw . info , meg = False , ecg = ecg , eog = eog ) #NEWLINE# these_picks = np . setdiff1d ( picks , diff_picks ) #NEWLINE# close = isclose ( raw_sim_3 [ these_picks ] [ 0 ] , raw_sim_4 [ these_picks ] [ 0 ] , atol = 1e-20 ) #NEWLINE# assert_true ( ( np . mean ( close ) > 0.7 ) ) #NEWLINE# far = ( ~ isclose ( raw_sim_3 [ diff_picks ] [ 0 ] , raw_sim_4 [ diff_picks ] [ 0 ] , atol = 1e-20 ) ) #NEWLINE# assert_true ( ( np . mean ( far ) > 0.99 ) ) #NEWLINE# #UNINDENT# del raw_sim_3 , raw_sim_4 #NEWLINE# raw_sim_meg = simulate_raw ( raw . copy ( ) . pick_types ( meg = True , eeg = False ) , stc , trans , src , sphere , cov = None , ecg = True , blink = True , random_state = seed ) #NEWLINE# raw_sim_eeg = simulate_raw ( raw . copy ( ) . pick_types ( meg = False , eeg = True ) , stc , trans , src , sphere , cov = None , ecg = True , blink = True , random_state = seed ) #NEWLINE# raw_sim_meeg = simulate_raw ( raw . copy ( ) . pick_types ( meg = True , eeg = True ) , stc , trans , src , sphere , cov = None , ecg = True , blink = True , random_state = seed ) #NEWLINE# assert_allclose ( np . concatenate ( ( raw_sim_meg [ : ] [ 0 ] , raw_sim_eeg [ : ] [ 0 ] ) ) , raw_sim_meeg [ : ] [ 0 ] , rtol = 1e-07 , atol = 1e-20 ) #NEWLINE# del raw_sim_meg , raw_sim_eeg , raw_sim_meeg #NEWLINE# raw_sim_cos = simulate_raw ( raw , stc , trans , src , sphere , head_pos = head_pos_sim , random_state = seed ) #NEWLINE# raw_sim_lin = simulate_raw ( raw , stc , trans , src , sphere , head_pos = head_pos_sim , interp = 'linear' , random_state = seed ) #NEWLINE# assert_allclose ( raw_sim_cos [ : ] [ 0 ] , raw_sim_lin [ : ] [ 0 ] , rtol = 1e-05 , atol = 1e-20 ) #NEWLINE# del raw_sim_cos , raw_sim_lin #NEWLINE# head_pos_sim_err = deepcopy ( head_pos_sim ) #NEWLINE# head_pos_sim_err [ 1.0 ] [ ( 2 , 3 ) ] -= 0.1 #NEWLINE# assert_raises ( RuntimeError , simulate_raw , raw , stc , trans , src , sphere , ecg = False , blink = False , head_pos = head_pos_sim_err ) #NEWLINE# assert_raises ( RuntimeError , simulate_raw , raw , stc , trans , src , bem_fname , ecg = False , blink = False , head_pos = head_pos_sim_err ) #NEWLINE# assert_raises ( TypeError , simulate_raw , 'foo' , stc , trans , src , sphere ) #NEWLINE# assert_raises ( TypeError , simulate_raw , raw , 'foo' , trans , src , sphere ) #NEWLINE# assert_raises ( ValueError , simulate_raw , raw , stc . copy ( ) . crop ( 0 , 0 ) , trans , src , sphere ) #NEWLINE# stc_bad = stc . copy ( ) #NEWLINE# stc_bad . tstep += 0.1 #NEWLINE# assert_raises ( ValueError , simulate_raw , raw , stc_bad , trans , src , sphere ) #NEWLINE# assert_raises ( RuntimeError , simulate_raw , raw , stc , trans , src , sphere , chpi = True ) #NEWLINE# assert_raises ( ValueError , simulate_raw , raw , stc , trans , src , sphere , interp = 'foo' ) #NEWLINE# assert_raises ( TypeError , simulate_raw , raw , stc , trans , src , sphere , head_pos = 1.0 ) #NEWLINE# assert_raises ( RuntimeError , simulate_raw , raw , stc , trans , src , sphere , head_pos = pos_fname ) #NEWLINE# head_pos_sim_err = deepcopy ( head_pos_sim ) #NEWLINE# head_pos_sim_err [ ( - 1.0 ) ] = head_pos_sim_err [ 1.0 ] #NEWLINE# assert_raises ( RuntimeError , simulate_raw , raw , stc , trans , src , sphere , head_pos = head_pos_sim_err )