@@ -402,7 +402,7 @@ def read_annot(filepath, orig_ids=False):
402402 return labels , ctab , names
403403
404404
405- def write_annot (filepath , labels , ctab , names ):
405+ def write_annot (filepath , labels , ctab , names , fill_ctab = True ):
406406 """Write out a Freesurfer annotation file.
407407
408408 See:
@@ -418,6 +418,11 @@ def write_annot(filepath, labels, ctab, names):
418418 RGBA + label id colortable array.
419419 names : list of str
420420 The names of the labels. The length of the list is n_labels.
421+ fill_ctab : bool
422+ If True, the annotation values for each vertex are automatically
423+ generated. In this case, the provided `ctab` may have shape
424+ (n_labels, 4) or (n_labels, 5) - if the latter, the final column is
425+ ignored.
421426 """
422427 with open (filepath , "wb" ) as fobj :
423428 dt = ">i4"
@@ -430,6 +435,16 @@ def write_string(s):
430435 write (len (s ))
431436 write (s , dtype = '|S%d' % len (s ))
432437
438+ # Generate annotation values for each ctab entry
439+ if fill_ctab :
440+ new_ctab = np .zeros ((ctab .shape [0 ], 5 ), dtype = np .int32 )
441+ new_ctab [:, :4 ] = ctab [:, :4 ]
442+ ctab = new_ctab
443+ ctab [:, 4 ] = (ctab [:, 0 ] +
444+ ctab [:, 1 ] * (2 ** 8 ) +
445+ ctab [:, 2 ] * (2 ** 16 ) +
446+ ctab [:, 3 ] * (2 ** 24 ))
447+
433448 # vtxct
434449 write (vnum )
435450
0 commit comments