random_state=0) # Also we decide to use all PCA components before mixing back to sensor space. # You can again use percentages (float) or set the total number of components # to be kept directly (int) which allows to control the amount of additional # denoising. ica.n_pca_components = 1.0 # decompose sources for raw data using each third sample. ica.decompose_raw(raw, picks=picks, decim=3) print(ica) # plot reasonable time window for inspection start_plot, stop_plot = 100., 103. ica.plot_sources_raw(raw, range(30), start=start_plot, stop=stop_plot) ############################################################################### # Automatically find the ECG component using correlation with ECG signal. # Defining a customized distance function. # You can pass any function object that # takes a n_sources X n_samples vector and, optionally, a second # n_samples vector, and returns a score vector of length n_sources. # Let's illustrate this by creating a function that, when passed as # `score_func` argument, does the same as the default value 'pearsonr'. from scipy.stats import pearsonr
# This allows to control the trade-off between denoising and preserving signal. ica = ICA(n_components=0.90, n_pca_components=None, max_pca_components=100, random_state=0) # 1 minute exposure should be sufficient for artifact detection. # However, rejection performance may significantly improve when using # the entire data range # decompose sources for raw data using each third sample. ica.decompose_raw(raw, picks=picks, decim=3) print ica # plot reasonable time window for inspection start_plot, stop_plot = 100., 103. ica.plot_sources_raw(raw, range(30), 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'. To improve detection, we filter the the channel and pass # it directly to find sources. The method then returns an array of correlation
# 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)