def run_ica(name, save_dir, lowpass, overwrite): ica_name = name + filter_string(lowpass) + '-ica.fif' ica_path = join(save_dir, ica_name) if overwrite or not isfile(ica_path): raw = io.read_filtered(name, save_dir, lowpass) epochs = io.read_epochs(name, save_dir, lowpass) ica = mne.preprocessing.ICA(n_components=0.95, method='fastica') ica.fit(epochs) eog_epochs = mne.preprocessing.create_eog_epochs(raw) ecg_epochs = mne.preprocessing.create_ecg_epochs(raw) eog_indices, eog_scores = ica.find_bads_eog(eog_epochs) ecg_indices, ecg_scores = ica.find_bads_ecg(ecg_epochs) ica.exclude += eog_indices ica.exclude += ecg_indices ica.save(ica_path) else: print('ica file: ' + ica_path + ' already exists')
def apply_ica(name, save_dir, lowpass, overwrite): ica_epochs_name = name + filter_string(lowpass) + '-ica-epo.fif' ica_epochs_path = join(save_dir, ica_epochs_name) if overwrite or not isfile(ica_epochs_path): epochs = io.read_epochs(name, save_dir, lowpass) ica = io.read_ica(name, save_dir, lowpass) ica_epochs = ica.apply(epochs) ica_epochs.save(ica_epochs_path) else: print('ica epochs file: ' + ica_epochs_path + ' already exists')
def estimate_noise_covariance(name, save_dir, lowpass, overwrite): covariance_name = name + filter_string(lowpass) + '-cov.fif' covariance_path = join(save_dir, covariance_name) if overwrite or not isfile(covariance_path): epochs = io.read_epochs(name, save_dir, lowpass) noise_covariance = mne.compute_covariance(epochs, n_jobs=1) noise_covariance = mne.cov.regularize(noise_covariance, epochs.info) mne.cov.write_cov(covariance_path, noise_covariance) else: print('noise covariance file: '+ covariance_path + \ ' already exists')
def plot_epochs_image(name, save_dir, lowpass, subject, save_plots, figures_path): channel = 'MEG1812' epochs = io.read_epochs(name, save_dir, lowpass) picks = mne.pick_channels(epochs.info['ch_names'], [channel]) for trial_type in epochs.event_id: epochs_image = mne.viz.plot_epochs_image(epochs[trial_type], picks) plt.title(trial_type) if save_plots: save_path = join(figures_path, subject, 'epochs', trial_type + '_' + channel + '_' + name + \ filter_string(lowpass) + '.jpg') epochs_image[0].savefig(save_path, dpi=600) print 'figure: ' + save_path + ' has been saved' else: print 'Not saving plots; set "save_plots" to "True" to save'