Scripting & automation

Turn every GUI click into a line of code, then run the pipeline on all participants unattended.

6 lessons: 2 core, 2 recommended, 2 optional

After this station you can:

  • Get the code for any step from eegh
  • Write a single-subject script from import to saving
  • Run it on all participants and organize files without exhausting your storage
  • Know when a ready-made pipeline fits and what to check before trusting it

Lessons

  1. Batch processingRecommended15 min
  2. Organizing files & storageRecommended12 min
  3. Running on HPCOptional12 min

Test yourself

5 questions. Your score stays with you and isn't sent anywhere.

1. You applied a filter from the GUI and want its exact matching line of code. What is the quickest way?
Show the answer

The correct answer: Type eegh in the command window

eegh prints every command of the session in order, including the filter line with its values, such as 'revfilt',1 for the notch box. Running eeglab again starts an empty history, so you lose the session history if you did not save it.

Review the lesson: eegh & the command history

2. A colleague sent you a .set file and you want to know how it was made. Where do you look?
Show the answer

The correct answer: In EEG.history, because it is saved inside the file itself

The dataset history (EEG.history) keeps the commands that changed this dataset, and it travels with it inside the .set file. eegh is your own session history. A file name is not enough: two files called clean may have gone through different filters.

Review the lesson: eegh & the command history

3. Put these lines in the order they come in the single-subject script:
  1. EEG = pop_reref(EEG, [], 'exclude', eogIdx);
  2. EEG = pop_epoch(EEG, {'square'}, [-1 2], 'newname', 'D1 epochs', 'epochinfo', 'yes');
  3. EEG = pop_loadset('filename', 'eeglab_data.set', 'filepath', dataDir);
  4. EEG = pop_interp(EEG, fullLocs, 'spherical');
  5. EEG_ica = pop_runica(EEG_ica, 'icatype', 'runica', 'extended', 1);
  6. EEG = pop_eegfiltnew(EEG, 'locutoff', 0.1);
Show the answer

The correct answer: 1. EEG = pop_loadset('filename', 'eeglab_data.set', 'filepath', dataDir);, 2. EEG = pop_eegfiltnew(EEG, 'locutoff', 0.1);, 3. EEG = pop_interp(EEG, fullLocs, 'spherical');, 4. EEG = pop_reref(EEG, [], 'exclude', eogIdx);, 5. EEG_ica = pop_runica(EEG_ica, 'icatype', 'runica', 'extended', 1);, 6. EEG = pop_epoch(EEG, {'square'}, [-1 2], 'newname', 'D1 epochs', 'epochinfo', 'yes');

The same order as the stations: load, then filter, then interpolate before the reference, then the average reference before ICA, then ICA on a 1 Hz copy, and epoching last. Do not move a step without a reason; each part of this order is explained in its lesson.

Review the lesson: A single-subject script

4. Your script works today, and you suspect it relies on a variable such as badChans left over from an earlier session. How do you check?
Show the answer

The correct answer: Run it after clear in a fresh MATLAB, without clicking anything in the GUI while it runs

A script that works only because EEG or badChans already exists will fail tomorrow. Always run it after clear. If a window opens and waits for you, a line is missing arguments: fix it.

Review the lesson: A single-subject script

5. Which changes make the script run on another computer and without your input? (Select all that apply)
Show the answer

The correct answer: Replace the fixed path with fullfile(eeglabRoot, 'sample_data'), Put the settings that change between projects in a Settings section at the top, Remove plotting lines such as figure and pop_viewprops from the processing script

A fixed path works on your computer only. Settings at the top mean you change one number instead of searching the code. Plotting lines open windows, slow the run, and stop it on a computing cluster. Every line is copied from its lesson unchanged, so the result matches what the lesson explained.

Review the lesson: A single-subject script