/usr/share/pyshared/surfer/io.py is in python-surfer 0.3+git15-gae6cbb1-1.1.
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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | import os
from os.path import join as pjoin
from subprocess import call
import gzip
import numpy as np
import nibabel as nib
from nibabel.spatialimages import ImageFileError
def _fread3(fobj):
"""Read a 3-byte int from an open binary file object."""
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."""
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 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_scalar_data(filepath):
"""Load in scalar data from an image.
Parameters
----------
filepath : str
path to scalar data file
Returns
-------
scalar_data : numpy array
flat numpy array of scalar data
"""
try:
scalar_data = nib.load(filepath).get_data()
scalar_data = np.ravel(scalar_data, order="F")
return scalar_data
except ImageFileError:
ext = os.path.splitext(filepath)[1]
if ext == ".mgz":
openfile = gzip.open
elif ext == ".mgh":
openfile = open
else:
raise ValueError("Scalar file format must be readable "
"by Nibabel or .mg{hz} format")
fobj = openfile(filepath, "rb")
# We have to use np.fromstring here as gzip fileobjects don't work
# with np.fromfile; same goes for try/finally instead of with statement
try:
v = np.fromstring(fobj.read(4), ">i4")[0]
if v != 1:
# I don't actually know what versions this code will read, so to be
# on the safe side, let's only let version 1 in for now.
# Scalar data might also be in curv format (e.g. lh.thickness)
# in which case the first item in the file is a magic number.
raise NotImplementedError("Scalar data file version not supported")
ndim1 = np.fromstring(fobj.read(4), ">i4")[0]
ndim2 = np.fromstring(fobj.read(4), ">i4")[0]
ndim3 = np.fromstring(fobj.read(4), ">i4")[0]
nframes = np.fromstring(fobj.read(4), ">i4")[0]
datatype = np.fromstring(fobj.read(4), ">i4")[0]
# Set the number of bytes per voxel and numpy data type according to
# FS codes
databytes, typecode = {0: (1, ">i1"), 1: (4, ">i4"), 3: (4, ">f4"),
4: (2, ">h")}[datatype]
# Ignore the rest of the header here, just seek to the data
fobj.seek(284)
nbytes = ndim1 * ndim2 * ndim3 * nframes * databytes
# Read in all the data, keep it in flat representation
# (is this ever a problem?)
scalar_data = np.fromstring(fobj.read(nbytes), typecode)
finally:
fobj.close()
return scalar_data
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
names : numpy array
Array of region names as stored in the annot file
"""
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, read_scalars=False):
"""Load in a Freesurfer .label file.
Parameters
----------
filepath : str
Path to label file
read_scalars : bool
If true, read and return scalars associated with each vertex
Returns
-------
label_array : numpy array (ints)
Array with indices of vertices included in label
scalar_array : numpy array (floats)
If read_scalars is True, array of scalar data for each vertex
"""
label_array = np.loadtxt(filepath, dtype=np.int, skiprows=2, usecols=[0])
if read_scalars:
scalar_array = np.loadtxt(filepath, skiprows=2, usecols=[-1])
return label_array, scalar_array
return label_array
def read_stc(filepath):
"""Read an STC file from the MNE package
STC files contain activations or source reconstructions
obtained from EEG and MEG data.
Parameters
----------
filepath: string
Path to STC file
Returns
-------
data: dict
The STC structure. It has the following keys:
tmin The first time point of the data in seconds
tstep Time between frames in seconds
vertices vertex indices (0 based)
data The data matrix (nvert * ntime)
"""
fid = open(filepath, 'rb')
stc = dict()
fid.seek(0, 2) # go to end of file
file_length = fid.tell()
fid.seek(0, 0) # go to beginning of file
# read tmin in ms
stc['tmin'] = float(np.fromfile(fid, dtype=">f4", count=1))
stc['tmin'] /= 1000.0
# read sampling rate in ms
stc['tstep'] = float(np.fromfile(fid, dtype=">f4", count=1))
stc['tstep'] /= 1000.0
# read number of vertices/sources
vertices_n = int(np.fromfile(fid, dtype=">I4", count=1))
# read the source vector
stc['vertices'] = np.fromfile(fid, dtype=">I4", count=vertices_n)
# read the number of timepts
data_n = int(np.fromfile(fid, dtype=">I4", count=1))
if ((file_length / 4 - 4 - vertices_n) % (data_n * vertices_n)) != 0:
raise ValueError('incorrect stc file size')
# read the data matrix
stc['data'] = np.fromfile(fid, dtype=">f4", count=vertices_n * data_n)
stc['data'] = stc['data'].reshape([data_n, vertices_n]).T
# close the file
fid.close()
return stc
def project_volume_data(filepath, hemi, reg_file=None, subject_id=None,
projmeth="frac", projsum="avg", projarg=[0, 1, .1],
surf="white", smooth_fwhm=3, mask_label=None,
target_subject=None, verbose=False):
"""Sample MRI volume onto cortical manifold.
Note: this requires Freesurfer to be installed with correct
SUBJECTS_DIR definition (it uses mri_vol2surf internally).
Parameters
----------
filepath : string
Volume file to resample (equivalent to --mov)
hemi : [lh, rh]
Hemisphere target
reg_file : string
Path to TKreg style affine matrix file
subject_id : string
Use if file is in register with subject's orig.mgz
projmeth : [frac, dist]
Projection arg should be understood as fraction of cortical
thickness or as an absolute distance (in mm)
projsum : [avg, max, point]
Average over projection samples, take max, or take point sample
projarg : single float or sequence of three floats
Single float for point sample, sequence for avg/max specifying
start, stop, and stop
surf : string
Target surface
smooth_fwhm : float
FWHM of surface-based smoothing to apply; 0 skips smoothing
mask_label : string
Path to label file to constrain projection; otherwise uses cortex
target_subject : string
Subject to warp data to in surface space after projection
verbose : boolean
If true, print command line
"""
# Set the basic commands
cmd_list = ["mri_vol2surf",
"--mov", filepath,
"--hemi", hemi,
"--surf", surf]
# Specify the affine registration
if reg_file is not None:
cmd_list.extend(["--reg", reg_file])
elif subject_id is not None:
cmd_list.extend(["--regheader", subject_id])
else:
raise ValueError("Must specify reg_file or subject_id")
# Specify the projection
proj_flag = "--proj" + projmeth
if projsum != "point":
proj_flag += "-"
proj_flag += projsum
if hasattr(projarg, "__iter__"):
proj_arg = "%.3f, %.3f, %.3f" % tuple(projarg)
else:
proj_arg = str(projarg)
cmd_list.extend([proj_flag, proj_arg])
# Set misc args
if smooth_fwhm:
cmd_list.extend(["--surf-fwhm", str(smooth_fwhm)])
if mask_label is not None:
cmd_list.extend(["--mask", mask_label])
if target_subject is not None:
cmd_list.extend(["--trgsubject", target_subject])
# Execute the command
out_file = ".tmp-pysurfer.mgz"
cmd_list.extend(["--o", out_file])
if verbose:
print " ".join(cmd_list)
out = call(cmd_list)
if out:
raise RuntimeError(("mri_vol2surf command failed "
"with command-line: ") + " ".join(cmd_list))
# Read in the data
surf_data = read_scalar_data(out_file)
os.remove(out_file)
return surf_data
class Surface(object):
"""Container for surface object
Attributes
----------
subject_id : string
Name of subject
hemi : {'lh', 'rh'}
Which hemisphere to load
surf : string
Name of the surface to load (eg. inflated, orig ...)
data_path : string
Path where to look for data
x: 1d array
x coordinates of vertices
y: 1d array
y coordinates of vertices
z: 1d array
z coordinates of vertices
coords : 2d array of shape [n_vertices, 3]
The vertices coordinates
faces : 2d array
The faces ie. the triangles
"""
def __init__(self, subject_id, hemi, surf):
"""Surface
Parameters
----------
subject_id : string
Name of subject
hemi : {'lh', 'rh'}
Which hemisphere to load
surf : string
Name of the surface to load (eg. inflated, orig ...)
"""
self.subject_id = subject_id
self.hemi = hemi
self.surf = surf
if 'SUBJECTS_DIR' not in os.environ:
raise ValueError('Surface relies on the definition of the '
'of the SUBJECTS_DIR environment variable')
subj_dir = os.environ["SUBJECTS_DIR"]
self.data_path = pjoin(subj_dir, subject_id)
def load_geometry(self):
surf_path = pjoin(self.data_path, "surf",
"%s.%s" % (self.hemi, self.surf))
self.coords, self.faces = read_geometry(surf_path)
@property
def x(self):
return self.coords[:, 0]
@property
def y(self):
return self.coords[:, 1]
@property
def z(self):
return self.coords[:, 2]
def load_curvature(self):
"""Load in curvature values from the ?h.curv file."""
curv_path = pjoin(self.data_path, "surf", "%s.curv" % self.hemi)
self.curv = read_morph_data(curv_path)
self.bin_curv = np.array(self.curv > 0, np.int)
def load_label(self, name):
"""Load in a Freesurfer .label file.
Label files are just text files indicating the vertices included
in the label. Each Surface instance has a dictionary of labels, keyed
by the name (which is taken from the file name if not given as an
argument.
"""
label = read_label(pjoin(self.data_path, 'label',
'%s.%s.label' % (self.hemi, name)))
label_array = np.zeros(len(self.x), np.int)
label_array[label] = 1
try:
self.labels[name] = label_array
except AttributeError:
self.labels = {name: label_array}
def apply_xfm(self, mtx):
"""Apply an affine transformation matrix to the x,y,z vectors."""
self.coords = np.dot(np.c_[self.coords, np.ones(len(self.coords))],
mtx.T)[:, :3]
|