Error in FEM headmodeling with Duneuro

Hi all,

I am trying to compute FEM headmodel using Brainstorm Duneuro. I was able to compute the FEM mesh for iso2mesh and ROAST. When I compute headmodel for iso2mesh (3 components), Duneuro works (but the range of values of FEM leadfields seems abnormally high as compared with BEM leadfield, so not sure if it is correct or not). On the other hand if I create FEM headmodel using ROAST FEM mesh, then Duneuro fails and gives following error:

BST> Plugin duneuro already loaded: /home/user/.brainstorm/plugins/duneuro
DUNEURO> Writing temporary files to: /home/user/.brainstorm/tmp/duneuro_240430_174450
DUNEURO> System call: "/home/user/.brainstorm/plugins/duneuro/bst_duneuro/bin/bst_duneuro_meeg_linux64.app" "/home/user/.brainstorm/tmp/duneuro_240430_174450/duneuro_minifile.mini"
Reading 3d Gmsh grid...
version 2.2 Gmsh file detected
file contains 438583 nodes
file contains 2545391 elements
number of real vertices = 438583
number of boundary elements = 0
number of elements = 2545391
"/home/user/.brainstorm/plugins/duneuro/bst_duneuro/bin/bst_duneuro_meeg_linux64.app" "/home/user/.brainstorm/tmp/duneuro_240430_174450/duneuro_minifile.mini": Segmentation fault

status =

139

This error comes in both individual and default anatomy with default cap. I use Brainstorm which was last updated on 21st March, 2024. Any help would be highly appreciated.

Hi @nikiita013

Have you used the same conductivity values?

You need also to make sure that the mesh is refined to compare the LFs, and in some case you need to make sure that the source are all within the Brain volum.

It seems the mesh has some errors. Have you tried to visualize it before calling the DUNEuro?

Hi @tmedani,

Thanks a lot for your help! The first part got resolved when I made the conductivities same. Regarding ROAST mesh, I viewed using display option and it looked fine. Is there any other way to check its goodness? Also when I checked the option to force the sources inside the gray layer at the beginning of the Duneuro, it moved all the sources to GM and then gave me same segmenatation fault error (139).

PS: At the moment I am trying everything on default head. Let me know if you need more info. to pinpoint the issue.

Thanks!

1 Like

Great to hear that you solved some issues.

Regarding the mesh, ROAST uses voxel-based segmentation, and if something goes wrong in the segmentation process, it will affect the mesh generation and then the FEM computation.

I would recommend using the SimNibs Headreco process for the FEM mesh generation; this is the option that we recommend.

Hi @tmedani ,

Thanks for your suggestion. I am trying to install simnibs but getting error.

Installing SimNIBS 4.1.0 to /home/agarwa02/SimNIBS-4.1
Found another SimNIBS installation in /home/agarwa02/SimNIBS-4.1. Remove it? [y/N] :y
Fontconfig error: Cannot load default config file: No such file: (null)
libGL error: MESA-LOADER: failed to open swrast: /usr/lib/dri/swrast_dri.so: cannot open shared object file: No such file or directory (search paths /usr/lib/x86_64-linux-gnu/dri:\$${ORIGIN}/dri:/usr/lib/dri, suffix _dri)
libGL error: failed to load driver: swrast
PyQt workaround: QT_QPA_PLATFORM_PLUGIN_PATH set to /home/agarwa02/SimNIBS-4.1/simnibs_env/plugins/platforms
Copying Gmsh Options
Copying /home/agarwa02/SimNIBS-4.1/simnibs_env/lib/python3.9/site-packages/simnibs/_internal_resources/gmsh-options_simnibsdefault -> /home/agarwa02/.gmsh-options
Adding Shortcut Icons
QXcbConnection: XCB error: 3 (BadWindow), sequence: 621, resource id: 7927609, major code: 40 (TranslateCoords), minor code: 0
Associating Files
Writing /home/agarwa02/SimNIBS-4.1/activate_simnibs
Writing /home/agarwa02/SimNIBS-4.1/uninstall_simnibs
...                                                                      [100%]QXcbConnection: XCB error: 3 (BadWindow), sequence: 726, resource id: 7928726, major code: 40 (TranslateCoords), minor code: 0

3 passed, 62 deselected in 0.30s

When I preload the libstdc++.so.6 file then I am able resolve libgl error but I get following:

Installing SimNIBS 4.1.0 to /home/agarwa02/SimNIBS-4.1
Found another SimNIBS installation in /home/agarwa02/SimNIBS-4.1. Remove it? [y/N] :y
Fontconfig error: Cannot load default config file: No such file: (null)
PyQt workaround: QT_QPA_PLATFORM_PLUGIN_PATH set to /home/agarwa02/SimNIBS-4.1/simnibs_env/plugins/platforms
Copying Gmsh Options
Copying /home/agarwa02/SimNIBS-4.1/simnibs_env/lib/python3.9/site-packages/simnibs/_internal_resources/gmsh-options_simnibsdefault -> /home/agarwa02/.gmsh-options
Adding Shortcut Icons
QXcbConnection: XCB error: 3 (BadWindow), sequence: 625, resource id: 7491623, major code: 40 (TranslateCoords), minor code: 0
Associating Files
Writing /home/agarwa02/SimNIBS-4.1/activate_simnibs
Writing /home/agarwa02/SimNIBS-4.1/uninstall_simnibs
...                                                                      [100%]
3 passed, 62 deselected in 0.29s

What else should I do? Any suggestions?

Thanks in advance!

Hi @tmedani,

It actually worked. Charm works, I am able to create simnibs 4 (charm) mesh but Duneuro takes a lot of time 24 hrs and still running. Does it take that long? Should I install SuperLU? I am running on Linux machine (server). What do you suggest?

I am also trying to run with headreco. Will update once done.

Thanks!

Hey @nikiita013
The computation can last longer depending on the performance of your computer, as well as some other parameters, including the resolution of the mesh, the number of channels, the source resolution, and the model...
Can you share a screenshot of the Matlab terminal just to see at what step the computation is?

Hi @tmedani ,

Thanks for your reply. Sorry for late response. Following is the terminal output:

BST> Checking latest online version for duneuro...
BST> Adding plugin duneuro to path: /home/user/.brainstorm/plugins/duneuro/bst_duneuro
BST> Adding plugin duneuro to path: /home/user/.brainstorm/plugins/duneuro/bst_duneuro/bin
DUNEURO> Writing temporary files to: /home/user/.brainstorm/tmp/duneuro_240522_140829
DUNEURO> System call: "/home/user/.brainstorm/plugins/duneuro/bst_duneuro/bin/bst_duneuro_meeg_linux64.app" "/home/user/.brainstorm/tmp/duneuro_240522_140829/duneuro_minifile.mini"
Reading 3d Gmsh grid...
version 2.2 Gmsh file detected
file contains 669243 nodes
file contains 3748503 elements
number of real vertices = 669243
number of boundary elements = 0
number of elements = 3748503
volume_conductor.time_reading_gmsh = 114.401
tensors = 12
time = 0.018281
volume_conductor.time_reading_tensors = 0.0183118
volume_conductor.time_reordering_indices = 0.286049
volume_conductor.time = 114.705
degree = 1
element_type = tetrahedron
WARNING: You are using AMG without SuperLU! Please consider installing SuperLU, or set the usesuperlu flag to false to suppress this warning.
time = 0.000307239

Sorry I could not take the full screenshot of the terminal as the whole text was not fitting in. The Duneuro computation never finished and also did not give any error.

Also, I was trying to install headreco but it gives me command not found via brainstorm on MATLAB. I posted my question Headreco command not found in matlab (brainstorm) · simnibs/simnibs · Discussion #342 (github.com). So far no help. Do you have suggestion for this as well?

From this screen, it seems that computation doesn't start. only the data are passed to the solver, and then nothing.
Can you share screenshot of the model that you are using, as well as some details of the model.
Can you also share a screenshot of the files in this folder:
/home/user/.brainstorm/tmp/duneuro_240522_140829/

it seems that matlab can't see simnibs.
have you tried to follow the instruction that Oulap shared?

can you open a terminal and try to run some simnibs commands?

Hi @tmedani ,

Thanks a lot for your reply! Following is the screenshot of the files in this folder:
/home/user/.brainstorm/tmp/duneuro_240522_140829/

I did not understand exactly which model you are referring to... Following are the settings of the Duneuro. To create mesh I use simnibs 4.1.0 in brainstorm with default values to create the mesh.
image

Please let me know if you want some other information. I will share. Thanks a lot!

Hi @tmedani , Thanks for your reply.

Yes I tried Oulap's reply and it worked. I tried to create the headreco FEM mesh but it fails due to CAT segmention. Following is the terminal output of MATLAB.

reading binary
Number of Nodes              : 660073
Number of Triangles          : 933289
Number of Triangle Regions   : 9
Number of Tetrahedra         : 3699670
Number of Tetrahedron Regions: 9
Elapsed time is 0.341284 seconds.
Warning: MATLAB has disabled some advanced graphics rendering features by switching to software OpenGL. For more information,
click here. 
FEM> T1 MRI: /home/user/SharedData/844/hbnetdata/HBN_headmodels/brainstorm_db_HBN/CMI-HBN_CS_1/anat/@default_subject/subjectimage_T1.mat
FEM> T2 MRI: 
BST> Processing dependencies: cat12 requires: spm12 
BST> Plugin cat12 already loaded: /home/user/.brainstorm/plugins/cat12

========================================================================
PREPARING VOLUMES FOR MESHING
========================================================================

Copying input files to m2m_default_subject

Copying T1fs.nii.gz to T1fs_conform.nii.gz
Preparing to segment

Starting MATLAB
========================================================================

                                                      < M A T L A B (R) >
                                            Copyright 1984-2024 The MathWorks, Inc.
                                       R2024a Update 2 (24.1.0.2578822) 64-bit (glnxa64)
                                                         April 9, 2024

 
To get started, type doc.
For product information, visit www.mathworks.com.
 
Downsampling factor for segmentation : 3



------------------------------------------------------------------------
Running job #1
------------------------------------------------------------------------
Running 'Segment'

SPM12: spm_preproc_run (v6365)                     14:32:45 - 31/05/2024
========================================================================
Segment /home/user/.brainstorm/tmp/headreco_240531_143226/m2m_default_subject/segment/spm/T1fs_conform.nii
Completed                               :          14:39:25 - 31/05/2024
Done    'Segment'
Running 'Normalise: Write'
Done    'Normalise: Write'
Running 'Deformations'
spmprior_tissue	1,1,1
spmprior_eye1	1,1,1
spmprior_eye2	1,1,1
spmprior_air	1,1,1
spmprior_ventricles_lateral	1,1,1
spmprior_spinal	1,1,1
Done    'Deformations'
Done

Getting transformation matrices from MNI to subject space...

SPM12: spm_coreg (v6435)                           14:39:32 - 31/05/2024
========================================================================
Completed                               :          14:39:39 - 31/05/2024
Item extopts: No field(s) named
darteltpm
Item cat: No field(s) named
extopts


------------------------------------------------------------------------
Running job #1
------------------------------------------------------------------------
Running 'CAT12: Segmentation'

------------------------------------------------------------------------
CAT12 r1278: 1/1:       ./m2m_default_subject/segment/cat/T1fs_conform.n
------------------------------------------------------------------------
APPs bias correction:                                             
  Preparation                                                         1s
  SPM bias correction (samp: 6.00 mm, fwhm: 120 mm)               bias=   39 mm    21s
  SPM bias correction (samp: 5.70 mm, fwhm: 105 mm)                  26s
  SPM bias correction (samp: 5.40 mm, fwhm:  90 mm)                  26s
  SPM bias correction (samp: 5.10 mm, fwhm:  75 mm)                  38s
  SPM bias correction (samp: 4.80 mm, fwhm:  60 mm)                  39s
  SPM bias correction (samp: 4.50 mm, fwhm:  45 mm)                  44s
  Postprocessing                                                    202s
SANLM denoising (NCstr=-Inf):                                        14s
Coarse affine registration:                                           3s
Affine registration                                                   6s
SPM preprocessing 1 (estimate):                                     147s
SPM preprocessing 2 (write):                                         41s
Global intensity correction:                                          8s
SANLM noise correction:                                               4s
Local adaptive segmentation (LASstr=0.50):                        
  Prepare maps                                                        3s
  Prepare partitions                                                  1s
  Prepare segments (LASmod = 1.00)                                    7s
  Estimate local tissue thresholds                                   35s
  SANLM noise correction for LAS                                      3s
                                                                     48s
ROI segmentation (partitioning):                                  
  Atlas -> subject space                                              4s
  Major structures                                                    4s
  Ventricle detection                                                 4s
  Blood vessel detection                                              2s
  WMH detection (WMHCstr=0.50)                                        6s
  Closing of deep structures                                          0s
  Side alignment                                                      2s
  Final corrections                                                   1s
                                                                     24s
Blood vessel correction (BVCstr=0.50):                                0s
Skull-stripping using graph-cut (gcutstr=0.50):                   
  WM initialisation                                                   2s
  GM region growing                                                   1s
  GM-CSF region growing                                               2s
  CSF region growing                                                  2s
  Ventricle filling                                                   1s
                                                                      8s
Amap using initial SPM12 segmentations (MRF filter strength 0.07):   15s
    AMAP peaks: [CSF,GM,WM] = [0.39±0.06,0.73±0.12,0.99±0.02]
Final cleanup (gcutstr=0.50):                                     
  Level 1 cleanup (ROI estimation)                                    2s
  Level 1 cleanup (brain masking)                                     0s
  Level 2 cleanup (CSF correction)                                    0s
  Level 3 cleanup (CSF/WM PVE)                                        0s
                                                                      3s
Internal WMH correction for spatial normalization (WMHCstr=0.50):     0s


Dartel registration with 1.50 mm on a 1.50 mm Template:           
  Template: "/home/user/SimNIBS-3.2/simnibs_env/lib/python3.7/site-packages/simnibs/resources/spm12/toolbox/cat12/templates_1.50mm/Template_1_IXI555_MNI152.nii"
    1 | 0.0948 |   301802        0   301802  213.851 
    2 | 0.0908 |   289106     4980   294086   71.464 
    3 | 0.0908 |   289136     4365   293501   51.517 
    4 | 0.0864 |   275161     2207   277368  116.543 
    5 | 0.0843 |   268539     6854   275393   55.038 
    6 | 0.0844 |   268675     6461   275135   49.528 
    7 | 0.0803 |   255560     3336   258897  109.457 
    8 | 0.0773 |   246207     9705   255912   48.686 
    9 | 0.0775 |   246643     9053   255697   45.472 
   10 | 0.0739 |   235232     4760   239993  108.121 
   11 | 0.0699 |   222633    12803   235436   48.842 
   12 | 0.0699 |   222677    12392   235070   40.963 
   13 | 0.0681 |   216799     6506   223304   97.787 
   14 | 0.0639 |   203355    14717   218071   44.700 
   15 | 0.0637 |   202720    14833   217553   38.166 
   16 | 0.0636 |   202489    15550   218039   44.347 
   17 | 0.0634 |   201815    15701   217515   37.054 
   18 | 0.0632 |   201313    16047   217359   34.312 
                                                                    107s
  Jacobian determinant (RMS):  0.086818 
           Template Matching:  0.063218 
Write result maps:                                                    6s
Surface and thickness estimation:                                 
lh:
  Thickness estimation (0.50 mm³):                                
    WM distance:                                                     33s
    CSF distance:                                                    27s
    PBT2x thickness:                                                 27s
    Final Corrections:                                                0s
                                                                     94s
                                                                     95s
  Create initial surface                                             26s
  Reduce surface to 100000 faces:                                    13s
  Topology correction and surface refinement:                        73s
  Thickness / Depth mapping                                           0s
  Final correction of central surface in highly folded areas          3s
  Spherical mapping with areal smoothing                            142s
  Spherical registration                                            264s
rh:
  Thickness estimation (0.50 mm³):                                
    WM distance:                                                     33s
    CSF distance:                                                    27s
    PBT2x thickness:                                                 27s
    Final Corrections:                                                0s
                                                                     93s
                                                                     94s
  Create initial surface                                             28s
  Reduce surface to 100000 faces:                                    13s
  Topology correction and surface refinement:                        77s
  Thickness / Depth mapping                                           0s
  Final correction of central surface in highly folded areas          3s
  Spherical mapping with areal smoothing                            141s
  Spherical registration                                            261s
Display thickness: /home/user/.brainstorm/tmp/headreco_240531_143226/m2m_default_subject/segment/cat/surf/lh.thickness.T1fs_conform
Display thickness: /home/user/.brainstorm/tmp/headreco_240531_143226/m2m_default_subject/segment/cat/surf/rh.thickness.T1fs_conform
Surface and thickness estimation:                                  1245s
Quality check:                                                    
------------------------------------------------------------------------
CAT Preprocessing error for T1fs_conform:
------------------------------------------------------------------------
Can't write XML-file '/home/user/.brainstorm/tmp/headreco_240531_143226/m2m_default_subject/segment/cat/report/cat_T1fs_conform.xml'!

------------------------------------------------------------------------
  155 - cat_io_xml
  837 - cat_vol_qa
 1811 - cat_main
 1185 - cat_run_job
   18 - cat_run_newcatch
  395 - run_job
  161 - cat_run
   29 - cfg_run_cm
 1688 - local_runcj
  959 - cfg_util
  469 - fill_run_job
  247 - spm_jobman
   73 - segment_CAT
------------------------------------------------------------------------
Failed  'CAT12: Segmentation'
Error using cat_io_xml (line 155)
Can't write XML-file '/home/user/.brainstorm/tmp/headreco_240531_143226/m2m_default_subject/segment/cat/report/cat_T1fs_conform.xml'!

In file "/home/user/SimNIBS-3.2/simnibs_env/lib/python3.7/site-packages/simnibs/resources/spm12/toolbox/cat12/cat_io_xml.m" (v1212), function "cat_io_xml" at line 155.
In file "/home/user/SimNIBS-3.2/simnibs_env/lib/python3.7/site-packages/simnibs/resources/spm12/toolbox/cat12/cat_vol_qa.m" (v1270), function "cat_vol_qa" at line 538.
In file "/home/user/SimNIBS-3.2/simnibs_env/lib/python3.7/site-packages/simnibs/resources/spm12/toolbox/cat12/cat_run_newcatch.m" (???), function "cat_run_newcatch" at line 66.
In file "/home/user/SimNIBS-3.2/simnibs_env/lib/python3.7/site-packages/simnibs/resources/spm12/toolbox/cat12/cat_run.m" (v1245), function "run_job" at line 395.
In file "/home/user/SimNIBS-3.2/simnibs_env/lib/python3.7/site-packages/simnibs/resources/spm12/toolbox/cat12/cat_run.m" (v1245), function "cat_run" at line 161.

The following modules did not run:
Failed: CAT12: Segmentation

This CAT12 module was installed by Brainstorm itself.
Any suggestions?

Thanks in advance!

Hi @tmedani,

I am able to run headreco FEM mesh. The earlier problem (XML issue) with headreco was solved using previous MATLAB version R2022a.

I ran Duneuro and it gives following error:

Do you know how to solve this issue?

> dipole_185300.density = sparse
> dipole_185300.time = 4.725e-05
> dipole_185301.density = sparse
> dipole_185301.time = 7.5569e-05
> dipole_185302.density = sparse
> dipole_185302.time = 6.085e-05
> dipole_185303.density = sparse
> dipole_185303.time = 6.317e-05
> dipole_185304.density = sparse
> dipole_185304.time = 7.9649e-05
> dipole_185305.density = sparse
> dipole_185305.time = 6.244e-05
> dipole_185306.density = sparse
> dipole_185306.time = 6.3159e-05
> Dune reported error: Dune::Exception [findEntityImpl:/home/juan/bst-duneuro/src/duneuro/duneuro/common/edgehopping.hh:84]: coordinate is outside of the grid, or grid is not convex
> "/home/agarwa02/.brainstorm/plugins/duneuro/bst_duneuro/bin/bst_duneuro_meeg_linux64.app" "/home/agarwa02/.brainstorm/tmp/duneuro_240704_153253/duneuro_minifile.mini": Signal 127
> 
> status =
> 
>    255

Are you using the cortex surface or the volume grid to compute the forward solution?

image

From this error, it seems that some dipoles are outside of the FEM mesh (Dipole 185306).

Hi @tmedani,

Thanks a lot for your reply. I am using cortex surface. It gives error even after checking the option 'Force source space inside gray'. I tried using volume grid and it works. Is there no way to use cortex surface?

Regards,
Nikita

Hi @nikiita013

How are you generating the FEM mesh of the head model?
How many layer are you using?

In some cases, when the segmentation/ mesh generation is not good,
it may require manual editing.

Hi @tmedani, thanks for your reply.

I am using default anatomy -> MRI segmentation -> Generate FEM mesh -> SIMNIBS 3 -> Following default parameters chosen:

Vertex density: 0.5
Number of vertrices: 15000
Import default cortical thickness and default cap: yes

After that it generates FEM mesh with 12 layers.

Is it also possible that for default head also there are problems in mesh generation? Also, how can manual editing be done?

Kind regards

Can you share this FEM model + the cortex file + channel location?
I'll have a look at it.