<s> import os . path as op <EOL> import numpy as np <EOL> from numpy . testing import assert_array_almost_equal , assert_array_equal <EOL> from nose . tools import assert_true <EOL> from mne . datasets import testing <EOL> from mne import read_label , read_forward_solution , pick_types_forward <EOL> from mne . label import Label <EOL> from mne . simulation . source import simulate_stc , simulate_sparse_stc <EOL> from mne . utils import run_tests_if_main <EOL> data_path = testing . data_path ( download = False ) <EOL> fname_fwd = op . join ( data_path , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> label_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> label_names_single_hemi = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def read_forward_solution_meg ( * args , ** kwargs ) : <EOL> fwd = read_forward_solution ( * args , ** kwargs ) <EOL> fwd = pick_types_forward ( fwd , meg = True , eeg = False ) <EOL> return fwd <EOL> @ testing . requires_testing_data <EOL> def test_simulate_stc ( ) : <EOL> """<STR_LIT>""" <EOL> fwd = read_forward_solution_meg ( fname_fwd , force_fixed = True ) <EOL> labels = [ read_label ( op . join ( data_path , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' % label ) ) for label in label_names ] <EOL> mylabels = [ ] <EOL> for i , label in enumerate ( labels ) : <EOL> new_label = Label ( vertices = label . vertices , <EOL> pos = label . pos , <EOL> values = <NUM_LIT:2> * i * np . ones ( len ( label . values ) ) , <EOL> hemi = label . hemi , <EOL> comment = label . comment ) <EOL> mylabels . append ( new_label ) <EOL> n_times = <NUM_LIT:10> <EOL> tmin = <NUM_LIT:0> <EOL> tstep = <NUM_LIT> <EOL> stc_data = np . ones ( ( len ( labels ) , n_times ) ) <EOL> stc = simulate_stc ( fwd [ '<STR_LIT:src>' ] , mylabels , stc_data , tmin , tstep ) <EOL> for label in labels : <EOL> if label . hemi == '<STR_LIT>' : <EOL> hemi_idx = <NUM_LIT:0> <EOL> else : <EOL> hemi_idx = <NUM_LIT:1> <EOL> idx = np . intersect1d ( stc . vertices [ hemi_idx ] , label . vertices ) <EOL> idx = np . searchsorted ( stc . vertices [ hemi_idx ] , idx ) <EOL> if hemi_idx == <NUM_LIT:1> : <EOL> idx += len ( stc . vertices [ <NUM_LIT:0> ] ) <EOL> assert_true ( np . all ( stc . data [ idx ] == <NUM_LIT:1.0> ) ) <EOL> assert_true ( stc . data [ idx ] . shape [ <NUM_LIT:1> ] == n_times ) <EOL> def fun ( x ) : <EOL> return x ** <NUM_LIT:2> <EOL> stc = simulate_stc ( fwd [ '<STR_LIT:src>' ] , mylabels , stc_data , tmin , tstep , fun ) <EOL> for i , label in enumerate ( labels ) : <EOL> if label . hemi == '<STR_LIT>' : <EOL> hemi_idx = <NUM_LIT:0> <EOL> else : <EOL> hemi_idx = <NUM_LIT:1> <EOL> idx = np . intersect1d ( stc . vertices [ hemi_idx ] , label . vertices ) <EOL> idx = np . searchsorted ( stc . vertices [ hemi_idx ] , idx ) <EOL> if hemi_idx == <NUM_LIT:1> : <EOL> idx += len ( stc . vertices [ <NUM_LIT:0> ] ) <EOL> res = ( ( <NUM_LIT> * i ) ** <NUM_LIT> ) * np . ones ( ( len ( idx ) , n_times ) ) <EOL> assert_array_almost_equal ( stc . data [ idx ] , res ) <EOL> @ testing . requires_testing_data <EOL> def test_simulate_sparse_stc ( ) : <EOL> """<STR_LIT>""" <EOL> fwd = read_forward_solution_meg ( fname_fwd , force_fixed = True ) <EOL> labels = [ read_label ( op . join ( data_path , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' % label ) ) for label in label_names ] <EOL> n_times = <NUM_LIT:10> <EOL> tmin = <NUM_LIT:0> <EOL> tstep = <NUM_LIT> <EOL> times = np . arange ( n_times , dtype = np . float ) * tstep + tmin <EOL> stc_1 = simulate_sparse_stc ( fwd [ '<STR_LIT:src>' ] , len ( labels ) , times , <EOL> labels = labels , random_state = <NUM_LIT:0> ) <EOL> assert_true ( stc_1 . data . shape [ <NUM_LIT:0> ] == len ( labels ) ) <EOL> assert_true ( stc_1 . data . shape [ <NUM_LIT:1> ] == n_times ) <EOL> stc_2 = simulate_sparse_stc ( fwd [ '<STR_LIT:src>' ] , len ( labels ) , times , <EOL> labels = labels , random_state = <NUM_LIT:0> ) <EOL> assert_array_equal ( stc_1 . lh_vertno , stc_2 . lh_vertno ) <EOL> assert_array_equal ( stc_1 . rh_vertno , stc_2 . rh_vertno ) <EOL> @ testing . requires_testing_data <EOL> def test_generate_stc_single_hemi ( ) : <EOL> """<STR_LIT>""" <EOL> fwd = read_forward_solution_meg ( fname_fwd , force_fixed = True ) <EOL> labels_single_hemi = [ read_label ( op . join ( data_path , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' % label ) ) <EOL> for label in label_names_single_hemi ] <EOL> mylabels = [ ] <EOL> for i , label in enumerate ( labels_single_hemi ) : <EOL> new_label = Label ( vertices = label . vertices , <EOL> pos = label . pos , <EOL> values = <NUM_LIT:2> * i * np . ones ( len ( label . values ) ) , <EOL> hemi = label . hemi , <EOL> comment = label . comment ) <EOL> mylabels . append ( new_label ) <EOL> n_times = <NUM_LIT:10> <EOL> tmin = <NUM_LIT:0> <EOL> tstep = <NUM_LIT> <EOL> stc_data = np . ones ( ( len ( labels_single_hemi ) , n_times ) ) <EOL> stc = simulate_stc ( fwd [ '<STR_LIT:src>' ] , mylabels , stc_data , tmin , tstep ) <EOL> for label in labels_single_hemi : <EOL> if label . hemi == '<STR_LIT>' : <EOL> hemi_idx = <NUM_LIT:0> <EOL> else : <EOL> hemi_idx = <NUM_LIT:1> <EOL> idx = np . intersect1d ( stc . vertices [ hemi_idx ] , label . vertices ) <EOL> idx = np . searchsorted ( stc . vertices [ hemi_idx ] , idx ) <EOL> if hemi_idx == <NUM_LIT:1> : <EOL> idx += len ( stc . vertices [ <NUM_LIT:0> ] ) <EOL> assert_true ( np . all ( stc . data [ idx ] == <NUM_LIT:1.0> ) ) <EOL> assert_true ( stc . data [ idx ] . shape [ <NUM_LIT:1> ] == n_times ) <EOL> def fun ( x ) : <EOL> return x ** <NUM_LIT:2> <EOL> stc = simulate_stc ( fwd [ '<STR_LIT:src>' ] , mylabels , stc_data , tmin , tstep , fun ) <EOL> for i , label in enumerate ( labels_single_hemi ) : <EOL> if label . hemi == '<STR_LIT>' : <EOL> hemi_idx = <NUM_LIT:0> <EOL> else : <EOL> hemi_idx = <NUM_LIT:1> <EOL> idx = np . intersect1d ( stc . vertices [ hemi_idx ] , label . vertices ) <EOL> idx = np . searchsorted ( stc . vertices [ hemi_idx ] , idx ) <EOL> if hemi_idx == <NUM_LIT:1> : <EOL> idx += len ( stc . vertices [ <NUM_LIT:0> ] ) <EOL> res = ( ( <NUM_LIT> * i ) ** <NUM_LIT> ) * np . ones ( ( len ( idx ) , n_times ) ) <EOL> assert_array_almost_equal ( stc . data [ idx ] , res ) <EOL> @ testing . requires_testing_data <EOL> def test_simulate_sparse_stc_single_hemi ( ) : <EOL> """<STR_LIT>""" <EOL> fwd = read_forward_solution_meg ( fname_fwd , force_fixed = True ) <EOL> n_times = <NUM_LIT:10> <EOL> tmin = <NUM_LIT:0> <EOL> tstep = <NUM_LIT> <EOL> times = np . arange ( n_times , dtype = np . float ) * tstep + tmin <EOL> labels_single_hemi = [ read_label ( op . join ( data_path , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' % label ) ) <EOL> for label in label_names_single_hemi ] <EOL> stc_1 = simulate_sparse_stc ( fwd [ '<STR_LIT:src>' ] , len ( labels_single_hemi ) , times , <EOL> labels = labels_single_hemi , random_state = <NUM_LIT:0> ) <EOL> assert_true ( stc_1 . data . shape [ <NUM_LIT:0> ] == len ( labels_single_hemi ) ) <EOL> assert_true ( stc_1 . data . shape [ <NUM_LIT:1> ] == n_times ) <EOL> stc_2 = simulate_sparse_stc ( fwd [ '<STR_LIT:src>' ] , len ( labels_single_hemi ) , times , <EOL> labels = labels_single_hemi , random_state = <NUM_LIT:0> ) <EOL> assert_array_equal ( stc_1 . lh_vertno , stc_2 . lh_vertno ) <EOL> assert_array_equal ( stc_1 . rh_vertno , stc_2 . rh_vertno ) <EOL> run_tests_if_main ( ) </s>
