<s> """<STR_LIT>""" <EOL> import os <EOL> import os . path as op <EOL> import numpy as np <EOL> from scipy . misc import imread <EOL> import matplotlib . pyplot as plt <EOL> import mne <EOL> from mne import io <EOL> from mne . datasets import spm_face <EOL> from mne . minimum_norm import apply_inverse , make_inverse_operator <EOL> from mne . cov import compute_covariance <EOL> print ( __doc__ ) <EOL> data_path = spm_face . data_path ( ) <EOL> subjects_dir = data_path + '<STR_LIT>' <EOL> raw_fname = data_path + '<STR_LIT>' <EOL> raw = io . read_raw_ctf ( raw_fname % <NUM_LIT:1> ) <EOL> raw = raw . crop ( <NUM_LIT:0> , <NUM_LIT> ) . load_data ( ) . resample ( <NUM_LIT> , npad = '<STR_LIT>' ) <EOL> picks = mne . pick_types ( raw . info , meg = True , exclude = '<STR_LIT>' ) <EOL> raw . filter ( <NUM_LIT:1> , None , method = '<STR_LIT>' , n_jobs = <NUM_LIT:1> ) <EOL> events = mne . find_events ( raw , stim_channel = '<STR_LIT>' ) <EOL> event_ids = { "<STR_LIT>" : <NUM_LIT:1> , "<STR_LIT>" : <NUM_LIT:2> } <EOL> tmin , tmax = - <NUM_LIT> , <NUM_LIT:0.5> <EOL> baseline = None <EOL> reject = dict ( mag = <NUM_LIT> ) <EOL> trans = data_path + '<STR_LIT>' <EOL> src = mne . setup_source_space ( '<STR_LIT>' , fname = None , spacing = '<STR_LIT>' , <EOL> subjects_dir = subjects_dir , add_dist = False ) <EOL> bem = data_path + '<STR_LIT>' <EOL> forward = mne . make_forward_solution ( raw . info , trans , src , bem ) <EOL> forward = mne . convert_forward_solution ( forward , surf_ori = True ) <EOL> del src <EOL> conditions = '<STR_LIT>' , '<STR_LIT>' <EOL> snr = <NUM_LIT> <EOL> lambda2 = <NUM_LIT:1.0> / snr ** <NUM_LIT:2> <EOL> method = '<STR_LIT>' <EOL> clim = dict ( kind = '<STR_LIT:value>' , lims = [ <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:5> ] ) <EOL> samples_epochs = <NUM_LIT:5> , <NUM_LIT:15> , <EOL> method = '<STR_LIT>' , '<STR_LIT>' <EOL> colors = '<STR_LIT>' , '<STR_LIT>' <EOL> evokeds = list ( ) <EOL> stcs = list ( ) <EOL> methods_ordered = list ( ) <EOL> for n_train in samples_epochs : <EOL> events_ = np . concatenate ( [ events [ events [ : , <NUM_LIT:2> ] == id_ ] [ : n_train ] <EOL> for id_ in [ event_ids [ k ] for k in conditions ] ] ) <EOL> epochs_train = mne . Epochs ( raw , events_ , event_ids , tmin , tmax , picks = picks , <EOL> baseline = baseline , preload = True , reject = reject ) <EOL> epochs_train . equalize_event_counts ( event_ids , copy = False ) <EOL> assert len ( epochs_train ) == <NUM_LIT:2> * n_train <EOL> noise_covs = compute_covariance ( <EOL> epochs_train , method = method , tmin = None , tmax = <NUM_LIT:0> , <EOL> return_estimators = True ) <EOL> evokeds = [ epochs_train [ k ] . average ( ) for k in conditions ] <EOL> del epochs_train , events_ <EOL> stcs . append ( list ( ) ) <EOL> methods_ordered . append ( list ( ) ) <EOL> for cov in noise_covs : <EOL> inverse_operator = make_inverse_operator ( evokeds [ <NUM_LIT:0> ] . info , forward , <EOL> cov , loose = <NUM_LIT> , depth = <NUM_LIT> , <EOL> rank = <NUM_LIT> ) <EOL> stc_a , stc_b = ( apply_inverse ( e , inverse_operator , lambda2 , "<STR_LIT>" , <EOL> pick_ori = None ) for e in evokeds ) <EOL> stc = stc_a - stc_b <EOL> methods_ordered [ - <NUM_LIT:1> ] . append ( cov [ '<STR_LIT>' ] ) <EOL> stcs [ - <NUM_LIT:1> ] . append ( stc ) <EOL> del inverse_operator , evokeds , cov , noise_covs , stc , stc_a , stc_b <EOL> del raw , forward <EOL> fig , ( axes1 , axes2 ) = plt . subplots ( <NUM_LIT:2> , <NUM_LIT:3> , figsize = ( <NUM_LIT> , <NUM_LIT:6> ) ) <EOL> def brain_to_mpl ( brain ) : <EOL> """<STR_LIT>""" <EOL> tmp_path = op . abspath ( op . join ( op . curdir , '<STR_LIT>' ) ) <EOL> brain . save_imageset ( tmp_path , views = [ '<STR_LIT>' ] ) <EOL> im = imread ( tmp_path + '<STR_LIT>' ) <EOL> os . remove ( tmp_path + '<STR_LIT>' ) <EOL> return im <EOL> for ni , ( n_train , axes ) in enumerate ( zip ( samples_epochs , ( axes1 , axes2 ) ) ) : <EOL> ax_dynamics = axes [ <NUM_LIT:1> ] <EOL> for stc , ax , method , kind , color in zip ( stcs [ ni ] , <EOL> axes [ : : <NUM_LIT:2> ] , <EOL> methods_ordered [ ni ] , <EOL> [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> colors ) : <EOL> brain = stc . plot ( subjects_dir = subjects_dir , hemi = '<STR_LIT>' , clim = clim ) <EOL> brain . set_time ( <NUM_LIT> ) <EOL> im = brain_to_mpl ( brain ) <EOL> brain . close ( ) <EOL> del brain <EOL> ax . axis ( '<STR_LIT>' ) <EOL> ax . get_xaxis ( ) . set_visible ( False ) <EOL> ax . get_yaxis ( ) . set_visible ( False ) <EOL> ax . imshow ( im ) <EOL> ax . set_title ( '<STR_LIT>' . format ( kind , n_train * <NUM_LIT:2> ) ) <EOL> stc_mean = stc . data . mean ( <NUM_LIT:0> ) <EOL> ax_dynamics . plot ( stc . times * <NUM_LIT> , stc_mean , <EOL> label = '<STR_LIT>' . format ( method , kind ) , <EOL> color = color ) <EOL> stc_var = stc . data . std ( <NUM_LIT:0> ) <EOL> ax_dynamics . fill_between ( stc . times * <NUM_LIT> , stc_mean - stc_var , <EOL> stc_mean + stc_var , alpha = <NUM_LIT> , color = color ) <EOL> ax_dynamics . set_title ( '<STR_LIT>' . format ( n_train * <NUM_LIT:2> ) ) <EOL> ax_dynamics . set_xlabel ( '<STR_LIT>' ) <EOL> ax_dynamics . set_ylabel ( '<STR_LIT>' ) <EOL> ax_dynamics . set_xlim ( tmin * <NUM_LIT> , tmax * <NUM_LIT> ) <EOL> ax_dynamics . set_ylim ( - <NUM_LIT:3> , <NUM_LIT:3> ) <EOL> ax_dynamics . legend ( loc = '<STR_LIT>' , fontsize = <NUM_LIT:10> ) <EOL> fig . subplots_adjust ( hspace = <NUM_LIT> , left = <NUM_LIT> , right = <NUM_LIT> , wspace = <NUM_LIT> ) <EOL> fig . canvas . draw ( ) <EOL> fig . show ( ) </s>
