FEM headmodel batch scripting

Hi Brainstorm experts!

I am currently trying to compute the head model from the FEM mesh generated by SimNIBS with DTI tensor calculated. My data is 256 channel EEG, I compute the head model with the template grid(head), it takes literally around ~ 72 hours per one subject on my Mac, which is too long for me. Therefore, I am going to compute it on a cluster that has a couple of nodes with multiple cores each. I've got some questions as follows:

  1. Previously I did the batch process for DTI and mesh generation in a For loop executing GUI generated script in every iteration. I did the same for the head model this time, the code is attached also. I noticed that the subject name is not passed to the process, but the channel file. It seems the further process will find corresponding FEM mesh and other relevent files. I just wanted to confirm with you that passing the channel file path to the loop is correct, right?

generate_script

% Script generated by Brainstorm (27-Apr-2021)

% Input files
sFiles = [];

% Start a new report
bst_report('Start', sFiles);

% Process: Compute head model
sFiles = bst_process('CallProcess', 'process_headmodel', sFiles, [], ...
    'sourcespace', 2, ...  % MRI volume
    'volumegrid',  struct(...
         'Method',        'group', ...
         'nLayers',       17, ...
         'Reduction',     3, ...
         'nVerticesInit', 4000, ...
         'Resolution',    0.005, ...
         'FileName',      'Group_analysis/@default_study/headmodel_grid_210514_1233.mat'), ...
    'meg',         1, ...  % 
    'eeg',         4, ...  % DUNEuro FEM
    'ecog',        1, ...  % 
    'seeg',        1, ...  % 
    'duneuro',     struct(...
         'FemCond',             [], ...
         'FemSelect',           [], ...
         'UseTensor',           0, ...
         'Isotropic',           1, ...
         'SrcShrink',           0, ...
         'SrcForceInGM',        0, ...
         'FemType',             'fitted', ...
         'SolverType',          'cg', ...
         'GeometryAdapted',     0, ...
         'Tolerance',           1e-08, ...
         'ElecType',            'normal', ...
         'MegIntorderadd',      0, ...
         'MegType',             'physical', ...
         'SolvSolverType',      'cg', ...
         'SolvPrecond',         'amg', ...
         'SolvSmootherType',    'ssor', ...
         'SolvIntorderadd',     0, ...
         'DgSmootherType',      'ssor', ...
         'DgScheme',            'sipg', ...
         'DgPenalty',           20, ...
         'DgEdgeNormType',      'houston', ...
         'DgWeights',           1, ...
         'DgReduction',         1, ...
         'SolPostProcess',      1, ...
         'SolSubstractMean',    0, ...
         'SolSolverReduction',  1e-10, ...
         'SrcModel',            'venant', ...
         'SrcIntorderadd',      0, ...
         'SrcIntorderadd_lb',   2, ...
         'SrcNbMoments',        3, ...
         'SrcRefLen',           20, ...
         'SrcWeightExp',        1, ...
         'SrcRelaxFactor',      6, ...
         'SrcMixedMoments',     1, ...
         'SrcRestrict',         1, ...
         'SrcInit',             'closest_vertex', ...
         'BstSaveTransfer',     0, ...
         'BstEegTransferFile',  'eeg_transfer.dat', ...
         'BstMegTransferFile',  'meg_transfer.dat', ...
         'BstEegLfFile',        'eeg_lf.dat', ...
         'BstMegLfFile',        'meg_lf.dat', ...
         'UseIntegrationPoint', 1, ...
         'EnableCacheMemory',   0, ...
         'MegPerBlockOfSensor', 0), ...
    'channelfile', '');

% Save and display report
ReportFile = bst_report('Save', sFiles);
bst_report('Open', ReportFile);
% bst_report('Export', ReportFile, ExportDir);
  1. I got a warning when starting computing headmodel:
    WARNING: You are using AMG without SuperLU! Please consider installing SuperLU, or set the usesuperlu flag to false to suppress this warning. How much faster would it be if I installed SuperLU? Is there a tutorial from Brainstorm that guides me to set SuperLu for DUNE in this case?

  2. In the anatomy files, each subject has tess_cortex_pial_high and tess_cortex_pial_low, shall I activate the higher one before computing headmodel for better result?
    resolution

  1. I just wanted to confirm with you that passing the channel file path to the loop is correct, right?

No, you can't pass the channel file, you can only provide a "data" file. You need to have some recordings imported in your subject in order to use the pipeline editor to compute a head model.
You probably already noticed that calling process_headmodel with sFiles = []; doesn't work... You can only execute from a script what you can execute from the GUI. And selecting the process "Sources > Compute head model" with the Process1 empty shows an error in the pipeline editor.

WARNING: You are using AMG without SuperLU! Please consider installing SuperLU, or set the usesuperlu flag to false to suppress this warning. How much faster would it be if I installed SuperLU? Is there a tutorial from Brainstorm that guides me to set SuperLu for DUNE in this case?

@tmedani @juangpc ?

In the anatomy files, each subject has tess_cortex_pial_high and tess_cortex_pial_low , shall I activate the higher one before computing headmodel for better result?

If the default 15000-vertex surface looks insufficiently sampled to you, you can try downsampling again the high-resolution surface to something higher. But 240000 sources looks like a crazy overkill.
This is EEG source estimation: don't expect to get a spatial precision much higher than a few centimeters.

Hi Francois
Thank you for the reply.

No, you can't pass the channel file, you can only provide a "data" file.

To make it clear, I mean passing a database path of channel file(.mat) for the field name channel file.

And selecting the process "Sources > Compute head model" with the Process1 empty shows an error in the pipeline editor.

You are right, it pops an error message once I click on the DUNE option, however, I tried to just pass the '.mat' channel file from the database(e.g. /home/user/brainstorm_db/protocol1/data/sub-123456/@intra/channel_GSN_HydroCel_256_E1.mat), the function seems working because I observed a few lines of code in panel_headmodel.m, it gets the corresponding subject's surface files. Do you think it is acceptable to perform batch head model in this way? because from the GUI, I did not need to import any EEG recording for head model computation. If not, probably I would do as you suggested that I will quickly import some dummy EEG recordings just for running the head computation because the EEG hasn't been cleaned yet.

Thank you very much.

You can't specify the channel file in this way. You pass a data file, and the batching system will fetch the associated channel file automatically.
Do not try to edit the contents of the sFiles structure. In most case, only the .FileName field will be read by the target process. To avoid any confusion, I'd recommend you only use sFiles as a cell array of input files ("data" files only for process_headmodel), as in the scrits generated automatically by the Pipeline Editor.

If not, probably I would do as you suggested that I will quickly import some dummy EEG recordings just for running the head computation because the EEG hasn't been cleaned yet.

This is the only way to go.

Thank you for the recommandation, just one more question regarding to the cell array of input files, do you mean I can pass an list of my EEG recordings data file from different subjects, the process will do one by one or I still need to make a for loop and pass one data file as cell array in the iteration?

It depends on the process:
https://neuroimage.usc.edu/brainstorm/Tutorials/TutUserProcess#Categories_of_process