# 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()
# 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