-
Notifications
You must be signed in to change notification settings - Fork 7
/
harvardoxford_nodes.py
executable file
·223 lines (156 loc) · 6.04 KB
/
harvardoxford_nodes.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
#!/usr/bin/env python3
"""
Create CSV of node coordinates from Harvard-Oxford atlas regions in 1mm MNI152 (FSL) space
AUTHOR
----
Mike Tyszka, Ph.D.
LICENSE
----
MIT License
Copyright (c) 2019 Mike Tyszka
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import os
import numpy as np
import xml.etree.ElementTree as ET
import argparse
def main():
# Parse command line arguments
parser = argparse.ArgumentParser(description='Create node and edge files from Harvard-Oxford atlasz')
parser.add_argument('--sparse', default=False, action='store_true', help='Decimate nodes')
parser.add_argument('--intra', default=False, action='store_true', help='Intrahemispheric edges only')
parser.add_argument('--homo', default=False, action='store_true', help='Homotopic edges only')
# Parse command line arguments
args = parser.parse_args()
# Construct options string for filenames
options_str = ''
if args.sparse:
options_str += '_sparse'
if args.intra:
options_str += '_intra'
if args.homo:
options_str += '_homo'
# Output CSV filename
fstub = 'HarvardOxford'
node_fname = fstub + options_str + '.node'
edge_fname = fstub + options_str + '.edge'
# Excluded labels
excludes = ['Left Cerebral White Matter',
'Left Cerebral Cortex ',
'Left Lateral Ventricle',
'Right Cerebral White Matter',
'Right Cerebral Cortex ',
'Right Lateral Ventricle',
'Brain-Stem']
cort_fname = os.path.join(os.environ['FSLDIR'], 'data', 'atlases', 'HarvardOxford-Cortical.xml')
cort_xml = ET.parse(cort_fname).getroot()
rh_cort_nodes = []
for tag in cort_xml.findall('data/label'):
name = 'Right ' + tag.text
index = int(tag.get('index'))
# Map voxel coordinates to MNI152 space
x, y, z = vox2mni(tag)
# Force RH (positive x)
x = np.abs(x)
# Enforce minimum distance from midline (3.0 mm)
x = np.clip(x, 3.0, None)
print('{:80s} {:2d} ({:0.1f}, {:0.1f}, {:0.1f})'.format(name, index, x, y, z))
rh_cort_nodes.append([name, index, x, y, z])
# Synthesize LH seed coordinates by reflection about midline
lh_cort_nodes = []
for node in rh_cort_nodes:
name = node[0].replace('Right', 'Left')
index = node[1] + len(rh_cort_nodes)
# Mirror about midline
x = -node[2]
y = node[3]
z = node[4]
print('{:80s} {:2d} ({:0.1f}, {:0.1f}, {:0.1f})'.format(name, index, x, y, z))
lh_cort_nodes.append([name, index, x, y, z])
subcort_fname = os.path.join(os.environ['FSLDIR'], 'data', 'atlases', 'HarvardOxford-Subcortical.xml')
subcort_xml = ET.parse(subcort_fname).getroot()
subcort_nodes = []
for tag in subcort_xml.findall('data/label'):
name = tag.text
if name in excludes:
print(' - {}'.format(name))
else:
index = int(tag.get('index')) + 2 * len(rh_cort_nodes)
# Map voxel coordinates to MNI152 space
x, y, z = vox2mni(tag)
print('{:80s} {:2d} ({:0.1f}, {:0.1f}, {:0.1f})'.format(name, index, x, y, z))
subcort_nodes.append([name, index, x, y, z])
# Optional node culling
if args.sparse:
rh_cort_nodes = rh_cort_nodes[0::5]
lh_cort_nodes = lh_cort_nodes[0::5]
subcort_nodes = []
# Concatenate RH cort, LH cort and subcort nodes
nodes = rh_cort_nodes + lh_cort_nodes + subcort_nodes
# Write nodes to CSV file
print('')
print('Writing nodes to {}'.format(node_fname))
with open(node_fname, 'w') as fd:
for node in nodes:
node, idx, x, y, z = node
# Replace whitespace with '.'
node = node.replace(' ', '.')
fd.write('{:10.3f}{:10.3f}{:10.3f}{:10d}{:10d} {:s}\n'.format(x, y, z, 1, 1, node))
# Generate demo random edge weights
n_nodes = len(nodes)
n_hemi = int(n_nodes / 2)
w_edge = np.random.random([n_nodes, n_nodes])
# Intrahemispheric edges only
if args.intra:
w_edge[0:n_hemi, n_hemi:] = 0.0
w_edge[n_hemi:, 0:n_hemi] = 0.0
# Homotopic edges only
if args.homo:
w_intra = w_edge[0:n_hemi, n_hemi:]
w_homo = np.diag(w_intra)
w_edge = np.diag(w_homo, n_hemi)
# Save edge weight array as CSV
print('Writing random edges to {}'.format(edge_fname))
np.savetxt(edge_fname,
w_edge,
fmt='%0.3f',
delimiter=',')
def vox2mni(tag):
"""
fslorient -getsform HarvardOxford-cort-maxprob-thr0-2mm.nii.gz
-2 0 0 90
0 2 0 -126
0 0 2 -72
0 0 0 1
Parameters
----------
tag
Returns
-------
"""
# Empirical adjustments for SurfIce mni152_2009 pial mesh
sf = 1.05
z_adjust = 3
xv = int(tag.get('x'))
yv = int(tag.get('y'))
zv = int(tag.get('z'))
x = sf * (-2.0 * xv + 90)
y = sf * (2.0 * yv - 126)
z = sf * (2.0 * zv - 72 + z_adjust)
return x, y, z
if '__main__' in __name__:
main()