[Neuroimaging] write_annotation issue in Nibabel

Benjamin Deck bdeck8317 at gmail.com
Tue Jun 7 10:28:28 EDT 2022


Hi there,

I am having an issue with writing an annotation file:

```
import numpy as np
from nibabel import freesurfer
from glob import glob

lh_labels= glob('*lh*.label')

# initialize variables
lh_vertices = []
# initialize names list starting with 'unknown'
names=['background']

for ii in lh_labels:
    lh_vertices.append(freesurfer.read_labels(ii))
    names.append(os.path.basename(ii.split('.',1)[0]))


lh_labels_array=np.concatente(lh_vertices)

# load in long version of li 2016 parcellation including all 116 ROIs
ctab_long=np.loadtxt(path_to_rgb, dtype='int')

# create ctab with the appropriate length
names_len=len(names)

ctab=np.delete(ctab_long, slice(names_len,116),0)

print('writing LH annotation file')
freesurfer.write_annot(outpath + 'lh.li2019_parc.annot',
labels=lh_labels_array, ctab=ctab, names=names)
```

However, I get the same error as @danjgale.

```
IndexError                                Traceback (most recent call last)
<ipython-input-31-dab2f8c108c2> in <module>
----> 1 freesurfer.write_annot('lh.li2019_parc.annot',
labels=lh_labels_array, ctab=ctab, names=names)
c:\programdata\miniconda3\lib\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 8505 is out of bounds for axis 0 with size 51

```


If I print the variables and their shapes I get the following:


```
In [35]: len(names)
Out[35]: 51

In [27]: names
Out[27]:
['background',
 'Net_10_ROIs_1_lh_native_dil2',
 'Net_11_ROIs_1_lh_native_dil2',
 'Net_12_ROIs_1_lh_native_dil2',
 'Net_12_ROIs_2_lh_native_dil2',
 'Net_13_ROIs_1_lh_native_dil2',
 .....


In [36]: ctab.shape
Out[36]: (51, 4)

In [32]: ctab
Out[32]:
array([[255, 255, 255,   0],
       [ 25,   5,  25, 255],
       [ 25, 100,  40, 255],
       [ 70, 130, 180, 255],
       ....
```
Would really appreciate any advice or help.
-- 
*Benjamin L. Deck*
*Doctoral Student*
*Applied Cognitive and Brain Sciences*
*Department of Psychological and Brain Sciences*
*Drexel University*

<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=icon>
Virus-free.
www.avast.com
<https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail&utm_term=link>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/neuroimaging/attachments/20220607/b7a6d099/attachment.html>


More information about the Neuroimaging mailing list