Problem generating BEM surfaces and head models

Hello,

I’m trying to generate BEM surfaces and head models in Brainstorm using the MATLAB functions from the Epilepsy tutorial, but I can’t get them to work properly.

When I run process_generate_bem, the process finishes without any error, but the output structure is empty. In the GUI, no BEM surfaces appear under the subject, as if nothing had been created. I’m also trying to compute the EEG-only head model, but since no BEM surfaces are generated, this step fails as well.

Although I am writing my script in Python (using the MATLAB engine), the same issue occurs when running the functions directly in MATLAB, so it doesn’t seem to be language-related. Here is my code:

#BEM Surfaces

self.eng.bst_process('CallProcess', 'process_generate_bem', [], [], 
   'subjectname', subject_name, 
   'nscalp',      1922.0, 
   'nouter',      1922.0, 
   'ninner',      1922.0, 
   'thickness',   4.0,
   'method',      'brainstorm', 
   nargout=0)


# Head model

headmodel = self.eng.bst_process('CallProcess', 'process_headmodel', output_eeg, [], 
            'sourcespace', 1,
            'eeg',         3, 
            'openmeeg',    {
                'BemSelect':    matlab.double([1, 1, 1]), 
                'BemCond':      matlab.double([1, 0.0125, 1]), 
                'BemNames':     {'Scalp', 'Skull', 'Brain'}, 
                'BemFiles':     {}, 
                'isAdjoint':    0, 
                'isSplit':      0, 
                'isAdaptative': 1, 
                'SplitLength':  4000}, 
                nargout=1)

Regarding the first function, I also tried to run it without the float numbers, but it did not work.

Is there anything I should change or add to make the BEM surfaces generate correctly?

Thank you in advance!

This is correct, creating the process aims to create BEM surfaces which are Anatomy files, thus no functional file is returned in the output argument sFiles. See note [1] at the bottom of this post.

I tested with the GUI and Matlab scripts and the process_generate_bem creates the BEM surfaces in the Subject Anatomy. Please make sure that you are looking for the files in the Anatomy view and not in the Functional view.

Note [1]: The returned sFiles by process_generate_bem has most of their fields empty, except for FileType, Comment and Condition which have the value import.

@claudiarodgadea, most of the problems you are encountering in calling Brainstorm functions from Python have their origin in how the data is passed from between Matlab and Python.

As example I prepared this set of 4 scripts:

  1. mini_epi.m (Matlab)
    Script that imports the anatomy and recordings of the Brainstorm tutorial "EEG/Epilepsy".

  2. call_mini_epi_m.m (Matlab)
    This is just a wrapper to call mini_epi.m

  3. call_mini_epi_m.py (Python)
    This uses the Matlab engine API for Python to call mini_epi.m

  4. mini_epi.py(Python)
    This is an implementation of mini_epi.m where the individual processes are called with the Matlab engine API for Python. This looks like the code you are trying to write, encountering many issues. As you can see, the issues in your code are with how the data is passed from Python to Matlab.

The scripts are available here:
Brainstorm: Example on using Brainstorm from Python · GitHub

Debugging issues with data pass requires some expertise in both Matlab and Python as it is necessary to understand the Matlab code, and with it what is the Matlab data types that are expected, then, using this information and the documentation, decide which Python data type is the correct. Even with this, it may be necessary to temporarily modify the Matlab scripts to print out the inputs, and verify their data types. That's how I wrote the shared scripts.

https://www.mathworks.com/help/matlab/matlab_external/pass-data-between-matlab-and-python.html


One thing that I don't quite understand, is why you are aiming to re-implement all the calls to Brainstorm process to be called from Python, when you can certainly leave them as Matlab code, as just call the matlab script that contains those processes. As shown in the script call_mini_epi_m.py. Unless I'm missing something, it seems like reinventing the wheel, and looking for unnecessary problems.

1 Like

Thank you, Raymundo! I will look into the files you sent.

Best,

Claudia