Automatically import annotations into pycortex

(repost of github issue opened at

i would like to be able to easily import a freesurfer annotation file to be used in the overlays.svg file. it’s easy enough by hand but for big parcellations such as HCPMMP1.0 it can get very time consuming and feels unnecessary.

i have used the pycortex tools to loop through all the ROIs in an annotation file (e.g., aparc, HCPMMP1) and figure out the border of the label, concatenating across hemispheres. i can load this label outline into inkscape with cortex.add_roi , but it is no more useful from that point than the original filled roi which I would do the drawing around.

does anybody know of a good way to turn the label border Vertex dataset into a line drawing automatically in inkscape, which could then be saved as the corresponding roi ?

code given below:

import cortex
import cortex.polyutils
import numpy as np
import os
import numpy as np
import mne

def fix_label(label):
    get rid of substrings in labels which would prevent merging
    for substr in ['L_', 'R_', '_ROI', '-lh', '-rh']:
        label = label.replace(substr, '')
    return label

subject = 'fsaverage'
parcellation = 'HCPMMP1'

surfs = [cortex.polyutils.Surface(*d)
         for d in cortex.db.get_surf(subject, "fiducial")]
num_verts = np.array([surfs[0].pts.shape[0], surfs[1].pts.shape[0]])
labels = mne.read_labels_from_annot(subject, parc=parcellation, hemi='both', subjects_dir=os.environ['SUBJECTS_DIR'])
label_names = np.unique([fix_label( for label in labels])
for l in label_names:
    matches = [label for label in labels if l in]
    verts = {}
    if len(matches) == 2:
        verts['lh'] = matches[0].vertices 
        verts['rh'] = matches[1].vertices
    elif len(matches) == 1:
        if 'rh' in matches[0].name:
            verts['rh'] = matches[0].vertices
            verts['lh'] = matches[0].vertices
    mask = np.zeros((num_verts.sum(),), dtype=bool)
    for hemi_i, (hemi, vertices) in enumerate(verts.items()):
        m = np.zeros((num_verts[hemi_i],), dtype=bool)
        m[vertices] = 1
        subsurf = surfs[hemi_i].create_subsurface(m)
        m = subsurf.lift_subsurface_data(subsurf.boundary_vertices)
        if hemi == 'rh':
            mask[num_verts[0]:] = m
            mask[:num_verts[0]] = m
    V = cortex.dataset.Vertex(mask, subject)
    cortex.add_roi(V, name=l, open_inkscape=True, add_path=True)