def test_epochs_vector_inverse(): """Test vector inverse consistency between evoked and epochs.""" raw = read_raw_fif(fname_raw) events = find_events(raw, stim_channel='STI 014')[:2] reject = dict(grad=2000e-13, mag=4e-12, eog=150e-6) epochs = Epochs(raw, events, None, 0, 0.01, baseline=None, reject=reject, preload=True) assert_equal(len(epochs), 2) evoked = epochs.average(picks=range(len(epochs.ch_names))) inv = read_inverse_operator(fname_inv) method = "MNE" snr = 3. lambda2 = 1. / snr ** 2 stcs_epo = apply_inverse_epochs(epochs, inv, lambda2, method=method, pick_ori='vector', return_generator=False) stc_epo = np.mean(stcs_epo) stc_evo = apply_inverse(evoked, inv, lambda2, method=method, pick_ori='vector') assert_allclose(stc_epo.data, stc_evo.data, rtol=1e-9, atol=0)
def apply_STC_epo(fnepo, event, method='MNE', snr=1.0, min_subject='fsaverage', subjects_dir=None): from mne import morph_data from mne.minimum_norm import read_inverse_operator, apply_inverse_epochs fnlist = get_files_from_list(fnepo) # loop across all filenames for fname in fnlist: fn_path = os.path.split(fname)[0] name = os.path.basename(fname) subject = name.split('_')[0] min_dir = subjects_dir + '/%s' %min_subject snr = snr lambda2 = 1.0 / snr ** 2 stcs_path = min_dir + '/stcs/%s/%s/' % (subject,event) reset_directory(stcs_path) # fn_inv = fname[:fname.rfind('-ave.fif')] + ',ave-inv.fif' fn_inv = fn_path + '/%s_epo-inv.fif' %subject # noise_cov = mne.read_cov(fn_cov) epo = mne.read_epochs(fname) epo.pick_types(meg=True, ref_meg=False) inv = read_inverse_operator(fn_inv) stcs = apply_inverse_epochs(epo, inv, lambda2, method, pick_ori='normal') s = 0 while s < len(stcs): stc_morph = morph_data(subject, min_subject, stcs[s]) stc_morph.save(stcs_path + '/trial%s_fsaverage' % (str(s)), ftype='stc') s = s + 1
def _create_stcs(params): subject, events_id, epochs, evoked, inv, inverse_method, baseline, apply_for_epochs, apply_SSP_projection_vectors, add_eeg_ref = params snr = 3.0 lambda2 = 1.0 / snr ** 2 local_inv_file_name = op.join(LOCAL_ROOT_DIR, 'inv', '{}_ecr_nTSSS_conflict-inv.fif'.format(subject)) if inv is None and os.path.isfile(local_inv_file_name): inv = read_inverse_operator(local_inv_file_name) if inv is None: return print([s['vertno'] for s in inv['src']]) for cond_name in events_id.keys(): if not apply_for_epochs: local_stc_file_name = op.join(LOCAL_ROOT_DIR, 'stc', '{}_{}_{}'.format(subject, cond_name, inverse_method)) if os.path.isfile('{}-lh.stc'.format(local_stc_file_name)): print('stc was already calculated for {}'.format(subject)) else: if evoked is None: evoked_cond = mne.read_evokeds(op.join(LOCAL_ROOT_DIR, 'evo', '{}_ecr_{}-ave.fif'.format(subject, cond_name))) else: evoked_cond = evoked[cond_name] stcs = apply_inverse(evoked_cond, inv, lambda2, inverse_method, pick_ori=None) stcs.save(local_stc_file_name, ftype='h5') else: local_stc_template = op.join(LOCAL_ROOT_DIR, 'stc_epochs', '{}_{}_{}_{}'.format(subject, cond_name, '{epoch_ind}', inverse_method)) if len(glob.glob(local_stc_template.format(epoch_ind='*') == 36)): print('stc was already calculated for {}'.format(subject)) else: stcs = apply_inverse_epochs(epochs[cond_name], inv, lambda2, inverse_method, pick_ori=None, return_generator=True) for epoch_ind, stc in enumerate(stcs): if not os.path.isfile(local_stc_template.format(epoch_ind=epoch_ind)): stc.save(local_stc_template.format(epoch_ind=epoch_ind), ftype='h5')
def single_epoch_tf_source(epochs, inv, src, label): """Calculates single trail power Parameter --------- epochs : ??? The subject number to use. inv = inverse_operator ... src : source space ... label : label ... """ snr = 1.0 lambda2 = 1.0 / snr ** 2 method = "dSPM" # use dSPM method (could also be MNE or sLORETA) frequencies = np.arange(8, 13, 1) stcs = apply_inverse_epochs(epochs, inv, lambda2=lambda2, method=method, label=None, pick_ori=None) time_series = [stc.extract_label_time_course(labels=label, src=src, mode="pca_flip")[0] for stc in stcs] ts_signed = [] for j in range(len(time_series)): tmp = time_series[j] tmp *= np.sign(tmp[np.argmax(np.abs(tmp))]) ts_signed.append(tmp) tfr = cwt_morlet(np.asarray(ts_signed), epochs.info["sfreq"], frequencies, use_fft=True, n_cycles=4) return tfr
def fiff_mne(ds, fwd='{fif}*fwd.fif', cov='{fif}*cov.fif', label=None, name=None, tstart= -0.1, tstop=0.6, baseline=(None, 0)): """ adds data from one label as """ if name is None: if label: _, lbl = os.path.split(label) lbl, _ = os.path.splitext(lbl) name = lbl.replace('-', '_') else: name = 'stc' info = ds.info['info'] raw = ds.info['raw'] fif_name = raw.info['filename'] fif_name, _ = os.path.splitext(fif_name) if fif_name.endswith('raw'): fif_name = fif_name[:-3] fwd = fwd.format(fif=fif_name) if '*' in fwd: d, n = os.path.split(fwd) names = fnmatch.filter(os.listdir(d), n) if len(names) == 1: fwd = os.path.join(d, names[0]) else: raise IOError("No unique fwd file matching %r" % fwd) cov = cov.format(fif=fif_name) if '*' in cov: d, n = os.path.split(cov) names = fnmatch.filter(os.listdir(d), n) if len(names) == 1: cov = os.path.join(d, names[0]) else: raise IOError("No unique cov file matching %r" % cov) fwd = mne.read_forward_solution(fwd, force_fixed=False, surf_ori=True) cov = mne.Covariance(cov) inv = _mn.make_inverse_operator(info, fwd, cov, loose=0.2, depth=0.8) epochs = mne_Epochs(ds, tstart=tstart, tstop=tstop, baseline=baseline) # mne example: snr = 3.0 lambda2 = 1.0 / snr ** 2 if label is not None: label = mne.read_label(label) stcs = _mn.apply_inverse_epochs(epochs, inv, lambda2, dSPM=False, label=label) x = np.vstack(s.data.mean(0) for s in stcs) s = stcs[0] dims = ('case', var(s.times, 'time'),) ds[name] = ndvar(x, dims, properties=None, info='') return stcs
def test_epochs_vector_inverse(): """Test vector inverse consistency between evoked and epochs.""" raw = read_raw_fif(fname_raw) events = find_events(raw, stim_channel='STI 014')[:2] reject = dict(grad=2000e-13, mag=4e-12, eog=150e-6) epochs = Epochs(raw, events, None, 0, 0.01, baseline=None, reject=reject, preload=True) assert_equal(len(epochs), 2) evoked = epochs.average(picks=range(len(epochs.ch_names))) inv = read_inverse_operator(fname_inv) method = "MNE" snr = 3. lambda2 = 1. / snr**2 stcs_epo = apply_inverse_epochs(epochs, inv, lambda2, method=method, pick_ori='vector', return_generator=False) stc_epo = np.mean(stcs_epo) stc_evo = apply_inverse(evoked, inv, lambda2, method=method, pick_ori='vector') assert_allclose(stc_epo.data, stc_evo.data, rtol=1e-9, atol=0)
inv_method = 'dSPM' # sLORETA, MNE, dSPM parc = 'aparc' # the parcellation to use, e.g., 'aparc' 'aparc.a2009s' lambda2 = 1.0 / snr**2 # Compute inverse operator inverse_operator = make_inverse_operator(raw.info, fwd, noise_cov, loose=None, depth=None, fixed=False) stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, inv_method, pick_ori=None, return_generator=True) # Get labels for FreeSurfer 'aparc' cortical parcellation with 34 labels/hemi labels_parc = mne.read_labels_from_annot(subject, parc=parc, subjects_dir=subjects_dir) # Average the source estimates within each label of the cortical parcellation # and each sub structures contained in the src space # If mode = 'mean_flip' this option is used only for the cortical label src = inverse_operator['src'] label_ts = mne.extract_label_time_course(stcs, labels_parc, src,
eog=150e-6)) # Compute inverse solution and for each epoch snr = 1.0 # use smaller SNR for raw data inv_method = 'dSPM' # sLORETA, MNE, dSPM parc = 'aparc' # the parcellation to use, e.g., 'aparc' 'aparc.a2009s' lambda2 = 1.0 / snr ** 2 # Compute inverse operator inverse_operator = make_inverse_operator(raw.info, fwd, noise_cov, loose=None, depth=None, fixed=False) stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, inv_method, pick_ori=None, return_generator=True) # Get labels for FreeSurfer 'aparc' cortical parcellation with 34 labels/hemi labels_parc = mne.read_labels_from_annot(subject, parc=parc, subjects_dir=subjects_dir) # Average the source estimates within each label of the cortical parcellation # and each sub structures contained in the src space # If mode = 'mean_flip' this option is used only for the cortical label src = inverse_operator['src'] label_ts = mne.extract_label_time_course(stcs, labels_parc, src, mode='mean_flip', allow_empty=True, return_generator=False) # We compute the connectivity in the alpha band and plot it using a circular
src = mne.read_source_spaces(src) fwd = mne.make_forward_solution(epochs.info, trans, src, bem) inv = make_inverse_operator(epochs.info, fwd, cov) del fwd, src ############################################################################## # Compute label time series and do envelope correlation # ----------------------------------------------------- labels = mne.read_labels_from_annot(subject, 'aparc_sub', subjects_dir=subjects_dir) epochs.apply_hilbert() # faster to apply in sensor space stcs = apply_inverse_epochs(epochs, inv, lambda2=1. / 9., pick_ori='normal', return_generator=True) label_ts = mne.extract_label_time_course(stcs, labels, inv['src'], return_generator=True) corr = envelope_correlation(label_ts, verbose=True) # let's plot this matrix fig, ax = plt.subplots(figsize=(4, 4)) ax.imshow(corr, cmap='viridis', clim=np.percentile(corr, [5, 95])) fig.tight_layout() ############################################################################## # Compute the degree and plot it
# Set up pick list: (MEG minus bad channels) include = [] exclude = raw.info['bads'] picks = fiff.pick_types(raw.info, meg=True, eeg=False, stim=False, eog=True, include=include, exclude=exclude) # Read epochs and remove bad epochs epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=(None, 0), preload=True, reject=dict(grad=4000e-13, mag=4e-12, eog=150e-6)) # Pull data for averaging later epc_array = epochs.get_data() # Compute the inverse solution inv = apply_inverse_epochs(epochs, inverse_operator, lambda2, method, label=label) #Need to add a line here to automatically create stc directory within subj # epoch_num = 1 epoch_num_str = str(epoch_num) for i in inv: i.save(data_path + subj + '/tmp/' + label_name[3:] + '_rest_raw_sss-oct-6-inv' + epoch_num_str) epoch_num = epoch_num + 1 epoch_num_str = str(epoch_num) # The following is used to remove the empty opposing hemisphere files # and then move the files to save into the appropriate directory if hemi == 'left':
#======================================================================= # inverse operator inverse_operator = make_inverse_operator(hcp_epochs.info, fwd, noise_cov=noise_cov, loose=0.2, depth=0.8) method = 'MNE' snr = 1. lambda2 = 1. / snr**2 src = inverse_operator['src'] stcs_list = [ apply_inverse_epochs(hcp_epochs[sti], inverse_operator, lambda2, method, label=None, pick_ori="normal", return_generator=True) for sti in hcp_epochs.event_id ] sti_names = [sti for sti in hcp_epochs.event_id] labels = mne.read_labels_from_annot(subject, parc='aparc', subjects_dir=subjects_dir) label_names = [label.name for label in labels] label_ts_list = [ mne.extract_label_time_course(stcs_list[ite], labels, src, mode='mean_flip',
exclude='bads') # Read epochs epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=(None, 0), preload=True, reject=dict(grad=4000e-13, eog=150e-6), decim=5) # decimate to save memory and increase speed # %% # Compute inverse solution snr = 3.0 noise_cov = mne.read_cov(fname_cov) inverse_operator = read_inverse_operator(fname_inv) stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2=1.0 / snr ** 2, verbose=False, method="dSPM", pick_ori="normal") # %% # Decoding in sensor space using a logistic regression # Retrieve source space data into an array X = np.array([stc.lh_data for stc in stcs]) # only keep left hemisphere y = epochs.events[:, 2] # prepare a series of classifier applied at each time sample clf = make_pipeline(StandardScaler(), # z-score normalization SelectKBest(f_classif, k=500), # select features for speed LinearModel(LogisticRegression(C=1, solver='liblinear'))) time_decod = SlidingEstimator(clf, scoring='roc_auc')
# Pick MEG channels picks = mne.pick_types(raw.info, meg=True, eeg=False, stim=False, eog=True, exclude='bads') # Define epochs for left-auditory condition event_id, tmin, tmax = 1, -0.2, 0.5 epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=dict(mag=4e-12, grad=4000e-13, eog=150e-6)) # Compute inverse solution and for each epoch. By using "return_generator=True" # stcs will be a generator object instead of a list. snr = 1.0 # use lower SNR for single epochs lambda2 = 1.0 / snr ** 2 method = "dSPM" # use dSPM method (could also be MNE or sLORETA) stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, method, pick_ori="normal", return_generator=True) # Read some labels names = ['Aud-lh', 'Aud-rh', 'Vis-lh', 'Vis-rh'] labels = [mne.read_label(data_path + '/MEG/sample/labels/%s.label' % name) for name in names] # Average the source estimates within each label using sign-flips to reduce # signal cancellations, also here we return a generator src = inverse_operator['src'] label_ts = mne.extract_label_time_course(stcs, labels, src, mode='mean_flip', return_generator=True) fmin, fmax = 7.5, 40. sfreq = raw.info['sfreq'] # the sampling frequency
def SN_functional_connectivity_bands_runs(i, method, SN_ROI): s = time.time() meg = subjects[i] sub_to = MRI_sub[i][1:15] stc_F_file_name = os.path.expanduser( '~' ) + '/old_semnet/my_semnet/json_files/connectivity/stc_' + method + '200_F_bands_SD_sub' + str( i) + '.json' stc_O_file_name = os.path.expanduser( '~' ) + '/old_semnet/my_semnet/json_files/connectivity/stc_' + method + '200_O_bands_LD_sub' + str( i) + '.json' stc_M_file_name = os.path.expanduser( '~' ) + '/old_semnet/my_semnet/json_files/connectivity/stc_' + method + '200_M_bands_SD_sub' + str( i) + '.json' stc_SD_file_name = os.path.expanduser( '~' ) + '/old_semnet/my_semnet/json_files/connectivity/stc_' + method + '200_mean_bands_SD_sub' + str( i) + '.json' stc_LD_file_name = os.path.expanduser( '~' ) + '/old_semnet/my_semnet/json_files/connectivity/stc_' + method + '200_mean_bands_LD_sub' + str( i) + '.json' morphed_labels = mne.morph_labels(SN_ROI, subject_to=sub_to, subject_from='fsaverage', subjects_dir=data_path) # Reading epochs # Reading epochs epo_name_LD = data_path + meg + 'block_LD_words_epochs-epo.fif' epochs_ld = mne.read_epochs(epo_name_LD, preload=True) epochs_LD = epochs_ld['words'].copy().resample(500) epoch_fname_fruit = data_path + meg + 'block_fruit_epochs-epo.fif' epoch_fname_odour = data_path + meg + 'block_odour_epochs-epo.fif' epoch_fname_milk = data_path + meg + 'block_milk_epochs-epo.fif' epochs_fruit = mne.read_epochs(epoch_fname_fruit, preload=True) epochs_odour = mne.read_epochs(epoch_fname_odour, preload=True) epochs_milk = mne.read_epochs(epoch_fname_milk, preload=True) epochs_f = mne.epochs.combine_event_ids( epochs_fruit, ['visual', 'hear', 'hand', 'neutral', 'emotional'], {'words': 15}) epochs_o = mne.epochs.combine_event_ids( epochs_odour, ['visual', 'hear', 'hand', 'neutral', 'emotional'], {'words': 15}) epochs_m = mne.epochs.combine_event_ids( epochs_milk, ['visual', 'hear', 'hand', 'neutral', 'emotional'], {'words': 15}) epochs_f = epochs_f['words'].copy().resample(500) epochs_o = epochs_o['words'].copy().resample(500) epochs_m = epochs_m['words'].copy().resample(500) # Reading inverse operator inv_fname_SD = data_path + meg + 'InvOp_SD_EMEG-inv.fif' inv_fname_LD = data_path + meg + 'InvOp_LD_EMEG-inv.fif' inv_op_SD = read_inverse_operator(inv_fname_SD) inv_op_LD = read_inverse_operator(inv_fname_LD) stc_f = apply_inverse_epochs(epochs_f, inv_op_SD, lambda2, method='MNE', pick_ori="normal", return_generator=False) stc_o = apply_inverse_epochs(epochs_o, inv_op_SD, lambda2, method='MNE', pick_ori="normal", return_generator=False) stc_m = apply_inverse_epochs(epochs_m, inv_op_SD, lambda2, method='MNE', pick_ori="normal", return_generator=False) stc_ld = apply_inverse_epochs(epochs_LD, inv_op_LD, lambda2, method='MNE', pick_ori="normal", return_generator=False) src_SD = inv_op_SD['src'] src_LD = inv_op_LD['src'] # Construct indices to estimate connectivity between the label time course # and all source space time courses vertices_SD = [src_SD[j]['vertno'] for j in range(2)] n_signals_tot = 1 + len(vertices_SD[0]) + len(vertices_SD[1]) indices = seed_target_indices([0], np.arange(1, n_signals_tot)) morph_SD = mne.compute_source_morph(src=inv_op_SD['src'], subject_from=sub_to, subject_to=C.subject_to, spacing=C.spacing_morph, subjects_dir=C.data_path) morph_LD = mne.compute_source_morph(src=inv_op_LD['src'], subject_from=sub_to, subject_to=C.subject_to, spacing=C.spacing_morph, subjects_dir=C.data_path) for win in np.arange(0, len(C.con_time_window) - 1): print('[i,win]: ', i, win) t_min = C.con_time_window[win] t_max = C.con_time_window[win + 1] stc_F = [] stc_O = [] stc_M = [] stc_LD = [] for n in np.arange(0, len(stc_f)): stc_F.append(stc_f[n].copy().crop(t_min * 1e-3, t_max * 1e-3)) for n in np.arange(0, len(stc_o)): stc_O.append(stc_o[n].copy().crop(t_min * 1e-3, t_max * 1e-3)) for n in np.arange(0, len(stc_m)): stc_M.append(stc_m[n].copy().crop(t_min * 1e-3, t_max * 1e-3)) for n in np.arange(0, len(stc_ld)): stc_LD.append(stc_ld[n].copy().crop(t_min * 1e-3, t_max * 1e-3)) for k in np.arange(0, 6): print('[i,win,k]: ', i, win, k) morphed_labels[k].name = C.rois_labels[k] seed_ts_f = mne.extract_label_time_course(stc_F, morphed_labels[k], src_SD, mode='mean_flip', return_generator=False) seed_ts_o = mne.extract_label_time_course(stc_O, morphed_labels[k], src_SD, mode='mean_flip', return_generator=False) seed_ts_m = mne.extract_label_time_course(stc_M, morphed_labels[k], src_SD, mode='mean_flip', return_generator=False) seed_ts_ld = mne.extract_label_time_course(stc_LD, morphed_labels[k], src_LD, mode='mean_flip', return_generator=False) for f in np.arange(0, len(C.con_freq_band) - 1): print('[i,win,k,f]: ', i, win, k, f) f_min = C.con_freq_band[f] f_max = C.con_freq_band[f + 1] print(f_min, f_max) comb_ts_f = zip(seed_ts_f, stc_F) comb_ts_o = zip(seed_ts_o, stc_O) comb_ts_m = zip(seed_ts_m, stc_M) comb_ts_ld = zip(seed_ts_ld, stc_LD) con_F, freqs, times, n_epochs, n_tapers = spectral_connectivity( comb_ts_f, method=method, mode='fourier', indices=indices, sfreq=500, fmin=f_min, fmax=f_max, faverage=True, n_jobs=10) con_O, freqs, times, n_epochs, n_tapers = spectral_connectivity( comb_ts_o, method=method, mode='fourier', indices=indices, sfreq=500, fmin=f_min, fmax=f_max, faverage=True, n_jobs=10) con_M, freqs, times, n_epochs, n_tapers = spectral_connectivity( comb_ts_m, method=method, mode='fourier', indices=indices, sfreq=500, fmin=f_min, fmax=f_max, faverage=True, n_jobs=10) con_LD, freqs, times, n_epochs, n_tapers = spectral_connectivity( comb_ts_ld, method=method, mode='fourier', indices=indices, sfreq=500, fmin=f_min, fmax=f_max, faverage=True, n_jobs=10) con_SD = (con_F + con_O + con_M) / 3 con_stc_F = mne.SourceEstimate(con_F, vertices=vertices_SD, tmin=t_min * 1e-3, tstep=2e-3, subject=sub_to) con_stc_O = mne.SourceEstimate(con_O, vertices=vertices_SD, tmin=t_min * 1e-3, tstep=2e-3, subject=sub_to) con_stc_M = mne.SourceEstimate(con_M, vertices=vertices_SD, tmin=t_min * 1e-3, tstep=2e-3, subject=sub_to) con_stc_SD = mne.SourceEstimate(con_SD, vertices=vertices_SD, tmin=t_min * 1e-3, tstep=2e-3, subject=sub_to) con_stc_LD = mne.SourceEstimate(con_LD, vertices=vertices_SD, tmin=t_min * 1e-3, tstep=2e-3, subject=sub_to) stc_total_F[win][k][f] = morph_SD.apply(con_stc_F) stc_total_O[win][k][f] = morph_SD.apply(con_stc_O) stc_total_M[win][k][f] = morph_SD.apply(con_stc_M) stc_total_SD[win][k][f] = morph_SD.apply(con_stc_SD) stc_total_LD[win][k][f] = morph_LD.apply(con_stc_LD) # with open(stc_F_file_name, "wb") as fp: #Pickling # pickle.dump(stc_total_F, fp) # with open(stc_O_file_name, "wb") as fp: #Pickling # pickle.dump(stc_total_O, fp) # with open(stc_M_file_name, "wb") as fp: #Pickling # pickle.dump(stc_total_M, fp) # with open(stc_SD_file_name, "wb") as fp: #Pickling # pickle.dump(stc_total_SD, fp) with open(stc_LD_file_name, "wb") as fp: # Pickling pickle.dump(stc_total_LD, fp) e = time.time() print(e - s)
events = mne.make_fixed_length_events(raw, 1, start=0, stop=None, duration=4.) # Set up pick list: (MEG minus bad channels) include = [] exclude = raw.info['bads'] picks = fiff.pick_types(raw.info, meg=True, eeg=False, stim=False, eog=True, include=include, exclude=exclude) # Read epochs and remove bad epochs epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=(None, 0), preload=True, reject=dict(grad=4000e-13, mag=4e-12, eog=150e-6)) # Compute the inverse solution stc = apply_inverse_epochs(epochs, inverse_operator, lambda2, method) #stc.save(data_path + subj + '/' + subj + '_rest_raw_sss-oct-6-inv.fif') # define frequencies of interest fmin, fmax = 0., 70. bandwidth = 4. # bandwidth of the windows in Hz # compute source space psd in label # Note: By using "return_generator=True" stcs will be a generator object # instead of a list. This allows us so to iterate without having to # keep everything in memory. stcs = compute_source_psd_epochs(epochs, inverse_operator, lambda2=lambda2, method=method, fmin=fmin, fmax=fmax, bandwidth=bandwidth, label=label, return_generator=True)
def intra(subj): """ Performs initial computations within subject and returns average PSD and variance of all epochs. """ print ("Now beginning intra processing on " + subj + "...\n") * 5 # Set function parameters fname_label = subjects_dir + "/" + subj + "/" + "label/%s.label" % label_name fname_raw = data_path + subj + "/" + subj + "_list" + list_num + "_raw_sss-ico-4-fwd.fif" if os.path.isfile(data_path + subj + "/" + subj + "_list" + list_num + "_raw_sss-ico-4-fwd.fif"): fname_fwd = data_path + subj + "/" + subj + "_list" + list_num + "_raw_sss-ico-4-fwd.fif" else: print ("Subject " + subj + " does not have a ico-4-fwd.fif on file.") if label_name.startswith("lh."): hemi = "left" elif label_name.startswith("rh."): hemi = "right" # Load data label = mne.read_label(fname_label) raw = fiff.Raw(fname_raw) forward_meg = mne.read_forward_solution(fname_fwd) # Estimate noise covariance from the raw data. precov = mne.compute_raw_data_covariance(raw, reject=dict(eog=150e-6)) write_cov(data_path + subj + "/" + subj + "-cov.fif", precov) # Find events from raw file events = mne.find_events(raw, stim_channel="STI 014") # Set up pick list: (MEG minus bad channels) include = [] exclude = raw.info["bads"] picks = fiff.pick_types(raw.info, meg=True, eeg=False, stim=False, eog=True, include=include, exclude=exclude) # Read epochs and remove bad epochs epochs = mne.Epochs( raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=(None, 0), preload=True, reject=dict(grad=4000e-13, mag=4e-12, eog=150e-6), ) # Average epochs and get an evoked dataset. Save to disk. evoked = epochs.average() evoked.save(data_path + subj + "/" + subj + "_list" + list_num + "_rest_raw_sss-ave.fif") # Regularize noise cov cov = mne.cov.regularize(precov, evoked.info, grad=4000e-13, mag=4e-12, eog=150e-6, proj=True) # Make inverse operator info = evoked.info inverse_operator = make_inverse_operator(info, forward_meg, cov, loose=None, depth=0.8) # Pull data for averaging later epc_array = epochs.get_data() # Compute the inverse solution inv = apply_inverse_epochs(epochs, inverse_operator, lambda2, method, label=label) # Need to add a line here to automatically create stc directory within subj epoch_num = 1 epoch_num_str = str(epoch_num) for i in inv: i.save(data_path + subj + "/tmp/" + label_name[3:] + "_rest_raw_sss-ico-4-inv" + epoch_num_str) epoch_num = epoch_num + 1 epoch_num_str = str(epoch_num) # The following is used to remove the empty opposing hemisphere files # and then move the files to save into the appropriate directory if hemi == "left": filelist = [f for f in os.listdir(data_path + subj + "/tmp") if f.endswith("-rh.stc")] for f in filelist: os.remove(data_path + subj + "/tmp/" + f) keepers = [f for f in os.listdir(data_path + subj + "/tmp") if f.endswith("-lh.stc")] for f in keepers: src = f os.rename(data_path + subj + "/tmp/" + src, data_path + subj + "/inv/" + src) elif hemi == "right": filelist = [f for f in os.listdir(data_path + subj + "/tmp") if f.endswith("-lh.stc")] for f in filelist: os.remove(data_path + subj + "/tmp/" + f) keepers = [f for f in os.listdir(data_path + subj + "/tmp") if f.endswith("-rh.stc")] for f in keepers: src = f os.rename(data_path + subj + "/tmp/" + src, data_path + subj + "/inv/" + src) # define frequencies of interest bandwidth = 4.0 # bandwidth of the windows in Hz # compute source space psd in label # Note: By using "return_generator=True" stcs will be a generator object # instead of a list. This allows us so to iterate without having to # keep everything in memory. psd = compute_source_psd_epochs( epochs, inverse_operator, lambda2=lambda2, method=method, fmin=fmin, fmax=fmax, bandwidth=bandwidth, label=label, return_generator=False, ) epoch_num = 1 epoch_num_str = str(epoch_num) for i in psd: i.save(data_path + subj + "/" + "tmp" + "/" + label_name[3:] + "_dspm_snr-1_PSD" + epoch_num_str) epoch_num = epoch_num + 1 epoch_num_str = str(epoch_num) if hemi == "left": filelist = [f for f in os.listdir(data_path + subj + "/tmp") if f.endswith("-rh.stc")] for f in filelist: os.remove(data_path + subj + "/tmp/" + f) keepers = [f for f in os.listdir(data_path + subj + "/tmp") if f.endswith("-lh.stc")] for f in keepers: src = f os.rename(data_path + subj + "/tmp/" + src, data_path + subj + "/psd/" + src) elif hemi == "right": filelist = [f for f in os.listdir(data_path + subj + "/tmp") if f.endswith("-lh.stc")] for f in filelist: os.remove(data_path + subj + "/tmp/" + f) keepers = [f for f in os.listdir(data_path + subj + "/tmp") if f.endswith("-rh.stc")] for f in keepers: src = f os.rename(data_path + subj + "/tmp/" + src, data_path + subj + "/psd/" + src) # This code computes the average PSDs of each epoch. Each PSD file is an array of shape N_vertices*N_frequencies. This code averages the PSD value of each vertex together and outputs the average PSD value of each frequency. Then, it averages the PSD values of each epoch, outputting one average PSD value per frequency value, i.e., this is the average across epochs. n_epochs = len(epc_array) for i, stc in enumerate(psd): if i >= n_epochs: break if i == 0: psd_avg = np.mean(stc.data, axis=0) else: psd_avg += np.mean(stc.data, axis=0) print ("Length of psd for subject " + subj + " is " + str(len(psd)) + ".") print ("Number of epochs for subject " + subj + " is " + str(n_epochs) + ".") if len(psd) != 0: psd_avg /= n_epochs # Compute variance for each epoch and then variance across epochs n_epochs = len(epc_array) for i, stc in enumerate(psd): if i >= n_epochs: psd_var = np.array() break if i == 0: psd_var = np.var(stc.data, axis=0) else: psd_var = np.vstack((psd_var, np.var(stc.data, axis=0))) if len(psd) >= 2: tot_var = np.var(psd_var, axis=0) if len(psd) <= 1: failed_subj = subj print (failed_subj + " failed. No PSD values calculated, likely because all epochs were rejected.") return failed_subj, failed_subj, failed_subj if len(psd) >= 2: return (psd_avg, tot_var, len(psd_avg))
def SN_functional_connectivity_betweenROIs(i, method): s = time.time() meg = subjects[i] sub_to = MRI_sub[i][1:15] con_SD_file_name = os.path.expanduser( '~' ) + '/my_semnet/json_files/connectivity/con_labels_' + method + '_bands_SD_sub' + str( i) + '.json' con_LD_file_name = os.path.expanduser( '~' ) + '/my_semnet/json_files/connectivity/con_labels_' + method + '_bands_LD_sub' + str( i) + '.json' morphed_labels = mne.morph_labels(SN_ROI,subject_to=data_path+sub_to,\ subject_from='fsaverage',subjects_dir=data_path) # Reading epochs epo_name_SD = data_path + meg + 'block_SD_words_epochs-epo.fif' epo_name_LD = data_path + meg + 'block_LD_words_epochs-epo.fif' epochs_sd = mne.read_epochs(epo_name_SD, preload=True) epochs_ld = mne.read_epochs(epo_name_LD, preload=True) epochs_SD = epochs_sd['words'].copy().resample(500) epochs_LD = epochs_ld['words'].copy().resample(500) # Equalize trial counts to eliminate bias equalize_epoch_counts([epochs_SD, epochs_LD]) # Reading inverse operator inv_fname_SD = data_path + meg + 'InvOp_SD_EMEG-inv.fif' inv_fname_LD = data_path + meg + 'InvOp_LD_EMEG-inv.fif' inv_op_SD = read_inverse_operator(inv_fname_SD) inv_op_LD = read_inverse_operator(inv_fname_LD) stc_sd = apply_inverse_epochs(epochs_SD, inv_op_SD, lambda2, method='MNE', pick_ori="normal", return_generator=False) stc_ld = apply_inverse_epochs(epochs_LD, inv_op_LD, lambda2, method='MNE', pick_ori="normal", return_generator=False) times = epochs_SD.times stc_SD_t = [] stc_LD_t = [] src_SD = inv_op_SD['src'] src_LD = inv_op_LD['src'] for n in np.arange(0, len(stc_sd)): stc_SD_t.append(stc_baseline_correction(stc_sd[n], times)) stc_LD_t.append(stc_baseline_correction(stc_ld[n], times)) for win in np.arange(0, len(C.con_time_window) - 1): print('[i,win]: ', i, win) t_min = C.con_time_window[win] t_max = C.con_time_window[win + 1] stc_SD = [] stc_LD = [] for n in np.arange(0, len(stc_sd)): stc_SD.append(stc_SD_t[n].copy().crop(t_min * 1e-3, t_max * 1e-3)) stc_LD.append(stc_LD_t[n].copy().crop(t_min * 1e-3, t_max * 1e-3)) for k in np.arange(0, 6): # print('[i,win,k]: ',i,win,k) morphed_labels[k].name = C.rois_labels[k] labels_ts_sd = mne.extract_label_time_course(stc_SD, morphed_labels, \ src_SD, mode='mean_flip',return_generator=False) labels_ts_ld = mne.extract_label_time_course(stc_LD, morphed_labels, \ src_LD, mode='mean_flip',return_generator=False) for f in np.arange(0, len(C.con_freq_band) - 1): print('[i,win,k,f]: ', i, win, k, f) f_min = C.con_freq_band[f] f_max = C.con_freq_band[f + 1] print(f_min, f_max) con_SD, freqs, times, n_epochs, n_tapers = spectral_connectivity( labels_ts_sd, method=method, mode='fourier', sfreq=500, fmin=f_min, fmax=f_max, faverage=True, n_jobs=10) con_LD, freqs, times, n_epochs, n_tapers = spectral_connectivity( labels_ts_ld, method=method, mode='fourier', sfreq=500, fmin=f_min, fmax=f_max, faverage=True, n_jobs=10) con_labels_SD[win][f] = con_SD.reshape(6, 6) con_labels_LD[win][f] = con_LD.reshape(6, 6) with open(con_SD_file_name, "wb") as fp: #Pickling pickle.dump(con_labels_SD, fp) with open(con_LD_file_name, "wb") as fp: #Pickling pickle.dump(con_labels_LD, fp) e = time.time() print(e - s)
def get_mne_sample(tmin=-0.1, tmax=0.4, baseline=(None, 0), sns=False, src=None, sub="modality=='A'", fixed=False, snr=2, method='dSPM', rm=False, stc=False): """Load events and epochs from the MNE sample data Parameters ---------- tmin, tmax baseline : Epoch parameters. sns : bool Add sensor space data as NDVar as ``ds['sns']``. src : None | 'ico' | 'vol' Add source space data as NDVar as ``ds['src']``. sub : str | None Expresion for subset of events to load. For a very small dataset use e.g. ``[0,1]``. fixed : bool MNE inverse parameter. snr : scalar MNE inverse parameter. method : str MNE inverse parameter. rm : bool Pretend to be a repeated measures dataset (adds 'subject' variable). stc : bool Add mne SourceEstimate for source space data as ``ds['stc']``. Returns ------- ds : Dataset Dataset with epochs from the MNE sample dataset. """ data_dir = mne.datasets.sample.data_path() meg_dir = os.path.join(data_dir, 'MEG', 'sample') raw_file = os.path.join(meg_dir, 'sample_audvis_filt-0-40_raw.fif') event_file = os.path.join(meg_dir, 'sample_audvis_filt-0-40-eve.fif') subjects_dir = os.path.join(data_dir, 'subjects') subject = 'sample' label_path = os.path.join(subjects_dir, subject, 'label', '%s.label') if not os.path.exists(event_file): raw = mne.io.Raw(raw_file) events = mne.find_events(raw, stim_channel='STI 014') mne.write_events(event_file, events) ds = load.fiff.events(raw_file, events=event_file) ds.index() ds.info['subjects_dir'] = subjects_dir ds.info['subject'] = subject ds.info['label'] = label_path # get the trigger variable form the dataset for eaier access trigger = ds['trigger'] # use trigger to add various labels to the dataset ds['condition'] = Factor(trigger, labels={1:'LA', 2:'RA', 3:'LV', 4:'RV', 5:'smiley', 32:'button'}) ds['side'] = Factor(trigger, labels={1: 'L', 2:'R', 3:'L', 4:'R', 5:'None', 32:'None'}) ds['modality'] = Factor(trigger, labels={1: 'A', 2:'A', 3:'V', 4:'V', 5:'None', 32:'None'}) if rm: ds = ds.sub('trigger < 5') ds = ds.equalize_counts('side % modality') subject_f = ds.eval('side % modality').enumerate_cells() ds['subject'] = subject_f.as_factor('s%r', random=True) if sub: ds = ds.sub(sub) load.fiff.add_mne_epochs(ds, tmin, tmax, baseline) if sns: ds['sns'] = load.fiff.epochs_ndvar(ds['epochs'], data='mag') if src is None: return ds elif src == 'ico': src_tag = 'ico-4' elif src == 'vol': src_tag = 'vol-10' else: raise ValueError("src = %r" % src) epochs = ds['epochs'] # get inverse operator inv_file = os.path.join(meg_dir, 'sample_eelbrain_%s-inv.fif' % src_tag) if os.path.exists(inv_file): inv = mne.minimum_norm.read_inverse_operator(inv_file) else: fwd_file = os.path.join(meg_dir, 'sample-%s-fwd.fif' % src_tag) bem_dir = os.path.join(subjects_dir, subject, 'bem') bem_file = os.path.join(bem_dir, 'sample-5120-5120-5120-bem-sol.fif') trans_file = os.path.join(meg_dir, 'sample_audvis_raw-trans.fif') if os.path.exists(fwd_file): fwd = mne.read_forward_solution(fwd_file) else: src_ = _mne_source_space(subject, src_tag, subjects_dir) fwd = mne.make_forward_solution(epochs.info, trans_file, src_, bem_file, fwd_file) cov_file = os.path.join(meg_dir, 'sample_audvis-cov.fif') cov = mne.read_cov(cov_file) inv = mn.make_inverse_operator(epochs.info, fwd, cov, None, None, fixed) mne.minimum_norm.write_inverse_operator(inv_file, inv) ds.info['inv'] = inv stcs = mn.apply_inverse_epochs(epochs, inv, 1. / (snr ** 2), method) ds['src'] = load.fiff.stc_ndvar(stcs, subject, src_tag, subjects_dir, method, fixed) if stc: ds['stc'] = stcs return ds
# pick MEG channels picks = pick_types(raw.info, meg=True, eeg=False, stim=False, eog=True, exclude='bads') # Read epochs epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=dict(mag=4e-12, grad=4000e-13, eog=150e-6)) # Compute inverse solution and for each epoch. Note that since we are passing # the output to both extract_label_time_course and the phase_slope_index # functions, we have to use "return_generator=False", since it is only possible # to iterate over generators once. snr = 1.0 # use lower SNR for single epochs lambda2 = 1.0 / snr ** 2 stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, method, pick_normal=True, return_generator=False) # Now, we generate seed time series by averaging the activity in the left # visual corex label = mne.read_label(fname_label) src = inverse_operator['src'] # the source space used seed_ts = mne.extract_label_time_course(stcs, label, src, mode='mean_flip') # Combine the seed time course with the source estimates. There will be a total # of 7500 signals: # index 0: time course extracted from label # index 1..7499: dSPM source space time courses comb_ts = zip(seed_ts, stcs) # Construct indices to estimate connectivity between the label time course # and all source space time courses
method = "MNE" n_jobs = 1 # Load data inverse_nrm = read_inverse_operator(inverse_fnrm) inverse_hyp = read_inverse_operator(inverse_fhyp) epochs_nrm = mne.read_epochs(epochs_fnrm) epochs_hyp = mne.read_epochs(epochs_fhyp) epochs_nrm = epochs_nrm["Tone"] epochs_hyp = epochs_hyp["Tone"] # stcs_nrm = apply_inverse_epochs(epochs_nrm, inverse_nrm, lambda2, method, pick_ori="normal", return_generator=False) stcs_hyp = apply_inverse_epochs(epochs_hyp, inverse_hyp, lambda2, method, pick_ori="normal", return_generator=False) # resample [stc.resample(300) for stc in stcs_nrm] [stc.resample(300) for stc in stcs_hyp] # Get labels from FreeSurfer cortical parcellation labels = mne.read_labels_from_annot("subject_1", parc="PALS_B12_Brodmann", regexp="Brodmann", subjects_dir=subjects_dir) # Average the source estimates within eachh label using sign-flips to reduce # signal cancellations, also here we return a generator src_nrm = inverse_nrm["src"] label_ts_nrm = mne.extract_label_time_course(stcs_nrm, labels, src_nrm, mode="mean_flip", return_generator=False)
def apply_inverse(fnepo, method='dSPM', event='LLst', min_subject='fsaverage', STC_US='ROI', snr=5.0): ''' Parameter --------- fnepo: string or list The epochs file with ECG, EOG and environmental noise free. method: inverse method, 'MNE' or 'dSPM' event: string The event name related with epochs. min_subject: string The subject name as the common brain. STC_US: string The using of the inversion for further analysis. 'ROI' stands for ROIs definition, 'CAU' stands for causality analysis. snr: signal to noise ratio for inverse solution. ''' #Get the default subjects_dir from mne.minimum_norm import (apply_inverse, apply_inverse_epochs) subjects_dir = os.environ['SUBJECTS_DIR'] fnlist = get_files_from_list(fnepo) # loop across all filenames for fname in fnlist: fn_path = os.path.split(fname)[0] name = os.path.basename(fname) stc_name = name[:name.rfind('-epo.fif')] subject = name.split('_')[0] subject_path = subjects_dir + '/%s' %subject min_dir = subjects_dir + '/%s' %min_subject fn_trans = fn_path + '/%s-trans.fif' % subject fn_cov = fn_path + '/%s_empty,nr-cov.fif' % subject fn_src = subject_path + '/bem/%s-ico-4-src.fif' % subject fn_bem = subject_path + '/bem/%s-5120-5120-5120-bem-sol.fif' % subject snr = snr lambda2 = 1.0 / snr ** 2 #noise_cov = mne.read_cov(fn_cov) epochs = mne.read_epochs(fname) noise_cov = mne.read_cov(fn_cov) if STC_US == 'ROI': # this path used for ROI definition stc_path = min_dir + '/%s_ROIs/%s' %(method,subject) #fn_cov = meg_path + '/%s_empty,fibp1-45,nr-cov.fif' % subject evoked = epochs.average() set_directory(stc_path) noise_cov = mne.cov.regularize(noise_cov, evoked.info, mag=0.05, grad=0.05, proj=True) fwd_ev = mne.make_forward_solution(evoked.info, trans=fn_trans, src=fn_src, bem=fn_bem, fname=None, meg=True, eeg=False, mindist=5.0, n_jobs=2, overwrite=True) fwd_ev = mne.convert_forward_solution(fwd_ev, surf_ori=True) forward_meg_ev = mne.pick_types_forward(fwd_ev, meg=True, eeg=False) inverse_operator_ev = mne.minimum_norm.make_inverse_operator( evoked.info, forward_meg_ev, noise_cov, loose=0.2, depth=0.8) # Compute inverse solution stc = apply_inverse(evoked, inverse_operator_ev, lambda2, method, pick_ori=None) # Morph STC subject_id = min_subject stc_morph = mne.morph_data(subject, subject_id, stc, grade=5, smooth=5) stc_morph.save(stc_path + '/%s' % (stc_name), ftype='stc') elif STC_US == 'CAU': stcs_path = min_dir + '/stcs/%s/%s/' % (subject,event) reset_directory(stcs_path) noise_cov = mne.cov.regularize(noise_cov, epochs.info, mag=0.05, grad=0.05, proj=True) fwd = mne.make_forward_solution(epochs.info, trans=fn_trans, src=fn_src, bem=fn_bem, meg=True, eeg=False, mindist=5.0, n_jobs=2, overwrite=True) fwd = mne.convert_forward_solution(fwd, surf_ori=True) forward_meg = mne.pick_types_forward(fwd, meg=True, eeg=False) inverse_operator = mne.minimum_norm.make_inverse_operator( epochs.info, forward_meg, noise_cov, loose=0.2, depth=0.8) # Compute inverse solution stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, method=method, pick_ori='normal') s = 0 while s < len(stcs): stc_morph = mne.morph_data( subject, min_subject, stcs[s], grade=5, smooth=5) stc_morph.save(stcs_path + '/trial%s_fsaverage' % (subject, str(s)), ftype='stc') s = s + 1
def _compute_inverse_solution(raw_filename, sbj_id, subjects_dir, fwd_filename, cov_fname, is_epoched=False, events_id=None, condition=None, is_ave=False, t_min=None, t_max=None, is_evoked=False, snr=1.0, inv_method='MNE', parc='aparc', aseg=False, aseg_labels=[], all_src_space=False, ROIs_mean=True, is_fixed=False): """ Compute the inverse solution on raw/epoched data and return the average time series computed in the N_r regions of the source space defined by the specified cortical parcellation Inputs raw_filename : str filename of the raw/epoched data sbj_id : str subject name subjects_dir : str Freesurfer directory fwd_filename : str filename of the forward operator cov_filename : str filename of the noise covariance matrix is_epoched : bool if True and events_id = None the input data are epoch data in the format -epo.fif if True and events_id is not None, the raw data are epoched according to events_id and t_min and t_max values events_id: dict the dict of events t_min, t_max: int define the time interval in which to epoch the raw data is_evoked: bool if True the raw data will be averaged according to the events contained in the dict events_id inv_method : str the inverse method to use; possible choices: MNE, dSPM, sLORETA snr : float the SNR value used to define the regularization parameter parc: str the parcellation defining the ROIs atlas in the source space aseg: bool if True a mixed source space will be created and the sub cortical regions defined in aseg_labels will be added to the source space aseg_labels: list list of substructures we want to include in the mixed source space all_src_space: bool if True we compute the inverse for all points of the s0urce space ROIs_mean: bool if True we compute the mean of estimated time series on ROIs Outputs ts_file : str filename of the file where are saved the estimated time series labels_file : str filename of the file where are saved the ROIs of the parcellation label_names_file : str filename of the file where are saved the name of the ROIs of the parcellation label_coords_file : str filename of the file where are saved the coordinates of the centroid of the ROIs of the parcellation """ print(('\n*** READ raw filename %s ***\n' % raw_filename)) if is_epoched: epochs = read_epochs(raw_filename) info = epochs.info elif is_ave: evokeds = read_evokeds(raw_filename) info = evokeds[0].info else: raw = read_raw_fif(raw_filename, preload=True) info = raw.info subj_path, basename, ext = split_f(raw_filename) print(('\n*** READ noise covariance %s ***\n' % cov_fname)) noise_cov = mne.read_cov(cov_fname) print(('\n*** READ FWD SOL %s ***\n' % fwd_filename)) forward = mne.read_forward_solution(fwd_filename) # TODO check use_cps for force_fixed=True if not aseg: print(('\n*** fixed orientation {} ***\n'.format(is_fixed))) # is_fixed=True => to convert the free-orientation fwd solution to # (surface-oriented) fixed orientation. forward = mne.convert_forward_solution(forward, surf_ori=True, force_fixed=is_fixed, use_cps=False) lambda2 = 1.0 / snr ** 2 # compute inverse operator print('\n*** COMPUTE INV OP ***\n') if is_fixed: loose = 0 depth = None pick_ori = None elif aseg: loose = 1 depth = None pick_ori = None else: loose = 0.2 depth = 0.8 pick_ori = 'normal' print(('\n *** loose {} depth {} ***\n'.format(loose, depth))) inverse_operator = make_inverse_operator(info, forward, noise_cov, loose=loose, depth=depth, fixed=is_fixed) # apply inverse operator to the time windows [t_start, t_stop]s print('\n*** APPLY INV OP ***\n') stc_files = list() if is_epoched and events_id != {}: if is_evoked: stc = list() if events_id != condition and condition: events_name = condition else: events_name = events_id evoked = [epochs[k].average() for k in events_name] if 'epo' in basename: basename = basename.replace('-epo', '') fname_evo = op.abspath(basename + '-ave.fif') write_evokeds(fname_evo, evoked) for k in range(len(events_name)): print(evoked[k]) stc_evo = apply_inverse(evoked[k], inverse_operator, lambda2, inv_method, pick_ori=pick_ori) print(('\n*** STC for event %s ***\n' % k)) print('***') print(('stc dim ' + str(stc_evo.shape))) print('***') stc_evo_file = op.abspath(basename + '-%d' % k) stc_evo.save(stc_evo_file) stc.append(stc_evo) stc_files.append(stc_evo_file) else: stc = apply_inverse_epochs(epochs, inverse_operator, lambda2, inv_method, pick_ori=pick_ori) elif is_epoched and events_id == {}: stc = apply_inverse_epochs(epochs, inverse_operator, lambda2, inv_method, pick_ori=pick_ori) elif is_ave: stc = list() for evo in evokeds: print(evo.comment) stc_evo = apply_inverse(evo, inverse_operator, lambda2, inv_method, pick_ori=pick_ori) print(('\n*** STC for event %s ***\n' % evo.comment)) print('***') print(('stc dim ' + str(stc_evo.shape))) print('***') stc_evo_file = op.join(subj_path, basename + '-%s' % evo.comment) stc_evo.save(stc_evo_file) stc.append(stc_evo) stc_files.append(stc_evo_file) else: stc = apply_inverse_raw(raw, inverse_operator, lambda2, inv_method, label=None, start=None, stop=None, buffer_size=1000, pick_ori=pick_ori) # None 'normal' ts_file, label_ts, labels_file, label_names_file, label_coords_file = \ _process_stc(stc, basename, sbj_id, subjects_dir, parc, forward, aseg, is_fixed, all_src_space=False, ROIs_mean=True) return ts_file, labels_file, label_names_file, \ label_coords_file, stc_files
include=include, exclude=exclude) # Read epochs epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=dict(mag=4e-12, grad=4000e-13, eog=150e-6)) # Compute inverse solution and stcs for each epoch stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, method, label, pick_normal=True) data = sum(stc.data for stc in stcs) / len(stcs) # compute sign flip to avoid signal cancelation when averaging signed values flip = mne.label_sign_flip(label, inverse_operator['src']) label_mean = np.mean(data, axis=0) label_mean_flip = np.mean(flip[:, np.newaxis] * data, axis=0) ############################################################################### # View activation time-series pl.figure() h0 = pl.plot(1e3 * stcs[0].times, data.T, 'k')
# Sort epochs according to experimental conditions in the post-stimulus interval allepochs.crop(tmin=-0.8, tmax=0) slow_epo_isi = allepochs.__getitem__('V1') fast_epo_isi = allepochs.__getitem__('V3') snr = 1.0 # use lower SNR for single epochs lambda2 = 1.0 / snr**2 v1_label = mne.read_label(datapath + 'Results_Alpha_and_Gamma/' + subject + '/' + subject + '_V1_lh.label') mt_label = mne.read_label(datapath + 'Results_Alpha_and_Gamma/' + subject + '/' + subject + '_MT_lh.label') stcs_slow_v1 = apply_inverse_epochs(slow_epo_isi, inverse_operator, lambda2, method='sLORETA', pick_ori="normal", label=v1_label) stcs_slow_mt = apply_inverse_epochs(slow_epo_isi, inverse_operator, lambda2, method='sLORETA', pick_ori="normal", label=mt_label) src = inverse_operator['src'] # the source space used stc_label_v1 = mne.stc_to_label(stcs_slow_v1[0], src=src, subjects_dir=subjects_dir, smooth=False) stc_label_mt = mne.stc_to_label(stcs_slow_mt[0],
meg_dir = '/Users/ea84/Dropbox/shepard_sourceloc/%s/' % (subject) stc_fname = meg_dir + 'stcs/%s_shepard_labels.npy' % (subject) # paths epochs_fname = meg_dir + subject + '_shepard-epo.fif' inv_fname = meg_dir + subject + '_shepard-inv.fif' print("Loading variables...") epochs = read_epochs(epochs_fname) inverse_operator = read_inverse_operator(inv_fname) src = inverse_operator['src'] # apply inverse to epochs print("Creating stcs...") stc_epochs = apply_inverse_epochs(epochs, inverse_operator, lambda2, method='dSPM') labels = read_labels_from_annot(subject, parc='aparc', subjects_dir=mri_dir) # HG and STC labels rois = ['transversetemporal', 'superiortemporal'] hemis = ['lh', 'rh'] lbs = [] for roi in rois: for hemi in hemis: lbs.append([ label for label in labels if label.name == '%s-%s' % (roi, hemi)
def test_source_psd_epochs(method): """Test multi-taper source PSD computation in label from epochs.""" raw = read_raw_fif(fname_data) inverse_operator = read_inverse_operator(fname_inv) label = read_label(fname_label) event_id, tmin, tmax = 1, -0.2, 0.5 lambda2 = 1. / 9. bandwidth = 8. fmin, fmax = 0, 100 picks = pick_types(raw.info, meg=True, eeg=False, stim=True, ecg=True, eog=True, include=['STI 014'], exclude='bads') reject = dict(grad=4000e-13, mag=4e-12, eog=150e-6) events = find_events(raw, stim_channel='STI 014') epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=reject) # only look at one epoch epochs.drop_bad() one_epochs = epochs[:1] inv = prepare_inverse_operator(inverse_operator, nave=1, lambda2=1. / 9., method="dSPM") # return list stc_psd = compute_source_psd_epochs(one_epochs, inv, lambda2=lambda2, method=method, pick_ori="normal", label=label, bandwidth=bandwidth, fmin=fmin, fmax=fmax, prepared=True)[0] # return generator stcs = compute_source_psd_epochs(one_epochs, inv, lambda2=lambda2, method=method, pick_ori="normal", label=label, bandwidth=bandwidth, fmin=fmin, fmax=fmax, return_generator=True, prepared=True) for stc in stcs: stc_psd_gen = stc assert_allclose(stc_psd.data, stc_psd_gen.data, atol=1e-7) # compare with direct computation stc = apply_inverse_epochs(one_epochs, inv, lambda2=lambda2, method=method, pick_ori="normal", label=label, prepared=True)[0] sfreq = epochs.info['sfreq'] psd, freqs = psd_array_multitaper(stc.data, sfreq=sfreq, bandwidth=bandwidth, fmin=fmin, fmax=fmax) assert_allclose(psd, stc_psd.data, atol=1e-7) assert_allclose(freqs, stc_psd.times) # Check corner cases caused by tiny bandwidth with pytest.raises(ValueError, match='use a value of at least'): compute_source_psd_epochs( one_epochs, inv, lambda2=lambda2, method=method, pick_ori="normal", label=label, bandwidth=0.01, low_bias=True, fmin=fmin, fmax=fmax, return_generator=False, prepared=True)
noise_cov, loose=0.2, depth=0.8) write_inverse_operator('sample_audvis-meg-oct-5-inv.fif', inverse_operator) ############################################################################### # Compute inverse solution # ------------------------ method = "MNE" snr = 3. lambda2 = 1. / snr**2 stc = apply_inverse_epochs(epochs, inverse_operator, lambda2, method=method, pick_ori=None) stc.save('sample_audvis-source-epochs') batch_size = len(stc) #number of events. we'll consider each event an example. n_steps = meg_data.shape[2] dipole = stc[0]._data for i in range(1, batch_size): dipole = np.dstack((dipole, stc[i]._data)) #pxn_stepsxbatchsize qtrue, p = meas_class.scale_dipole(dipole) #bxnxp
def get_mne_sample( tmin=-0.1, tmax=0.4, baseline=(None, 0), sns=False, src=None, sub="modality=='A'", fixed=False, snr=2, method="dSPM", rm=False, stc=False, ): """Load events and epochs from the MNE sample data Parameters ---------- tmin, tmax baseline : Epoch parameters. sns : bool Add sensor space data as NDVar as ``ds['meg']`` (default ``False``). src : False | 'ico' | 'vol' Add source space data as NDVar as ``ds['src']`` (default ``False``). sub : str | list | None Expresion for subset of events to load. For a very small dataset use e.g. ``[0,1]``. fixed : bool MNE inverse parameter. snr : scalar MNE inverse parameter. method : str MNE inverse parameter. rm : bool Pretend to be a repeated measures dataset (adds 'subject' variable). stc : bool Add mne SourceEstimate for source space data as ``ds['stc']`` (default ``False``). Returns ------- ds : Dataset Dataset with epochs from the MNE sample dataset in ``ds['epochs']``. """ data_dir = mne.datasets.sample.data_path() meg_dir = os.path.join(data_dir, "MEG", "sample") raw_file = os.path.join(meg_dir, "sample_audvis_filt-0-40_raw.fif") event_file = os.path.join(meg_dir, "sample_audvis_filt-0-40-eve.fif") subjects_dir = os.path.join(data_dir, "subjects") subject = "sample" label_path = os.path.join(subjects_dir, subject, "label", "%s.label") if not os.path.exists(event_file): raw = mne.io.Raw(raw_file) events = mne.find_events(raw, stim_channel="STI 014") mne.write_events(event_file, events) ds = load.fiff.events(raw_file, events=event_file) ds.index() ds.info["subjects_dir"] = subjects_dir ds.info["subject"] = subject ds.info["label"] = label_path # get the trigger variable form the dataset for eaier access trigger = ds["trigger"] # use trigger to add various labels to the dataset ds["condition"] = Factor(trigger, labels={1: "LA", 2: "RA", 3: "LV", 4: "RV", 5: "smiley", 32: "button"}) ds["side"] = Factor(trigger, labels={1: "L", 2: "R", 3: "L", 4: "R", 5: "None", 32: "None"}) ds["modality"] = Factor(trigger, labels={1: "A", 2: "A", 3: "V", 4: "V", 5: "None", 32: "None"}) if rm: ds = ds.sub("trigger < 5") ds = ds.equalize_counts("side % modality") subject_f = ds.eval("side % modality").enumerate_cells() ds["subject"] = subject_f.as_factor("s%r", random=True) if sub: ds = ds.sub(sub) load.fiff.add_mne_epochs(ds, tmin, tmax, baseline) if sns: ds["meg"] = load.fiff.epochs_ndvar(ds["epochs"], data="mag", sysname="neuromag306mag") if not src: return ds elif src == "ico": src_tag = "ico-4" elif src == "vol": src_tag = "vol-10" else: raise ValueError("src = %r" % src) epochs = ds["epochs"] # get inverse operator inv_file = os.path.join(meg_dir, "sample_eelbrain_%s-inv.fif" % src_tag) if os.path.exists(inv_file): inv = mne.minimum_norm.read_inverse_operator(inv_file) else: fwd_file = os.path.join(meg_dir, "sample-%s-fwd.fif" % src_tag) bem_dir = os.path.join(subjects_dir, subject, "bem") bem_file = os.path.join(bem_dir, "sample-5120-5120-5120-bem-sol.fif") trans_file = os.path.join(meg_dir, "sample_audvis_raw-trans.fif") if os.path.exists(fwd_file): fwd = mne.read_forward_solution(fwd_file) else: src_ = _mne_source_space(subject, src_tag, subjects_dir) fwd = mne.make_forward_solution(epochs.info, trans_file, src_, bem_file, fwd_file) cov_file = os.path.join(meg_dir, "sample_audvis-cov.fif") cov = mne.read_cov(cov_file) inv = mn.make_inverse_operator(epochs.info, fwd, cov, None, None, fixed) mne.minimum_norm.write_inverse_operator(inv_file, inv) ds.info["inv"] = inv stcs = mn.apply_inverse_epochs(epochs, inv, 1.0 / (snr ** 2), method) ds["src"] = load.fiff.stc_ndvar(stcs, subject, src_tag, subjects_dir, method, fixed) if stc: ds["stc"] = stcs return ds
# crop epochs epochs = epochs_cond['words'].copy().crop(-.200, .900).resample(f_down_sampling) # equalize trial counts to eliminate bias # equalize_epoch_counts([epochs_SD, epochs_LD]) inv_fname_epoch = data_path + meg + 'InvOp_' + cond + '_EMEG-inv.fif' output = [0] * 2 # read inverse operator,apply inverse operator inv_op = read_inverse_operator(inv_fname_epoch) stc = apply_inverse_epochs(epochs, inv_op, lambda2, method='MNE', pick_ori="normal", return_generator=False) for j, idx in enumerate([ROI_x, ROI_y]): labels[idx].subject = sub_to # define dimentions of matrix (vertices X timepoints), & initializing v, t = stc[0].in_label(labels[idx]).data.shape X = np.zeros([len(stc), v, t]) # create output array of size (vertices X stimuli X timepoints) for s in np.arange(0, len(stc)): S = stc[s].in_label(labels[idx]).data # X[s,:,:]=S.copy() -np.matlib.repmat(np.mean(S,0).reshape(1,t),v,1) X[s, :, :] = S output[j] = X
stc = mne.read_source_estimate(fn_stc + '-lh.stc') pos, t_peak = stc.get_peak(hemi=None, tmin=0., tmax=0.5, mode='abs') brain = stc.plot(subject=subj, surface='inflated', hemi='both', colormap='auto', time_label='auto', subjects_dir=subjects_dir, figure=None, colorbar=True, clim='auto', initial_time=t_peak, time_viewer=time_viewer) stc_plot_fname = op.join(basedir, 'plots', op.basename(fn_stc) + ',plot.png') time.sleep(1) if not time_viewer: # works only if time viewer is disabled brain.save_montage(stc_plot_fname, order=['lat', 'dor', 'med']) brain.close() time.sleep(1) # compute inverse solution for epochs if do_inv_epo: # Compute inverse solution for epoch data fn_stc = fn_epo.rsplit('-epo.fif')[0] + ',epo' # get a list of one stc per epoch # usually it is best to create the # stcs when you need them instead # of saving them stc = apply_inverse_epochs(epochs, inv, lambda2, method, pick_ori=None)
method = "dSPM" snr = 1. lambda2 = 1. / snr**2 labels = mne.read_labels_from_annot( subject=subject, parc="PALS_B12_Brodmann", regexp="Brodmann") condition = "interupt" inv = read_inverse_operator(mne_folder + "%s_%s-inv.fif" % (subject, condition )) epochs = mne.read_epochs(epochs_folder + "%s_%s-epo.fif" % (subject, condition )) # epochs.resample(500) stcs = apply_inverse_epochs( epochs["press"], inv, lambda2, method=method, pick_ori=None) ts = [ mne.extract_label_time_course( stc, labels, inv["src"], mode="mean_flip") for stc in stcs ] # for h, tc in enumerate(ts): # for j, t in enumerate(tc): # t *= np.sign(t[np.argmax(np.abs(t))]) # tc[j, :] = t # ts[h] = tc ts = np.asarray(ts) stc.save(source_folder + "%s_%s_epo" % (subject, condition)) np.save(source_folder + "ave_ts/%s_%s_ts-epo.npy" % (subject, condition), ts)
parser.add_argument("subject", help="subject id") args = parser.parse_args() subj = args.subject fwd_path = bp.fwd.fpath(subject=subj) fwd = read_forward_solution(fwd_path) inv_path = bp.inv.fpath(subject=subj) epochs_path = bp.epochs.fpath(subject=subj) epochs = read_epochs(epochs_path)["answer"] inverse_operator = read_inverse_operator(inv_path) epochs.apply_baseline() stcs = apply_inverse_epochs( epochs, inverse_operator, lambda2=1, method="MNE" ) src_path = dirs.fsf_subjects / "fsaverage/bem/fsaverage-oct-6-src.fif" src = read_source_spaces(src_path) fsave_vertices = [s["vertno"] for s in src] morph = compute_source_morph( src=inverse_operator["src"], subject_to="fsaverage", spacing=fsave_vertices, subjects_dir=dirs.fsf_subjects, ) subj_dir = SOURCES_PSD_DIR / f"sub-{subj}" subj_dir.mkdir(exist_ok=True, parents=True)
def get_mne_sample(tmin=-0.1, tmax=0.4, baseline=(None, 0), sns=False, src=None, sub="modality=='A'", fixed=False, snr=2, method='dSPM'): """Load events and epochs from the MNE sample data Parameters ---------- tmin, tmax baseline : Epoch parameters. sns : bool Add sensor space data as NDVar as ``ds['sns']``. src : None | 'ico' | 'vol' Add source space data as NDVar as ``ds['src']``. sub : str | None Expresion for subset of events to load. fixed : bool MNE inverse parameter. snr : scalar MNE inverse parameter. method : str MNE inverse parameter. Returns ------- ds : Dataset Dataset with epochs from the MNE sample dataset. """ data_dir = mne.datasets.sample.data_path() meg_dir = os.path.join(data_dir, 'MEG', 'sample') raw_file = os.path.join(meg_dir, 'sample_audvis_filt-0-40_raw.fif') subjects_dir = os.path.join(data_dir, 'subjects') subject = 'sample' label_path = os.path.join(subjects_dir, subject, 'label', '%s.label') ds = load.fiff.events(raw_file, stim_channel='STI 014') ds.info['subjects_dir'] = subjects_dir ds.info['subject'] = subject ds.info['label'] = label_path # get the trigger variable form the dataset for eaier access trigger = ds['trigger'] # use trigger to add various labels to the dataset ds['condition'] = Factor(trigger, labels={1:'LA', 2:'RA', 3:'LV', 4:'RV', 5:'smiley', 32:'button'}) ds['side'] = Factor(trigger, labels={1: 'L', 2:'R', 3:'L', 4:'R', 5:'None', 32:'None'}) ds['modality'] = Factor(trigger, labels={1: 'A', 2:'A', 3:'V', 4:'V', 5:'None', 32:'None'}) if sub: ds = ds.sub(sub) load.fiff.add_mne_epochs(ds, tmin, tmax, baseline) if sns: ds['sns'] = load.fiff.epochs_ndvar(ds['epochs']) if not src: return ds bem_dir = os.path.join(subjects_dir, subject, 'bem') bem_file = os.path.join(bem_dir, 'sample-5120-5120-5120-bem-sol.fif') trans_file = os.path.join(meg_dir, 'sample_audvis_raw-trans.fif') epochs = ds['epochs'] if src == 'ico': src_tag = 'ico-4' elif src == 'vol': src_tag = 'vol-10' else: raise ValueError("src = %r" % src) fwd_file = os.path.join(meg_dir, 'sample-%s-fwd.fif' % src_tag) if os.path.exists(fwd_file): fwd = mne.read_forward_solution(fwd_file) else: src_file = os.path.join(bem_dir, 'sample-%s-src.fif' % src_tag) if os.path.exists(src_file): src_ = src_file elif src == 'ico': src_ = mne.setup_source_space(subject, src_file, 'ico4', subjects_dir=subjects_dir) elif src == 'vol': mri_file = os.path.join(subjects_dir, subject, 'mri', 'orig.mgz') src_ = mne.setup_volume_source_space(subject, src_file, pos=10., mri=mri_file, bem=bem_file, mindist=0., exclude=0., subjects_dir=subjects_dir) fwd = mne.make_forward_solution(epochs.info, trans_file, src_, bem_file, fwd_file) cov_file = os.path.join(meg_dir, 'sample_audvis-cov.fif') cov = mne.read_cov(cov_file) inv = mn.make_inverse_operator(epochs.info, fwd, cov, None, None, fixed) ds.info['inv'] = inv stcs = mn.apply_inverse_epochs(epochs, inv, 1. / (snr ** 2), method) ds['src'] = load.fiff.stc_ndvar(stcs, subject, src_tag, subjects_dir) return ds
def SN_functional_connectivity_bands(i, method): s = time.time() meg = subjects[i] sub_to = MRI_sub[i][1:15] stc_SD_file_name = os.path.expanduser( '~' ) + '/my_semnet/json_files/connectivity/stc_' + method + '200_equalized_bands_SD_sub' + str( i) + '.json' stc_LD_file_name = os.path.expanduser( '~' ) + '/my_semnet/json_files/connectivity/stc_' + method + '200_equalized_bands_LD_sub' + str( i) + '.json' # stc_SD_file_name=os.path.expanduser('~') +'/my_semnet/json_files/connectivity/stc_'+method+'bl_bands_SD_sub'+str(i)+'.json' # stc_LD_file_name=os.path.expanduser('~') +'/my_semnet/json_files/connectivity/stc_'+method+'bl_bands_LD_sub'+str(i)+'.json' morphed_labels = mne.morph_labels(SN_ROI,subject_to=data_path+sub_to,\ subject_from='fsaverage',subjects_dir=data_path) # Reading epochs epo_name_SD = data_path + meg + 'block_SD_words_epochs-epo.fif' epo_name_LD = data_path + meg + 'block_LD_words_epochs-epo.fif' epochs_sd = mne.read_epochs(epo_name_SD, preload=True) epochs_ld = mne.read_epochs(epo_name_LD, preload=True) epochs_SD = epochs_sd['words'].copy().resample(500) epochs_LD = epochs_ld['words'].copy().resample(500) equalize_epoch_counts([epochs_SD, epochs_LD]) # Reading inverse operator inv_fname_SD = data_path + meg + 'InvOp_SD_EMEG-inv.fif' inv_fname_LD = data_path + meg + 'InvOp_LD_EMEG-inv.fif' inv_op_SD = read_inverse_operator(inv_fname_SD) inv_op_LD = read_inverse_operator(inv_fname_LD) stc_sd = apply_inverse_epochs(epochs_SD, inv_op_SD, lambda2, method='MNE', pick_ori="normal", return_generator=False) stc_ld = apply_inverse_epochs(epochs_LD, inv_op_LD, lambda2, method='MNE', pick_ori="normal", return_generator=False) src_SD = inv_op_SD['src'] src_LD = inv_op_LD['src'] # Construct indices to estimate connectivity between the label time course # and all source space time courses vertices_SD = [src_SD[j]['vertno'] for j in range(2)] n_signals_tot = 1 + len(vertices_SD[0]) + len(vertices_SD[1]) indices = seed_target_indices([0], np.arange(1, n_signals_tot)) morph_SD = mne.compute_source_morph(src=inv_op_SD['src'],\ subject_from=sub_to, subject_to=C.subject_to,\ spacing=C.spacing_morph, subjects_dir=C.data_path) morph_LD = mne.compute_source_morph(src= inv_op_LD['src'],\ subject_from=sub_to, subject_to=C.subject_to,\ spacing=C.spacing_morph, subjects_dir=C.data_path) for win in np.arange(0, len(C.con_time_window) - 1): print('[i,win]: ', i, win) t_min = C.con_time_window[win] t_max = C.con_time_window[win + 1] stc_SD = [] stc_LD = [] for n in np.arange(0, len(stc_sd)): stc_SD.append(stc_sd[n].copy().crop(t_min * 1e-3, t_max * 1e-3)) for n in np.arange(0, len(stc_ld)): stc_LD.append(stc_ld[n].copy().crop(t_min * 1e-3, t_max * 1e-3)) for k in np.arange(0, 6): print('[i,win,k]: ', i, win, k) morphed_labels[k].name = C.rois_labels[k] seed_ts_sd = mne.extract_label_time_course(stc_SD, morphed_labels[k], \ src_SD, mode='mean_flip',return_generator=False) seed_ts_ld = mne.extract_label_time_course(stc_LD, morphed_labels[k], \ src_LD, mode='mean_flip',return_generator=False) for f in np.arange(0, len(C.con_freq_band) - 1): print('[i,win,k,f]: ', i, win, k, f) f_min = C.con_freq_band[f] f_max = C.con_freq_band[f + 1] print(f_min, f_max) comb_ts_sd = zip(seed_ts_sd, stc_SD) comb_ts_ld = zip(seed_ts_ld, stc_LD) con_SD, freqs, times, n_epochs, n_tapers = spectral_connectivity( comb_ts_sd, method=method, mode='fourier', indices=indices, sfreq=500, fmin=f_min, fmax=f_max, faverage=True, n_jobs=10) con_LD, freqs, times, n_epochs, n_tapers = spectral_connectivity( comb_ts_ld, method=method, mode='fourier', indices=indices, sfreq=500, fmin=f_min, fmax=f_max, faverage=True, n_jobs=10) con_stc_SD = mne.SourceEstimate(con_SD, vertices=vertices_SD,\ tmin=t_min*1e-3, tstep=2e-3,subject=sub_to) con_stc_LD = mne.SourceEstimate(con_LD, vertices=vertices_SD,\ tmin=t_min*1e-3, tstep=2e-3,subject=sub_to) stc_total_SD[win][k][f] = morph_SD.apply(con_stc_SD) stc_total_LD[win][k][f] = morph_LD.apply(con_stc_LD) with open(stc_SD_file_name, "wb") as fp: #Pickling pickle.dump(stc_total_SD, fp) with open(stc_LD_file_name, "wb") as fp: #Pickling pickle.dump(stc_total_LD, fp) e = time.time() print(e - s)
def compute_rois_inv_sol(raw_filename, sbj_id, sbj_dir, fwd_filename, cov_fname, is_epoched=False, events_id=[], t_min=None, t_max=None, is_evoked=False, snr=1.0, inv_method='MNE', parc='aparc', aseg=False, aseg_labels=[], save_stc=False, is_fixed=False): """Compute the inverse solution on raw/epoched data. This function return the average time series computed in the N_r regions of the source space defined by the specified cortical parcellation Parameters ---------- raw_filename : str filename of the raw/epoched data sbj_id : str subject name sbj_dir : str Freesurfer directory fwd_filename : str filename of the forward operator cov_filename : str filename of the noise covariance matrix is_epoched : bool if True and events_id = None the input data are epoch data in the format -epo.fif if True and events_id is not None, the raw data are epoched according to events_id and t_min and t_max values events_id: dict the dict of events t_min, t_max: int define the time interval in which to epoch the raw data is_evoked: bool if True the raw data will be averaged according to the events contained in the dict events_id inv_method : str the inverse method to use; possible choices: MNE, dSPM, sLORETA snr : float the SNR value used to define the regularization parameter parc: str the parcellation defining the ROIs atlas in the source space aseg: bool if True a mixed source space will be created and the sub cortical regions defined in aseg_labels will be added to the source space aseg_labels: list list of substructures we want to include in the mixed source space save_stc: bool if True the stc will be saved Returns ------- ts_file : str filename of the file where are saved the ROIs time series labels_file : str filename of the file where are saved the ROIs of the parcellation label_names_file : str filename of the file where are saved the name of the ROIs of the parcellation label_coords_file : str filename of the file where are saved the coordinates of the centroid of the ROIs of the parcellation """ import os.path as op import numpy as np import mne from mne.io import read_raw_fif from mne import read_epochs from mne.minimum_norm import make_inverse_operator, apply_inverse_raw from mne.minimum_norm import apply_inverse_epochs, apply_inverse from mne import get_volume_labels_from_src from nipype.utils.filemanip import split_filename as split_f from ephypype.preproc import create_reject_dict from ephypype.source_space import create_mni_label_files try: traits.undefined(events_id) except NameError: events_id = None print(('\n*** READ raw filename %s ***\n' % raw_filename)) if is_epoched and events_id is None: epochs = read_epochs(raw_filename) info = epochs.info else: raw = read_raw_fif(raw_filename, preload=True) # raw.set_eeg_reference() info = raw.info subj_path, basename, ext = split_f(raw_filename) print(('\n*** READ noise covariance %s ***\n' % cov_fname)) noise_cov = mne.read_cov(cov_fname) print(('\n*** READ FWD SOL %s ***\n' % fwd_filename)) forward = mne.read_forward_solution(fwd_filename) if not aseg: print(('\n*** fixed orientation {} ***\n'.format(is_fixed))) forward = mne.convert_forward_solution(forward, surf_ori=True, force_fixed=is_fixed) lambda2 = 1.0 / snr**2 # compute inverse operator print('\n*** COMPUTE INV OP ***\n') if is_fixed: loose = None depth = None pick_ori = None elif aseg: loose = 1 depth = None pick_ori = None else: loose = 0.2 depth = 0.8 pick_ori = 'normal' print(('\n *** loose {} depth {} ***\n'.format(loose, depth))) inverse_operator = make_inverse_operator(info, forward, noise_cov, loose=loose, depth=depth, fixed=is_fixed) # apply inverse operator to the time windows [t_start, t_stop]s print('\n*** APPLY INV OP ***\n') if is_epoched and events_id is not None: events = mne.find_events(raw) picks = mne.pick_types(info, meg=True, eog=True, exclude='bads') reject = create_reject_dict(info) if is_evoked: epochs = mne.Epochs(raw, events, events_id, t_min, t_max, picks=picks, baseline=(None, 0), reject=reject) evoked = [epochs[k].average() for k in events_id] snr = 3.0 lambda2 = 1.0 / snr**2 ev_list = list(events_id.items()) for k in range(len(events_id)): stc = apply_inverse(evoked[k], inverse_operator, lambda2, inv_method, pick_ori=pick_ori) print(('\n*** STC for event %s ***\n' % ev_list[k][0])) stc_file = op.abspath(basename + '_' + ev_list[k][0]) print('***') print(('stc dim ' + str(stc.shape))) print('***') if not aseg: stc.save(stc_file) else: epochs = mne.Epochs(raw, events, events_id, t_min, t_max, picks=picks, baseline=(None, 0), reject=reject) stc = apply_inverse_epochs(epochs, inverse_operator, lambda2, inv_method, pick_ori=pick_ori) print('***') print(('len stc %d' % len(stc))) print('***') elif is_epoched and events_id is None: stc = apply_inverse_epochs(epochs, inverse_operator, lambda2, inv_method, pick_ori=pick_ori) print('***') print(('len stc %d' % len(stc))) print('***') else: stc = apply_inverse_raw(raw, inverse_operator, lambda2, inv_method, label=None, start=None, stop=None, buffer_size=1000, pick_ori=pick_ori) # None 'normal' print('***') print(('stc dim ' + str(stc.shape))) print('***') if not isinstance(stc, list): stc = [stc] if save_stc: for i in range(len(stc)): stc_file = op.abspath(basename + '_stc_' + str(i) + '.npy') np.save(stc_file, stc[i].data) # these coo are in MRI space and we have to convert to MNI space labels_cortex = mne.read_labels_from_annot(sbj_id, parc=parc, subjects_dir=sbj_dir) print(('\n*** %d ***\n' % len(labels_cortex))) src = inverse_operator['src'] # allow_empty : bool -> Instead of emitting an error, return all-zero time # courses for labels that do not have any vertices in the source estimate if is_fixed: mode = 'mean_flip' else: mode = 'mean' label_ts = mne.extract_label_time_course(stc, labels_cortex, src, mode=mode, allow_empty=True, return_generator=False) # save results in .npy file that will be the input for spectral node print('\n*** SAVE ROI TS ***\n') print((len(label_ts))) ts_file = op.abspath(basename + '_ROI_ts.npy') np.save(ts_file, label_ts) if aseg: print(sbj_id) labels_aseg = get_volume_labels_from_src(src, sbj_id, sbj_dir) labels = labels_cortex + labels_aseg else: labels = labels_cortex labels_aseg = None print((labels[0].pos)) print((len(labels))) # labels_file, label_names_file, label_coords_file = \ # create_label_files(labels) labels_file, label_names_file, label_coords_file = \ create_mni_label_files(forward, labels_cortex, labels_aseg, sbj_id, sbj_dir) return ts_file, labels_file, label_names_file, label_coords_file
def get_mne_sample(tmin=-0.1, tmax=0.4, baseline=(None, 0), sns=False, src=None, sub="modality=='A'", ori='free', snr=2, method='dSPM', rm=False, stc=False, hpf=0): """Load events and epochs from the MNE sample data Parameters ---------- tmin : scalar Relative time of the first sample of the epoch. tmax : scalar Relative time of the last sample of the epoch. baseline : {None, tuple of 2 {scalar, None}} Period for baseline correction. sns : bool | str Add sensor space data as NDVar as ``ds['meg']`` (default ``False``). Set to ``'grad'`` to load gradiometer data. src : False | 'ico' | 'vol' Add source space data as NDVar as ``ds['src']`` (default ``False``). sub : str | list | None Expression for subset of events to load. For a very small dataset use e.g. ``[0,1]``. ori : 'free' | 'fixed' | 'vector' Orientation of sources. snr : scalar MNE inverse parameter. method : str MNE inverse parameter. rm : bool Pretend to be a repeated measures dataset (adds 'subject' variable). stc : bool Add mne SourceEstimate for source space data as ``ds['stc']`` (default ``False``). hpf : scalar High pass filter cutoff. Returns ------- ds : Dataset Dataset with epochs from the MNE sample dataset in ``ds['epochs']``. """ if ori == 'free': loose = 1 fixed = False pick_ori = None elif ori == 'fixed': loose = 0 fixed = True pick_ori = None elif ori == 'vector': if LooseVersion(mne.__version__) < LooseVersion('0.17'): raise RuntimeError(f'mne version {mne.__version__}; vector source estimates require mne 0.17') loose = 1 fixed = False pick_ori = 'vector' else: raise ValueError(f"ori={ori!r}") data_dir = mne.datasets.sample.data_path() meg_dir = os.path.join(data_dir, 'MEG', 'sample') raw_file = os.path.join(meg_dir, 'sample_audvis_filt-0-40_raw.fif') event_file = os.path.join(meg_dir, 'sample_audvis_filt-0-40-eve.fif') subjects_dir = os.path.join(data_dir, 'subjects') subject = 'sample' label_path = os.path.join(subjects_dir, subject, 'label', '%s.label') if not os.path.exists(event_file): raw = mne.io.Raw(raw_file) events = mne.find_events(raw, stim_channel='STI 014') mne.write_events(event_file, events) ds = load.fiff.events(raw_file, events=event_file) if hpf: ds.info['raw'].load_data() ds.info['raw'].filter(hpf, None) ds.index() ds.info['subjects_dir'] = subjects_dir ds.info['subject'] = subject ds.info['label'] = label_path # get the trigger variable form the dataset for eaier access trigger = ds['trigger'] # use trigger to add various labels to the dataset ds['condition'] = Factor(trigger, labels={ 1: 'LA', 2: 'RA', 3: 'LV', 4: 'RV', 5: 'smiley', 32: 'button'}) ds['side'] = Factor(trigger, labels={ 1: 'L', 2: 'R', 3: 'L', 4: 'R', 5: 'None', 32: 'None'}) ds['modality'] = Factor(trigger, labels={ 1: 'A', 2: 'A', 3: 'V', 4: 'V', 5: 'None', 32: 'None'}) if rm: ds = ds.sub('trigger < 5') ds = ds.equalize_counts('side % modality') subject_f = ds.eval('side % modality').enumerate_cells() ds['subject'] = subject_f.as_factor('s%r', random=True) if sub: ds = ds.sub(sub) load.fiff.add_mne_epochs(ds, tmin, tmax, baseline) if sns: ds['meg'] = load.fiff.epochs_ndvar(ds['epochs'], data='mag' if sns is True else sns, sysname='neuromag') if not src: return ds elif src == 'ico': src_tag = 'ico-4' elif src == 'vol': src_tag = 'vol-10' else: raise ValueError("src = %r" % src) epochs = ds['epochs'] # get inverse operator inv_file = os.path.join(meg_dir, f'sample_eelbrain_{src_tag}-inv.fif') if os.path.exists(inv_file): inv = mne.minimum_norm.read_inverse_operator(inv_file) else: fwd_file = os.path.join(meg_dir, 'sample-%s-fwd.fif' % src_tag) bem_dir = os.path.join(subjects_dir, subject, 'bem') bem_file = os.path.join(bem_dir, 'sample-5120-5120-5120-bem-sol.fif') trans_file = os.path.join(meg_dir, 'sample_audvis_raw-trans.fif') if os.path.exists(fwd_file): fwd = mne.read_forward_solution(fwd_file) else: src_ = _mne_source_space(subject, src_tag, subjects_dir) fwd = mne.make_forward_solution(epochs.info, trans_file, src_, bem_file) mne.write_forward_solution(fwd_file, fwd) cov_file = os.path.join(meg_dir, 'sample_audvis-cov.fif') cov = mne.read_cov(cov_file) inv = mn.make_inverse_operator(epochs.info, fwd, cov, loose=loose, depth=None, fixed=fixed) mne.minimum_norm.write_inverse_operator(inv_file, inv) ds.info['inv'] = inv stcs = mn.apply_inverse_epochs(epochs, inv, 1. / (snr ** 2), method, pick_ori=pick_ori) ds['src'] = load.fiff.stc_ndvar(stcs, subject, src_tag, subjects_dir, method, fixed) if stc: ds['stc'] = stcs return ds
# ATT_30/KER27, ATT_27, ATT_32/EAM67 excluded for too much head movement between blocks subjs = ["ATT_10"] runs = ["rest", "audio", "visselten", "visual", "zaehlen"] runs = ["rest"] subjects_dir = "/home/jeff/freesurfer/subjects/" spacing = "oct4" frequencies = [list(np.linspace(7, 14, 8)) for x in range(5)] cov = mne.read_cov("{dir}empty-cov.fif".format(dir=proc_dir)) for sub in subjs: l_sens = mne.read_label("{dir}nc_{sub}_{sp}-lh.label".format(dir=proc_dir, sub=sub, sp=spacing)) r_sens = mne.read_label("{dir}nc_{sub}_{sp}-rh.label".format(dir=proc_dir, sub=sub, sp=spacing)) src = mne.read_source_spaces("{}{}_{}-src.fif".format( proc_dir, sub, spacing)) for run_idx, run in enumerate(runs): fwd_name = "{dir}nc_{sub}_{run}_{sp}-fwd.fif".format(dir=proc_dir, sub=sub, run=run, sp=spacing) fwd = mne.read_forward_solution(fwd_name) epo_name = "{dir}nc_{sub}_{run}_hand-epo.fif".format(dir=proc_dir, sub=sub, run=run) epo = mne.read_epochs(epo_name) inv_op = make_inverse_operator(epo.info, fwd, cov) stcs = apply_inverse_epochs(epo, inv_op, 1, method="sLORETA")
def SN_functional_connectivity_betweenROIs_runs_BL(i, method): s = time.time() meg = subjects[i] sub_to = MRI_sub[i][1:15] # stc_SD_file_name=os.path.expanduser('~') +'/my_semnet/json_files/connectivity/con_labels_'+method+'_bl_bands_SD_sub'+str(i)+'.json' # stc_LD_file_name=os.path.expanduser('~') +'/my_semnet/json_files/connectivity/con_labels_'+method+'_bl_bands_LD_sub'+str(i)+'.json' stc_F_file_name = os.path.expanduser( '~' ) + '/my_semnet/json_files/connectivity/con_labels_' + method + '_bl_bands_F_sub' + str( i) + '.json' stc_M_file_name = os.path.expanduser( '~' ) + '/my_semnet/json_files/connectivity/con_labels_' + method + '_bl_bands_M_sub' + str( i) + '.json' stc_O_file_name = os.path.expanduser( '~' ) + '/my_semnet/json_files/connectivity/con_labels_' + method + '_bl_bands_O_sub' + str( i) + '.json' morphed_labels = mne.morph_labels(SN_ROI,subject_to=data_path+sub_to,\ subject_from='fsaverage',subjects_dir=data_path) # Reading epochs epoch_fname_fruit = data_path + meg + 'block_fruit_epochs-epo.fif' epoch_fname_odour = data_path + meg + 'block_odour_epochs-epo.fif' epoch_fname_milk = data_path + meg + 'block_milk_epochs-epo.fif' epo_name_LD = data_path + meg + 'block_LD_words_epochs-epo.fif' epochs_fruit = mne.read_epochs(epoch_fname_fruit, preload=True) epochs_odour = mne.read_epochs(epoch_fname_odour, preload=True) epochs_milk = mne.read_epochs(epoch_fname_milk, preload=True) epochs_ld = mne.read_epochs(epo_name_LD, preload=True) epochs_f = mne.epochs.combine_event_ids( epochs_fruit, ['visual', 'hear', 'hand', 'neutral', 'emotional'], {'words': 15}) epochs_o = mne.epochs.combine_event_ids( epochs_odour, ['visual', 'hear', 'hand', 'neutral', 'emotional'], {'words': 15}) epochs_m = mne.epochs.combine_event_ids( epochs_milk, ['visual', 'hear', 'hand', 'neutral', 'emotional'], {'words': 15}) epochs_f = epochs_f['words'].copy().crop(-.200, 0).resample(500) epochs_o = epochs_o['words'].copy().crop(-.200, 0).resample(500) epochs_m = epochs_m['words'].copy().crop(-.200, 0).resample(500) epochs_LD = epochs_ld['words'].copy().crop(-.200, 0).resample(500) # Reading inverse operator inv_fname_SD = data_path + meg + 'InvOp_SD_EMEG-inv.fif' inv_fname_LD = data_path + meg + 'InvOp_LD_EMEG-inv.fif' inv_op_SD = read_inverse_operator(inv_fname_SD) inv_op_LD = read_inverse_operator(inv_fname_LD) stc_F = apply_inverse_epochs(epochs_f, inv_op_SD, lambda2, method='MNE', pick_ori="normal", return_generator=False) stc_O = apply_inverse_epochs(epochs_o, inv_op_SD, lambda2, method='MNE', pick_ori="normal", return_generator=False) stc_M = apply_inverse_epochs(epochs_m, inv_op_SD, lambda2, method='MNE', pick_ori="normal", return_generator=False) stc_LD = apply_inverse_epochs(epochs_LD, inv_op_LD, lambda2, method='MNE', pick_ori="normal", return_generator=False) src_SD = inv_op_SD['src'] src_LD = inv_op_LD['src'] for k in np.arange(0, 6): # print('[i,win,k]: ',i,win,k) morphed_labels[k].name = C.rois_labels[k] for f in np.arange(0, len(C.con_freq_band) - 1): print('[i,k,f]: ', i, k, f) f_min = C.con_freq_band[f] f_max = C.con_freq_band[f + 1] print(f_min, f_max) labels_ts_f = mne.extract_label_time_course(stc_F, morphed_labels, \ src_SD, mode='mean_flip',return_generator=False) labels_ts_o = mne.extract_label_time_course(stc_O, morphed_labels, \ src_SD, mode='mean_flip',return_generator=False) labels_ts_m = mne.extract_label_time_course(stc_M, morphed_labels, \ src_SD, mode='mean_flip',return_generator=False) labels_ts_ld= mne.extract_label_time_course(stc_LD, morphed_labels, \ src_LD, mode='mean_flip',return_generator=False) con_F, freqs, times, n_epochs, n_tapers = spectral_connectivity( labels_ts_f, method=method, mode='fourier', sfreq=500, fmin=f_min, fmax=f_max, faverage=True, n_jobs=10) con_O, freqs, times, n_epochs, n_tapers = spectral_connectivity( labels_ts_o, method=method, mode='fourier', sfreq=500, fmin=f_min, fmax=f_max, faverage=True, n_jobs=10) con_M, freqs, times, n_epochs, n_tapers = spectral_connectivity( labels_ts_m, method=method, mode='fourier', sfreq=500, fmin=f_min, fmax=f_max, faverage=True, n_jobs=10) # con_LD, freqs, times, n_epochs, n_tapers = spectral_connectivity( # labels_ts_ld, method=method, mode='fourier', # sfreq=500, fmin=f_min, fmax=f_max, faverage=True, n_jobs=10) # con_SD=(con_F+ con_O+ con_M)/3 # con_labels_SD[f]= con_SD.reshape(6,6) # con_labels_LD[f]= con_LD.reshape(6,6) con_labels_F[f] = con_F.reshape(6, 6) con_labels_M[f] = con_M.reshape(6, 6) con_labels_O[f] = con_O.reshape(6, 6) # with open(stc_SD_file_name, "wb") as fp: #Pickling # pickle.dump(con_labels_SD, fp) # with open(stc_LD_file_name, "wb") as fp: #Pickling # pickle.dump(con_labels_LD, fp) with open(stc_F_file_name, "wb") as fp: #Pickling pickle.dump(con_labels_F, fp) with open(stc_M_file_name, "wb") as fp: #Pickling pickle.dump(con_labels_M, fp) with open(stc_O_file_name, "wb") as fp: #Pickling pickle.dump(con_labels_O, fp) e = time.time() print(e - s)
# Set up pick list include = [] # Add a bad channel raw.info['bads'] += ['EEG 053'] # bads + 1 more # pick MEG channels picks = pick_types(raw.info, meg=True, eeg=False, stim=False, eog=True, include=include, exclude='bads') # Read epochs epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=dict(mag=4e-12, grad=4000e-13, eog=150e-6)) # Compute inverse solution and stcs for each epoch stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, method, label, pick_ori="normal") mean_stc = sum(stcs) / len(stcs) # compute sign flip to avoid signal cancelation when averaging signed values flip = mne.label_sign_flip(label, inverse_operator['src']) label_mean = np.mean(mean_stc.data, axis=0) label_mean_flip = np.mean(flip[:, np.newaxis] * mean_stc.data, axis=0) ############################################################################### # View activation time-series plt.figure() h0 = plt.plot(1e3 * stcs[0].times, mean_stc.data.T, 'k') h1, = plt.plot(1e3 * stcs[0].times, label_mean, 'r', linewidth=3) h2, = plt.plot(1e3 * stcs[0].times, label_mean_flip, 'g', linewidth=3)
def get_mne_sample(tmin=-0.1, tmax=0.4, baseline=(None, 0), sns=False, src=None, sub="modality=='A'", fixed=False, snr=2, method='dSPM', rm=False, stc=False): """Load events and epochs from the MNE sample data Parameters ---------- tmin, tmax baseline : Epoch parameters. sns : bool Add sensor space data as NDVar as ``ds['sns']``. src : None | 'ico' | 'vol' Add source space data as NDVar as ``ds['src']``. sub : str | None Expresion for subset of events to load. For a very small dataset use e.g. ``[0,1]``. fixed : bool MNE inverse parameter. snr : scalar MNE inverse parameter. method : str MNE inverse parameter. rm : bool Pretend to be a repeated measures dataset (adds 'subject' variable). stc : bool Add mne SourceEstimate for source space data as ``ds['stc']``. Returns ------- ds : Dataset Dataset with epochs from the MNE sample dataset. """ data_dir = mne.datasets.sample.data_path() meg_dir = os.path.join(data_dir, 'MEG', 'sample') raw_file = os.path.join(meg_dir, 'sample_audvis_filt-0-40_raw.fif') event_file = os.path.join(meg_dir, 'sample_audvis_filt-0-40-eve.fif') subjects_dir = os.path.join(data_dir, 'subjects') subject = 'sample' label_path = os.path.join(subjects_dir, subject, 'label', '%s.label') if not os.path.exists(event_file): raw = mne.io.Raw(raw_file) events = mne.find_events(raw, stim_channel='STI 014') mne.write_events(event_file, events) ds = load.fiff.events(raw_file, events=event_file) ds.index() ds.info['subjects_dir'] = subjects_dir ds.info['subject'] = subject ds.info['label'] = label_path # get the trigger variable form the dataset for eaier access trigger = ds['trigger'] # use trigger to add various labels to the dataset ds['condition'] = Factor(trigger, labels={ 1: 'LA', 2: 'RA', 3: 'LV', 4: 'RV', 5: 'smiley', 32: 'button' }) ds['side'] = Factor(trigger, labels={ 1: 'L', 2: 'R', 3: 'L', 4: 'R', 5: 'None', 32: 'None' }) ds['modality'] = Factor(trigger, labels={ 1: 'A', 2: 'A', 3: 'V', 4: 'V', 5: 'None', 32: 'None' }) if rm: ds = ds.sub('trigger < 5') ds = ds.equalize_counts('side % modality') subject_f = ds.eval('side % modality').enumerate_cells() ds['subject'] = subject_f.as_factor('s%r', random=True) if sub: ds = ds.sub(sub) load.fiff.add_mne_epochs(ds, tmin, tmax, baseline) if sns: ds['sns'] = load.fiff.epochs_ndvar(ds['epochs'], data='mag') if src is None: return ds elif src == 'ico': src_tag = 'ico-4' elif src == 'vol': src_tag = 'vol-10' else: raise ValueError("src = %r" % src) epochs = ds['epochs'] # get inverse operator inv_file = os.path.join(meg_dir, 'sample_eelbrain_%s-inv.fif' % src_tag) if os.path.exists(inv_file): inv = mne.minimum_norm.read_inverse_operator(inv_file) else: fwd_file = os.path.join(meg_dir, 'sample-%s-fwd.fif' % src_tag) bem_dir = os.path.join(subjects_dir, subject, 'bem') bem_file = os.path.join(bem_dir, 'sample-5120-5120-5120-bem-sol.fif') trans_file = os.path.join(meg_dir, 'sample_audvis_raw-trans.fif') if os.path.exists(fwd_file): fwd = mne.read_forward_solution(fwd_file) else: src_ = _mne_source_space(subject, src_tag, subjects_dir) fwd = mne.make_forward_solution(epochs.info, trans_file, src_, bem_file, fwd_file) cov_file = os.path.join(meg_dir, 'sample_audvis-cov.fif') cov = mne.read_cov(cov_file) inv = mn.make_inverse_operator(epochs.info, fwd, cov, None, None, fixed) mne.minimum_norm.write_inverse_operator(inv_file, inv) ds.info['inv'] = inv stcs = mn.apply_inverse_epochs(epochs, inv, 1. / (snr**2), method) ds['src'] = load.fiff.stc_ndvar(stcs, subject, src_tag, subjects_dir, method, fixed) if stc: ds['stc'] = stcs return ds
n_times = len(epochs.times) n_vertices = 3732 n_epochs = len(epochs.events) # Load data and compute inverse solution and stcs for each epoch. noise_cov = mne.read_cov(fname_cov) inverse_operator = read_inverse_operator(fname_inv) X = np.zeros([n_epochs, n_vertices, n_times]) # to save memory, we'll load and transform our epochs step by step. for condition_count, ep in zip([0, n_epochs / 2], epochs_list): stcs = apply_inverse_epochs( ep, inverse_operator, lambda2, method, pick_ori="normal", # saves us memory return_generator=True) for jj, stc in enumerate(stcs): X[condition_count + jj] = stc.lh_data ############################################################################### # Decoding in sensor space using a linear SVM # Make arrays X and y such that : # X is 3d with X.shape[0] is the total number of epochs to classify # y is filled with integers coding for the class to predict # We must have X.shape[0] equal to y.shape[0] # we know the first half belongs to the first class, the second one
def compute_sources(subject, run, mri_available=True): epochs_fname = get_SAflow_bids(FOLDERPATH, subject, run, stage='epo')[1] epochs = read_epochs(epochs_fname) info = epochs.info noise_fname = '/storage/Yann/saflow_DATA/saflow_bids/sub-06/ses-recording/meg/sub-06_ses-recording_NOISE_meg.ds' noise_raw = read_raw_ctf(noise_fname, preload=True) noise_raw.pick_channels(epochs.info['ch_names']) # Choose channels cov = mne.compute_raw_covariance(noise_raw, method='shrunk', cv=5, tmin=0, tmax=0.8) #change tmin and tmax ? src = mne.setup_source_space('sub-' + str(subject), subjects_dir=subjects_dir, add_dist=False) fname_src_fsaverage = subjects_dir + '/fsaverage/bem/fsaverage-vol-5-src.fif' surface = op.join(subjects_dir, 'sub-' + str(subject), 'bem', 'inner_skull.surf') vol_src = mne.setup_volume_source_space( 'sub-' + str(subject), subjects_dir=subjects_dir, mri='aseg.mgz', surface=surface) #,volume_label='Right-Pallidum') trans = get_SAflow_bids(FOLDERPATH, subject, run, stage='epotrans')[1] conductivity = (0.3, ) # for single layer fwd_filename = get_SAflow_bids(FOLDERPATH, subject, run, stage='epofwd')[1] #if not op.isfile(fwd_filename): model = mne.make_bem_model(subject='sub-' + str(subject), ico=4, conductivity=conductivity, subjects_dir=subjects_dir) bem = mne.make_bem_solution(model) fwd = mne.make_forward_solution(info, trans, vol_src, bem, eeg=False) mne.write_forward_solution(fwd_filename, fwd, overwrite=True) # else: # fwd = mne.read_forward_solution(fwd_filename) inverse_operator = make_inverse_operator(info, fwd, cov, loose=1) snr = 1.0 lambda2 = 1.0 / snr**2 for j, epoch in enumerate(epochs): print('Epoch {} of {}'.format(j, len(epochs))) epoch = epoch[np.newaxis, ...] epoch = EpochsArray(epoch, info) epoch.pick_types(meg='mag') if method == 'dSPM': stc = apply_inverse_epochs(epoch, inverse_operator, lambda2, method='dSPM') src_fs = mne.read_source_spaces(fname_src_fsaverage) morph = mne.compute_source_morph(inverse_operator['src'], subject_from='sub-' + str(subject), subjects_dir=subjects_dir, src_to=src_fs, verbose=True) stc_fsaverage = morph.apply(stc[0]) savepath = get_SAflow_bids(FOLDERPATH, subject, run, stage='eposources')[1] stc_fsaverage.save(savepath) del stc_fsaverage
lambda2 = 1.0 / snr ** 2 method = "dSPM" # use dSPM method (could also be MNE or sLORETA) n_times = len(epochs.times) n_vertices = 3732 n_epochs = len(epochs.events) # Load data and compute inverse solution and stcs for each epoch. noise_cov = mne.read_cov(fname_cov) inverse_operator = read_inverse_operator(fname_inv) X = np.zeros([n_epochs, n_vertices, n_times]) # to save memory, we'll load and transform our epochs step by step. for condition_count, ep in zip([0, n_epochs / 2], epochs_list): stcs = apply_inverse_epochs(ep, inverse_operator, lambda2, method, pick_normal=True, # this saves us memory return_generator=True) for jj, stc in enumerate(stcs): X[condition_count + jj] = stc.lh_data ############################################################################### # Decoding in sensor space using a linear SVM # Make arrays X and y such that : # X is 3d with X.shape[0] is the total number of epochs to classify # y is filled with integers coding for the class to predict # We must have X.shape[0] equal to y.shape[0] # we know the first half belongs to the first class, the second one y = np.repeat([0, 1], len(X) / 2) # belongs to the second class X = X.reshape(n_epochs, n_vertices * n_times)
regexp="Bro", subjects_dir=subjects_dir) labels_sel = [labels[6], labels[7]] inverse_operator = read_inverse_operator(mne_folder + "%s-inv.fif" % subject) src = inverse_operator["src"] epochs = mne.read_epochs(epochs_folder + "%s_trial_start-epo.fif" % subject) # epochs.drop_bad_epochs(reject_params) # epochs.resample(250, n_jobs=4) for condition in conditions: stcs = apply_inverse_epochs(epochs[condition], inverse_operator, lambda2, method, pick_ori=None) for label in labels_sel: label_ts = [] for j in range(len(stcs)): ts = mne.extract_label_time_course(stcs[j], labels=label, src=src, mode="pca_flip") ts = np.squeeze(ts) ts *= np.sign(ts[np.argmax(np.abs(ts))]) label_ts.append(ts) label_ts = np.asarray(label_ts)
# load labels labels = mne.read_labels_from_annot('0001', parc='PALS_B12_Lobes', # regexp="Bro", subjects_dir=subjects_dir) labels_occ = [labels[9], labels[10]] # Load data inverse_operator = read_inverse_operator(fname_inv) epochs = mne.read_epochs(fname_epochs) epochs.crop(0, 1.1) epochs.resample(200) stcs_ent_left = apply_inverse_epochs(epochs["ent_left"], inverse_operator, lambda2, method, pick_ori="normal") stcs_ent_right = apply_inverse_epochs(epochs["ent_right"], inverse_operator, lambda2, method, pick_ori="normal") stcs_ctl_left = apply_inverse_epochs(epochs["ctl_left"], inverse_operator, lambda2, method, pick_ori="normal") #src_ctl_l = np.asarray([stc.data.reshape(-1) for stc in stcs_ctl_left]) #src_ent_l = np.asarray([stc.data.reshape(-1) for stc in stcs_ent_left]) #data_ctl_l = np.squeeze(np.asarray( # mne.extract_label_time_course(stcs_ctl_left, # labels_occ[1], # inverse_operator["src"], # mode="pca_flip")))
exclude='bads') # Read epochs epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=(None, 0), preload=True, reject=dict(grad=4000e-13, eog=150e-6), decim=5) # decimate to save memory and increase speed ############################################################################### # Compute inverse solution snr = 3.0 noise_cov = mne.read_cov(fname_cov) inverse_operator = read_inverse_operator(fname_inv) stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2=1.0 / snr ** 2, verbose=False, method="dSPM", pick_ori="normal") ############################################################################### # Decoding in sensor space using a logistic regression # Retrieve source space data into an array X = np.array([stc.lh_data for stc in stcs]) # only keep left hemisphere y = epochs.events[:, 2] # prepare a series of classifier applied at each time sample clf = make_pipeline(StandardScaler(), # z-score normalization SelectKBest(f_classif, k=500), # select features for speed LinearModel(LogisticRegression(C=1))) time_decod = SlidingEstimator(clf, scoring='roc_auc')
def _compute_power_envelopes(subject, kind, freqs): ########################################################################### # Compute source space # ------------------- src = mne.setup_source_space(subject, spacing='oct6', add_dist=False, subjects_dir=cfg.mne_camcan_freesurfer_path) trans = trans_map[subject] bem = cfg.mne_camcan_freesurfer_path + \ "/%s/bem/%s-meg-bem.fif" % (subject, subject) ########################################################################### # Compute handle MEG data # ----------------------- fname = op.join(cfg.camcan_meg_raw_path, subject, kind, '%s_raw.fif' % kind) raw = mne.io.read_raw_fif(fname) mne.channels.fix_mag_coil_types(raw.info) if DEBUG: # raw.crop(0, 180) raw.crop(0, 120) else: raw.crop(0, 300) raw = _run_maxfilter(raw, subject, kind) _compute_add_ssp_exg(raw) # get empty room fname_er = op.join(cfg.camcan_meg_path, "emptyroom", subject, "emptyroom_%s.fif" % subject) raw_er = mne.io.read_raw_fif(fname_er) mne.channels.fix_mag_coil_types(raw.info) raw_er = _run_maxfilter(raw_er, subject, kind, coord_frame="meg") raw_er.info["projs"] += raw.info["projs"] cov = mne.compute_raw_covariance(raw_er, method='oas') # compute before band-pass of interest event_length = 5. event_overlap = 0. raw_length = raw.times[-1] events = mne.make_fixed_length_events(raw, duration=event_length, start=0, stop=raw_length - event_length) ####################################################################### # Compute the forward and inverse # ------------------------------- info = mne.Epochs(raw, events=events, tmin=0, tmax=event_length, baseline=None, reject=None, preload=False, decim=10).info fwd = mne.make_forward_solution(info, trans, src, bem) inv = make_inverse_operator(info, fwd, cov) del fwd ####################################################################### # Compute label time series and do envelope correlation # ----------------------------------------------------- mne_subjects_dir = "/storage/inria/agramfor/MNE-sample-data/subjects" labels = mne.read_labels_from_annot('fsaverage', 'aparc_sub', subjects_dir=mne_subjects_dir) labels = mne.morph_labels(labels, subject_from='fsaverage', subject_to=subject, subjects_dir=cfg.mne_camcan_freesurfer_path) labels = [ll for ll in labels if 'unknown' not in ll.name] results = dict() for fmin, fmax, band in freqs: print(f"computing {subject}: {fmin} - {fmax} Hz") this_raw = raw.copy() this_raw.filter(fmin, fmax, n_jobs=1) reject = _get_global_reject_epochs(this_raw, decim=5) this_raw.apply_hilbert(envelope=False) epochs = mne.Epochs(this_raw, events=events, tmin=0, tmax=event_length, baseline=None, reject=reject, preload=True, decim=5) if DEBUG: epochs = epochs[:3] result = { 'subject': subject, 'fmin': fmin, 'fmax': fmax, 'band': band, 'label_names': [ll.name for ll in labels] } stcs = apply_inverse_epochs(epochs, inv, lambda2=1. / 9., pick_ori='normal', method='MNE', return_generator=True) label_ts = np.concatenate(mne.extract_label_time_course( stcs, labels, inv['src'], mode="pca_flip", return_generator=False), axis=-1) result['cov'], _ = oas(np.abs(label_ts).T, assume_centered=False) for orth in ("pairwise", False): corr = envelope_correlation(label_ts[np.newaxis], combine="mean", orthogonalize=orth) result[f"corr{'_orth' if orth else ''}"] = corr[np.triu_indices( len(corr))] results[band] = result if False: # failsafe mode with intermediate steps written out out_fname = op.join( cfg.derivative_path, f'{subject + ("-debug" if DEBUG else "")}_' f'power_envelopes_{band}.h5') mne.externals.h5io.write_hdf5(out_fname, result, overwrite=True) return results
def compute_ROIs_inv_sol(raw_filename, sbj_id, sbj_dir, fwd_filename, cov_fname, is_epoched=False, event_id=None, t_min=None, t_max=None, is_evoked=False, events_id=[], snr=1.0, inv_method='MNE', parc='aparc', aseg=False, aseg_labels=[], is_blind=False, labels_removed=[], save_stc=False): import os import os.path as op import numpy as np import mne import pickle from mne.io import read_raw_fif from mne import read_epochs from mne.minimum_norm import make_inverse_operator, apply_inverse_raw from mne.minimum_norm import apply_inverse_epochs, apply_inverse from mne import get_volume_labels_from_src from nipype.utils.filemanip import split_filename as split_f from neuropype_ephy.preproc import create_reject_dict try: traits.undefined(event_id) except NameError: event_id = None print '\n*** READ raw filename %s ***\n' % raw_filename if is_epoched and event_id is None: epochs = read_epochs(raw_filename) info = epochs.info else: raw = read_raw_fif(raw_filename) info = raw.info subj_path, basename, ext = split_f(info['filename']) print '\n*** READ noise covariance %s ***\n' % cov_fname noise_cov = mne.read_cov(cov_fname) print '\n*** READ FWD SOL %s ***\n' % fwd_filename forward = mne.read_forward_solution(fwd_filename) if not aseg: forward = mne.convert_forward_solution(forward, surf_ori=True, force_fixed=False) lambda2 = 1.0 / snr ** 2 # compute inverse operator print '\n*** COMPUTE INV OP ***\n' if not aseg: loose = 0.2 depth = 0.8 else: loose = None depth = None inverse_operator = make_inverse_operator(info, forward, noise_cov, loose=loose, depth=depth, fixed=False) # apply inverse operator to the time windows [t_start, t_stop]s print '\n*** APPLY INV OP ***\n' if is_epoched and event_id is not None: events = mne.find_events(raw) picks = mne.pick_types(info, meg=True, eog=True, exclude='bads') reject = create_reject_dict(info) if is_evoked: epochs = mne.Epochs(raw, events, events_id, t_min, t_max, picks=picks, baseline=(None, 0), reject=reject) evoked = [epochs[k].average() for k in events_id] snr = 3.0 lambda2 = 1.0 / snr ** 2 ev_list = events_id.items() for k in range(len(events_id)): stc = apply_inverse(evoked[k], inverse_operator, lambda2, inv_method, pick_ori=None) print '\n*** STC for event %s ***\n' % ev_list[k][0] stc_file = op.abspath(basename + '_' + ev_list[k][0]) print '***' print 'stc dim ' + str(stc.shape) print '***' if not aseg: stc.save(stc_file) else: epochs = mne.Epochs(raw, events, event_id, t_min, t_max, picks=picks, baseline=(None, 0), reject=reject) stc = apply_inverse_epochs(epochs, inverse_operator, lambda2, inv_method, pick_ori=None) print '***' print 'len stc %d' % len(stc) print '***' elif is_epoched and event_id is None: stc = apply_inverse_epochs(epochs, inverse_operator, lambda2, inv_method, pick_ori=None) print '***' print 'len stc %d' % len(stc) print '***' else: stc = apply_inverse_raw(raw, inverse_operator, lambda2, inv_method, label=None, start=None, stop=None, buffer_size=1000, pick_ori=None) # None 'normal' print '***' print 'stc dim ' + str(stc.shape) print '***' if save_stc: if aseg: for i in range(len(stc)): try: os.mkdir(op.join(subj_path, 'TS')) except OSError: pass stc_file = op.join(subj_path, 'TS', basename + '_' + inv_method + '_stc_' + str(i) + '.npy') if not op.isfile(stc_file): np.save(stc_file, stc[i].data) labels_cortex = mne.read_labels_from_annot(sbj_id, parc=parc, subjects_dir=sbj_dir) if is_blind: for l in labels_cortex: if l.name in labels_removed: print l.name labels_cortex.remove(l) print '\n*** %d ***\n' % len(labels_cortex) src = inverse_operator['src'] # allow_empty : bool -> Instead of emitting an error, return all-zero time # courses for labels that do not have any vertices in the source estimate label_ts = mne.extract_label_time_course(stc, labels_cortex, src, mode='mean', allow_empty=True, return_generator=False) # save results in .npy file that will be the input for spectral node print '\n*** SAVE ROI TS ***\n' print len(label_ts) ts_file = op.abspath(basename + '_ROI_ts.npy') np.save(ts_file, label_ts) if aseg: print sbj_id labels_aseg = get_volume_labels_from_src(src, sbj_id, sbj_dir) labels = labels_cortex + labels_aseg else: labels = labels_cortex print labels[0].pos print len(labels) labels_file = op.abspath('labels.dat') with open(labels_file, "wb") as f: pickle.dump(len(labels), f) for value in labels: pickle.dump(value, f) label_names_file = op.abspath('label_names.txt') label_coords_file = op.abspath('label_coords.txt') label_names = [] label_coords = [] for value in labels: label_names.append(value.name) # label_coords.append(value.pos[0]) label_coords.append(np.mean(value.pos, axis=0)) np.savetxt(label_names_file, np.array(label_names, dtype=str), fmt="%s") np.savetxt(label_coords_file, np.array(label_coords, dtype=float), fmt="%f %f %f") return ts_file, labels_file, label_names_file, label_coords_file
'_isi-epo.fif') # Sort epochs according to experimental conditions in the post-stimulus interval #slow_epo_isi = allepochs.__getitem__('V1') #fast_epo_isi = allepochs.__getitem__('V3') fname_inv = savepath + subject + '/' + subject + '_inv' inverse_operator = read_inverse_operator(fname_inv, verbose=None) src = inverse_operator['src'] snr = 1.0 # use lower SNR for single epochs lambda2 = 1.0 / snr**2 # Source Estimates stcs_fast = apply_inverse_epochs(allepochs, inverse_operator, lambda2, method='sLORETA', pick_ori="normal") # Read labels v1_label = mne.read_label(datapath + 'Results_Alpha_and_Gamma/' + subject + '/' + subject + '_V1_rh.label') v4_label = mne.read_label(datapath + 'Results_Alpha_and_Gamma/' + subject + '/' + subject + '_V4_rh.label') # Extract Source Estimates from labels stcs_fast_v1 = apply_inverse_epochs(allepochs, inverse_operator, lambda2, method='sLORETA', pick_ori="normal", label=v1_label)
subject_name = "eric_sps_04" sps_dir = os.environ['SPS_DIR'] subjects_dir = mne.utils.get_subjects_dir() epochs, inv, labels_morphed = load_subject_data(subject_name, sps_dir, subjects_dir, labels, epochs_fmt=epochs_fmt, sps_dir=sps_dir, tmin=tmin, tmax=tmax, cond=cond) # compute inverse solutions print("applying inverse operators") ep_inv = apply_inverse_epochs(epochs, inv, lambda2=1.0 / 9, method='MNE') ep_inv_ndarray = np.array([np.ascontiguousarray(ep.data.T) for ep in ep_inv]) # get vertex indices for each label src = inv['src'] roiidx = list() vertidx = list() n_lhverts = len(src[0]['vertno']) n_rhverts = len(src[1]['vertno']) n_verts = n_lhverts + n_rhverts offsets = {'lh': 0, 'rh': n_lhverts} for li, lab in enumerate(labels): if isinstance(lab, mne.Label): comp_labs = [lab]
def _compute_inverse_solution(raw_filename, sbj_id, subjects_dir, fwd_filename, cov_fname, is_epoched=False, events_id=None, events_file=None, t_min=None, t_max=None, is_evoked=False, snr=1.0, inv_method='MNE', parc='aparc', aseg=False, aseg_labels=[], all_src_space=False, ROIs_mean=True, is_fixed=False): """ Compute the inverse solution on raw/epoched data and return the average time series computed in the N_r regions of the source space defined by the specified cortical parcellation Inputs raw_filename : str filename of the raw/epoched data sbj_id : str subject name subjects_dir : str Freesurfer directory fwd_filename : str filename of the forward operator cov_filename : str filename of the noise covariance matrix is_epoched : bool if True and events_id = None the input data are epoch data in the format -epo.fif if True and events_id is not None, the raw data are epoched according to events_id and t_min and t_max values events_id: dict the dict of events t_min, t_max: int define the time interval in which to epoch the raw data is_evoked: bool if True the raw data will be averaged according to the events contained in the dict events_id inv_method : str the inverse method to use; possible choices: MNE, dSPM, sLORETA snr : float the SNR value used to define the regularization parameter parc: str the parcellation defining the ROIs atlas in the source space aseg: bool if True a mixed source space will be created and the sub cortical regions defined in aseg_labels will be added to the source space aseg_labels: list list of substructures we want to include in the mixed source space all_src_space: bool if True we compute the inverse for all points of the s0urce space ROIs_mean: bool if True we compute the mean of estimated time series on ROIs Outputs ts_file : str filename of the file where are saved the estimated time series labels_file : str filename of the file where are saved the ROIs of the parcellation label_names_file : str filename of the file where are saved the name of the ROIs of the parcellation label_coords_file : str filename of the file where are saved the coordinates of the centroid of the ROIs of the parcellation """ print(('\n*** READ raw filename %s ***\n' % raw_filename)) if is_epoched and events_id == {}: epochs = read_epochs(raw_filename) info = epochs.info else: raw = read_raw_fif(raw_filename, preload=True) info = raw.info subj_path, basename, ext = split_f(raw_filename) print(('\n*** READ noise covariance %s ***\n' % cov_fname)) noise_cov = mne.read_cov(cov_fname) print(('\n*** READ FWD SOL %s ***\n' % fwd_filename)) forward = mne.read_forward_solution(fwd_filename) # TODO check use_cps for force_fixed=True if not aseg: print(('\n*** fixed orientation {} ***\n'.format(is_fixed))) # is_fixed=True => to convert the free-orientation fwd solution to # (surface-oriented) fixed orientation. forward = mne.convert_forward_solution(forward, surf_ori=True, force_fixed=is_fixed, use_cps=False) lambda2 = 1.0 / snr**2 # compute inverse operator print('\n*** COMPUTE INV OP ***\n') if is_fixed: loose = 0 depth = None pick_ori = None elif aseg: loose = 1 depth = None pick_ori = None else: loose = 0.2 depth = 0.8 pick_ori = 'normal' print(('\n *** loose {} depth {} ***\n'.format(loose, depth))) inverse_operator = make_inverse_operator(info, forward, noise_cov, loose=loose, depth=depth, fixed=is_fixed) # apply inverse operator to the time windows [t_start, t_stop]s print('\n*** APPLY INV OP ***\n') good_events_file = '' print(events_id) if is_epoched and events_id != {}: if events_file: events = mne.read_events(events_file) else: events = mne.find_events(raw) picks = mne.pick_types(info, meg=True, eog=True, exclude='bads') reject = _create_reject_dict(info) if is_evoked: epochs = mne.Epochs(raw, events, events_id, t_min, t_max, picks=picks, baseline=(t_min, 0), reject=reject) evoked = [epochs[k].average() for k in events_id] snr = 3.0 lambda2 = 1.0 / snr**2 ev_list = list(events_id.items()) for k in range(len(events_id)): stc = apply_inverse(evoked[k], inverse_operator, lambda2, inv_method, pick_ori=pick_ori) print(('\n*** STC for event %s ***\n' % ev_list[k][0])) stc_file = op.abspath(basename + '_' + ev_list[k][0]) print('***') print(('stc dim ' + str(stc.shape))) print('***') else: epochs = mne.Epochs(raw, events, events_id, t_min, t_max, picks=picks, baseline=(None, 0), reject=reject) epochs.drop_bad() good_events_file = op.abspath('good_events.txt') np.savetxt(good_events_file, epochs.events) stc = apply_inverse_epochs(epochs, inverse_operator, lambda2, inv_method, pick_ori=pick_ori) elif is_epoched and events_id == {}: stc = apply_inverse_epochs(epochs, inverse_operator, lambda2, inv_method, pick_ori=pick_ori) else: stc = apply_inverse_raw(raw, inverse_operator, lambda2, inv_method, label=None, start=None, stop=None, buffer_size=1000, pick_ori=pick_ori) # None 'normal' if not isinstance(stc, list): print('***') print(('stc dim ' + str(stc.shape))) print('***') stc = [stc] else: print('***') print(('len stc %d' % len(stc))) print('***') print('**************************************************************') print('all_src_space: {}'.format(all_src_space)) print('ROIs_mean: {}'.format(ROIs_mean)) print('**************************************************************') if all_src_space: stc_data = list() stc_file = op.abspath(basename + '_stc.hdf5') for i in range(len(stc)): stc_data.append(stc[i].data) write_hdf5(stc_file, stc_data, dataset_name='stc_data') if ROIs_mean: label_ts, labels_file, label_names_file, label_coords_file = \ _compute_mean_ROIs(stc, sbj_id, subjects_dir, parc, inverse_operator, forward, aseg, is_fixed) ts_file = op.abspath(basename + '_ROI_ts.npy') np.save(ts_file, label_ts) else: ts_file = stc_file labels_file = '' label_names_file = '' label_coords_file = '' return ts_file, labels_file, label_names_file, \ label_coords_file, good_events_file
events = mne.read_events(fname_event) # Set up pick list include = [] exclude = raw.info['bads'] + ['EEG 053'] # bads + 1 more # pick MEG channels picks = pick_types(raw.info, meg=True, eeg=False, stim=False, eog=True, include=include, exclude=exclude) # Read epochs epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=dict(mag=4e-12, grad=4000e-13, eog=150e-6)) # Compute inverse solution and stcs for each epoch stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, method, label, pick_normal=True) data = sum(stc.data for stc in stcs) / len(stcs) # compute sign flip to avoid signal cancelation when averaging signed values flip = mne.label_sign_flip(label, inverse_operator['src']) label_mean = np.mean(data, axis=0) label_mean_flip = np.mean(flip[:, np.newaxis] * data, axis=0) ############################################################################### # View activation time-series pl.figure() h0 = pl.plot(1e3 * stcs[0].times, data.T, 'k') h1 = pl.plot(1e3 * stcs[0].times, label_mean, 'r', linewidth=3) h2 = pl.plot(1e3 * stcs[0].times, label_mean_flip, 'g', linewidth=3)
lambda2 = 1.0 / snr ** 2 method = "dSPM" # use dSPM method (could also be MNE or sLORETA) n_times = len(epochs.times) n_vertices = 3732 n_epochs = len(epochs.events) # Load data and compute inverse solution and stcs for each epoch. noise_cov = mne.read_cov(fname_cov) inverse_operator = read_inverse_operator(fname_inv) X = np.zeros([n_epochs, n_vertices, n_times]) # to save memory, we'll load and transform our epochs step by step. for condition_count, ep in zip([0, n_epochs / 2], epochs_list): stcs = apply_inverse_epochs(ep, inverse_operator, lambda2, method, pick_ori="normal", # saves us memory return_generator=True) for jj, stc in enumerate(stcs): X[condition_count + jj] = stc.lh_data ############################################################################### # Decoding in sensor space using a linear SVM # Make arrays X and y such that : # X is 3d with X.shape[0] is the total number of epochs to classify # y is filled with integers coding for the class to predict # We must have X.shape[0] equal to y.shape[0] # we know the first half belongs to the first class, the second one y = np.repeat([0, 1], len(X) / 2) # belongs to the second class X = X.reshape(n_epochs, n_vertices * n_times)
tmin, tmax, picks=picks, baseline=(None, 0), reject=dict(mag=4e-12, grad=4000e-13, eog=150e-6)) # Get evoked data (averaging across trials in sensor space) evoked = epochs.average() # Compute inverse solution and stcs for each epoch # Use the same inverse operator as with evoked data (i.e., set nave) # If you use a different nave, dSPM just scales by a factor sqrt(nave) stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, method, label, pick_ori="normal", nave=evoked.nave) stc_evoked = apply_inverse(evoked, inverse_operator, lambda2, method, pick_ori="normal") stc_evoked_label = stc_evoked.in_label(label) # Mean across trials but not across vertices in label mean_stc = sum(stcs) / len(stcs)
# Pick MEG channels picks = pick_types(raw.info, meg=True, eeg=False, stim=False, eog=True, exclude='bads') # Define epochs for left-auditory condition event_id, tmin, tmax = 1, -0.2, 0.5 epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=dict(mag=4e-12, grad=4000e-13, eog=150e-6)) # Compute inverse solution and for each epoch. By using "return_generator=True" # stcs will be a generator object instead of a list. snr = 1.0 # use lower SNR for single epochs lambda2 = 1.0 / snr ** 2 method = "dSPM" # use dSPM method (could also be MNE or sLORETA) stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, method, pick_ori="normal", return_generator=True) # Get labels for FreeSurfer 'aparc' cortical parcellation with 34 labels/hemi labels, label_colors = mne.labels_from_parc('sample', parc='aparc', subjects_dir=subjects_dir) # Average the source estimates within each label using sign-flips to reduce # signal cancellations, also here we return a generator src = inverse_operator['src'] label_ts = mne.extract_label_time_course(stcs, labels, src, mode='mean_flip', return_generator=True) # Now we are ready to compute the connectivity in the alpha band. Notice # from the status messages, how mne-python: 1) reads an epoch from the raw # file, 2) applies SSP and baseline correction, 3) computes the inverse to # obtain a source estimate, 4) averages the source estimate to obtain a
def intra(subj): ''' Performs initial computations within subject and returns average PSD and variance of all epochs. ''' print('Now beginning intra processing on ' + subj + '...\n') * 5 # Set function parameters fname_label = subjects_dir + '/' + subj + '/' + 'label/%s.label' % label_name fname_raw = data_path + subj + '/' + subj + '_rest_raw_sss.fif' if os.path.isfile(data_path + subj + '/' + subj + '_rest_raw_sss-ico-4-fwd.fif'): fname_fwd = data_path + subj + '/' + subj + '_rest_raw_sss-ico-4-fwd.fif' else: print('Subject ' + subj + ' does not have a ico-4-fwd.fif on file.') if label_name.startswith('lh.'): hemi = 'left' elif label_name.startswith('rh.'): hemi = 'right' # Load data label = mne.read_label(fname_label) raw = fiff.Raw(fname_raw) forward_meg = mne.read_forward_solution(fname_fwd) # Estimate noise covariance from teh raw data cov = mne.compute_raw_data_covariance(raw, reject=dict(eog=150e-6)) write_cov(data_path + subj + '/' + subj + '-cov.fif', cov) # Make inverse operator info = raw.info inverse_operator = make_inverse_operator(info, forward_meg, cov, loose=None, depth=0.8) # Epoch data into 4s intervals events = mne.make_fixed_length_events(raw, 1, start=0, stop=None, duration=4.) # Set up pick list: (MEG minus bad channels) include = [] exclude = raw.info['bads'] picks = fiff.pick_types(raw.info, meg=True, eeg=False, stim=False, eog=True, include=include, exclude=exclude) # Read epochs and remove bad epochs epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks, baseline=(None, 0), preload=True, reject=dict(grad=4000e-13, mag=4e-12, eog=150e-6)) # Pull data for averaging later epc_array = epochs.get_data() # Compute the inverse solution inv = apply_inverse_epochs(epochs, inverse_operator, lambda2, method, label=label) #Need to add a line here to automatically create stc directory within subj epoch_num = 1 epoch_num_str = str(epoch_num) for i in inv: # i.save(data_path + subj + '/tmp/' + label_name[3:] + '_rest_raw_sss-oct-6-inv' + epoch_num_str) i.save(data_path + subj + '/tmp/' + label_name[3:] + '_rest_raw_sss-ico-4-inv' + epoch_num_str) epoch_num = epoch_num + 1 epoch_num_str = str(epoch_num) # The following is used to remove the empty opposing hemisphere files # and then move the files to save into the appropriate directory if hemi == 'left': filelist = [ f for f in os.listdir(data_path + subj + '/tmp') if f.endswith("-rh.stc") ] for f in filelist: os.remove(data_path + subj + '/tmp/' + f) keepers = [ f for f in os.listdir(data_path + subj + '/tmp') if f.endswith("-lh.stc") ] for f in keepers: src = f os.rename(data_path + subj + '/tmp/' + src, data_path + subj + '/inv/' + src) elif hemi == 'right': filelist = [ f for f in os.listdir(data_path + subj + '/tmp') if f.endswith("-lh.stc") ] for f in filelist: os.remove(data_path + subj + '/tmp/' + f) keepers = [ f for f in os.listdir(data_path + subj + '/tmp') if f.endswith("-rh.stc") ] for f in keepers: src = f os.rename(data_path + subj + '/tmp/' + src, data_path + subj + '/inv/' + src) # define frequencies of interest bandwidth = 4. # bandwidth of the windows in Hz # compute source space psd in label # Note: By using "return_generator=True" stcs will be a generator object # instead of a list. This allows us so to iterate without having to # keep everything in memory. psd = compute_source_psd_epochs(epochs, inverse_operator, lambda2=lambda2, method=method, fmin=fmin, fmax=fmax, bandwidth=bandwidth, label=label, return_generator=False) epoch_num = 1 epoch_num_str = str(epoch_num) for i in psd: i.save(data_path + subj + '/' + 'tmp' + '/' + label_name[3:] + '_dspm_snr-1_PSD'+ epoch_num_str) epoch_num = epoch_num + 1 epoch_num_str = str(epoch_num) if hemi == 'left': filelist = [ f for f in os.listdir(data_path + subj + '/tmp') if f.endswith("-rh.stc") ] for f in filelist: os.remove(data_path + subj + '/tmp/' + f) keepers = [ f for f in os.listdir(data_path + subj + '/tmp') if f.endswith("-lh.stc") ] for f in keepers: src = f os.rename(data_path + subj + '/tmp/' + src,data_path + subj + '/psd/' + src) elif hemi == 'right': filelist = [ f for f in os.listdir(data_path + subj + '/tmp') if f.endswith("-lh.stc") ] for f in filelist: os.remove(data_path + subj + '/tmp/' + f) keepers = [ f for f in os.listdir(data_path + subj + '/tmp') if f.endswith("-rh.stc") ] for f in keepers: src = f os.rename(data_path + subj + '/tmp/' + src,data_path + subj + '/psd/' + src) # This code computes the average PSDs of each epoch. Each PSD file is an array of shape N_vertices*N_frequencies. This code averages the PSD value of each vertex together and outputs the average PSD value of each frequency. Then, it averages the PSD values of each epoch, outputting one average PSD value per frequency value, i.e., this is the average across epochs. n_epochs = len(epc_array) for i, stc in enumerate(psd): if i >= n_epochs: break if i == 0: psd_avg = np.mean(stc.data, axis=0) else: psd_avg += np.mean(stc.data, axis=0) print('Length of psd for subject ' + subj + ' is ' + str(len(psd)) + '.') print('Number of epochs for subject ' + subj + ' is ' + str(n_epochs) + '.') if len(psd) != 0: psd_avg /= n_epochs # Compute variance for each epoch and then variance across epochs n_epochs = len(epc_array) for i, stc in enumerate(psd): if i >= n_epochs: psd_var = np.array() break if i == 0: psd_var = np.var(stc.data, axis=0) else: psd_var = np.vstack((psd_var,np.var(stc.data, axis=0))) if len(psd) >= 2: tot_var = np.var(psd_var, axis=0) if len(psd) <= 1: failed_subj = subj print(failed_subj + ' failed. No PSD values calculated, likely because all epochs were rejected.') return failed_subj, failed_subj, failed_subj if len(psd) >= 2: return (psd_avg, tot_var, len(psd_avg))