Finite element model from labels

The main problems in shamo depend on a finite element model to serve as a support for the computation. Generating such a model can be achieved in a few lines of code.

To start, we import the FEM class and initialize a model with a name and a parent directory.

[ ]:
from shamo import FEM

model = FEM("fem_from_labels_sensors-circle", "../../derivatives")

Mesh generation

Several methods can be used ot generate the mesh:

  • mesh_from_array()

  • mesh_from_nii()

  • mesh_from_masks()

  • mesh_from_niis()

All those methods skip the step of generating surfaces and allow us to directly produce a high definition mesh from the segmented image. Here, we start from a labelled volume created from the well known Suzanne model from Blender.


Several parameters can be used to refine the mesh. Make sure to tweak them to obtain the mesh you want.

[ ]:
from pathlib import Path

data_path = Path("../../rawdata/suzanne")
    data_path / "suzanne_labels.nii",
    ["wm", "gm", "scalp"],

After this first step, a mesh built of triangles and tetrahedra and annotated with the name of the tissues is saved to the directory of the object.

Sensors additions

Sensors can be added to the mesh with the following methods:

  • add_point_sensor() and the partials add_point_sensor_on() and add_point_sensor_in()

  • add_point_sensors() and the partials add_point_sensors_on() and add_point_sensors_in()

  • add_point_sensors_from_tsv() and the partials add_point_sensors_from_tsv_on() and add_point_sensors_from_tsv_in()

In the case of Suzanne, the electrodes are defined in a TSV file with the format:

Electrodes TSV file









[ ]:
import numpy as np
from pathlib import Path

model.add_circle_sensors_from_tsv_on(data_path / "suzanne_sensors.tsv", "scalp", 0.005)