/usr/share/pyshared/nibabel/freesurfer/io.py is in python-nibabel 1.3.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | from __future__ import with_statement
import numpy as np
import getpass
import time
def _fread3(fobj):
"""Read a 3-byte int from an open binary file object
Parameters
----------
fobj : file
File descriptor
Returns
-------
n : int
A 3 byte int
"""
b1, b2, b3 = np.fromfile(fobj, ">u1", 3)
return (b1 << 16) + (b2 << 8) + b3
def _fread3_many(fobj, n):
"""Read 3-byte ints from an open binary file object.
Parameters
----------
fobj : file
File descriptor
Returns
-------
out : 1D array
An array of 3 byte int
"""
b1, b2, b3 = np.fromfile(fobj, ">u1", 3 * n).reshape(-1,
3).astype(np.int).T
return (b1 << 16) + (b2 << 8) + b3
def read_geometry(filepath):
"""Read a triangular format Freesurfer surface mesh.
Parameters
----------
filepath : str
Path to surface file
Returns
-------
coords : numpy array
nvtx x 3 array of vertex (x, y, z) coordinates
faces : numpy array
nfaces x 3 array of defining mesh triangles
"""
with open(filepath, "rb") as fobj:
magic = _fread3(fobj)
if magic == 16777215: # Quad file
nvert = _fread3(fobj)
nquad = _fread3(fobj)
coords = np.fromfile(fobj, ">i2", nvert * 3).astype(np.float)
coords = coords.reshape(-1, 3) / 100.0
quads = _fread3_many(fobj, nquad * 4)
quads = quads.reshape(nquad, 4)
#
# Face splitting follows
#
faces = np.zeros((2 * nquad, 3), dtype=np.int)
nface = 0
for quad in quads:
if (quad[0] % 2) == 0:
faces[nface] = quad[0], quad[1], quad[3]
nface += 1
faces[nface] = quad[2], quad[3], quad[1]
nface += 1
else:
faces[nface] = quad[0], quad[1], quad[2]
nface += 1
faces[nface] = quad[0], quad[2], quad[3]
nface += 1
elif magic == 16777214: # Triangle file
create_stamp = fobj.readline()
_ = fobj.readline()
vnum = np.fromfile(fobj, ">i4", 1)[0]
fnum = np.fromfile(fobj, ">i4", 1)[0]
coords = np.fromfile(fobj, ">f4", vnum * 3).reshape(vnum, 3)
faces = np.fromfile(fobj, ">i4", fnum * 3).reshape(fnum, 3)
else:
raise ValueError("File does not appear to be a Freesurfer surface")
coords = coords.astype(np.float) # XXX: due to mayavi bug on mac 32bits
return coords, faces
def write_geometry(filepath, coords, faces, create_stamp=None):
"""Write a triangular format Freesurfer surface mesh.
Parameters
----------
filepath : str
Path to surface file
coords : numpy array
nvtx x 3 array of vertex (x, y, z) coordinates
faces : numpy array
nfaces x 3 array of defining mesh triangles
create_stamp : str
User/time stamp (default: "created by <user> on <ctime>")
"""
magic_bytes = np.array([255, 255, 254], dtype=np.uint8)
if create_stamp is None:
create_stamp = "created by %s on %s" % (getpass.getuser(),
time.ctime())
with open(filepath, 'wb') as fobj:
magic_bytes.tofile(fobj)
fobj.write("%s\n\n" % create_stamp)
np.array([coords.shape[0], faces.shape[0]], dtype='>i4').tofile(fobj)
# Coerce types, just to be safe
coords.astype('>f4').reshape(-1).tofile(fobj)
faces.astype('>i4').reshape(-1).tofile(fobj)
def read_morph_data(filepath):
"""Read a Freesurfer morphometry data file.
This function reads in what Freesurfer internally calls "curv" file types,
(e.g. ?h. curv, ?h.thickness), but as that has the potential to cause
confusion where "curv" also refers to the surface curvature values,
we refer to these files as "morphometry" files with PySurfer.
Parameters
----------
filepath : str
Path to morphometry file
Returns
-------
curv : numpy array
Vector representation of surface morpometry values
"""
with open(filepath, "rb") as fobj:
magic = _fread3(fobj)
if magic == 16777215:
vnum = np.fromfile(fobj, ">i4", 3)[0]
curv = np.fromfile(fobj, ">f4", vnum)
else:
vnum = magic
_ = _fread3(fobj)
curv = np.fromfile(fobj, ">i2", vnum) / 100
return curv
def read_annot(filepath, orig_ids=False):
"""Read in a Freesurfer annotation from a .annot file.
Parameters
----------
filepath : str
Path to annotation file
orig_ids : bool
Whether to return the vertex ids as stored in the annotation
file or the positional colortable ids
Returns
-------
labels : n_vtx numpy array
Annotation id at each vertex
ctab : numpy array
RGBA + label id colortable array
"""
with open(filepath, "rb") as fobj:
dt = ">i4"
vnum = np.fromfile(fobj, dt, 1)[0]
data = np.fromfile(fobj, dt, vnum * 2).reshape(vnum, 2)
labels = data[:, 1]
ctab_exists = np.fromfile(fobj, dt, 1)[0]
if not ctab_exists:
raise Exception('Color table not found in annotation file')
n_entries = np.fromfile(fobj, dt, 1)[0]
if n_entries > 0:
length = np.fromfile(fobj, dt, 1)[0]
orig_tab = np.fromfile(fobj, '>c', length)
orig_tab = orig_tab[:-1]
names = list()
ctab = np.zeros((n_entries, 5), np.int)
for i in xrange(n_entries):
name_length = np.fromfile(fobj, dt, 1)[0]
name = np.fromfile(fobj, "|S%d" % name_length, 1)[0]
names.append(name)
ctab[i, :4] = np.fromfile(fobj, dt, 4)
ctab[i, 4] = (ctab[i, 0] + ctab[i, 1] * (2 ** 8) +
ctab[i, 2] * (2 ** 16) +
ctab[i, 3] * (2 ** 24))
else:
ctab_version = -n_entries
if ctab_version != 2:
raise Exception('Color table version not supported')
n_entries = np.fromfile(fobj, dt, 1)[0]
ctab = np.zeros((n_entries, 5), np.int)
length = np.fromfile(fobj, dt, 1)[0]
_ = np.fromfile(fobj, "|S%d" % length, 1)[0] # Orig table path
entries_to_read = np.fromfile(fobj, dt, 1)[0]
names = list()
for i in xrange(entries_to_read):
_ = np.fromfile(fobj, dt, 1)[0] # Structure
name_length = np.fromfile(fobj, dt, 1)[0]
name = np.fromfile(fobj, "|S%d" % name_length, 1)[0]
names.append(name)
ctab[i, :4] = np.fromfile(fobj, dt, 4)
ctab[i, 4] = (ctab[i, 0] + ctab[i, 1] * (2 ** 8) +
ctab[i, 2] * (2 ** 16))
ctab[:, 3] = 255
if not orig_ids:
ord = np.argsort(ctab[:, -1])
labels = ord[np.searchsorted(ctab[ord, -1], labels)]
return labels, ctab, names
def read_label(filepath):
"""Load in a Freesurfer .label file.
Parameters
----------
filepath : str
Path to label file
Returns
-------
label_array : numpy array
Array with indices of vertices included in label
"""
label_array = np.loadtxt(filepath, dtype=np.int, skiprows=2, usecols=[0])
return label_array
|