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)
ica.plot_sources_raw(raw, eog_source_idx, title=title, stop=3.0) # plot spatial sensitivities of EOG and ECG ICA components title = 'Spatial patterns of ICA components for ECG+EOG (Magnetometers)' source_idx = range(15) ica.plot_topomap([ecg_source_idx, eog_source_idx], ch_type='mag') plt.suptitle(title, fontsize=12) ############################################################################### # Show MEG data before and after ICA cleaning. # We now add the eog artifacts to the ica.exclusion list ica.exclude += [eog_source_idx] # Restore sensor space data and keep all PCA components raw_ica = ica.pick_sources_raw(raw, include=None, n_pca_components=1.0) # let's now compare the date before and after cleaning. start_compare, stop_compare = raw.time_as_index([100, 106]) data, times = raw[picks, start_compare:stop_compare] data_clean, _ = raw_ica[picks, start_compare:stop_compare] # first the raw data plt.figure() plt.plot(times, data.T, color='r') plt.plot(times, data_clean.T, color='k') plt.xlabel('time (s)') plt.xlim(100, 106) plt.show() # now the affected channel
ica.plot_sources_raw(raw, eog_source_idx, title=title, stop=3.0) # plot spatial sensitivities of EOG and ECG ICA components title = 'Spatial patterns of ICA components for ECG+EOG (Magnetometers)' source_idx = range(15) ica.plot_topomap([ecg_source_idx, eog_source_idx], ch_type='mag') plt.suptitle(title, fontsize=12) ############################################################################### # Show MEG data before and after ICA cleaning. # We now add the eog artifacts to the ica.exclusion list ica.exclude += [eog_source_idx] # Restore sensor space data raw_ica = ica.pick_sources_raw(raw, include=None) start_compare, stop_compare = raw.time_as_index([100, 106]) data, times = raw[picks, start_compare:stop_compare] data_clean, _ = raw_ica[picks, start_compare:stop_compare] plt.figure() plt.plot(times, data.T) plt.xlabel('time (s)') plt.xlim(100, 106) plt.ylabel('Raw MEG data (T)') y0, y1 = plt.ylim() plt.figure() plt.plot(times, data_clean.T)
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')