I’m hoping to create a binary annotation file using Nibabel’s write_annot
function (version 3.2.1). However, I keep getting this bug. A reproducible example is below:
import numpy as np
import nibabel as nib
# create binary mask
n_vertices = 10242
indices = np.random.randint(0, n_vertices, (50,))
mask = np.zeros((n_vertices), dtype=int)
mask[indices] = 1
ctab = np.array([[255, 255, 255, 0]])
names = ['mask']
nib.freesurfer.write_annot('mask.annot', labels=mask, ctab=ctab, names=names)
This produces:
IndexError Traceback (most recent call last)
<ipython-input-91-36733fdfa0f8> in <module>
10 names = ['mask']
11
---> 12 nib.freesurfer.write_annot('mask.annot', labels=mask, ctab=ctab, names=names)
~/miniconda3/lib/python3.8/site-packages/nibabel/freesurfer/io.py in write_annot(filepath, labels, ctab, names, fill_ctab)
542
543 # convert labels into coded CLUT values
--> 544 clut_labels = ctab[:, -1][labels]
545 clut_labels[np.where(labels == -1)] = 0
546
IndexError: index 1 is out of bounds for axis 0 with size 1
I get the same error when fill_ctab=False
and len(ctab)=5
(i.e. added label id to ctab
).
Based on the error, ctab
is being indexed with the labels. Not sure if this is a bug or something I’m doing wrong.
Thoughts? Thanks!