-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconvert_to_vep_parc.py
388 lines (302 loc) · 13.6 KB
/
convert_to_vep_parc.py
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
import sys
import warnings
import numpy as np
import nibabel as nib
from sklearn.manifold import Isomap
class ColorLut():
"""Color look-up table"""
def __init__(self, filename):
self.inds = np.genfromtxt(filename, usecols=(0,), dtype=int)
self.names = np.genfromtxt(filename, usecols=(1,), dtype=str)
self.colors = np.genfromtxt(filename, usecols=(2,3,4,5), dtype=int)
self.name2ind = {name: ind for name, ind in zip(self.names, self.inds)}
self.ind2name = {ind: name for name, ind in zip(self.names, self.inds)}
def load_rules(filename, section=None):
with open(filename, 'r') as fl:
lines = [line.strip() for line in fl.readlines()]
# Remove comments
rules = [line.split() for line in lines if len(line) > 0 and line[0] != "#"]
# Get section index
index = [('__begin__', 0)]
rules_ = []
for rule in rules:
if rule[0] == 'Section':
if len(rule) != 2:
raise ValueError(f"Unexpected Section: '{rule}'")
index.append((rule[1], len(rules_)))
else:
rules_.append(rule)
index.append(('__end__', len(rules_)))
rules = rules_
if section is None:
return rules
else:
ind = [a[0] for a in index].index(section)
return rules[index[ind][1]:index[ind+1][1]]
def expand_wildcards_hemisphere(rules):
rules_l = []
rules_r = []
rules_n = []
for rule in rules:
if any([("%h" in elem) or ("%H" in elem) for elem in rule]):
rules_l.append([elem.replace("%h", "lh").replace("%H", "Left") for elem in rule])
rules_r.append([elem.replace("%h", "rh").replace("%H", "Right") for elem in rule])
else:
rules_n.append(rule)
return rules_n + rules_l + rules_r
def project_on_principal_axis(points):
"""
Project all `points` (Nx3 array) on its principal axis.
The axis is set to be oriented to positive in its second component (anterior direction in RAS coordinates).
"""
# find principal eigendirection
m_cov = np.dot(points.T, points)
w, vr = np.linalg.eig(m_cov)
eigendir = vr[:, np.argmax(w)]
# orient them from posterior to anterior
if eigendir[1] < 0:
eigendir *= -1
proj = np.dot(points, eigendir)
return proj, eigendir
def find_interface_voxels(parc, regs):
"""Return indices of all voxels on the interface of multiple regions
Straightforward and slow version.
"""
# Add boundary layers
assert -1 not in regs
bparc = -1 * np.ones((parc.shape[0] + 2, parc.shape[1] + 2, parc.shape[2] + 2))
bparc[1:-1, 1:-1, 1:-1] = parc
# Possible voxels
mask = np.zeros_like(parc, dtype=bool)
for reg in regs:
mask[parc == reg] = True
# Get those on the interface
interface = []
for i, j, k in np.argwhere(mask):
neigh_regs = set([bparc[i+1,j+1,k+1],
bparc[i ,j+1,k+1],
bparc[i+2,j+1,k+1],
bparc[i+1,j ,k+1],
bparc[i+1,j+2,k+1],
bparc[i+1,j+1,k ],
bparc[i+1,j+1,k+2]])
if all([reg in neigh_regs for reg in regs]):
interface.append((i, j, k))
return np.array(interface)
def find_interface_verts(triangs, labels, reg1, reg2, reg3):
"""
Not the most efficient version, but it is enough.
"""
interface = []
for v1, v2, v3 in triangs:
neighs = set([labels[v1], labels[v2], labels[v3]])
if all([reg in neighs for reg in [reg1, reg2, reg3]]):
interface.extend([v1, v2, v3])
return np.array(interface)
def op_merge(labels, labels_in, label_out):
"""In-place region merge"""
for lab in labels_in:
labels[labels == lab] = label_out
def op_rename(labels, label_in, label_out):
"""In-place region rename"""
op_merge(labels, [label_in], label_out)
def op_split(labels, mode, geom, label_in, labels_out, method, factors=None):
"""
In-place split of a single region along a anterior-posterior axis.
Method can be either:
'pca' for split after a linear projection
'isomap' for split after a nonlinear projection
Regions in `labels_out` should be ordered in the anterior-posterior direction.
If `factors` are missing, equal length split is performed.
"""
if factors is None:
factors = np.ones(len(labels_out), dtype=int)
assert len(labels_out) == len(factors)
inds = np.argwhere(labels == label_in)
indsl = np.nonzero(labels == label_in) # Just a different format
if mode == 'voxel':
# Affine transformation
coords = (geom.dot(np.c_[inds, np.ones(inds.shape[0])].T).T)[:, :3]
elif mode == 'triang':
# Just coordinates
coords = geom[inds[:, 0]]
if method == 'pca':
center = np.mean(coords, axis=0)
xcoords, _ = project_on_principal_axis(coords - center)
elif method == 'isomap':
isomap = Isomap(n_components=1, n_neighbors=20)
xcoords = isomap.fit_transform(coords)[:, 0]
isomap_ori = 1
if np.mean(coords[:, 1][xcoords < np.median(xcoords)]) > np.mean(coords[:, 1][xcoords > np.median(xcoords)]):
isomap_ori = -1
xcoords *= isomap_ori
else:
raise ValueError("Unknown method %s." % method)
# Normalize
xcoords -= np.min(xcoords)
xcoords /= np.max(xcoords)
limits = np.hstack([-np.inf, np.cumsum(factors)])
limits = limits/limits[-1]
limits[-1] = np.inf
for label_out, xfr, xto in zip(reversed(labels_out), limits[:-1], limits[1:]):
imask = (xcoords >= xfr) * (xcoords < xto)
mask = [idxs[imask] for idxs in indsl]
labels[mask] = label_out
def op_splitto(labels, mode, geom, label_in, labels_out, method):
"""
In-place split of a single region between multiple other regions.
Method can be either:
'pca' for split after a linear projection
'isomap' for split after a nonlinear projection
`labels_out` should be ordered in the anterior-posterior direction.
"""
# Reorder posterior-anterior
labels_out = list(reversed(labels_out))
inds = np.argwhere(labels == label_in)
indsl = np.nonzero(labels == label_in) # Just a different format
if mode == 'voxel':
# Affine transformation
coords = (geom.dot(np.c_[inds, np.ones(inds.shape[0])].T).T)[:, :3]
elif mode == 'triang':
# Just coordinates
verts, triangs = geom
coords = verts[inds[:, 0]]
if method == 'pca':
center = np.mean(coords, axis=0)
xcoords, direc = project_on_principal_axis(coords - center)
elif method == 'isomap':
isomap = Isomap(n_components=1, n_neighbors=20)
xcoords = isomap.fit_transform(coords)[:, 0]
isomap_ori = 1
if np.mean(coords[:, 1][xcoords < np.median(xcoords)]) > np.mean(coords[:, 1][xcoords > np.median(xcoords)]):
isomap_ori = -1
xcoords *= isomap_ori
else:
raise ValueError("Unknown method %s." % method)
# For each pair of to-regions
limits = [-np.inf]
for lab1, lab2 in zip(labels_out[:-1], labels_out[1:]):
if mode == 'voxel':
inds = find_interface_voxels(labels, (label_in, lab1, lab2))
elif mode == 'triang':
inds = find_interface_verts(triangs, labels, label_in, lab1, lab2)
if len(inds) > 0:
if mode == 'voxel':
posras = np.mean((geom.dot(np.c_[inds, np.ones(inds.shape[0])].T).T)[:, :3], axis=0)
elif mode == 'triang':
posras = np.mean(verts[inds], axis=0)
if method == 'pca':
limits.append(np.dot(posras - center, direc))
elif method == 'isomap':
limits.append(isomap_ori * isomap.transform(posras.reshape(1, -1))[0, 0])
else:
warnings.warn("No interface found between %s,%s,%s." % (label_in, lab1, lab2))
limits.append(limits[-1])
limits.append(np.inf)
for lab, x_fr, x_to in zip(labels_out, limits[:-1], limits[1:]):
imask = (xcoords >= x_fr) * (xcoords < x_to)
mask = [idxs[imask] for idxs in indsl]
labels[mask] = lab
def op_splitmes(labels, hemi, verts, triangs, label_in, labels_out):
"""
In-place split of a regions to mesial and 'lateral' parts.
Criterion for this split is a orientation of a normal on an inflated surface.
The operation is available only for triangulated surface.
"""
NORMAL_THRESHOLD = -0.5
inds = np.where(labels == label_in)
triang_coords = verts[triangs]
triang_normals = np.cross(triang_coords[:, 1] - triang_coords[:, 0],
triang_coords[:, 2] - triang_coords[:, 0])
vert_normals = np.zeros_like(verts)
vert_normals[triangs[:, 0]] += triang_normals
vert_normals[triangs[:, 1]] += triang_normals
vert_normals[triangs[:, 2]] += triang_normals
vert_normals /= np.linalg.norm(vert_normals, axis=1)[:, None]
mask = (labels == label_in)
direc = 1 if hemi in ['rh', 'Right'] else -1
labels[mask] = labels_out[0]
labels[mask * (direc * vert_normals[:, 0] > NORMAL_THRESHOLD)] = labels_out[1]
def dehemize_name(name):
if name[:7] == "ctx_%h_":
return name[7:]
elif name[:3] == "%H-":
return name[3:]
else:
return name
def convert_parc(destrieux_annot_file, pial_file, inflated_file, hemisphere, parc_lut_file, rules_file, vep_annot_file):
labels, _, names = nib.freesurfer.io.read_annot(destrieux_annot_file)
names = [n.decode('ascii').replace("&", "_and_") for n in names]
verts_pial, triangs_pial = nib.freesurfer.io.read_geometry(pial_file)
verts_infl, triangs_infl = nib.freesurfer.io.read_geometry(inflated_file)
rules = load_rules(rules_file, section='Cortex')
colorlut = ColorLut(parc_lut_file)
# Create master region list with old regions, new regions, and temporary regions
names.extend(colorlut.names) # Names from VEP parcellation
names.extend(["%%%d" % i for i in range(10)]) # Temporary regions
# Shorthands
def n2i(n): return names.index(dehemize_name(n))
def ns2i(ns): return [names.index(dehemize_name(n)) for n in ns.split(",")]
# Perform the in-place operations
for rule in rules:
if rule[0] == "merge":
op_merge(labels, ns2i(rule[1]), n2i(rule[2]))
elif rule[0] == "rename":
op_rename(labels, n2i(rule[1]), n2i(rule[2]))
elif rule[0] == "split":
factors = [int(a) for a in rule[3].split(",")] if (len(rule) == 4) else None
op_split(labels, 'triang', verts_pial, n2i(rule[1]), ns2i(rule[2]), method='pca', factors=factors)
elif rule[0] == "splitnl":
factors = [int(a) for a in rule[3].split(",")] if (len(rule) == 4) else None
op_split(labels, 'triang', verts_pial, n2i(rule[1]), ns2i(rule[2]), method='isomap', factors=factors)
elif rule[0] == "splitto":
op_splitto(labels, 'triang', (verts_pial, triangs_pial), n2i(rule[1]), ns2i(rule[2]), method='pca')
elif rule[0] == "splittonl":
op_splitto(labels, 'triang', (verts_pial, triangs_pial), n2i(rule[1]), ns2i(rule[2]), method='isomap')
elif rule[0] == "splitmes":
op_splitmes(labels, hemisphere, verts_infl, triangs_infl, n2i(rule[1]), ns2i(rule[2]))
else:
raise ValueError("Unknown rule %s" % rule[0])
# Keep only those in VEP parcellation color table
newlabels = -1 * np.ones_like(labels)
for i, name in zip(colorlut.inds, colorlut.names):
newlabels[labels == names.index(name)] = i
# Save
nib.freesurfer.io.write_annot(vep_annot_file, newlabels, colorlut.colors, colorlut.names, fill_ctab=True)
def convert_seg(orig_label_file, lut_file, rules_file, vep_label_file):
mgz_orig = nib.load(orig_label_file)
labelvol = mgz_orig.get_data().copy()
affine = mgz_orig.affine
colorlut = ColorLut(lut_file)
rules = load_rules(rules_file, section='Subcortical')
rules = expand_wildcards_hemisphere(rules)
n2i_ = {name: ind for name, ind in zip(colorlut.names, colorlut.inds)}
n2i_.update({("%%%d" % i): -10-i for i in range(10)}) # Temporary regions
# Shorthands
def n2i(n): return n2i_[n]
def ns2i(ns): return [n2i_[n] for n in ns.split(",")]
for rule in rules:
if rule[0] == "merge":
op_merge(labelvol, ns2i(rule[1]), n2i(rule[2]))
elif rule[0] == "rename":
op_rename(labelvol, n2i(rule[1]), n2i(rule[2]))
elif rule[0] == "split":
factors = [int(a) for a in rule[3].split(",")] if (len(rule) == 4) else None
op_split(labelvol, 'voxel', affine, n2i(rule[1]), ns2i(rule[2]), method='pca', factors=factors)
elif rule[0] == "splitnl":
factors = [int(a) for a in rule[3].split(",")] if (len(rule) == 4) else None
op_split(labelvol, 'voxel', affine, n2i(rule[1]), ns2i(rule[2]), method='isomap', factors=factors)
elif rule[0] == "splitto":
op_splitto(labelvol, 'voxel', affine, n2i(rule[1]), ns2i(rule[2]), method='pca')
elif rule[0] == "splittonl":
op_splitto(labelvol, 'voxel', affine, n2i(rule[1]), ns2i(rule[2]), method='isomap')
elif rule[0] == "splitmes":
raise ValueError("Rule 'splitmes' is available only in surface mode.")
else:
raise ValueError("Unknown rule %s" % rule[0])
assert np.sum(labelvol < 0) == 0
mgz_vep = nib.freesurfer.mghformat.MGHImage(labelvol, affine, mgz_orig.header)
nib.save(mgz_vep, vep_label_file)
if __name__ == '__main__':
cmd = sys.argv[1]
eval(cmd)(*sys.argv[2:])