def preprocess_raw_ica_only(sub_id, session): """ This function removes the ICA component that correlates woth the EOG channel(s) best. No filtering or downsampling is applied! """ # SETUP AND LOAD FILES #### # name with subject id & session name fname = "sub_%d_%s" % (sub_id, session) # load the raw fif print '\nLoading raw file' raw = fiff.Raw(fname + "_tsss_mc.fif", preload=True) picks = mne.fiff.pick_types(raw.info, meg=True, eeg=False, eog=False, stim=False, exclude='bads') # ICA #### print '\nRun ICA' ica = ICA(n_components=0.90, n_pca_components=64, max_pca_components=100, noise_cov=None, random_state=0) start, stop = None, None # decompose sources for raw data ica.decompose_raw(raw, start=start, stop=stop, picks=picks) corr = lambda x, y: np.array([pearsonr(a, y.ravel()) for a in x])[:, 0] eog_scores_1 = ica.find_sources_raw(raw, target='EOG001', score_func=corr) eog_scores_2 = ica.find_sources_raw(raw, target='EOG002', score_func=corr) # get maximum correlation index for EOG eog_source_idx_1 = np.abs(eog_scores_1).argmax() eog_source_idx_2 = np.abs(eog_scores_2).argmax() # We now add the eog artifacts to the ica.exclusion list if eog_source_idx_1 == eog_source_idx_2: ica.exclude += [eog_source_idx_1] elif eog_source_idx_1 != eog_source_idx_2: ica.exclude += [eog_source_idx_1, eog_source_idx_2] print eog_source_idx_1, eog_source_idx_2 print ica.exclude # Restore sensor space data raw_ica = ica.pick_sources_raw(raw, include=None) # SAVE FILES #### raw_ica.save(fname + '_tsss_mc_preproc_ica.fif', overwrite=True)
and target time series """ correlation, pval = np.array([pearsonr(a, y) for a in x]).T return correlation # As we don't have an ECG channel we use one that correlates a lot with heart # beats: 'MEG 1531'. To improve detection, we filter the the channel and pass # it directly to find sources. The method then returns an array of correlation # scores for each ICA source. ecg_ch_name = 'MEG 1531' l_freq, h_freq = 8, 16 ecg = raw[[raw.ch_names.index(ecg_ch_name)], :][0] ecg = band_pass_filter(ecg, raw.info['sfreq'], l_freq, h_freq) ecg_scores = ica.find_sources_raw(raw, target=ecg, score_func=score_func) # get maximum correlation index for ECG ecg_source_idx = np.abs(ecg_scores).argmax() title = 'ICA source matching ECG' ica.plot_sources_raw(raw, ecg_source_idx, title=title, stop=3.0) # let us have a look which other components resemble the ECG. # We can do this by reordering the plot by our scores using order # and generating sort indices for the sources: ecg_order = np.abs(ecg_scores).argsort()[::-1] # ascending order ica.plot_sources_raw(raw, ecg_order[:15], start=start_plot, stop=stop_plot) ica.plot_topomap(ecg_order[:15], colorbar=False)
# This is to illustrate the way ica.find_sources_raw works. Actually, this is # the default score_func. from scipy.stats import pearsonr corr = lambda x, y: np.array([pearsonr(a, y.ravel()) for a in x])[:, 0] # As we don't have an ECG channel we use one that correlates a lot with heart # beats: 'MEG 1531'. To improve detection, we filter the the channel and pass # it directly to find sources. The method then returns an array of correlation # scores for each ICA source. ecg_ch_name = 'MEG 1531' l_freq, h_freq = 8, 16 ecg = raw[[raw.ch_names.index(ecg_ch_name)], :][0] ecg = band_pass_filter(ecg, raw.info['sfreq'], l_freq, h_freq) ecg_scores = ica.find_sources_raw(raw, target=ecg, score_func=corr) # get maximum correlation index for ECG ecg_source_idx = np.abs(ecg_scores).argmax() title = 'ICA source matching ECG' ica.plot_sources_raw(raw, ecg_source_idx, title=title, stop=3.0) # let us have a look which other components resemble the ECG. # We can do this by reordering the plot by our scores using order # and generating sort indices for the sources: ecg_order = np.abs(ecg_scores).argsort()[::-1][:30] # ascending order ica.plot_sources_raw(raw, ecg_order, start=start_plot, stop=stop_plot) # Let's make our ECG component selection more liberal and include sources
# setup reasonable time window for inspection start_plot, stop_plot = raw.time_as_index([100, 103]) # plot components ica.plot_sources_raw(raw, start=start_plot, stop=stop_plot) ############################################################################### # Automatically find the ECG component using correlation with ECG signal # As we don't have an ECG channel we use one that correlates a lot with heart # beats: 'MEG 1531'. We can directly pass the name to the find_sources method. # We select the pearson correlation from scipy stats via string label. # The function is internally modified to be applicable to 2D arrays and, # hence, returns product-moment correlation scores for each ICA source. eog_scores = ica.find_sources_raw(raw, target='EOG 061', score_func='pearsonr') # get sources for the entire time range. sources = ica.get_sources_raw(raw) # get maximum correlation index for ECG eog_source_idx = np.abs(eog_scores).argmax() ############################################################################### # Find ECG event onsets from ICA source event_id = 999 eog_events = ica_find_eog_events(raw=raw, eog_source=sources[eog_source_idx], event_id=event_id) # Read epochs
############################################################################### # Automatically find the ECG component using correlation with ECG signal # As we don't have an ECG channel we use one that correlates a lot with heart # beats: 'MEG 1531'. We can directly pass the name to the find_sources method. # We select the pearson correlation from scipy stats via string label. # The function is internally modified to be applicable to 2D arrays and, # hence, returns product-moment correlation scores for each ICA source. # pick ECG affected channel ch_idx = raw.ch_names.index('MEG 1531') ecg = raw[ch_idx, :][0] ecg = mne.filter.high_pass_filter(ecg.ravel(), raw.info['sfreq'], 1.0) ecg_scores = ica.find_sources_raw(raw, target=ecg, score_func='pearsonr') # get sources for the entire time range. sources = ica.get_sources_raw(raw) # get maximum correlation index for ECG ecg_source_idx = np.abs(ecg_scores).argmax() # high pass filter source ecg_source = mne.filter.high_pass_filter(sources[ecg_source_idx], raw.info['sfreq'], 1.0) ############################################################################### # Find ECG event onsets from ICA source event_id = 999
def preprocess_raw(sub_id, session): """ This function preprocessess data """ # SETUP AND LOAD FILES #### # name with subject id & session name fname = "sub_%d_%s" % (sub_id, session) # load the raw fif print '\nLoading raw file' raw = fiff.Raw(fname + "_tsss_mc.fif", preload=True) picks = mne.fiff.pick_types(raw.info, meg=True, eeg=False, eog=False, stim=False, exclude='bads') print 'Computing Covariance matrix' cov = mne.compute_raw_data_covariance(raw, picks=picks, reject=None) # FILTER #### # filter raw, lp 128, bp at 50 & 100 raw.filter(None, 128, n_jobs=n_jobs, verbose=True) # steps = np.arange(50, 151, 50) # print '\nBand stop filter at %s' % steps # raw.notch_filter(steps, n_jobs=n_jobs, verbose=True) # ICA #### print '\nRun ICA' ica = ICA(n_components=0.90, n_pca_components=64, max_pca_components=100, noise_cov=None, random_state=0) start, stop = None, None # decompose sources for raw data ica.decompose_raw(raw, start=start, stop=stop, picks=picks) corr = lambda x, y: np.array([pearsonr(a, y.ravel()) for a in x])[:, 0] eog_scores_1 = ica.find_sources_raw(raw, target='EOG001', score_func=corr) eog_scores_2 = ica.find_sources_raw(raw, target='EOG002', score_func=corr) # get maximum correlation index for EOG eog_source_idx_1 = np.abs(eog_scores_1).argmax() eog_source_idx_2 = np.abs(eog_scores_2).argmax() # We now add the eog artifacts to the ica.exclusion list if eog_source_idx_1 == eog_source_idx_2: ica.exclude += [eog_source_idx_1] elif eog_source_idx_1 != eog_source_idx_2: ica.exclude += [eog_source_idx_1, eog_source_idx_2] print eog_source_idx_1, eog_source_idx_2 print ica.exclude # Restore sensor space data raw_ica = ica.pick_sources_raw(raw, include=None) # EPOCHS #### events = mne.find_events(raw_ica, stim_channel="STI101") events_classic = [] events_interupt = [] for i in range(len(events)): if i > 0: if events[i, 2] == 1 and events[i - 1, 2] == 1: events_classic.append(i) elif events[i, 2] == 1 and events[i - 1, 2] == 2: events_interupt.append(i) picks = mne.fiff.pick_types(raw_ica.info, meg=True, eeg=False, eog=False, emg=True, stim=False, exclude='bads') reject = dict(grad=4000e-13) epochs = mne.Epochs(raw_ica, events[events_classic], event_id, tmin, tmax, proj=True, picks=picks, baseline=baseline, preload=False, reject=reject) # SAVE FILES #### raw_ica.save(fname + '_tsss_mc_ica.fif', overwrite=True) cov.save((fname + '_tsss_mc_cov.fif')) epochs.save(fname + '_tsss_mc_ica_epochs.fif')
# First, we create a helper function that iteratively applies the pearson # correlation function to sources and returns an array of r values # This is to illustrate the way ica.find_sources_raw works. Actually, this is # the default score_func. from scipy.stats import pearsonr corr = lambda x, y: np.array([pearsonr(a, y.ravel()) for a in x])[:, 0] # As we don't have an ECG channel we use one that correlates a lot with heart # beats: 'MEG 1531'. We can directly pass the name to the find_sources method. # In our example, the find_sources method returns and array of correlation # scores for each ICA source. ecg_scores = ica.find_sources_raw(raw, target='MEG 1531', score_func=corr) # get sources sources = ica.get_sources_raw(raw, start=start_plot, stop=stop_plot) # get times times = raw.time_as_index(np.arange(stop_plot - start_plot)) # get maximum correlation index for ECG ecg_source_idx = np.abs(ecg_scores).argmax() pl.figure() pl.plot(times, sources[ecg_source_idx]) pl.title('ICA source matching ECG') pl.show()