max_n_components=100, noise_cov=None, random_state=0) # For maximum rejection performance we will compute the decomposition on # the entire time range # decompose sources for raw data, select n_components by explained variance ica.decompose_raw(raw, start=None, stop=None, picks=picks) print ica # 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. # 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)
# 1 minute exposure should be sufficient for artifact detection. # However, rejection performance may significantly improve when using # the entire data range start, stop = raw.time_as_index([100, 160]) # decompose sources for raw data ica.decompose_raw(raw, start=start, stop=stop, picks=picks) print ica sources = ica.get_sources_raw(raw, start=start, stop=stop) # 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. # 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.
random_state=0) # For maximum rejection performance we will compute the decomposition on # the entire time range # decompose sources for raw data, select n_components by explained variance ica.decompose_raw(raw, start=None, stop=None, picks=picks) print ica sources = ica.get_sources_raw(raw) # 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)