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

Hi Raymundo,

I created the MATLAB file as you suggested, including the functions for generating the BEM surfaces, the head model (based on the file you provided, “mini_epi.m”), and the sources. I am calling this file from Python using the MATLAB engine. However, I am still encountering the same issue: no errors are reported, but the BEM surfaces are not generated.

What would you recommend I check or modify to resolve this?

Hi @claudiarodgadea, were you able to run successfully the provided Python scripts?
These should generate the following database:

Peek 2026-02-09 13-30

Yes, that worked fine. Thank you!

So, now you scripts are working?

No, for some reason, even though I am apparently following your script, the BEM surfaces are not being generated. However, I get this message in the terminal:

PROGRESS> Process: Running process: Generate BEM surfaces...
PROGRESS> Create BEM surfaces: Initialization...
PROGRESS> Create envelope: Initialization
PROGRESS> Envelope: Loading surface...
PROGRESS> Envelope: Computing MRI mask...
PROGRESS> Unload all: Closing figures...
PROGRESS> Compute interpolation: surface/MRI: Initialization...
PROGRESS> Loading surface...
PROGRESS> Computing interpolation...
PROGRESS> MRI/surface interpolation: Computing the interpolation matrix...
PROGRESS> Fixing the interpolation matrix...
PROGRESS> Envelope: Filling holes...
PROGRESS> Envelope: Creating isosurface...
PROGRESS> Envelope: Smoothing surface...
PROGRESS> Envelope: Downsampling surface...
PROGRESS> Envelope: Remeshing surface...
PROGRESS> Remesh surface: Remesh: Initializations...
PROGRESS> Remesh: Growing sphere...
PROGRESS> Create envelope: Initialization
PROGRESS> Envelope: Loading surface...
PROGRESS> Envelope: Computing convex hull...
PROGRESS> Envelope: Remeshing surface...
PROGRESS> Remesh surface: Remesh: Initializations...
PROGRESS> Remesh: Growing sphere...
PROGRESS> Create envelope: Initialization
PROGRESS> Envelope: Loading surface...

  • Do the scripts that I shared work as expected?

  • Is this behaviour of not created BEM also present when using your data and performing the steps manually in the GUI?

  • Could you share, as example the data and script that causes the issue, to understand what is going on? Upload the file somewhere and post the download link either in this post of send it by to me as a direct message.

No, the scripts you shared do not generate the surfaces either. They stop after the electrodes are projected onto the scalp. I am able to generate them manually in the GUI, though.

That's when running the script within Matlab?

No, that happens when calling the mini_epi.m script from the call_mini_epi_m.py script.

That's strange. Just to be in the same page, from these 4 scripts, the only one that does not works as expected is call_mini_epi_m.py?

  1. mini_epi.m (Matlab)
  2. call_mini_epi_m.m (Matlab)
  3. call_mini_epi_m.py (Python)
  4. mini_epi.py(Python)

Please provide as much detailed information as possible, it's hard to pinpoint the issue with incomplete information.

Of course, I apologize if the problem was not clear before, and thank you very much for your help and for your attention.

I have tried running mini_epi.m both from call_mini_epi_m.py and from call_mini_epi_m.m. In neither case are the BEM surfaces generated. The script simply stops when mini_epi.m reaches the BEM surface generation step.

Initially, I had a Python script that was calling the Brainstorm functions directly. Since it was not working, I thought the issue might be that the subject or the anatomy was not being properly activated. Therefore, I rewrote it using MATLAB functions that call the Brainstorm processes, following the script you sent me. However, the BEM surfaces are still not generated, and everything appears to be properly activated. The anatomy is imported correctly, exactly as in the script you provided. The raw file is also imported correctly, and the electrode projection works as well.

With the script I wrote, the BEM function is processed and returns the message I sent earlier. The head model function is also processed. However, when I load the protocol in the GUI, the BEM surfaces do not appear, and consequently, the head model does not appear either.

Please let me know if you need any additional details. Thank you again.

Do the following sanity check:

  1. Download a fresh copy of Brainstorm. To discard that the Brainstorm code was modified by accident

  2. Start Matlab

  3. Start Brainstorm (running brainstorm.m)

  4. Run in the Matlab command line

    mini_epi(TutorialDir, '_sanitiy')
    

    with TutorialDir is the full path where the sample_epilepsy.zip file has been unzipped

  5. Once it is done. In the main Brainstorm go to File > Report viewer.
    Please share the a screenshot of the report.

2 posts were split to a new topic: Unable to convert 'X' to datetime using the format 'dd-MMM-yyyy', EDF file

Hi @edelaire . Thank you for the clarification and your help. I have sent you a message with the link. Please let me know if you are able to download the file.

@claudiarodgadea, if possible, please send me the link as well.
Please note that this issue is different from the Matlab-Python interaction, so continue on the other thread:

Of course. Thank you.