Read and display FreeSurfer .annot annotation files.
Prints region names, RGBA colour table entries, and (optionally) the
region membership of a single vertex.
$ python -m tit.tools.read_annot lh.aparc.annot
$ python -m tit.tools.read_annot lh.aparc.annot --vertex 12345
read_annot_file(annot_path, vertex_id=None)
Read a FreeSurfer annotation and print its contents.
annot_path : str
Path to the .annot file.
vertex_id : int, optional
If given, also print the region label for this vertex.
Source code in tit/tools/read_annot.py
| def read_annot_file(annot_path, vertex_id=None):
"""Read a FreeSurfer annotation and print its contents.
Parameters
----------
annot_path : str
Path to the ``.annot`` file.
vertex_id : int, optional
If given, also print the region label for this vertex.
"""
# Load annotation data
labels, ctab, names = fsio.read_annot(annot_path)
print(f"\nLoaded .annot file: {annot_path}")
print(f"Number of vertices: {len(labels)}")
print(f"Number of regions: {len(names)}\n")
# Print all region names and their colors
print("Regions and RGBA colors:")
for i, name in enumerate(names):
color = ctab[i][:4] # R, G, B, A
print(f" {i:2d}: {name.decode('utf-8')} | Color: {color}")
# If a specific vertex is given, show its label
if vertex_id is not None:
if 0 <= vertex_id < len(labels):
label_val = labels[vertex_id]
try:
index = list(ctab[:, -1]).index(label_val)
region_name = names[index].decode("utf-8")
print(f"\nVertex {vertex_id} belongs to region: {region_name}")
except ValueError:
print(f"\nVertex {vertex_id} has an unknown label value: {label_val}")
else:
print(f"\nVertex ID {vertex_id} is out of range (0 to {len(labels) - 1})")
|