Station 9 · Lesson 6
Running on HPC
sub-002_task-FaceRecognition_eeg.set (D4) · Nothing yet: the data as downloaded.Download the data (opens in a new tab)Script to reach this state
[ALLEEG, EEG, CURRENTSET, ALLCOM] = eeglab;
% Run from inside faces_project, where ds002718 sits in rawdata
bidsRoot = fullfile(pwd, 'rawdata');
EEG = pop_loadset('sub-002_task-FaceRecognition_eeg.set', fullfile(bidsRoot, 'sub-002', 'eeg'));
% Show the dataset in the main EEGLAB window
[ALLEEG, EEG, CURRENTSET] = pop_newset(ALLEEG, EEG, 0, 'setname', 'sub-002 raw', 'gui', 'off');
eeglab redraw;Your browser's back button takes you to the exact place you were, so open anything you like.
The idea in brief
The batch scriptA MATLAB file of commands that runs processing steps in order. from the previous lesson runs on your computer, one participantThe person whose data were recorded in the study. after another. That is enough for most projects. But some steps are slow: ICAA method that separates the recording into independent sources, some brain and some artifact. on 72 channels and fifty minutes of recording can take a long time per participant, and AMICA and DIPFIT are slower. Multiply that by 18 participants, or by 100, and your computer is busy for days.
Before and after
Your next questions, each answered in its own section:
Why do I need this step?
The reasons in detail, and the terms you need
A computing cluster (HPC, high-performance computing) is a large group of computers that runs your jobs at the same time. Twenty participants become twenty parallel jobs, finishing in about the time of one participant.
But a cluster shows no windows, and you cannot click anything on it. The script must run from start to end on its own. This lesson prepares your script for that.
When is it worth it? Compute: time for one participant × number of participants. If the result is a few hours, run it on your computer overnight. If it is days, or you rerun often, the cluster is worth it.
Why do I need this step?
A computing cluster (HPC, high-performance computing) is a large group of computers that runs your jobs at the same time. Twenty participants become twenty parallel jobs, finishing in about the time of one participant.
But a cluster shows no windows, and you cannot click anything on it. The script must run from start to end on its own. This lesson prepares your script for that.
When is it worth it? Compute: time for one participant × number of participants. If the result is a few hours, run it on your computer overnight. If it is days, or you rerun often, the cluster is worth it.
How do I do it in EEGLAB?
10 steps in the menus
A cluster runs without a GUIThe windows and menus used with the mouse instead of typed commands., so the steps here are preparation and submission.
First: prepare the script
- Start from the batch script and check four conditions:
- It starts with
eeglab nogui, so no window opens. - It does not plot, or it saves the plot to a file with
printand then closes it. - All paths are built from the project folder with
fullfile, not paths from your computer such asC:\Users\.... - It processes one participant, chosen from a number the cluster gives it. This way the cluster sends the same script twenty times with different numbers.
- It starts with
- Test it on your computer from the system command line (not from MATLAB):
matlab -batch "run_ica_d4"-batch runs the script without a GUI, then closes MATLAB. If an error occurs, it exits with an error code that the cluster sees.
Second: your university cluster
- Ask your university computing department: is MATLAB installed? Which job scheduler do they use (SLURM is the most common)? Where should you put EEGLAB?
- Copy the project folder, the script and EEGLAB to your account on the cluster.
- Write the job file. This is a SLURM example for three jobs, one per participant. Names and values differ between universities, so adjust them to their instructions:
#!/bin/bash
#SBATCH --job-name=d4-ica
#SBATCH --array=1-3
#SBATCH --cpus-per-task=4
#SBATCH --mem=16G
#SBATCH --time=04:00:00
#SBATCH --output=logs/ica_%a.out
module load matlab
matlab -batch "run_ica_d4"- Submit it with
sbatch job_ica.sh, and follow it withsqueue -uand your user name.
Third: the NSG portal (a free alternative)
- If your university has no cluster, the Neuroscience Gateway (NSG) portal is free for academic researchers, with MATLAB and EEGLAB ready. Register an account on its website.
- Install the nsgportal pluginA package that adds new functions or menus to EEGLAB. from File then Manage EEGLAB extensions (the latest release when this lesson was written is 2.1.1).
- Open Tools then NSG Tools then NSG account info and enter your user name and password.
- Put the script and the data it needs in one folder, then open Tools then NSG Tools then Manage NSG jobs, choose the folder, type the script name, and submit the job. From the same window you follow its status and download the results.
How do I do it in EEGLAB?
A cluster runs without a GUI, so the steps here are preparation and submission.
First: prepare the script
- Start from the batch script and check four conditions:
- It starts with
eeglab nogui, so no window opens. - It does not plot, or it saves the plot to a file with
printand then closes it. - All paths are built from the project folder with
fullfile, not paths from your computer such asC:\Users\.... - It processes one participant, chosen from a number the cluster gives it. This way the cluster sends the same script twenty times with different numbers.
- It starts with
- Test it on your computer from the system command line (not from MATLAB):
matlab -batch "run_ica_d4"-batch runs the script without a GUI, then closes MATLAB. If an error occurs, it exits with an error code that the cluster sees.
Second: your university cluster
- Ask your university computing department: is MATLAB installed? Which job scheduler do they use (SLURM is the most common)? Where should you put EEGLAB?
- Copy the project folder, the script and EEGLAB to your account on the cluster.
- Write the job file. This is a SLURM example for three jobs, one per participant. Names and values differ between universities, so adjust them to their instructions:
#!/bin/bash
#SBATCH --job-name=d4-ica
#SBATCH --array=1-3
#SBATCH --cpus-per-task=4
#SBATCH --mem=16G
#SBATCH --time=04:00:00
#SBATCH --output=logs/ica_%a.out
module load matlab
matlab -batch "run_ica_d4"- Submit it with
sbatch job_ica.sh, and follow it withsqueue -uand your user name.
Third: the NSG portal (a free alternative)
- If your university has no cluster, the Neuroscience Gateway (NSG) portal is free for academic researchers, with MATLAB and EEGLAB ready. Register an account on its website.
- Install the nsgportal plugin from File then Manage EEGLAB extensions (the latest release when this lesson was written is 2.1.1).
- Open Tools then NSG Tools then NSG account info and enter your user name and password.
- Put the script and the data it needs in one folder, then open Tools then NSG Tools then Manage NSG jobs, choose the folder, type the script name, and submit the job. From the same window you follow its status and download the results.
How do I write it as code?
The same steps in 58 lines of MATLAB
The script reads the files that pop_importbids wrote to derivatives/eeglab in the batch processingRunning the same script automatically on many participants' files. lesson, so run that import once first. Run it from inside faces_project.
% run_ica_d4.m - one subject per job, no display
% Local test: matlab -batch "run_ica_d4"
% On SLURM: the array index selects the subject
eeglab nogui
projectRoot = pwd;
outRoot = fullfile(projectRoot, 'derivatives', 'eeglab');
logRoot = fullfile(projectRoot, 'logs');
subjects = {'sub-002', 'sub-003', 'sub-004'};
% Subject index from the cluster, or 1 when run locally
taskId = str2double(getenv('SLURM_ARRAY_TASK_ID'));
if isnan(taskId), taskId = 1; end
subj = subjects{taskId};
fprintf('Job %d: %s\n', taskId, subj);
cfg.lineFreq = 50; % same settings as the batch lesson
cfg.eogLabels = {'EEG061', 'EEG062'};
cfg.dropLabels = {'EEG063', 'EEG064'};
try
% File written by pop_importbids in the batch processing lesson
outDir = fullfile(outRoot, subj, 'eeg');
EEG = pop_loadset('filename', sprintf('%s_task-FaceRecognition_eeg.set', subj), ...
'filepath', outDir);
% Same steps as process_subject up to ICA, so the weights fit its main dataset
EEG = pop_select(EEG, 'rmchannel', cfg.dropLabels);
fullLocs = EEG.chanlocs;
EEG = pop_eegfiltnew(EEG, 'locutoff', 0.1);
EEG = pop_eegfiltnew(EEG, 'locutoff', cfg.lineFreq - 2, 'hicutoff', cfg.lineFreq + 2, 'revfilt', 1);
EEG_test = pop_clean_rawdata(EEG, 'FlatlineCriterion', 5, 'ChannelCriterion', 0.8, ...
'LineNoiseCriterion', 4, 'Highpass', 'off', 'BurstCriterion', 'off', ...
'WindowCriterion', 'off', 'BurstRejection', 'off', 'Distance', 'Euclidian', ...
'channels_ignore', cfg.eogLabels);
badChans = setdiff({EEG.chanlocs.labels}, {EEG_test.chanlocs.labels}, 'stable');
if ~isempty(badChans)
EEG = pop_select(EEG, 'rmchannel', badChans);
end
if ~isempty(badChans)
EEG = pop_interp(EEG, fullLocs, 'spherical');
end
clear EEG_test
eogIdx = find(ismember({EEG.chanlocs.labels}, cfg.eogLabels));
EEG = pop_reref(EEG, [], 'exclude', eogIdx);
% ICA on a 1-Hz high-passed copy (the slow step worth a cluster)
EEG_ica = pop_eegfiltnew(EEG, 'locutoff', 1);
tic;
EEG_ica = pop_runica(EEG_ica, 'icatype', 'runica', 'extended', 1);
fprintf('ICA finished in %.1f min\n', toc / 60);
% Save only the small result: the ICA weights and the bad channel list
icaInfo = struct('icaweights', EEG_ica.icaweights, 'icasphere', EEG_ica.icasphere, ...
'icachansind', EEG_ica.icachansind, 'badChans', {badChans});
save(fullfile(outDir, sprintf('%s_task-FaceRecognition_desc-ica.mat', subj)), ...
'-struct', 'icaInfo');
catch err
% Write the error to a log file, then exit with an error code
if ~exist(logRoot, 'dir'), mkdir(logRoot); end
fid = fopen(fullfile(logRoot, sprintf('%s_error.txt', subj)), 'w');
fprintf(fid, '%s\n', getReport(err, 'extended', 'hyperlinks', 'off'));
fclose(fid);
rethrow(err);
endHow do I write it as code?
The script reads the files that pop_importbids wrote to derivatives/eeglab in the batch processing lesson, so run that import once first. Run it from inside faces_project.
% run_ica_d4.m - one subject per job, no display
% Local test: matlab -batch "run_ica_d4"
% On SLURM: the array index selects the subject
eeglab nogui
projectRoot = pwd;
outRoot = fullfile(projectRoot, 'derivatives', 'eeglab');
logRoot = fullfile(projectRoot, 'logs');
subjects = {'sub-002', 'sub-003', 'sub-004'};
% Subject index from the cluster, or 1 when run locally
taskId = str2double(getenv('SLURM_ARRAY_TASK_ID'));
if isnan(taskId), taskId = 1; end
subj = subjects{taskId};
fprintf('Job %d: %s\n', taskId, subj);
cfg.lineFreq = 50; % same settings as the batch lesson
cfg.eogLabels = {'EEG061', 'EEG062'};
cfg.dropLabels = {'EEG063', 'EEG064'};
try
% File written by pop_importbids in the batch processing lesson
outDir = fullfile(outRoot, subj, 'eeg');
EEG = pop_loadset('filename', sprintf('%s_task-FaceRecognition_eeg.set', subj), ...
'filepath', outDir);
% Same steps as process_subject up to ICA, so the weights fit its main dataset
EEG = pop_select(EEG, 'rmchannel', cfg.dropLabels);
fullLocs = EEG.chanlocs;
EEG = pop_eegfiltnew(EEG, 'locutoff', 0.1);
EEG = pop_eegfiltnew(EEG, 'locutoff', cfg.lineFreq - 2, 'hicutoff', cfg.lineFreq + 2, 'revfilt', 1);
EEG_test = pop_clean_rawdata(EEG, 'FlatlineCriterion', 5, 'ChannelCriterion', 0.8, ...
'LineNoiseCriterion', 4, 'Highpass', 'off', 'BurstCriterion', 'off', ...
'WindowCriterion', 'off', 'BurstRejection', 'off', 'Distance', 'Euclidian', ...
'channels_ignore', cfg.eogLabels);
badChans = setdiff({EEG.chanlocs.labels}, {EEG_test.chanlocs.labels}, 'stable');
if ~isempty(badChans)
EEG = pop_select(EEG, 'rmchannel', badChans);
end
if ~isempty(badChans)
EEG = pop_interp(EEG, fullLocs, 'spherical');
end
clear EEG_test
eogIdx = find(ismember({EEG.chanlocs.labels}, cfg.eogLabels));
EEG = pop_reref(EEG, [], 'exclude', eogIdx);
% ICA on a 1-Hz high-passed copy (the slow step worth a cluster)
EEG_ica = pop_eegfiltnew(EEG, 'locutoff', 1);
tic;
EEG_ica = pop_runica(EEG_ica, 'icatype', 'runica', 'extended', 1);
fprintf('ICA finished in %.1f min\n', toc / 60);
% Save only the small result: the ICA weights and the bad channel list
icaInfo = struct('icaweights', EEG_ica.icaweights, 'icasphere', EEG_ica.icasphere, ...
'icachansind', EEG_ica.icachansind, 'badChans', {badChans});
save(fullfile(outDir, sprintf('%s_task-FaceRecognition_desc-ica.mat', subj)), ...
'-struct', 'icaInfo');
catch err
% Write the error to a log file, then exit with an error code
if ~exist(logRoot, 'dir'), mkdir(logRoot); end
fid = fopen(fullfile(logRoot, sprintf('%s_error.txt', subj)), 'w');
fprintf(fid, '%s\n', getReport(err, 'extended', 'hyperlinks', 'off'));
fclose(fid);
rethrow(err);
endWhich value should I choose?
Suitable values for your analysis goal
| Setting | Value in this lesson | Why |
|---|---|---|
| Participants per job | One | If one participant fails, the others do not stop, and you rerun it alone. The same idea as error logging in the batch lesson. |
| How to run | matlab -batch | Runs without a GUI, closes MATLAB at the end, and returns an error code on failure. |
| Memory | 16G in the example | One D4 participant is about 234 MB on disk, but the filterA tool that weakens some frequencies in a signal and keeps others. and ICA create copies in memory. Start with a generous value, then read what was actually used in the job report. |
| Time limit | 4 hours in the example | Measure one participant on your computer and ask for twice that. A job that exceeds its time is stopped by the cluster. |
| What is saved | ICA weights only | A small file, quick to move from the cluster. It was computed after the same steps as process_subject in the batch lesson (same channels, same referenceThe point against which each channel's voltage is measured., same interpolationEstimating a removed channel's signal from the channels around it.), so you can copy it into that lesson’s main datasetOne EEG data file loaded in EEGLAB together with its information., as in the lesson on copying ICA weights. Weights only fit data with the same channels and spatial processing. |
Which value should I choose?
| Setting | Value in this lesson | Why |
|---|---|---|
| Participants per job | One | If one participant fails, the others do not stop, and you rerun it alone. The same idea as error logging in the batch lesson. |
| How to run | matlab -batch | Runs without a GUI, closes MATLAB at the end, and returns an error code on failure. |
| Memory | 16G in the example | One D4 participant is about 234 MB on disk, but the filter and ICA create copies in memory. Start with a generous value, then read what was actually used in the job report. |
| Time limit | 4 hours in the example | Measure one participant on your computer and ask for twice that. A job that exceeds its time is stopped by the cluster. |
| What is saved | ICA weights only | A small file, quick to move from the cluster. It was computed after the same steps as process_subject in the batch lesson (same channels, same reference, same interpolation), so you can copy it into that lesson’s main dataset, as in the lesson on copying ICA weights. Weights only fit data with the same channels and spatial processing. |
How do I check it worked?
4 quick checks
matlab -batch "run_ica_d4"on your computer printsJob 1: sub-002, then the ICA duration, and closes MATLAB without error.derivatives/eeglab/sub-002/eegholds the filesub-002_task-FaceRecognition_desc-ica.mat.- On the cluster: three jobs, three weights files, and no error file in
logs. size(icaweights)in each file is the data rankThe number of truly independent channels; it drops after interpolation or average reference. × 72, that is 71 × 72 if no channelThe signal recorded from one electrode, as shown in EEGLAB. was interpolated (one rank is lost to the average reference).
How do I check it worked?
matlab -batch "run_ica_d4"on your computer printsJob 1: sub-002, then the ICA duration, and closes MATLAB without error.derivatives/eeglab/sub-002/eegholds the filesub-002_task-FaceRecognition_desc-ica.mat.- On the cluster: three jobs, three weights files, and no error file in
logs. size(icaweights)in each file is the data rank × 72, that is 71 × 72 if no channel was interpolated (one rank is lost to the average reference).
What mistakes do people make?
5 mistakes and how to avoid them
- A command that opens a window. Any
pop_without enough arguments, oreeglabinstead ofeeglab nogui, tries to open a window, so the script stops or fails. Test withmatlab -batchon your computer before submitting. - Paths from your computer.
C:\Users\...does not exist on the cluster. Build every path from the project folder. - A loop over all participants in a single job. You lose the parallelism, and an error in one participant stops everything if you do not catch it.
- Asking for less time or memory than needed. The cluster stops the job with no result. Read the
logsfile and the job report before resubmitting. - Forgetting versions. The EEGLAB or MATLAB version on the cluster may differ from your computer. Print
eeg_getversionandversionat the start of the script, and write them in your Methods.
What mistakes do people make?
- A command that opens a window. Any
pop_without enough arguments, oreeglabinstead ofeeglab nogui, tries to open a window, so the script stops or fails. Test withmatlab -batchon your computer before submitting. - Paths from your computer.
C:\Users\...does not exist on the cluster. Build every path from the project folder. - A loop over all participants in a single job. You lose the parallelism, and an error in one participant stops everything if you do not catch it.
- Asking for less time or memory than needed. The cluster stops the job with no result. Read the
logsfile and the job report before resubmitting. - Forgetting versions. The EEGLAB or MATLAB version on the cluster may differ from your computer. Print
eeg_getversionandversionat the start of the script, and write them in your Methods.
Your browser doesn't allow saving progress. All lessons are open, but completion marks won't be saved.
Done. Your progress is saved in this browser.UndoHow do I write it in my paper?
A Methods template: edit the data, settings and versions to match what you ran
A template: edit the data, settings and versions to match what you ran.
Independent component analysis (extended Infomax; Lee et al., 1999) was computed with runica (EEGLAB 2026.1) for each participant on a copy of the preprocessed data high-pass filtered at 1 Hz, and the resulting weights were applied to the 0.1 Hz high-pass filtered data; jobs ran one per participant on [computing cluster] with MATLAB [version].How do I write it in my paper?
A template: edit the data, settings and versions to match what you ran.
Independent component analysis (extended Infomax; Lee et al., 1999) was computed with runica (EEGLAB 2026.1) for each participant on a copy of the preprocessed data high-pass filtered at 1 Hz, and the resulting weights were applied to the 0.1 Hz high-pass filtered data; jobs ran one per participant on [computing cluster] with MATLAB [version].Where can I read more?
4 sources, official docs first
- EEGLAB and HPCDocumentationEEGLAB and the Neuroscience Gateway
- nsgportal (GitHub wiki)Setting up the plug-in; Creating and managing an NSG job using pop_nsg from the command line
- EEGLAB TutorialDocumentation11. Scripting
- EEGLAB on high performance computing ressourcesVideoFull video · 8:32
Written for EEGLAB 2026.1 · 2026-10-28
Going further
- EEGLAB and HPC: https://eeglab.org/others/EEGLAB_and_high_performance_computing.html (EEGLAB and the Neuroscience Gateway)
- nsgportal (GitHub wiki): https://github.com/sccn/nsgportal/wiki (Setting up the plug-in; Creating and managing an NSG job using pop_nsg from the command line)
- EEGLAB Tutorial: https://eeglab.org/tutorials/11_Scripting/ (11. Scripting)
- EEGLAB on high performance computing ressources: https://www.youtube.com/watch?v=1_h8NFS_ipI (Full video)
Need help?
Ask, share feedback, or report an error
- Report a scientific error · Needs a GitHub account. Don't have one? Use "Share your thoughts".