# 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 ecg_events = ica_find_ecg_events(raw=raw, ecg_source=ecg_source, event_id=event_id)
# explained variance. ica = ICA(n_components=0.90, max_n_components=100, noise_cov=None, random_state=0) print ica # 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.
# setting the random state to 0 makes the solution reproducible. # Instead of the actual number of components we pass a float value # between 0 and 1 to select n_components by a percentage of # explained variance. ica = ICA(n_components=0.90, 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 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.