def create_grand_averages(self, start_anew=False): self.evoked_fam_path = os.path.join(self.path, "evoked_fam-evo.fif") self.evoked_unf_path = os.path.join(self.path, "evoked_unf-evo.fif") self.evoked_scr_path = os.path.join(self.path, "evoked_scr-evo.fif") if os.path.isfile(self.evoked_fam_path): if start_anew: os.remove(self.evoked_fam_path) os.remove(self.evoked_unf_path) os.remove(self.evoked_unf_path) else: return evoked_fams = [] evoked_unfs = [] evoked_scrs = [] for triple in self.triples: epochs = self.triple_to_epochs(triple) if epochs == None: continue evoked_fams.append(epochs['5'].average()) evoked_unfs.append(epochs['13'].average()) evoked_scrs.append(epochs['17'].average()) print("Finished with {}".format(triple[0])) evoked_fam = mne.grand_average(evoked_fams) evoked_unf = mne.grand_average(evoked_unfs) evoked_scr = mne.grand_average(evoked_scrs) evoked_fam.save(self.evoked_fam_path) evoked_unf.save(self.evoked_unf_path) evoked_scr.save(self.evoked_scr_path)
def within_subject_cis(insts, ci=0.95): # see Morey (2008): Confidence Intervals from Normalized Data: # A correction to Cousineau (2005) # the type of the provided instances should be the same if not all(isinstance(inst, (dict, list)) for inst in insts): raise ValueError( 'instances must be of same type (either dict ot list)') # the number of subjects should be the same n_subj = np.unique([len(i) for i in insts]) if len(n_subj) > 1: raise ValueError('inst must be of same length') if isinstance(insts[0], dict): subjs = insts[0].keys() else: subjs = np.arange(0, len(insts[0])) # correction factor for number of conditions n_cond = len(insts) corr_factor = np.sqrt(n_cond / (n_cond - 1)) # compute overall subject ERPs subject_erp = { subj: grand_average([insts[cond][subj] for cond in range(n_cond)]) for subj in subjs } # compute condition grand averages grand_averages = [grand_average(list(cond.values())) for cond in insts] # place holder for results n_channels = grand_averages[0].data.shape[0] n_times = grand_averages[0].data.shape[1] norm_erps = np.zeros((n_cond, int(n_subj), n_channels, n_times)) # compute normed ERPs, # ((condition ERP - subject ERP) + grand average) * corr_factor for n_s, subj in enumerate(subjs): for ic, cond in enumerate(insts): erp_data = cond[subj].data.copy() - subject_erp[subj].data erp_data = (erp_data + grand_averages[ic].data) * corr_factor norm_erps[ic, n_s, :] = erp_data erp_sem = np.zeros((n_cond, n_channels, n_times)) for n_c in range(n_cond): erp_sem[n_c, :] = stats.sem(norm_erps[n_c, :], axis=0) confidence = np.zeros((n_cond, n_channels, n_times)) for n_c in range(n_cond): confidence[n_c, :] = erp_sem[n_c, :] * \ stats.t.ppf((1 + ci) / 2.0, int(n_subj) - 1) return confidence
def extract_plot(filenames, compare, outfolder, group, event_id): sleep_group=list() wake_group=list() for line in filenames: k=line[2:4].split() if k[0] in sleep_list: sleep_group.append(line) elif k[0] in wake_list: wake_group.append(line) if group == 'sleep': filenames = sleep_group elif group == 'wake': filenames = wake_group elif group == 'all': filenames = filenames # print(filenames) ss_avg = dict() # new empty dict ss_avg_by_cond = {e: list() for e in event_id} ss_avg_all = list() try: for filename in filenames: epochs = mne.read_epochs(filename) evokeds = {str(cond): epochs[str(cond)].average() for cond in epochs.event_id} ss_avg[filename] = evokeds for cond in event_id: ss_avg_by_cond[cond].append(ss_avg[filename][cond]) ss_avg_all.append(ss_avg[filename][cond]) except (IOError, ValueError) as e: print(filename, 'skipped for no epochs') grand_avs = {cond:mne.grand_average(ss_avg_by_cond[cond]) for cond in event_id} grand_all=mne.grand_average(ss_avg_all) com = {k:grand_avs[k] for k in compare} for name in grand_all.ch_names: name_int=grand_all.ch_names.index(name) fig= mne.viz.plot_compare_evokeds((com),picks=[name_int],invert_y=True, show= False) fig.savefig('figures/' + zone + '-mon' + '_' + 'g-avg.pdf') A=fig.axes[0].lines[0] U=fig.axes[0].lines[1] Ax=A.get_xdata() Ay=A.get_ydata() Uy=U.get_ydata() x=Ax.tolist() Ay=Ay.tolist() Uy=Uy.tolist() x.insert(0,'x') Ay.insert(0,'Ay') Uy.insert(0,'Uy') d = zip(x, Ay, Uy) with open('Baseline/plotdata/' + outfolder + '/' + group + '/' + name + '.csv', "w") as f: writer = csv.writer(f) for row in d: writer.writerow(row)
def plot_eeg_erp_topo(all_epochs, colors=None): """ Plot butterfly plot. DOCS INCOMPLETE :( """ all_evokeds = eeg_to_all_evokeds(all_epochs) data = {} for participant, epochs in all_evokeds.items(): for cond, epoch in epochs.items(): data[cond] = [] for participant, epochs in all_evokeds.items(): for cond, epoch in epochs.items(): data[cond].append(epoch) if colors is not None: color_list = [] else: color_list = None evokeds = [] for condition, evoked in data.items(): grand_average = mne.grand_average(evoked) grand_average.comment = condition evokeds += [grand_average] if colors is not None: color_list.append(colors[condition]) plot = mne.viz.plot_evoked_topo(evokeds, background_color="w", color=color_list) return (plot)
def grand_avg_tfr(ga_group): trial_dict = {} for name in ga_group.group_list: sub = CurrentSub(name, ga_group.mw) print(f'Add {name} to grand_average') powers = sub.load_power_tfr() for pw in powers: if pw.nave != 0: if pw.comment in trial_dict: trial_dict[pw.comment].append(pw) else: trial_dict.update({pw.comment: [pw]}) else: print(f'{pw.comment} for {name} got nave=0') for trial in trial_dict: if len(trial_dict[trial]) != 0: # Make sure, all have the same number of channels commons = set() for pw in trial_dict[trial]: if len(commons) == 0: for c in pw.ch_names: commons.add(c) commons = commons & set(pw.ch_names) print(f'{trial}:Reducing all n_channels to {len(commons)}') for idx, pw in enumerate(trial_dict[trial]): trial_dict[trial][idx] = pw.pick_channels(list(commons)) ga = mne.grand_average(trial_dict[trial], interpolate_bads=True, drop_bads=True) ga.comment = trial ga_group.save_ga_tfr(ga, trial)
def test_evoked_arithmetic(): """Test evoked arithmetic """ ev = read_evokeds(fname, condition=0) ev1 = EvokedArray(np.ones_like(ev.data), ev.info, ev.times[0], nave=20) ev2 = EvokedArray(-np.ones_like(ev.data), ev.info, ev.times[0], nave=10) # combine_evoked([ev1, ev2]) should be the same as ev1 + ev2: # data should be added according to their `nave` weights # nave = ev1.nave + ev2.nave ev = ev1 + ev2 assert_equal(ev.nave, ev1.nave + ev2.nave) assert_allclose(ev.data, 1. / 3. * np.ones_like(ev.data)) ev = ev1 - ev2 assert_equal(ev.nave, ev1.nave + ev2.nave) assert_equal(ev.comment, ev1.comment + ' - ' + ev2.comment) assert_allclose(ev.data, np.ones_like(ev1.data)) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') ev = merge_evoked([ev1, ev2]) assert_true(len(w) >= 1) assert_allclose(ev.data, 1. / 3. * np.ones_like(ev.data)) # default comment behavior if evoked.comment is None old_comment1 = ev1.comment old_comment2 = ev2.comment ev1.comment = None with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') ev = ev1 - ev2 assert_equal(ev.comment, 'unknown') ev1.comment = old_comment1 ev2.comment = old_comment2 # equal weighting ev = combine_evoked([ev1, ev2], weights='equal') assert_allclose(ev.data, np.zeros_like(ev1.data)) # combine_evoked([ev1, ev2], weights=[1, 0]) should yield the same as ev1 ev = combine_evoked([ev1, ev2], weights=[1, 0]) assert_equal(ev.nave, ev1.nave) assert_allclose(ev.data, ev1.data) # simple subtraction (like in oddball) ev = combine_evoked([ev1, ev2], weights=[1, -1]) assert_allclose(ev.data, 2 * np.ones_like(ev1.data)) assert_raises(ValueError, combine_evoked, [ev1, ev2], weights='foo') assert_raises(ValueError, combine_evoked, [ev1, ev2], weights=[1]) # grand average evoked1, evoked2 = read_evokeds(fname, condition=[0, 1], proj=True) ch_names = evoked1.ch_names[2:] evoked1.info['bads'] = ['EEG 008'] # test interpolation evoked1.drop_channels(evoked1.ch_names[:1]) evoked2.drop_channels(evoked2.ch_names[1:2]) gave = grand_average([evoked1, evoked2]) assert_equal(gave.data.shape, [len(ch_names), evoked1.data.shape[1]]) assert_equal(ch_names, gave.ch_names) assert_equal(gave.nave, 2)
def grand_avg_evokeds(ga_group): trial_dict = {} for name in ga_group.group_list: sub = CurrentSub(name, ga_group.mw) print(f'Add {name} to grand_average') evokeds = sub.load_evokeds() for evoked in evokeds: if evoked.nave != 0: if evoked.comment in trial_dict: trial_dict[evoked.comment].append(evoked) else: trial_dict.update({evoked.comment: [evoked]}) else: print(f'{evoked.comment} for {name} got nave=0') ga_evokeds = [] for trial in trial_dict: if len(trial_dict[trial]) != 0: ga = mne.grand_average(trial_dict[trial], interpolate_bads=True, drop_bads=True) ga.comment = trial ga_evokeds.append(ga) ga_group.save_ga_evokeds(ga_evokeds)
def group_average_evoked(experiment, evoked_name, groups, new_name): """ """ keys = [] time_arrays = [] for group_key, group_subjects in groups.items(): for subject_name in group_subjects: try: subject = experiment.subjects.get(subject_name) evoked = subject.evoked.get(evoked_name) mne_evokeds = evoked.content for mne_evoked in mne_evokeds.values(): time_arrays.append(mne_evoked.times) except Exception as exc: continue assert_arrays_same(time_arrays) grand_evokeds = {} for group_key, group_subjects in groups.items(): for subject in experiment.subjects.values(): if subject.name not in group_subjects: continue evoked = subject.evoked.get(evoked_name) if not evoked: continue for evoked_item_key, evoked_item in evoked.content.items(): grand_key = (group_key, evoked_item_key) if grand_key not in grand_evokeds: grand_evokeds[grand_key] = [] grand_evokeds[grand_key].append(evoked_item) grand_averages = {} new_keys = [] for key, grand_evoked in grand_evokeds.items(): new_key = str(key[1]) + '_' + str(key[0]) if len(grand_evoked) == 1: grand_averages[new_key] = grand_evoked[0].copy() else: grand_averages[new_key] = mne.grand_average(grand_evoked) new_keys.append(new_key) grand_averages[new_key].comment = new_key subject = experiment.active_subject evoked_directory = subject.evoked_directory params = {'conditions': new_keys, 'groups': groups} grand_average_evoked = Evoked(new_name, evoked_directory, params, content=grand_averages) grand_average_evoked.save_content() subject.add(grand_average_evoked, 'evoked')
def container_process(conf): print('\trun tfr container...') path_home = conf.path_home kind = conf.kind train = conf.train frequency = conf.frequency spec = conf.spec data_path = conf.data_path verbose = conf.verbose donor = mne.Evoked(f'{path_home}donor-ave.fif', verbose = 'ERROR') fpath_events = conf.path_mio + '/mio_out_{}/{}_run{}_mio_corrected_{}{}.txt' plot_spectrogram = conf.plot_spectrogram single_trial = conf.single_trial #get rid of runs, leave frequency data for pos and neg feedback for time course plotting for i in range(len(kind)): for subject in conf.subjects: data = [] processing_done = False if not plot_spectrogram: out_file = conf.path_container + "{}_{}{}_{}{}-ave.fif".format(subject, spec, kind[i], frequency, train) else: out_file = conf.path_container + "{0}_{1}{2}_{3}{4}-50ms-tfr.h5".format(subject, spec, kind[i], frequency, train) for run in conf.runs: print('\t\t', kind[i], run, subject) path_events = fpath_events.format(kind[i], subject, run, kind[i], train) if pathlib.Path(path_events).exists(): if verbose: print('This file is being processed: ', path_events) freq_file = conf.path_tfr + data_path.format(subject, run, spec, frequency, kind[i], train) old_level = mne.set_log_level(verbose='ERROR', return_old_level=True) freq_data = mne.time_frequency.read_tfrs(freq_file)[0] mne.set_log_level(verbose=old_level) data.append(freq_data.data) processing_done = True if run == conf.runs[-1] and processing_done: container_results(plot_spectrogram, single_trial, freq_data, data, donor, out_file, verbose) if plot_spectrogram: sdata = [] for subject in conf.subjects: out_file = conf.path_container + "{0}_{1}{2}_{3}{4}-50ms-tfr.h5".format(subject, spec, kind[0], frequency, train) if pathlib.Path(out_file).exists(): freq_subject_data = mne.time_frequency.read_tfrs(out_file)[0] sdata.append(freq_subject_data) freq_spec_data = mne.grand_average(sdata) title = f'Spectrogram ~{conf.L_freq}-{conf.H_freq} Hz TFR in {kind[0]}' PM = freq_spec_data.plot(picks='meg', fmin=conf.L_freq, fmax=conf.H_freq, vmin=-0.15, vmax=0.15, title=title) os.chdir('/home/asmyasnikova83/') PM.savefig('output.png') print('\tSpectrogramm completed') print('\ttfr container completed')
def average_evokeds(session): # Container for all conditions: all_evokeds = defaultdict(list) for subject in config.get_subjects(): fname_in = BIDSPath(subject=subject, session=session, task=config.get_task(), acquisition=config.acq, run=None, recording=config.rec, space=config.space, suffix='ave', extension='.fif', datatype=config.get_datatype(), root=config.deriv_root, check=False) msg = f'Input: {fname_in}' logger.info( gen_log_message(message=msg, step=9, subject=subject, session=session)) evokeds = mne.read_evokeds(fname_in) for idx, evoked in enumerate(evokeds): all_evokeds[idx].append(evoked) # Insert into the container for idx, evokeds in all_evokeds.items(): all_evokeds[idx] = mne.grand_average( evokeds, interpolate_bads=config.interpolate_bads_grand_average ) # Combine subjects subject = 'average' fname_out = BIDSPath(subject=subject, session=session, task=config.get_task(), acquisition=config.acq, run=None, processing=config.proc, recording=config.rec, space=config.space, suffix='ave', extension='.fif', datatype=config.get_datatype(), root=config.deriv_root, check=False) if not fname_out.fpath.parent.exists(): os.makedirs(fname_out.fpath.parent) msg = f'Saving grand-averaged evoked sensor data: {fname_out}' logger.info( gen_log_message(message=msg, step=9, subject=subject, session=session)) mne.write_evokeds(fname_out, list(all_evokeds.values())) return list(all_evokeds.values())
def test_grand_average(): """Test grand average """ evoked1, evoked2 = read_evokeds(fname, condition=[0, 1], proj=True) ch_names = evoked1.ch_names[2:] evoked1.info['bads'] = ['EEG 008'] # test interpolation evoked1.drop_channels(evoked1.ch_names[:1]) evoked2.drop_channels(evoked2.ch_names[1:2]) gave = grand_average([evoked1, evoked2]) assert_equal(gave.data.shape, [len(ch_names), evoked1.data.shape[1]]) assert_equal(ch_names, gave.ch_names) assert_equal(gave.nave, 2)
def GDAVG(wdir, ListCondition, ListSubject): GrandAverages = [] for c, cond in enumerate(ListCondition): Evokeds = [] for s, subject in enumerate(ListSubject): EvokedPath = wdir + '/data/epochs/' + subject + '_' + cond + '-ave.fif' Evokeds.append(mne.read_evokeds(EvokedPath)[0]) GrandAverages.append(mne.grand_average(Evokeds)) return GrandAverages
def grand_average(pairs, cond, component, plot=0): evokeds = [] for i in pairs: for root, dirs, files in os.walk(path): for f in files: # If statement to merge 233 and 231 if cond == '233' or cond == '231': if f.startswith(str(i) + '_evoked_') and f.endswith( '233_' + component + '_combined-ave.fif'): evokeds.append(mne.read_evokeds(f)) if f.startswith(str(i) + '_evoked_') and f.endswith( '231_' + component + '_combined-ave.fif'): evokeds.append(mne.read_evokeds(f)) elif f.startswith(str(i) + '_evoked_') and f.endswith( cond + '_' + component + '_combined-ave.fif'): evokeds.append(mne.read_evokeds(f)) elif f.startswith(str(i) + '_evoked_') and f.endswith( cond + 'a_' + component + '_combined-ave.fif'): evokeds.append(mne.read_evokeds(f)) elif f.startswith(str(i) + '_evoked_') and f.endswith( cond + 'b_' + component + '_combined-ave.fif'): evokeds.append(mne.read_evokeds(f)) for i in range(len(evokeds)): evokeds[i] = evokeds[i][0] grand_average = mne.grand_average(evokeds) if plot: grand_average.plot() return grand_average
all_evokeds = [aud_l, aud_r, vis_l, vis_r] print(all_evokeds) ############################################################################### # This can be simplified with a Python list comprehension: all_evokeds = [epochs[cond].average() for cond in sorted(event_id.keys())] print(all_evokeds) # Then, we construct and plot an unweighted average of left vs. right trials # this way, too: mne.combine_evoked(all_evokeds, weights=(0.25, -0.25, 0.25, -0.25)).plot_joint() ############################################################################### # Often, it makes sense to store Evoked objects in a dictionary or a list - # either different conditions, or different subjects. # If they are stored in a list, they can be easily averaged, for example, # for a grand average across subjects (or conditions). grand_average = mne.grand_average(all_evokeds) mne.write_evokeds('/tmp/tmp-ave.fif', all_evokeds) # If Evokeds objects are stored in a dictionary, they can be retrieved by name. all_evokeds = dict((cond, epochs[cond].average()) for cond in event_id) print(all_evokeds['left/auditory']) # Besides for explicit access, this can be used for example to set titles. for cond in all_evokeds: all_evokeds[cond].plot_joint(title=cond)
param['path'], 'glms', 'cuelocked', 'epochs_glm' + str(glmnum), 'wmc_' + param['subid'] + '_cuelocked_tl_' + lapstr + name + '_betas-ave.fif'))[0]) data_t[name].append( mne.read_evokeds(fname=op.join( param['path'], 'glms', 'cuelocked', 'epochs_glm' + str(glmnum), 'wmc_' + param['subid'] + '_cuelocked_tl_' + lapstr + name + '_tstats-ave.fif'))[0]) # crop to view just cue period for i in range(subs.size): for name in contrasts: data[name][i].crop(tmin=-0.3, tmax=1.25) data_t[name][i].crop(tmin=-0.3, tmax=1.25) #%% gave = mne.grand_average(data['grandmean']) times = gave.times fig = plt.figure(figsize=(9, 9)) ax = fig.add_subplot(111) gave.plot_sensors(show_names=True, axes=ax) #fig.savefig(fname = op.join(figpath, 'sensor_locations.eps'), format = 'eps', dpi = 300) #fig.savefig(fname = op.join(figpath, 'sensor_locations.pdf'), format = 'pdf', dpi = 300) del (gave) plt.close() #%% ##note here, the actual delay between offset of the cue and onset of the probe is ONE SECOND, NOT 1.5 SECONDS. #I think that the script on the eeglab is different to in my local data, because it says 1.5s there but the saved behavioural data has delay2 as 1 mne.grand_average(deepcopy(data['grandmean'])).plot_joint( times=np.arange(0.05, 0.7, 0.05), title='grand mean ERP') mne.grand_average(deepcopy(data['neutralleft'])).plot_joint(
## Selecting relevant epochs nodis = epochs['target_number_block_Nodis'] buzz = epochs['target_number_block_Buzz'] neutral = epochs['target_number_block_Neutral'] s_neutral = epochs['target_sound_neutral'] s_buzz = epochs['target_sound_buzz'] ## And appending them to list target_nodis.append(nodis.average()) target_buzz.append(buzz.average()) target_neutral.append(neutral.average()) sound_neutral.append(s_neutral.average()) sound_buzz.append(s_buzz.average()) ## Create grand average target_nodis_grand = mne.grand_average(target_nodis) target_buzz_grand = mne.grand_average(target_buzz) target_neutral_grand = mne.grand_average(target_neutral) sound_neutral_grand = mne.grand_average(sound_neutral) sound_buzz_grand = mne.grand_average(sound_buzz) # # Visualize grand averages target_nodis_grand.plot_joint(title='Grand Average Nodis') target_buzz_grand.plot_joint(title='Grand Average Buzz') target_neutral_grand.plot_joint(title='Grand Average Neutral') sound_neutral_grand.plot_joint(title='Grand Average Sound Neutral') sound_buzz_grand.plot_joint(title='Grand Average Sound Buzz') ##PLots to compare conditions and different electrodes ##For blocks P3b
name + '_betas-ave.fif'))[0]) data_t[name].append( mne.read_evokeds(fname=op.join( param['path'], 'glms', 'feedback', 'epochs_glm' + str(glmnum), 'wmc_' + param['subid'] + '_feedbacklocked_tl_' + name + '_tstats-ave.fif'))[0]) #drop right mastoid from literally everything here lol its not useful anymore for cope in data.keys(): for i in range(subs.size): data[cope][i] = data[cope][i].drop_channels( ['RM']) #.set_eeg_reference(ref_channels='average') data_t[cope][i] = data_t[cope][i].drop_channels( ['RM']) #.set_eeg_reference(ref_channels='average') #%% gave = mne.grand_average(data['incorrect']) times = gave.times del (gave) #%% #some quick and dirty visualisation of error regressor at different electrodes in the three trial groupings #for channel in ['FCz', 'Cz', 'CPz', 'Pz']: for channel in ['Cz']: mne.viz.plot_compare_evokeds(evokeds=dict(defcorr=data['confdefcorrect'], justcorr=data['confjustcorrect'], incorr=data['confincorrect']), colors=dict(defcorr='#31a354', justcorr='#a1d99b', incorr='#de2d26'), linestyles=dict(justcorr='dashed'), show_legend='upper right', picks=channel,
data_t[i] = [] for i in subs: print('\n\ngetting subject ' + str(i) +'\n\n') sub = dict(loc = 'workstation', id = i) param = get_subject_info_wmConfidence(sub) #_baselined for name in contrasts: data[name].append( mne.read_evokeds(fname = op.join(param['path'], 'glms', 'feedback', 'epochs_glm'+str(glmnum), 'wmc_' + param['subid'] + '_feedbacklocked_tl_'+ name + '_betas-ave.fif'))[0]) data_t[name].append( mne.read_evokeds(fname = op.join(param['path'], 'glms', 'feedback', 'epochs_glm'+str(glmnum), 'wmc_' + param['subid'] + '_feedbacklocked_tl_'+ name + '_tstats-ave.fif'))[0]) #drop right mastoid from literally everything here lol its not useful anymore for cope in data.keys(): for i in range(subs.size): data[cope][i] = data[cope][i].drop_channels(['RM'])#.set_eeg_reference(ref_channels='average') data_t[cope][i] = data_t[cope][i].drop_channels(['RM'])#.set_eeg_reference(ref_channels='average') #%% gave = mne.grand_average(data['incorrect']); times = gave.times; del(gave) #%% this is just a quick and dirty visualisation cos easy for channel in ['FCz', 'Cz', 'CPz', 'Pz']: mne.viz.plot_compare_evokeds( evokeds = dict( defscorrect = data['defcorrect'], justcorrect = data['justcorrect'], incorrect = data['incorrect']), colors = dict( defscorrect = '#fc8d59', justcorrect = '#91cf60', incorrect = '#91bfdb' ), ci = .68, show_legend = 'upper right', picks = channel, show_sensors = False,#ylim = dict(eeg=[-4,4]), truncate_xaxis = False)
(.8, 10): (.4, 4), (1., 10): (.4, 4) } visleftchans = ['PO3', 'PO7', 'O1'] visrightchans = ['PO4', 'PO8', 'O2'] motrightchans = ['C2', 'C4'] #channels contra to the left hand (space bar) motleftchans = ['C1', 'C3'] #channels contra to the right hand (mouse) topoargs = dict(outlines='head', contours=0) topoargs_t = dict(outlines='head', contours=0, vmin=-2, vmax=2) #%% gave_clvsr = mne.grand_average(alldata_cuedlvsr_t) gave_clvsr.data = toverparam(alldata_cuedlvsr_t) gave_clvsr.drop_channels(['RM']) gave_clvsr.plot_joint(topomap_args=topoargs_t, timefreqs=timefreqs_alpha) times, freqs = gave_clvsr.times, gave_clvsr.freqs cvsn_vis = np.subtract( np.nanmean(deepcopy(gave_clvsr).pick_channels(visrightchans).data, 0), np.nanmean(deepcopy(gave_clvsr).pick_channels(visleftchans).data, 0)) cvsn_mouse = np.subtract( np.nanmean(deepcopy(gave_clvsr).pick_channels(motleftchans).data, 0), np.nanmean(deepcopy(gave_clvsr).pick_channels(motrightchans).data, 0)) cvsn_space = np.subtract( np.nanmean(deepcopy(gave_clvsr).pick_channels(motrightchans).data, 0), np.nanmean(deepcopy(gave_clvsr).pick_channels(motleftchans).data, 0))
def group_average_tfr(experiment, tfr_name, groups, new_name): """ Computes a group average item.""" # check data cohesion keys = [] freq_arrays = [] time_arrays = [] for group_key, group_subjects in groups.items(): for subject_name in group_subjects: try: subject = experiment.subjects.get(subject_name) tfr = subject.tfr.get(tfr_name) keys.append(tuple(sorted(tfr.content.keys()))) freq_arrays.append(tuple(tfr.freqs)) time_arrays.append(tuple(tfr.times)) except Exception as exc: continue assert_arrays_same(keys, 'Conditions do no match') assert_arrays_same(freq_arrays, 'Freqs do not match') assert_arrays_same(time_arrays) # handle channel differences ch_names = [] for group_key, group_subjects in groups.items(): for subject_name in group_subjects: try: subject = experiment.subjects.get(subject_name) tfr = subject.tfr.get(tfr_name) ch_names.append(tuple(clean_names(tfr.ch_names))) except Exception as exc: continue if len(set(ch_names)) != 1: logging.getLogger('ui_logger').info( "TFR's contain different sets of channels. Identifying common ones.." ) common_ch_names = list(set.intersection(*map(set, ch_names))) logging.getLogger('ui_logger').info( str(len(common_ch_names)) + ' common channels found.') else: common_ch_names = ch_names[0] grand_tfrs = {} for group_key, group_subjects in groups.items(): for subject in experiment.subjects.values(): if subject.name not in group_subjects: continue meggie_tfr = subject.tfr.get(tfr_name) if not meggie_tfr: continue for tfr_item_key, tfr_item in meggie_tfr.content.items(): grand_key = (group_key, tfr_item_key) # get common channels in "subject specific space" subject_ch_names = tfr_item.info['ch_names'] for ch_idx, ch_name in enumerate( clean_names(subject_ch_names)): drop_names = [] if ch_name not in common_ch_names: drop_names.append(subject_ch_names[ch_idx]) tfr_item = tfr_item.copy().drop_channels(drop_names) # sanity check if len(tfr_item.info['ch_names']) != len(common_ch_names): raise Exception('Something wrong with the channels') if grand_key in grand_tfrs: grand_tfrs[grand_key].append(tfr_item) else: grand_tfrs[grand_key] = [tfr_item] grand_averages = {} for key, grand_tfr in grand_tfrs.items(): new_key = str(key[1]) + '_group_' + str(key[0]) if len(grand_tfr) == 1: grand_averages[new_key] = grand_tfr[0].copy() else: grand_averages[new_key] = mne.grand_average(grand_tfr) active_subject = experiment.active_subject meggie_tfr = active_subject.tfr.get(tfr_name) params = { 'decim': meggie_tfr.decim, 'n_cycles': meggie_tfr.n_cycles, 'evoked_subtracted': meggie_tfr.evoked_subtracted, 'conditions': list(grand_averages.keys()), 'groups': groups } meggie_tfr = TFR(new_name, active_subject.tfr_directory, params, grand_averages) meggie_tfr.save_content() active_subject.add(meggie_tfr, "tfr")
raw_file = fpath_raw.format(subject, run) raw_data = mne.io.Raw(raw_file, preload=True) raw_data = raw_data.filter(None, 50, fir_design='firwin') # for low frequencies, below the peaks of power-line noise low pass filter the data raw_data = raw_data.filter(1., None, fir_design='firwin') #remove slow drifts picks = mne.pick_types(raw_data.info, meg = True) events_with_cross, events_of_interest = retrieve_events_for_baseline(raw_data, rf, picks) print('Done with the events!') BASELINE, b_line = compute_baseline_substraction_and_power(raw_data, events_with_cross, picks) print('\n\nDone with the BASELINE I!') CORRECTED_DATA = correct_baseline_substraction(BASELINE, events_of_interest, raw_data, picks) print('\n\nDone with the CORRECTED!') plot_created_epochs_evoked = False epochs_of_interest, evoked = create_mne_epochs_evoked(kind, subject, run, CORRECTED_DATA, events_of_interest, plot_created_epochs_evoked, raw_data, picks) evoked_ave.append(evoked) else: print('This file: ', rf, 'does not exit') continue grand_averages = mne.grand_average(evoked_ave) if batterfly: GA = grand_averages.plot(spatial_colors=True, gfp=True, picks=picks, show=False) if topomap: GA = grand_averages.pick_types(meg='grad').plot_topo(color='r', legend=False) print(grand_averages) if mode == 'server': os.chdir('/home/asmyasnikova83/DATA/evoked_ave') GA.savefig('output.png') else: plt.show()
def test_arithmetic(): """Test evoked arithmetic.""" ev = read_evokeds(fname, condition=0) ev1 = EvokedArray(np.ones_like(ev.data), ev.info, ev.times[0], nave=20) ev2 = EvokedArray(-np.ones_like(ev.data), ev.info, ev.times[0], nave=10) # combine_evoked([ev1, ev2]) should be the same as ev1 + ev2: # data should be added according to their `nave` weights # nave = ev1.nave + ev2.nave ev = combine_evoked([ev1, ev2], weights='nave') assert_equal(ev.nave, ev1.nave + ev2.nave) assert_allclose(ev.data, 1. / 3. * np.ones_like(ev.data)) # with same trial counts, a bunch of things should be equivalent for weights in ('nave', 'equal', [0.5, 0.5]): ev = combine_evoked([ev1, ev1], weights=weights) assert_allclose(ev.data, ev1.data) assert_equal(ev.nave, 2 * ev1.nave) ev = combine_evoked([ev1, -ev1], weights=weights) assert_allclose(ev.data, 0., atol=1e-20) assert_equal(ev.nave, 2 * ev1.nave) ev = combine_evoked([ev1, -ev1], weights='equal') assert_allclose(ev.data, 0., atol=1e-20) assert_equal(ev.nave, 2 * ev1.nave) ev = combine_evoked([ev1, -ev2], weights='equal') expected = int(round(1. / (0.25 / ev1.nave + 0.25 / ev2.nave))) assert_equal(expected, 27) # this is reasonable assert_equal(ev.nave, expected) # default comment behavior if evoked.comment is None old_comment1 = ev1.comment old_comment2 = ev2.comment ev1.comment = None ev = combine_evoked([ev1, -ev2], weights=[1, -1]) assert_equal(ev.comment.count('unknown'), 2) assert_true('-unknown' in ev.comment) assert_true(' + ' in ev.comment) ev1.comment = old_comment1 ev2.comment = old_comment2 # equal weighting ev = combine_evoked([ev1, ev2], weights='equal') assert_allclose(ev.data, np.zeros_like(ev1.data)) # combine_evoked([ev1, ev2], weights=[1, 0]) should yield the same as ev1 ev = combine_evoked([ev1, ev2], weights=[1, 0]) assert_equal(ev.nave, ev1.nave) assert_allclose(ev.data, ev1.data) # simple subtraction (like in oddball) ev = combine_evoked([ev1, ev2], weights=[1, -1]) assert_allclose(ev.data, 2 * np.ones_like(ev1.data)) assert_raises(ValueError, combine_evoked, [ev1, ev2], weights='foo') assert_raises(ValueError, combine_evoked, [ev1, ev2], weights=[1]) # grand average evoked1, evoked2 = read_evokeds(fname, condition=[0, 1], proj=True) ch_names = evoked1.ch_names[2:] evoked1.info['bads'] = ['EEG 008'] # test interpolation evoked1.drop_channels(evoked1.ch_names[:1]) evoked2.drop_channels(evoked2.ch_names[1:2]) gave = grand_average([evoked1, evoked2]) assert_equal(gave.data.shape, [len(ch_names), evoked1.data.shape[1]]) assert_equal(ch_names, gave.ch_names) assert_equal(gave.nave, 2) assert_raises(ValueError, grand_average, [1, evoked1])
def find_contrasts(names, stim_files, contrasts, suffix="_noblink", **params): contrast_labels = {} for i, key in enumerate(contrasts.keys()): contrast_labels[key] = 2**i evoked = collections.defaultdict(list) coverage = collections.defaultdict(list) counts = collections.defaultdict(list) for name, stim in zip(names, stim_files): events = pd.read_table(op.join(data_dir, "events", stim)) events['samples'] = np.floor(events.Tmu * 1e-6 * 512) events['dummy'] = 0 events['contrasts'] = 0 for i, vals in enumerate(contrasts.values()): events['contrasts'] += (2**i) * events['TriNo'].isin(vals) event_mat = events[['samples', 'dummy', 'contrasts']].as_matrix().astype('int_') raw = mne.io.read_raw_fif(op.join(temp_dir, name + suffix + ".fif")) picks = mne.pick_types(raw.info, eeg=True) epochs = mne.Epochs(raw, event_mat, event_id=contrast_labels, picks=picks, **params) # import pdb; pdb.set_trace() means = {} for key, val in contrast_labels.items(): evoked_dir = op.join(temp_dir, "evokeds") if not op.isdir(evoked_dir): os.mkdir(evoked_dir) evoked_file = op.join(evoked_dir, name + "_" + key + ".fif") if op.isfile(evoked_file): print evoked_file, "already generated, skipping..." means[key] = mne.Evoked(evoked_file, key) else: means[key] = epochs[key].average() means[key].comment = key means[key].save(evoked_file) N = (event_mat[:, 2] == contrast_labels[key]).sum() percent = (float(means[key].nave) / N) print "%2.1f%% of %s kept" % (100 * percent, key) coverage[key].append(percent) counts[key].append(N) for key, val in means.items(): evoked[key].append(val) keys = list(means.keys()) for i in range(len(keys) - 1): for j in range(i + 1, len(keys)): a = keys[i] b = keys[j] ab = mne.combine_evoked([evoked[a][-1], evoked[b][-1]], [1, -1]) print "Working on: ", a + ' - ' + b evoked[a + ' - ' + b].append(ab) grand_average = {} for key, val in evoked.items(): grand_average[key] = mne.grand_average(val) grand_average[key].comment = key return dict(ind=evoked, mean=grand_average, coverage=coverage, counts=counts)
# placeholder for results betas_evoked = dict() r2_evoked = dict() # ############################################################################### # 2) loop through subjects and extract betas for n_subj, subj in enumerate(subjects): subj_beta = betas[n_subj, :] subj_beta = subj_beta.reshape((n_channels, n_times)) betas_evoked[str(subj)] = EvokedArray(subj_beta, epochs_info, tmin) subj_r2 = r2[n_subj, :] subj_r2 = subj_r2.reshape((n_channels, n_times)) r2_evoked[str(subj)] = EvokedArray(subj_r2, epochs_info, tmin) effect_of_cue = grand_average([betas_evoked[str(subj)] for subj in subjects]) cue_r2 = grand_average([r2_evoked[str(subj)] for subj in subjects]) ############################################################################### # 3) Plot beta weights for the effect of condition # arguments fot the time-series maps ts_args = dict(gfp=False, time_unit='s', ylim=dict(eeg=[-6.5, 6.5]), xlim=[-.25, 2.5]) # times to plot ttp = [0.20, 0.35, 0.60, 0.70, 1.00, 1.25, 2.35] # arguments fot the topographical maps
vis_r = epochs["visual", "right"].average() all_evokeds = [aud_l, aud_r, vis_l, vis_r] print(all_evokeds) ############################################################################### # This can be simplified with a Python list comprehension: all_evokeds = [epochs[cond].average() for cond in sorted(event_id.keys())] print(all_evokeds) # Then, we construct and plot an unweighted average of left vs. right trials # this way, too: mne.combine_evoked(all_evokeds, weights=(0.25, -0.25, 0.25, -0.25)).plot_joint() ############################################################################### # Often, it makes sense to store Evoked objects in a dictionary or a list - # either different conditions, or different subjects. # If they are stored in a list, they can be easily averaged, for example, # for a grand average across subjects (or conditions). grand_average = mne.grand_average(all_evokeds) mne.write_evokeds("/tmp/tmp-ave.fif", all_evokeds) # If Evokeds objects are stored in a dictionary, they can be retrieved by name. all_evokeds = dict((cond, epochs[cond].average()) for cond in event_id) print(all_evokeds["left/auditory"]) # Besides for explicit access, this can be used for example to set titles. for cond in all_evokeds: all_evokeds[cond].plot_joint(title=cond)
def script_allsensors_heatmap_figures(): cleaned = True # ------------------------------------------------------------------ # # ---------------- GROUP ALLSENSORS HEATMAP FIGURES ---------------- # # ------------------------------------------------------------------ # # ================= PLOT THE HEATMAPS OF THE GROUP-AVERAGED DATA per CHANNEL - VIOL ================ # fig_path = op.join(config.fig_path, 'Evoked_and_GFP_plots', 'GROUP') utils.create_folder(fig_path) # evoked = evoked_all_standard[list(evoked_all_standard.keys())[0]][0] # first key (only one key when all sequences combined) # evoked = evoked[0] evokeds_name = 'items_viol_seq' evoked, _ = evoked_funcs.load_evoked(subject='all', filter_name=evokeds_name, filter_not='pos', cleaned=cleaned) # # Loop over the 2/3 ch_types plt.close('all') ch_types = ['grad', 'mag'] for ch_type in ch_types: fig, axes = plt.subplots(1, len(evoked.keys()), figsize=(len(evoked.keys()) * 4, 10), sharex=False, sharey=False, constrained_layout=True) fig.suptitle(ch_type, fontsize=12, weight='bold') ax = axes.ravel()[::1] # Loop over the different seq for x, condname in enumerate(evoked.keys()): # ---- Data all_evokeds = [evoked[condname][i][0] for i in range(len(evoked[condname]))] evokedcondmean = mne.grand_average(all_evokeds) if ch_type == 'eeg': data = evokedcondmean.copy().pick_types(eeg=True, meg=False).data data = data * 1e6 # scaling? clim = [-1.5, 1.5] elif ch_type == 'mag': data = evokedcondmean.copy().pick_types(eeg=False, meg='mag').data data = data * 1e15 # scaling? clim = [-70, 70] elif ch_type == 'grad': data = evokedcondmean.copy().pick_types(eeg=False, meg='grad').data data = data * 1e13 # scaling? clim = [-20, 20] minT = min(evokedcondmean.times) * 1000 maxT = max(evokedcondmean.times) * 1000 # ---- Plot im = ax[x].imshow(data, origin='upper', extent=[minT, maxT, data.shape[0], 0], aspect='auto', cmap='viridis', clim=clim) # cmap='RdBu_r' ax[x].axvline(0, linestyle='-', color='black', linewidth=1) # for xx in range(17): # ax[x].axvline(250 * xx, linestyle='--', color='black', linewidth=0.5) ax[x].set_xlabel('Time (ms)') ax[x].set_ylabel('Channels') ax[x].set_title(condname, loc='center', weight='normal') fig.colorbar(im, ax=ax, shrink=0.5, location='right') fig_name = fig_path + op.sep + (evokeds_name + ch_type + '.png') print('Saving ' + fig_name) plt.savefig(fig_name, dpi=300) plt.close(fig) # ================= PLOT THE HEATMAPS OF THE GROUP-AVERAGED DATA per CHANNEL - STAND ================ # fig_path = op.join(config.fig_path, 'Evoked_and_GFP_plots', 'GROUP') utils.create_folder(fig_path) # evoked = evoked_all_standard[list(evoked_all_standard.keys())[0]][0] # first key (only one key when all sequences combined) # evoked = evoked[0] evokeds_name = 'items_standard_seq' evoked, _ = evoked_funcs.load_evoked(subject='all', filter_name=evokeds_name, filter_not='pos', cleaned = cleaned) # # Loop over the 2/3 ch_types plt.close('all') for ch_type in config.ch_types: fig, axes = plt.subplots(1, len(evoked.keys()), figsize=(len(evoked.keys()) * 4, 10), sharex=False, sharey=False, constrained_layout=True) fig.suptitle(ch_type, fontsize=12, weight='bold') ax = axes.ravel()[::1] # Loop over the different seq for x, condname in enumerate(evoked.keys()): # ---- Data all_evokeds = [evoked[condname][i][0] for i in range(len(evoked[condname]))] evokedcondmean = mne.grand_average(all_evokeds) if ch_type == 'eeg': data = evokedcondmean.copy().pick_types(eeg=True, meg=False).data data = data * 1e6 # scaling? clim = [-1.5, 1.5] elif ch_type == 'mag': data = evokedcondmean.copy().pick_types(eeg=False, meg='mag').data data = data * 1e15 # scaling? clim = [-70, 70] elif ch_type == 'grad': data = evokedcondmean.copy().pick_types(eeg=False, meg='grad').data data = data * 1e13 # scaling? clim = [-20, 20] minT = min(evokedcondmean.times) * 1000 maxT = max(evokedcondmean.times) * 1000 # ---- Plot im = ax[x].imshow(data, origin='upper', extent=[minT, maxT, data.shape[0], 0], aspect='auto', cmap='viridis', clim=clim) # cmap='RdBu_r' ax[x].axvline(0, linestyle='-', color='black', linewidth=1) # for xx in range(17): # ax[x].axvline(250 * xx, linestyle='--', color='black', linewidth=0.5) ax[x].set_xlabel('Time (ms)') ax[x].set_ylabel('Channels') ax[x].set_title(condname, loc='center', weight='normal') fig.colorbar(im, ax=ax, shrink=0.5, location='right') fig_name = fig_path + op.sep + (evokeds_name + ch_type + '.png') print('Saving ' + fig_name) plt.savefig(fig_name, dpi=300) plt.close(fig)
pdat = [] data[cond] = [] for p in part: data[cond].append( read_tfrs( opj('/data/derivatives', p, 'eeg', p + '_task-fearcond_' + cond + '_avg-tfr.h5'))[0]) data[cond][-1].apply_baseline(mode='logratio', baseline=(-0.2, 0)) data[cond][-1].crop(tmin=0, tmax=1) pdat.append(np.float32(data[cond][-1].data)) anova_data.append(np.stack(pdat)) gavg[cond] = mne.grand_average(data[cond]) anova_data = np.stack(anova_data) # # Take difference of interest for each part diff_data = np.empty((2, ) + anova_data.shape[1:]) for s in range(anova_data.shape[1]): diff_data[0, s, ::] = (anova_data[0, s, :] - anova_data[1, s, :]) diff_data[1, s, ::] = (anova_data[2, s, :] - anova_data[3, s, :]) diff_data = np.squeeze(diff_data) # Always use chan x freq x time pvals = np.load(opj(outpath, 'cues4_tfr_anova_pvals.npy')) Fvals = np.load(opj(outpath, 'cues4_tfr_anova_Fvals.npy'))
X1, y1 = get_Xy_balanced(sleep_epochs, contrast1) clf = make_pipeline(Vectorizer(), StandardScaler(), LinearModel(LogisticRegression(max_iter = 4000))) #StandardScaler(), cv = StratifiedKFold(n_splits=2, shuffle=True) coef_folds = [] for train_idx, test_idx in cv.split(X1, y1): clf.fit(X1[train_idx], y=y1[train_idx]) #scores1.append(clf.score(X1[test_idx], y=y1[test_idx])) coef_folds.append(get_coef(clf, attr='patterns_', inverse_transform=True)) coef = np.asarray(coef_folds).mean(0).reshape([173, -1]) #mean folds and reshape evoked = EvokedArray(coef, sleep_epochs.info, tmin=tmin) evokeds.append(evoked) ga = mne.grand_average(evokeds) #SLEEP f = ga.plot_topomap([0., 0.2, 0.4, 0.6, 0.8], scalings=0.1, vmin=-2, vmax=2) #f = ga.plot_topomap([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8], scalings=0.1, vmin=-6, vmax=6) #f = ga.plot_topomap([0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5], scalings=0.1, vmin=-6, vmax=6) f.savefig(os.path.join(save_path, contrast1[0].split('/')[1] + 'coefs_new_detail.tiff')) #WAKE # f = ga.plot_topomap([0., 0.2, 0.4, 0.6, 0.8], scalings=0.1, vmin=-2, vmax=2) # f.savefig(os.path.join(save_path, 'wakecoefs_new.tiff') ) #plt.close()
# Collect betas betas[idx].append(res[regvar + '_z'].beta) betasnp.append(res[regvar + '_z'].beta.data) allbetasnp.append(np.stack(betasnp)) all_epos.append(epo) # Stack all data allbetas = np.stack(allbetasnp) all_epos = mne.concatenate_epochs(all_epos) # Grand average beta_gavg = [] for idx, regvar in enumerate(regvars): beta_gavg.append(mne.grand_average(betas[idx])) # _________________________________________________________________ # Second level test on betas # Find channel adjacency connect, names = mne.channels.find_ch_adjacency(epo.info, 'eeg') # Perform test for each regressor tvals, pvals = [], [] for idx, regvar in enumerate(regvars): # Reshape sub x time x vertices testdata = np.swapaxes(allbetas[:, idx, :, :], 2, 1) # TFCE tval, _, pval, _, = st_clust_1s_ttest(testdata, n_permutations=param['nperms'],
# Consider adding p_variance values to the plot. # Plot the topomap of the power spectral density across epochs. MNEstable_all[1].plot_psd_topomap(proj=False) # Category-wise MNEstable_all[0]['face'].plot_psd_topomap(proj=False) MNEstable_all[0]['scene'].plot_psd_topomap(proj=True) # Plot topomap (possibiity of adding specific times) MNEstable_all[0].average().plot_topomap(proj=True, times=np.linspace(0.05, 0.15, 5)) MNEstable_all[0].average().plot_topomap(proj=False) # Grand averaging MNEevoked_scene = mne.grand_average(MNEscene_all) MNEevoked_face = mne.grand_average(MNEface_all) #%% Stats ch_idx = [6, 7, 12, 13, 22] scene_5chs = np.zeros((22, 90)) dat_5chs = np.zeros((22, 5, 90)) for idx, evokedentry in enumerate(MNEscene_all): dat = evokedentry._data dat5chs = dat[ch_idx] dat_5chs[idx] = dat5chs dat5chs_mean = np.mean(dat5chs, axis=0) print(dat5chs_mean.shape) scene_5chs[idx] = dat5chs_mean
(-1.0, 10):(.4, 4), (-0.8, 10):(.4, 4), (-0.6, 10):(.4, 4)} visleftchans = ['PO3', 'PO7', 'O1'] visrightchans = ['PO4','PO8','O2'] motrightchans = ['C2', 'C4'] #channels contra to the left hand (space bar) motleftchans = ['C1', 'C3'] #channels contra to the right hand (mouse) topoargs = dict(outlines= 'head', contours = 0) topoargs_t = dict(outlines = 'head', contours = 0, vmin=-2, vmax = 2) #%% gave_cleft = mne.grand_average(data_baselined_t['cuedleft']) gave_cleft.data = toverparam(data_baselined_t['cuedleft']); gave_cleft.drop_channels(['RM']) gave_cleft.plot_joint(topomap_args = dict(outlines='head', contours=0, vmin=-4, vmax=4), timefreqs = timefreqs_cue, title = 'cued left '+ model+ ' t over tstats') cleft_vrchans = np.subtract(np.nanmean(deepcopy(gave_cleft).pick_channels(visrightchans).data,0),0) #np.nanmean(deepcopy(gave_cleft).pick_channels(visleftchans).data,0)) times, freqs = deepcopy(gave_cleft).crop(tmin=-2, tmax=0).times, gave_cleft.freqs - fig = plt.figure() ax = fig.add_subplot(111) #ax.pcolormesh(gave_dtlvsr.times, gave_dtlvsr.freqs, cvsi_dtlvsr, cmap = 'RdBu_r', antialiased = False, vmin = -2, vmax = 2) ax.imshow(cleft_vrchans, cmap = 'RdBu_r', aspect = 'auto', vmin = -2, vmax = 2, interpolation = 'none', origin = 'lower', extent = (np.min(times), np.max(times), np.min(freqs), np.max(freqs))) ax.vlines([0, -1.5], linestyle = 'dashed', lw = .75, ymin=1, ymax = 39) ax.set_ylim(1,39) ax.set_title('contra-ipsi to cued left')
def test_arithmetic(): """Test evoked arithmetic""" ev = read_evokeds(fname, condition=0) ev1 = EvokedArray(np.ones_like(ev.data), ev.info, ev.times[0], nave=20) ev2 = EvokedArray(-np.ones_like(ev.data), ev.info, ev.times[0], nave=10) # combine_evoked([ev1, ev2]) should be the same as ev1 + ev2: # data should be added according to their `nave` weights # nave = ev1.nave + ev2.nave with warnings.catch_warnings(record=True): # deprecation no weights ev = combine_evoked([ev1, ev2]) assert_equal(ev.nave, ev1.nave + ev2.nave) assert_allclose(ev.data, 1. / 3. * np.ones_like(ev.data)) # with same trial counts, a bunch of things should be equivalent for weights in ('nave', 'equal', [0.5, 0.5]): ev = combine_evoked([ev1, ev1], weights=weights) assert_allclose(ev.data, ev1.data) assert_equal(ev.nave, 2 * ev1.nave) ev = combine_evoked([ev1, -ev1], weights=weights) assert_allclose(ev.data, 0., atol=1e-20) assert_equal(ev.nave, 2 * ev1.nave) ev = combine_evoked([ev1, -ev1], weights='equal') assert_allclose(ev.data, 0., atol=1e-20) assert_equal(ev.nave, 2 * ev1.nave) ev = combine_evoked([ev1, -ev2], weights='equal') expected = int(round(1. / (0.25 / ev1.nave + 0.25 / ev2.nave))) assert_equal(expected, 27) # this is reasonable assert_equal(ev.nave, expected) # default comment behavior if evoked.comment is None old_comment1 = ev1.comment old_comment2 = ev2.comment ev1.comment = None ev = combine_evoked([ev1, -ev2], weights=[1, -1]) assert_equal(ev.comment.count('unknown'), 2) assert_true('-unknown' in ev.comment) assert_true(' + ' in ev.comment) ev1.comment = old_comment1 ev2.comment = old_comment2 # equal weighting ev = combine_evoked([ev1, ev2], weights='equal') assert_allclose(ev.data, np.zeros_like(ev1.data)) # combine_evoked([ev1, ev2], weights=[1, 0]) should yield the same as ev1 ev = combine_evoked([ev1, ev2], weights=[1, 0]) assert_equal(ev.nave, ev1.nave) assert_allclose(ev.data, ev1.data) # simple subtraction (like in oddball) ev = combine_evoked([ev1, ev2], weights=[1, -1]) assert_allclose(ev.data, 2 * np.ones_like(ev1.data)) assert_raises(ValueError, combine_evoked, [ev1, ev2], weights='foo') assert_raises(ValueError, combine_evoked, [ev1, ev2], weights=[1]) # grand average evoked1, evoked2 = read_evokeds(fname, condition=[0, 1], proj=True) ch_names = evoked1.ch_names[2:] evoked1.info['bads'] = ['EEG 008'] # test interpolation evoked1.drop_channels(evoked1.ch_names[:1]) evoked2.drop_channels(evoked2.ch_names[1:2]) gave = grand_average([evoked1, evoked2]) assert_equal(gave.data.shape, [len(ch_names), evoked1.data.shape[1]]) assert_equal(ch_names, gave.ch_names) assert_equal(gave.nave, 2) assert_raises(ValueError, grand_average, [1, evoked1])
timefreqs_alpha ={(.4, 10):(.4, 4), (.6, 10):(.4, 4), (.8, 10):(.4, 4), (1., 10):(.4, 4)} visleftchans = ['PO4','PO8','O2'] visrightchans = ['PO3', 'PO7', 'O1'] #%% Question 1 -- Do cues facilitate selection of an item from WM in our task? #analysis needed: # -- cued vs neutral (i.e. do visual responses look different in the two?) # -- cued left vs cued right (do things lateralise in a way that suggests selection) #just plot the average for all neutral trials first gave_neutral = mne.grand_average(alldata_neutral_baselined); gave_neutral.drop_channels(['RM']) gave_neutral.plot_joint(title = 'evoked response to neutral cues, average betas', timefreqs = timefreqs_alpha, topomap_args = dict(outlines = 'head', contours = 0)) gave_neutral = mne.grand_average(alldata_neutral_baselined); gave_neutral.data = toverparam(alldata_neutral_baselined); gave_neutral.drop_channels(['RM']) gave_neutral.plot_joint(title = 'evoked response to neutral cues, t over betas', timefreqs = timefreqs_alpha, topomap_args = dict(outlines = 'head', contours = 0), vmin = -2, vmax = 2) gave_neutral_t = mne.grand_average(alldata_neutral_baselined_t); gave_neutral_t.drop_channels(['RM']) gave_neutral_t.plot_joint(title = 'evoked response to neutral cues, average tstats', timefreqs = timefreqs_alpha, topomap_args = dict(outlines = 'head', contours = 0))
'_arraylocked_tfr_prevtrialconfdiffoverconf_tstats_baselined-tfr.h5' ))[0]) #%% #---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- #visualise some regressors #we're not going to plot the grandmean, this shouldn't be in the glm as it doesnt make sense anyways. #we will visualise the regressors for neutral and cued trials though as these should be different (no lateralisation in in neutral trials) timefreqs = { (-.2, 10): (.2, 4), (.4, 10): (.4, 4), (.6, 10): (.4, 4), (.8, 10): (.4, 4) } gave_gmean = mne.grand_average(alldata_grandmean) gave_gmean.drop_channels(['RM']) gave_gmean.plot_joint( title='all trials response to array, average betas, preglm baseline', timefreqs=timefreqs, topomap_args=dict(outlines='head', contours=0)) plot_singlesubs = False from copy import deepcopy if plot_singlesubs: for i in range(len(alldata_grandmean)): tmp = deepcopy(alldata_grandmean[i]) tmp = tmp.drop_channels(['RM']) tmp.plot_joint(title='ave response, betas, subject %d' % subs[i], timefreqs=timefreqs, topomap_args=dict(outlines='head', contours=0))