@@ -402,7 +402,7 @@ def read_annot(filepath, orig_ids=False):
402
402
return labels , ctab , names
403
403
404
404
405
- def write_annot (filepath , labels , ctab , names ):
405
+ def write_annot (filepath , labels , ctab , names , fill_ctab = True ):
406
406
"""Write out a Freesurfer annotation file.
407
407
408
408
See:
@@ -418,6 +418,11 @@ def write_annot(filepath, labels, ctab, names):
418
418
RGBA + label id colortable array.
419
419
names : list of str
420
420
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.
421
426
"""
422
427
with open (filepath , "wb" ) as fobj :
423
428
dt = ">i4"
@@ -430,6 +435,16 @@ def write_string(s):
430
435
write (len (s ))
431
436
write (s , dtype = '|S%d' % len (s ))
432
437
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
+
433
448
# vtxct
434
449
write (vnum )
435
450
0 commit comments