Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automating annotation imports #320

nblauch opened this Issue Mar 14, 2019 · 1 comment


None yet
1 participant
Copy link

nblauch commented Mar 14, 2019

this builds on (closed) issue #312

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 ?

my code is given below, which again, builds upon #312.

thanks so much for the amazing software. i am just getting acquainted and it is fantastic.

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)

This comment has been minimized.

Copy link

nblauch commented Mar 21, 2019

i was able to semi-automate this using the bitmap tracing tool in inkscape. it still takes some time to go through all of the ROIs but at least saves the effort of hand-tracing. it doesn't really help to have the border images i computed :-)

if nobody has other thoughts, this can be closed. thanks,


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.