Skip to content

Commit 53f7e4a

Browse files
committed
BF: Fix various bugs in nibabel.freesurfer.io.read_annot. Also a sprinkling of
comments.
1 parent ee4ce2a commit 53f7e4a

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

nibabel/freesurfer/io.py

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -318,51 +318,78 @@ def read_annot(filepath, orig_ids=False):
318318
The names of the labels. The length of the list is n_labels.
319319
"""
320320
with open(filepath, "rb") as fobj:
321+
# all data (apart from strings) in an .annot file is stored as
322+
# big-endian int32
321323
dt = ">i4"
324+
325+
# number of vertices
322326
vnum = np.fromfile(fobj, dt, 1)[0]
327+
328+
# vertex ids + annotation values
323329
data = np.fromfile(fobj, dt, vnum * 2).reshape(vnum, 2)
324330
labels = data[:, 1]
325331

332+
# is there a colour table?
326333
ctab_exists = np.fromfile(fobj, dt, 1)[0]
327334
if not ctab_exists:
328335
raise Exception('Color table not found in annotation file')
336+
337+
# in old-format files, the next field will contain the number of
338+
# entries in the colour table. In new-format files, this will be
339+
# equal to -2
329340
n_entries = np.fromfile(fobj, dt, 1)[0]
341+
342+
# We've got an old-format .annot file.
330343
if n_entries > 0:
344+
345+
# orig_tab string length + string
331346
length = np.fromfile(fobj, dt, 1)[0]
332347
orig_tab = np.fromfile(fobj, '>c', length)
333348
orig_tab = orig_tab[:-1]
334-
335349
names = list()
336-
ctab = np.zeros((n_entries, 5), np.int)
350+
ctab = np.zeros((n_entries, 5), np.int32)
337351
for i in xrange(n_entries):
352+
# structure name length + string
338353
name_length = np.fromfile(fobj, dt, 1)[0]
339354
name = np.fromfile(fobj, "|S%d" % name_length, 1)[0]
340355
names.append(name)
356+
# RGBA
341357
ctab[i, :4] = np.fromfile(fobj, dt, 4)
342-
ctab[i, 4] = (ctab[i, 0] + ctab[i, 1] * (2 ** 8) +
358+
# generate the annotation value
359+
ctab[i, 4] = (ctab[i, 0] +
360+
ctab[i, 1] * (2 ** 8) +
343361
ctab[i, 2] * (2 ** 16) +
344362
ctab[i, 3] * (2 ** 24))
363+
# We've got a new-format .annot file
345364
else:
365+
# file version number
346366
ctab_version = -n_entries
347367
if ctab_version != 2:
348368
raise Exception('Color table version not supported')
349-
n_entries = np.fromfile(fobj, dt, 1)[0]
350-
ctab = np.zeros((n_entries, 5), np.int)
369+
# maximum colour table index used
370+
max_index = np.fromfile(fobj, dt, 1)[0]
371+
ctab = np.zeros((max_index, 5), np.int32)
372+
# orig_tab string length + string
351373
length = np.fromfile(fobj, dt, 1)[0]
352374
np.fromfile(fobj, "|S%d" % length, 1)[0] # Orig table path
375+
# number of entries to read from the file
353376
entries_to_read = np.fromfile(fobj, dt, 1)[0]
354377
names = list()
355378
for i in xrange(entries_to_read):
356-
np.fromfile(fobj, dt, 1)[0] # Structure
379+
# index of this entry
380+
idx = np.fromfile(fobj, dt, 1)[0]
381+
# structure name length + string
357382
name_length = np.fromfile(fobj, dt, 1)[0]
358383
name = np.fromfile(fobj, "|S%d" % name_length, 1)[0]
359384
names.append(name)
385+
# RGBA
360386
ctab[i, :4] = np.fromfile(fobj, dt, 4)
361-
ctab[i, 4] = (ctab[i, 0] + ctab[i, 1] * (2 ** 8) +
362-
ctab[i, 2] * (2 ** 16))
363-
ctab[:, 3] = 255
387+
ctab[i, 4] = (ctab[i, 0] +
388+
ctab[i, 1] * (2 ** 8) +
389+
ctab[i, 2] * (2 ** 16) +
390+
ctab[i, 3] * (2 ** 24))
364391

365-
labels = labels.astype(np.int)
392+
labels = labels.astype(np.int32)
366393

367394
if not orig_ids:
368395
ord = np.argsort(ctab[:, -1])

0 commit comments

Comments
 (0)