MCPcopy
hub / github.com/mne-tools/mne-python / _read_annot

Function _read_annot

mne/label.py:2126–2204  ·  view source on GitHub ↗

Read a Freesurfer annotation from a .annot file. Note : Copied from PySurfer Parameters ---------- fname : str Path to annotation file Returns ------- annot : numpy array, shape=(n_verts) Annotation id at each vertex ctab : numpy array, shape=(n_ent

(fname)

Source from the content-addressed store, hash-verified

2124
2125
2126def _read_annot(fname):
2127 """Read a Freesurfer annotation from a .annot file.
2128
2129 Note : Copied from PySurfer
2130
2131 Parameters
2132 ----------
2133 fname : str
2134 Path to annotation file
2135
2136 Returns
2137 -------
2138 annot : numpy array, shape=(n_verts)
2139 Annotation id at each vertex
2140 ctab : numpy array, shape=(n_entries, 5)
2141 RGBA + label id colortable array
2142 names : list of str
2143 List of region names as stored in the annot file
2144
2145 """
2146 if not op.isfile(fname):
2147 dir_name = op.split(fname)[0]
2148 cands = _read_annot_cands(dir_name)
2149 if len(cands) == 0:
2150 raise OSError(
2151 f"No such file {fname}, no candidate parcellations found in directory"
2152 )
2153 else:
2154 raise OSError(
2155 f"No such file {fname}, candidate parcellations in "
2156 "that directory:\n" + "\n".join(cands)
2157 )
2158 with open(fname, "rb") as fid:
2159 n_verts = np.fromfile(fid, ">i4", 1)[0]
2160 data = np.fromfile(fid, ">i4", n_verts * 2).reshape(n_verts, 2)
2161 annot = data[data[:, 0], 1]
2162 ctab_exists = np.fromfile(fid, ">i4", 1)[0]
2163 if not ctab_exists:
2164 raise Exception("Color table not found in annotation file")
2165 n_entries = np.fromfile(fid, ">i4", 1)[0]
2166 if n_entries > 0:
2167 length = np.fromfile(fid, ">i4", 1)[0]
2168 np.fromfile(fid, ">c", length) # discard orig_tab
2169
2170 names = list()
2171 ctab = np.zeros((n_entries, 5), np.int64)
2172 for i in range(n_entries):
2173 name_length = np.fromfile(fid, ">i4", 1)[0]
2174 name = np.fromfile(fid, f"|S{name_length}", 1)[0]
2175 names.append(name)
2176 ctab[i, :4] = np.fromfile(fid, ">i4", 4)
2177 ctab[i, 4] = (
2178 ctab[i, 0]
2179 + ctab[i, 1] * (2**8)
2180 + ctab[i, 2] * (2**16)
2181 + ctab[i, 3] * (2**24)
2182 )
2183 else:

Callers 3

read_labels_from_annotFunction · 0.85
add_annotationMethod · 0.85

Calls 3

_read_annot_candsFunction · 0.85
splitMethod · 0.80
appendMethod · 0.45

Tested by 1