def task_ica(): """Step 3: ICA""" for subject in subjects: yield dict( name=str(subject), file_dep=[fname.raw_detrend(subject=subject), '03_ica.py'], targets=[fname.ica(subject=subject)], actions=[f'ipython 03_ica.py {subject:d}'], )
def task_ica(): """Step 03: Use ICA to clean up ECG and EOG artifacts""" for subject in subjects: filt_fnames = [fname.filt(subject=subject, run=run, fmin=bandpass_fmin, fmax=bandpass_fmax) for run in range(1, 7)] ica_fname = fname.ica(subject=subject) yield dict( name=subject, task_dep=['filter'], file_dep=filt_fnames + ['03_ica.py'], targets=[ica_fname], actions=['python 03_ica.py %s' % subject], )
def task_epochs(): """Step 4: Construct epochs""" for subject in subjects: yield dict( name=str(subject), file_dep=[ fname.raw_filt(subject=subject), fname.ica(subject=subject), '04_epochs.py' ], targets=[ fname.epochs(subject=subject), fname.epochs_long(subject=subject) ], actions=[f'ipython 04_epochs.py {subject:d}'], )
def task_epochs(): """Step 04: Cut the data into epochs""" for subject in subjects: filt_fnames = [fname.filt(subject=subject, run=run, fmin=bandpass_fmin, fmax=bandpass_fmax) for run in range(1, 7)] ica_fname = fname.ica(subject=subject) epo_fname = fname.epo(subject=subject) yield dict( name=subject, task_dep=['ica'], file_dep=filt_fnames + [ica_fname, '04_epochs.py'], targets=[epo_fname], actions=['python 04_epochs.py %s' % subject], )
import mne import argparse import numpy as np from config import fname, events_id, subjects_with_extra_stim_artifacts, stim_artifact_sensor, n_jobs # Handle command line arguments parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('subject', metavar='sub###', type=int, help='The subject to process') args = parser.parse_args() subject = args.subject print('Processing subject:', subject) report = mne.open_report(fname.report(subject=subject)) raw = mne.io.read_raw_fif(fname.raw_filt(subject=subject)) ica = mne.preprocessing.read_ica(fname.ica(subject=subject)) # Create short epochs for evoked analysis events = mne.find_events(raw, shortest_event=0.01) epochs = mne.Epochs(raw, events, events_id, tmin=-0.2, tmax=0.5, reject=None, baseline=(-0.2, 0), preload=True) report.add_figs_to_section(epochs.average().plot_joint(times=[0.035, 0.1]), ['Evokeds without cleaning (grads)', 'Evokeds without cleaning (mags)'], 'Sensor level', replace=True) # Apply ICA to remove EOG and ECG artifacts epochs = ica.apply(epochs) # Do a first pass for fixing the stim artifact mne.preprocessing.fix_stim_artifact(epochs) epochs.save(fname.epochs(subject=subject), overwrite=True) # For some subjects, there are more stim artifacts that we need to remove using ICA if subject in subjects_with_extra_stim_artifacts:
# Find onsets of heart beats and blinks. Create epochs around them ecg_epochs = create_ecg_epochs(raw, tmin=-.3, tmax=.3, preload=False) eog_epochs = create_eog_epochs(raw, tmin=-.5, tmax=.5, preload=False) # Find ICA components that correlate with heart beats. ecg_epochs.decimate(5) ecg_epochs.load_data() ecg_epochs.apply_baseline((None, None)) ecg_inds, ecg_scores = ica.find_bads_ecg(ecg_epochs, method='ctps') ecg_scores = np.abs(ecg_scores) rank = np.argsort(ecg_scores)[::-1] rank = [r for r in rank if ecg_scores[r] > 0.05] ica.exclude = rank[:n_ecg_components] print(' Found %d ECG indices' % (len(ecg_inds),)) # Find ICA components that correlate with eye blinks eog_epochs.decimate(5) eog_epochs.load_data() eog_epochs.apply_baseline((None, None)) eog_inds, eog_scores = ica.find_bads_eog(eog_epochs) eog_scores = np.max(np.abs(eog_scores), axis=0) # Remove all components with a correlation > 0.1 to the EOG channels and that # have not already been flagged as ECG components rank = np.argsort(eog_scores)[::-1] rank = [r for r in rank if eog_scores[r] > 0.1 and r not in ecg_inds] ica.exclude += rank[:n_eog_components] print(' Found %d EOG indices' % (len(eog_inds),)) # Save the ICA decomposition ica.save(fname.ica(subject=subject))
# Read events from the stim channel mask = 4096 + 256 # mask for excluding high order bits events = mne.find_events(raw, stim_channel='STI101', consecutive='increasing', mask=mask, mask_type='not_and', min_duration=0.003) # Compensate for projector delay events[:, 0] += int(round(0.0345 * raw.info['sfreq'])) # Load the ICA object print(' Using ICA') ica = read_ica(fname.ica(subject=subject)) # Make epochs. Because the original 1000Hz sampling rate is a bit excessive # for what we're going for, we only read every 5th sample. This gives us a # sampling rate of ~200Hz. epochs = mne.Epochs(raw, events, events_id, epoch_tmin, epoch_tmax, baseline=baseline, decim=5, preload=True) # Save evoked plot to the report with mne.open_report(fname.report(subject=subject)) as report: