1 Introduction

Non-invasive brain stimulation (NIBS) aims at modulating brain activity by inducing electric fields in the brain [1]. The electric fields are generated either by a magnetic coil, in the case of transcranial magnetic stimulation (TMS), or by a current source and electrodes placed directly on the scalp, in the case of transcranial electric stimulation (TES). In both cases, the induced electric fields in the brain have a complex and often counter-intuitive spatial distribution, which is dependent on the individual anatomy of a target subject. In recent years, there has been a growing interest in moving away from a one-size-fits-all stimulation approach in NIBS to more individually informed protocols [2]. The driving force behind this shift is the widely reported variation of NIBS effects within and between individuals [3], which could be explained in part by the interplay of the individual anatomy and the electric field propagation [4]. Although software tools have become available that generate realistic anatomical models of the head based on magnetic resonance imaging (MRI) scans and use those models to numerically estimate the electric field induced in the brain, they are still not predominantly used in NIBS studies. This is likely due to the lack of robustness and usability of the previous generation of tools, in turn hampering the individualized application of NIBS in both mapping the human brain function and as a rehabilitation tool in various neuropathologies [5, 6].

The aim of SimNIBS is to facilitate the use of individualized stimulation modelling by providing easy-to-use software tools for creating head models, setting up electric field simulations, and visualizing and post-processing the results both at individual and group levels. SimNIBS was first released in 2013 [7], had a major update in 2015, with the release of version 2 [2], and more recently another major update with the release of version 2.1, described in the current work. SimNIBS 2.1 is a free software, distributed under a GPL 3 license, and runs on all major operating systems (Windows, Linux and MacOS). In this tutorial, we will concentrate on what SimNIBS 2.1 can be used for and how the analyses are performed in practice with step-by-step examples. The chapter is structured as follows: First, we give a general overview of the simulation pipeline and of its building blocks. Next, we provide a step-by-step example of how to run a simulation in a single subject, and then we demonstrate a set of MATLAB tools developed for easy processing of multiple subjects. Finally, we conclude with an analysis of the accuracy of automated electrode positioning approaches. More information, as well as detailed tutorials and documentation can be found from the website www.simnibs.org.

2 Overview of the SimNIBS Workflow

Figure 1.1 shows an overview of the SimNIBS workflow for an individualized electric field simulation. The workflow starts with the subject’s anatomical MRI images, and optionally diffusion-weighted MRI images. These images are segmented into major head tissues (white and grey matter, cerebrospinal fluid, skull and scalp). From the segmentations, a volume conductor model is created, and used for performing the electric field simulations. The simulations can be set up in a graphical user interface (GUI) or by scripting. Finally, the results can be mapped into standard spaces, such as the Montreal Neurological Institute (MNI) space or FreeSurfer’s FsAverage.

Fig. 1.1
figure 1

Overview of the SimNIBS workflow

2.1 Structural Magnetic Resonance Imaging Scans

The minimum requirement for running an individualized SimNIBS simulation is a T1-weighted structural scan of a subject’s head anatomy. Although SimNIBS will run on almost all types of T1-weighted scans, we have found that setting the readout bandwidth low to ensure a good signal-to-noise ratio in the brain region and using a fat suppression method, such as selective water excitation, to minimize the signal from spongy bone, typically ensure a high quality of the resulting head models. See Fig. 1.2 for an example of good quality scans we found to work well with SimNIBS and [8] for the details of the sequences.

Fig. 1.2
figure 2

Example of high-quality T1- and T2-weighted scans likely to work well with SimNIBS. Note that in the T1-weighted scan, the skull appears dark due to the fat suppression

Including a T2-weighted scan is optional, but highly recommended as it facilitates accurate segmentation of the border between skull and cerebrospinal fluid (CSF). Both skull and CSF appear dark in T1-weighted scans, whereas in T2-weighted scans the CSF lights up, thus guiding the separation between the tissues. Skull has a low electric conductivity, while CSF is highly conducting, meaning that any segmentation errors in these two compartments can have a large effect on the resulting electric field distribution inside the head, especially when TES is applied [8]. If you are interested in modelling the neck region in detail, we recommend using neck coils if these are available at the imaging site.

Optionally, SimNIBS also supports modelling of anisotropic conductivities for grey (GM) and white matter (WM), which requires a diffusion-weighted MRI scan (dMRI). Only single shell data (i.e. with a single b-value in addition to some b = 0 images) with a single phase encoding direction for the echo planar imaging (EPI) readout is supported.

2.2 Volume Conductor Modelling

The first step in the pipeline is the generation of a volume conductor model of the head, which is needed for simulating the induced electric fields. In order to create this finite element (FEM) mesh, we need to assign each voxel in the MRI scan(s) to a specific tissue class, i.e. to segment the scan into the different head tissues. Currently, SimNIBS offers two options for segmentation: mri2mesh [7] and headreco [8].

mri2mesh combines FSL [9] (version 5.0.5 or newer) and FreeSurfer [10] (version 5.3.0 or newer) to segment the head tissues. FSL is used to segment the extra-cerebral tissues, while FreeSurfer is used to segment the brain and to generate accurate surface reconstructions of the grey matter sheet. Note that mri2mesh is restricted only to the head and does not create models of the neck region.

headreco uses the SPM12 [11] toolbox for segmenting the MRI scan, and is now the recommended option in SimNIBS. It has been shown to be more accurate in segmenting the extra-cerebral structures, especially the skull, compared to mri2mesh [8], while also providing accurate segmentations of the brain tissues. The computational anatomy toolbox (CAT12, recommended) [12] provided with SPM can be used to create surface reconstructions of the grey matter sheet which are on par with the accuracy of those generated by FreeSurfer [12]. In addition, headreco has an extended field of view, also modelling the neck region. For ease of use, both SPM12 and CAT12 are distributed together with SimNIBS.

Once the segmentation by either method has finished successfully, the tissue maps are cleaned by applying simple morphological operations, and used to create surface reconstructions. As a final step, the FEM mesh is generated by filling in tetrahedrons between the tissue surfaces using Gmsh [13].

Neither mri2mesh nor headreco have off-the-shelf support for pathologies such as tumours or lesions. These can however be included into the head models by manually editing the segmentation masks generated by the methods. When using mri2mesh, please consult the FreeSurfer website (https://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/WhiteMatterEdits_freeview) on how to handle scans with pathologies. Manual edits using headreco should be done on the output segmentation masks in the mask_prep folder located within the m2m_{subID} folder. Once corrections have been made, the surface meshing step (“headreco surfacemesh subID”) and volume meshing step (“headreco volumemesh subID”) should be re-run to generate the edited head model. Note that when creating head models from scans with pathologies, the CAT12 toolbox should not be used.

dwi2cond (optional) uses FSL (version 5.0.5 or newer) to prepare diffusion tensors for GM and WM from dMRI data. The tensors are used by SimNIBS to estimate anisotropic conductivities in WM and GM during the FEM calculations.

2.3 Simulation Setup

Simulations can be set up using the graphical user interface (GUI), which provides an interactive view of the head model. This allows users to easily select parameters such as coil positions, electrode positions and shapes, as well as more advanced settings such as tissue conductivities and post-processing options.

It might also be of interest to do simulations of one or a few different setups across a group of subjects. With this in mind, version 2.1.1 introduced a new interface for setting up simulations using MATLAB or Python scripts.

The GUI as well as the scripts will be described in more detail in Sect. 1.3, as well as on the website www.simnibs.org.

2.4 Finite Element Method Calculations

Transcranial direct current stimulation (tDCS) simulations begin by adding electrodes to the head model. In this step, nodes in the skin surface are shifted to form the shape of the electrode, while keeping good quality elements. Afterwards, the body of the electrodes is constructed by filling in tetrahedra. As this step does not require re-meshing the entire head, it can be done much more efficiently compared to other methods that require re-meshing, especially when only a few electrodes are used.

TMS simulations start by calculating the change in the magnetic vector potential A, that is the \( \raisebox{1ex}{$ dA$}\!\left/ \!\raisebox{-1ex}{$ dt$}\right. \) field in the elements of the volume conductor mesh for the appropriate coil model, position and current. There are currently two types of coil models:

  • .ccd files: Created from geometric models of the coil and represented as a set of magnetic dipoles from which we can calculate the \( \raisebox{1ex}{$ dA$}\!\left/ \!\raisebox{-1ex}{$ dt$}\right. \) field using a simple formula [14].

  • .nii files: Created either from geometric models of the coils or direct measurement of the magnetic field [15]. Here, the \( \raisebox{1ex}{$ dA$}\!\left/ \!\raisebox{-1ex}{$ dt$}\right. \) field is defined over a large volume, and the calculation of the \( \raisebox{1ex}{$ dA$}\!\left/ \!\raisebox{-1ex}{$ dt$}\right. \) at the mesh elements is done via interpolation. This allows for faster simulation setup at little to no cost in simulation accuracy.

Both simulation problems are solved using the FEM with linear basis functions. This consists of constructing and solving a linear system of the type Mu = b, where M is a large (in SimNIBS typically ~106 × 106) but sparse matrix, called the “stiffness matrix”, u are the electric potentials at the nodes and the right-hand side b contains information about boundary conditions (such as potentials in electrode surfaces in tDCS simulations), and source terms (such as the \( \raisebox{1ex}{$ dA$}\!\left/ \!\raisebox{-1ex}{$ dt$}\right. \) field in TMS simulations). SimNIBS solves the linear system using an iterative preconditioned conjugate gradient method [16]. SimNIBS 2.1 uses GetDP [17] to form the linear system, which in turn calls PETSc [18] to solve it.

TDCS simulations can also be easily extended to simulations of transcranial alternating current stimulation (tACS). In the frequency ranges used in tACS, a quasi-static approximation holds [19]. In the quasi-static approximation, the relationship between input currents I(t) and the electric field at the positions x, E(x) is linear:

$$ E\left(x,t\right)=\alpha (x)I(t) $$

where α(x) is a proportionality constant, meaning that it does not vary during the oscillation. This constant can be obtained simply by running a simulation where we set the input current to unity. I(t) is the input current. For example, a sinusoidal current input can be written as

$$ I(t)={I}_o\sin \left(2\pi \raisebox{1ex}{$t$}\!\left/ \!\raisebox{-1ex}{$f$}\right.+\phi \right) $$

where f is the stimulator frequency, ϕ the stimulator phase and Io the stimulator amplitude, which corresponds to half of the peak-to-peak current. Usually, we would visualize the electric field at the maximum or minimum of I(t), which corresponds to ±Io. In case several stimulators are used at different frequencies of phases, we have several pairs (αi(x)Ii(t)), one for each stimulator, and the total electric field at a given time point is given by the sum of their individual contributions

$$ E\left(x,t\right)=\sum \limits_{i=1}^n{\alpha}_i(x){I}_i(t) $$

2.5 Mapping Fields

The result of the FEM calculation is the electric field at each tetrahedral element of the subject’s head mesh. However, visualization is often easier using cortical surfaces or NifTI volumes. Therefore, SimNIBS 2.1 can transform fields from the native mesh format to these formats via interpolation. Our interpolation algorithm is based on the superconvergent patch recovery method [20], which ensures interpolated electric field values that are consistent with tissue boundaries.

When performing simulations on multiple subjects, we often want to be able to directly compare the electric field across subjects to, for example, correlate the electric field with behavioural or physiological data on the stimulation effects [21]. For this purpose, SimNIBS can also transform simulation results to the MNI template, using linear and non-linear co-registrations, as well as to the FreeSurfer’s FsAverage surface.

3 Practical Examples and Use Cases

3.1 Hello SimNIBS: How to Process a Single Subject

Here we describe how to run a TMS and a tDCS simulation on a single example subject. The example subject “Ernie” can be downloaded from the SimNIBS website, and the steps below can be reproduced step by step to get familiar with SimNIBS.

3.1.1 Generating the Volume Conductor Model

Open a terminal and go to the directory “ernie” to access the example data set. Copy the content of the “org”-subfolder to another location in order to not overwrite the files of the original example dataset. Next, go to the folder where you copied the data, and call headreco to generate the volume conductor model:

headreco all --cat ernie ernie_T1.nii.gz ernie_T2.nii.gz

In the command, the first argument, “all”, tells headreco to run all reconstruction steps including: segmentation, clean-up of tissue maps, surface meshing, and volume meshing. The second argument, “--cat” is a flag for using the CAT12 toolbox for accurate reconstruction of the cortical surface. The third argument, “ernie”, is a subject identifier (subID), which is used to name generated folders, e.g. m2m_ernie, and output files, e.g. ernie.msh. The two final arguments are the paths to the T1- and T2-weighted structural scans.

A few extra input options are useful to know:

  • -d no-conform Adding this option will prevent headreco from modifying, i.e. transforming and resampling, the original MRI scan. This might be desirable when a one-to-one correspondence between the head model coordinates and the neural navigation system coordinates is required.

  • -v < density > This option allows you to set the resolution, or vertex density (nodes per mm2), of the FEM mesh surfaces. By default, SimNIBS uses 0.5 nodes/mm2 as the <density > value.

In general, we recommend using the --cat option; however, the execution time will be longer compared to omitting the option. In addition, if you want to process scans with pathologies, you should not use CAT12, as the cortical reconstruction is not designed to work with pathologies.

After headreco has finished, please check the quality of the head model by calling:

headreco check ernie

If needed, open a new terminal for this operation and go into the folder in which you started headreco the first time. For our example case, the subject identifier is “ernie”, but please replace this one with whichever subID was used in the first call to headreco. Note that we recommend that you have installed freeview (provided by FreeSurfer, available on Linux and Mac OS X platforms) to visualize the results. The check function displays two windows for inspecting the output. The first window shows the T1-weighted scan with the segmentation and structure borders overlaid (Fig. 1.3, left). We recommend de-selecting the segmentation (ernie_final_contr.nii) in freeview, and checking that the segmentation borders follow the intensity gradients of different tissues (Fig. 1.3, middle). Fig. 1.4 shows the second freeview window, which displays the T1-weighted scan co-registered to the MNI template. We recommend checking if the T1-weighted scan overlaps well with the MNI template by de-selecting the T1-weighted scan (T1fs_nu_nonlin_MNI.nii) in freeview (Fig. 1.4, right). Figure 1.5 shows an example of a segmentation error where the skull is erroneously labelled as skin. This can be seen in the front of the head, where the skin label protrudes into the skull. This example emphasizes the need for fat-suppressed data when only a T1-weighted scan is used. In the scan shown in Fig. 1.5, spongy bone is bright with intensities comparable to those of scalp, causing the segmentation method to mis-classify it as extra-cerebral soft tissue. Small segmentation errors like this can be corrected by manually re-labelling the segmentation masks in the “mask_prep” folder located in the m2m_{subID} folder, and re-running the surface and volume meshing steps. If you are not familiar with using freeview, please refer to the tutorial on the SimNIBS website ( http://www.simnibs.org/_media/docu2.1.1/tutorial_2.1.pdf ). If you do not have access to freeview, the visualizations will be displayed using SPM. However, these are very primitive and are not recommended for checking the output from headreco.

Fig. 1.3
figure 3

Data displayed after calling the check option. Left: T1-weighted scan with the segmentation and structure borders overlaid. Middle: structure borders overlaid on the T1-weighted scan after de-selecting the segmentation in freeview. Right: zoom-in of the cortex. Note that the segmentation borders nicely follow the intensity borders between the tissues

Fig. 1.4
figure 4

Data displayed after calling the check option. Left: T1-weighted scan co-registered to the MNI template. Right: MNI template shown after de-selecting the T1-weighted scan in freeview. Note that the scans seem to be well registered

Fig. 1.5
figure 5

Example of a segmentation error after headreco processing. The spongy bone is erroneously labelled as skin. This example emphasizes the need for fat-suppression when using only a T1-weighted scan

Finally, you should inspect the volume conductor mesh for any obvious errors. This can be done by calling:

gmsh ernie.mshin the subject folder. This call opens a gmsh window displaying the generated head model; please see the tutorial on the website if you are not familiar with gmsh ( http://www.simnibs.org/_media/docu2.1.1/tutorial_2.1.pdf ).

The folder structure and most important files are shown in Table 1.1.

  • eeg_positions/ Folder containing the 10-10 electrode positions for the subject both as a “.csv”, used for acquiring electrode positions, and a “.geo” file, used for visualization of the positions in Gmsh. If you have custom electrode positions, they should be added here as a .csv file.

  • mask_prep/ Folder containing the cleaned tissue maps along with the white matter and pial surface files if CAT12 was used. In case there are errors in the segmentation, the masks can be manually corrected and a new head model can subsequently be generated. Note that the CAT12 WM and GM surfaces can currently not be modified.

  • headreco_log.html, a log-file with output from the headreco run. If something goes wrong, the log-file helps with troubleshooting, and should be sent as an attachment when contacting the SimNIBS support email list (support@simnibs.org).

  • ernie.msh, the FEM head model used for the simulations.

  • ernie_T1fs_conform.nii.gz, the input scan in the conform space defined by the –d option. This scan has the same millimetre space as the head model, and can be used to annotate landmarks which can then be directly transformed onto the head model.

Table 1.1 The folder structure after headreco has finished. In this table, only the most important folders and files are listed

3.1.2 Setting Up a Simulation

Once the head model is ready, we can set up tDCS and TMS simulations interactively using the GUI. The GUI can be started on the command line by calling:


In the GUI, the user can:

  • Visualize and interact with head models.

  • Define electrode and coil positions by clicking in the model or selecting a position from the EEG 10-10 system.

  • Visually define electrode shapes and sizes.

  • Select from the available coil models.

  • Change tissue conductivity parameters and set up simulations with anisotropic conductivity distributions.

  • Run simulations.

In the GUI, there are two types of tabs, one for tDCS simulations, and another for TMS simulations, shown respectively in the top and bottom of Fig. 1.6. The tDCS tabs define a single tDCS field simulation with an arbitrary number of electrodes. On the other hand, TMS tabs can define several TMS field simulations using the same coil. For this example, we will set up a tDCS simulation with a 5 × 5 cm anode placed over C3 and a 7 × 5 cm cathode placed over AF4, and a TMS simulation with the coil placed over the motor cortex, pointing posteriorly. Details on how to use the graphical interface can be found on the website ( http://www.simnibs.org/_media/docu2.1.1/tutorial_2.1.pdf ).

Fig. 1.6
figure 6

Set-up of a tDCS (top) and a TMS (bottom) simulation in the graphical user interface

After the simulation setup, click on the Run button to start the simulations. Running both simulations takes 10–15 minutes, depending on the computer, and uses around 6 GB of memory. As a note, before starting the simulations, you can set additional options (in the menu Edit➔Simulation Options) to let SimNIBS write out the results as surface data or NifTI volume data. This is not further covered in this basic example, but the output files created in these cases are described in the next example. The results of the simulation will be written in the output folder specified in the GUI, in this case “simnibs_simulation/”. The folder has the files shown below in Table 1.2.

Table 1.2 The output folder of a simple tDCS and TMS simulation
  • ernie_TDCS_1_scalar.msh” is the output from the tDCS simulation, in Gmsh “.msh” format. The first part of the file name, “ernie”, is the subID. The second part, “TDCS”, informs us that this is a tDCS simulation. The third part, “1”, denotes that this was the first simulation we have defined in the GUI, and finally, “scalar” tells us have used scalar (as opposed to anisotropic) conductivities for the simulations.

  • ernie_TMS_2-0001_Magstim_70mm_Fig8_nii_scalar.msh” is the output of the second simulation, also in gmsh “.msh” format. As is the case for the tDCS output, the first part of the file name is the subID, and the second is the number of the simulation in the simulation list. We next see the number of the TMS position, as it might happen that several TMS positions are defined in a single TMS list. Following this, “Magstim_40mm_Fig8_nii” gives us the name of the coil used for the simulation, and “scalar” the type of conductivity.

  • ernie_TMS_2-0001_Magstim_70mm_Fig8_nii_coil_pos.geo” is a Gmsh “.geo” file which shows the coil position for the corresponding simulation.

  • simnibs_simulation_20180920-13041.log” is a text file with a detailed log of the simulation steps. This file can be used for troubleshooting. Here, the second part of the file is date and time information of when the simulation started.

  • simnibs_simulation_20180920-13041.mat” is a MATLAB data file with the simulation setups. This file can be loaded into the GUI or MATLAB at a later time to check the simulation parameters, or to change them and re-run the simulation.

3.1.3 Visualizing Fields

The electric field E is a vector field meaning that the electric field has both a norm (i.e. vector length or magnitude) and a direction in space, as shown in Fig. 1.7. As visualizations of the entire vector are challenging and often unclear, in SimNIBS we usually visualize the norm (or strength) of the electric field instead. The norm of the electric field corresponds to the size of the electric field vector, and therefore is always positive and does not contain any information about the direction of the electric field.

Fig. 1.7
figure 7

Decomposition of a vector E in relation to a surface. The norm corresponds to the length of the vector. At each point, the surface defines a normal vector \( \hat{\mathbf{n}} \), and this vector is perpendicular to the tangent plane to the surface at that point. Given the normal vector, we can decompose the vector E into normal and tangent components. The normal component is the part of E in the same line as the normal vector, and the tangent component is perpendicular to it. The normal component also has a sign, indicating if the field is entering or leaving the surface. In SimNIBS, a positive normal indicates that the field is entering the surface, and a negative normal indicate the field is leaving the surface

One way we can quickly visualize the simulation results is to use the mesh_show_results MATLAB function. This function comes as a part of SimNIBS version 2.1.2, and provides visualizations of the output fields using MATLAB plotting tools, as well as some summary values for the field strength and focality. For example, when running the function on the output tDCS mesh, we obtain the plot shown in Fig. 1.8a, and the values below in Table 1.3.

Fig. 1.8
figure 8

Visualization of (a) tDCS and (b) TMS electric field norms in MATLAB

Table 1.3 Output of mesh_show_results for the tDCS simulation

The first lines in Table 1.3 show that the displayed data is the field “norm E”, that is the norm or strength of the electric field, calculated in the region number 2, which corresponds to the GM volume. Afterwards, we have information on the peak electric fields. We see that the value of 0.161 V/m corresponds to the 95th percentile of the norm of the electric field, the value of 0.201 V/m to the 99th percentile and 0.249 to the 99.9th percentile. We also have information about the focality of the electric field. Here, focality is measured as the GM volume with an electric field greater or equal to 50% or 75% of the peak value. To avoid the effect of outliers, the peak value is defined as the 99.9th percentile.

Running the same function on the TMS result file, we obtain the plot shown in Fig. 1.8b, as well as the peak fields and focality measures shown below in Table 1.4.

Table 1.4 Output of mesh_show_results for the TMS simulation

We can see that the peak fields for TMS are much higher than for tDCS, even though we simulated with a current of 106 A/s, very low for TMS. In the focality measures, we see that the TMS electric fields are much more focal than the tDCS electric fields, with around five times less GM volume exceeding 75% of the peak value than tDCS.

Additionally, the “.msh” files can be opened with the Gmsh viewer, producing 3D visualizations as shown in Fig. 1.9. Gmsh has a vast range of functionalities, such as clipping planes, but can be harder to use than mesh_show_results.

Fig. 1.9
figure 9

Visualization in Gmsh of (a) electric field vectors around central gyrus for the tDCS simulation and (b) TMS electric field depth profile in the hotspot

3.2 Advanced Usage: Group Analysis

Now, we want to simulate one tDCS montage, with a 5 × 5 cm electrode over C3 and a 5 × 7 cm electrode over AF4 in five subjects, called “sub01”, “sub09”, “sub10”, “sub12”, “sub15” and visualize the results in a common space, namely the FsAverage surface. The subjects and example scripts can be downloaded from: https://osf.io/ah5eu/

3.2.1 Head Meshing

For each subject, follow the steps in section “Generating the Volume Conductor Model”.

3.2.2 Write a Python or MATLAB Script

We can set up the simulation of each subject using the GUI, as described in the first example. However, when working with multiple subjects, it can be advantageous to script the simulations for efficiency. SimNIBS provides both MATLAB and Python interfaces to set up simulations. Script 1.1 shows how to set up and run a simulation with a 5 × 5cm anode placed over C3 and a 7 × 5cm cathode placed over AF4 for all subjects. The output of Script 1.1 for sub01 is shown in Table 1.5.

Table 1.5 Output files and folders of Script 1 for sub01. The “.angle”, “.norm”,.. files are FreeSurfer overlay files and the “.central” files are FreeSurfer surface files

To define the rectangular electrodes, we need two coordinates. The “centre” defines where the electrode will be centred, and “pos_ydir” how the electrode will be rotated. More precisely, the electrode’s “y” axis is defined as a unit vector starting at “centre” and pointing towards “pos_ydir”. Fig. 1.10 shows one of the cathodes (return electrode) defined using the script above, with the coordinate system and EEG positions overlaid. We can see that the electrode is centred in AF4, and its Y axis points towards F6. “pos_ydir” does not need to be set when the electrodes are round.

Fig. 1.10
figure 10

50 × 70 mm electrode defined with a “centre” in AF4 and a “pos_ydir” in F6

When the map_to_fsavg option is set to true, SimNIBS computes the electric fields in a surface located in the middle of the GM layer. This cortical surface, along with the norm, normal and tangent components of the electric field at the cortical surface and the angle between the electric field and the cortical surface can found in the subject_overlays folder, for both the left hemisphere (lh) and for the right hemisphere (rh) as shown in Table 1.5. Afterwards, these quantities are transformed into the FsAverage space. The transformed quantities can be found in the fsavg_overlays folder, as shown in Table 1.5. Additionally, we have the electric field and its norm in MNI space in the mni_volumes folder.

figure a

Script 1.1 Script for running a tDCS simulations with an anode over C3 and a cathode over AF4 in five subjects and transforming the results to FSAverage and MNI spaces.

figure b

Script 1.2 Analysis of simulation results in FSAverage space.

3.2.3 Visualizing Results

We can also make use of the MATLAB library within SimNIBS to analyze the results from the simulations. Here, we are interested in the average and standard deviation of the normal component of the electric field in the cortex. The normal component, as shown in Fig. 1.7, is the part of the electric field which is either entering or leaving the cortex.

Script 1.2 loads the normal field component data for each subject and calculates the mean and the standard deviation across subjects at each position of the FsAverage template. The fields are then visualized using MATLAB visualization tools. The results are shown in Fig. 1.11. We can, for example, see strong current in-flow in the central gyrus, and large variations in the normal component in frontal regions.

Fig. 1.11
figure 11

(a) Mean and (b) Standard deviation of the normal field component across 5 subjects. The fields were caused by tDCS with an anode over C3 and a cathode over AF4. Positive values in (a) denote inflowing currents, and negative values outflowing currents

4 The Accuracy of Automatic EEG Positioning

Here, we compare EEG 10-10 positions obtained either from:

  1. A.

    Transforming EEG 10-10 electrode positions defined in MNI space to the subject space using a non-linear transform, and then projecting the positions to the scalp. This is done for both mri2mesh and headreco head models.

  2. B.

    Manually locating the fiducials: left pre-auricular point (LPA), right pre-auricular point (RPA), nasion (Nz) and inion (Iz) on MRI images, and afterwards calculating the EEG positions using the definitions in [22].

Calculations using method A require no user input and are automatically performed in both mri2mesh and headreco head modelling pipelines, while calculations using method B require the user to manually select the fiducial positions.

To compare the methods A and B to position the electrodes, we calculated the EEG 10-10 positions using both ways for MR data of 17 subjects. The data was acquired as part of a larger study. The subjects gave written informed consent before the scan, and the study was approved by the local ethics committee of the University of Greifswald (Germany). The 17 datasets were acquired on a 3-Tesla Siemens Verio scanner (Siemens Healthcare, Erlangen, Germany) using a 32-channel head coil (T1: 1 × 1 × 1 mm3, TR 2300 ms, TE 900 ms, flip angle 9°, with selective water excitation for fat suppression; T2: 1 × 1 × 1 mm3, TR 12770 ms, TE 86 ms, flip angle 111°). For method B, the fiducials were manually located for each subject by a trained investigator on the T1- and T2-weighted images. The later had no knowledge of the automatically determined positions. The fiducials Nz, Iz, LPA and RPA were set in freeview, following the procedure described in [22] and additionally verified using the SimNIBS GUI. The subject-specific coordinates of the fiducials were extracted, and these manually set positions were then compared with those calculated by the automatic algorithm in each individual.

Table 1.6 shows the maximal distance across all subjects between the fiducials obtained using method A and manually selected fiducials (B). We see that Nz is the most consistent fiducial, where we have the least deviation, whereas Iz is where we have the highest deviation. Also, the maximal difference in position across the two methods is ~1 cm, indicating that method A works well to approximate the positions of the fiducials.

Table 1.6 Maximum and mean distance between the fiducial positions selected by hand and obtained from the MNI transformations across 17 subjects, for the two head modelling pipelines

Furthermore, in Fig. 1.12, we compare the two methods for all electrode positions in the EEG 10-10 system. The deviation in positioning each electrode was calculated as the mean of the distance between the positions obtained with either headreco or mri2mesh to the manually located fiducial positions, across all 17 subjects and for each electrode.

Fig. 1.12
figure 12

Positioning error for electrodes in the EEG 10-10 system. The error is calculated by comparing the positions calculated based on manually selected fiducials to positions calculated based on non-linear MNI transformations

The errors for all electrodes are below 1 cm, indicating that the two algorithms for placing EEG electrodes are in agreement. We can also see that the errors in the EEG positions obtained from headreco are on average lower than the ones obtained from mri2mesh . It also seems that the anterior electrodes have less errors than the posterior electrodes. Interestingly, the location of the errors is different across the two pipelines, with mri2mesh being more inaccurate in superior regions and headreco more inaccurate in posterior regions. This might be caused by differences in the way FSL (mri2mesh) and SPM (headreco) calculate non-linear MNI transformations is different. The average error across all positions was 5.6 mm for mri2mesh head models and 4.9 mm for headreco head models indicating good accuracy.

5 Conclusion

We presented SimNIBS 2.1 (www.simnibs.org), a software for individualized modelling of electric fields caused by non-invasive brain stimulation. SimNIBS is free software and avaliable for all major platforms. SimNIBS does not require the installation of any additional software in order to run simulations on the example dataset. To construct head models, SimNIBS relies either on MATLAB, SPM12 and CAT12 (headreco) or on FSL and FreeSurfer (mri2mesh).

We also presented two examples of workflows in SimNIBS. In the first example, we started by using headreco to construct a head model. Following this, we used the GUI to set up a tDCS and a TMS simulation in an interactive way, and finally visualized the results. In the second example, we constructed several head models and used a MATLAB script to run simulations for each subject. We then calculated the mean and the stardard deviation of the electric field norm across all subjects, using the FreeSurfer’s FsAverage brain template. Finally, we show results validating our automatic procedure to obtain electrode positions for the EEG 10-10 system.

SimNIBS is still being actively developed, and we expect further updates to be implemented in the future.