Skip to main content

Python Library to Read and Write Surface Files in Freesurfer's TriangularSurface Format

Project description

Python Library to Read and Write Surface Files in Freesurfer’s TriangularSurface Format



pip3 install --user freesurfer-surface


Read Surface File

from freesurfer_surface import Surface

surface = Surface.read_triangular('bert/surf/lh.pial')

for vertex in surface.vertices[:3]:

vertex_0 = surface.vertices[0]
print('coordinates of vertex #0:', (vertex_0.right, vertex_0.anterior, vertex_0.superior))

for triangle_index, triangle in enumerate(surface.triangles[:4]):
    print(f'\ntriangle #{triangle_index}:')
    print('vertex indices:', triangle.vertex_indices)
    print('vertex coordinates:')
    for vertex in surface.select_vertices(triangle.vertex_indices):
        print((vertex.right, vertex.anterior, vertex.superior))

Edit Surface File

from freesurfer_surface import Surface, Vertex, Triangle
surface = Surface.read_triangular('bert/surf/lh.pial'))
vertex_a = surface.add_vertex(Vertex(0.0, 0.0, 0.0))
vertex_b = surface.add_vertex(Vertex(1.0, 1.0, 1.0))
vertex_c = surface.add_vertex(Vertex(2.0, 2.0, 2.0))
surface.triangles.append(Triangle((vertex_a, vertex_b, vertex_c)))

List Labels in Annotation File

from freesurfer_surface import Annotation

annotation ='tests/subjects/fabian/label/lh.aparc.annot')
for label in annotation.labels.values():
    print(label.index, label.hex_color_code,


$ freesurfer-annotation-labels tests/subjects/fabian/label/lh.aparc.annot
index  color    name
0      #190519  unknown
1      #196428  bankssts
2      #7d64a0  caudalanteriorcingulate
3      #641900  caudalmiddlefrontal
33     #4614aa  temporalpole
34     #9696c8  transversetemporal
35     #ffc020  insula

Find Border of Labelled Region

from freesurfer_surface import Surface
surface = Surface.read_triangular('bert/surf/lh.pial'))
region, = filter(lambda l: == 'precentral',


pip3 install --user pipenv
git clone
cd freesurfer-surface
pipenv run pylint freesurfer_surface
pipenv run pytest --cov=freesurfer_surface

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for freesurfer-surface, version 2.0.0
Filename, size File type Python version Upload date Hashes
Filename, size freesurfer_surface-2.0.0-py3-none-any.whl (23.1 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size freesurfer-surface-2.0.0.tar.gz (3.4 MB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page