-
Notifications
You must be signed in to change notification settings - Fork 14
/
volume_eigenmodes.py
374 lines (299 loc) · 14.7 KB
/
volume_eigenmodes.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Calculate the eigenmodes of a volume
@author: James Pang and Kevin Aquino, Monash University, 2022
"""
# Import all the libraries
from lapy import Solver, TetIO
import nibabel as nib
import numpy as np
from scipy.interpolate import griddata
import os
from argparse import ArgumentParser
import subprocess
def get_tkrvox2ras(voldim, voxres):
"""Generate transformation matrix to switch between tetrahedral and volume space.
Parameters
----------
voldim : array (1x3)
Dimension of the volume (number of voxels in each of the 3 dimensions)
voxres : array (!x3)
Voxel resolution (resolution in each of the 3 dimensions)
Returns
------
T : array (4x4)
Transformation matrix
"""
T = np.zeros([4,4]);
T[3,3] = 1;
T[0,0] = -voxres[0];
T[0,3] = voxres[0]*voldim[0]/2;
T[1,2] = voxres[2];
T[1,3] = -voxres[2]*voldim[2]/2;
T[2,1] = -voxres[1];
T[2,3] = voxres[1]*voldim[1]/2;
return T
# Define the tetrahedral mesh using Gmsh
def make_tetra_file(nifti_input_filename):
"""Generate tetrahedral version of the ROI in the nifti file.
Parameters
----------
nifti_input_filename : str
Filename of input volume where the relevant ROI have voxel values = 1
Returns
------
tetra_file : str
Filename of output tetrahedral vtk file
"""
nifti_input_file_head, nifti_input_file_tail = os.path.split(nifti_input_filename)
nifti_input_file_main, nifti_input_file_ext = os.path.splitext(nifti_input_file_tail)
os.system('mri_mc ' + nifti_input_filename + ' 1 ' + nifti_input_file_head + '/rh.tmp_surface.vtk')
os.system('mv -f ' + nifti_input_file_head + '/rh.tmp_surface.vtk ' + nifti_input_filename + '.vtk')
geo_file = nifti_input_filename + '.geo'
tria_file = nifti_input_filename + '.vtk'
tetra_file = nifti_input_filename + '.tetra.vtk'
file = tria_file.rsplit('/')
inputGeo = file[len(file)-1]
with open(geo_file, 'w') as writer:
writer.write('Mesh.Algorithm3D=4;\n')
writer.write('Mesh.Optimize=1;\n')
writer.write('Mesh.OptimizeNetgen=1;\n')
writer.write('Merge "'+inputGeo+'";\n')
writer.write('Surface Loop(1) = {1};\n')
writer.write('Volume(1) = {1};\n')
writer.write('Physical Volume(1) = {1};\n')
cmd = 'gmsh -3 -o ' + tetra_file + ' ' + geo_file
output = subprocess.check_output(cmd,shell="True")
output = output.splitlines()
cmd = "sed 's/double/float/g;s/UNSTRUCTURED_GRID/POLYDATA/g;s/CELLS/POLYGONS/g;/CELL_TYPES/,$d' " + tetra_file + " > " + tetra_file + "'_fixed'"
os.system(cmd)
os.system('mv -f ' + tetra_file + '_fixed ' + tetra_file)
return tetra_file
def calc_volume(nifti_input_filename):
"""Calculate the physical volume of the ROI in the nifti file.
Parameters
----------
nifti_input_filename : str
Filename of input volume where the relevant ROI have voxel values = 1
Returns
------
ROI_number : int
Total number of non-zero voxels
ROI_volume : float
Total volume of non-zero voxels in physical dimensions
"""
# Load data
ROI_data = nib.load(nifti_input_filename)
roi_data = ROI_data.get_fdata()
# Get voxel dimensions in mm
voxel_dims = (ROI_data.header["pixdim"])[1:4]
voxel_vol = np.prod(voxel_dims)
# Compute volume
ROI_number = np.count_nonzero(roi_data)
ROI_volume = ROI_number * voxel_vol
# print("Number of non-zero voxels = {}".format(ROI_number))
# print("Volume of non-zero voxels = {} mm^3".format(ROI_volume))
return ROI_number, ROI_volume
def normalize_vtk(tet, nifti_input_filename, normalization_type='none', normalization_factor=1):
"""Normalize tetrahedral surface.
Parameters
----------
tet : lapy compatible object
Loaded vtk object corresponding to a surface tetrahedral mesh
nifti_input_filename : str
Filename of input volume where the relevant ROI have voxel values = 1
normalization_type : str (default: 'none')
Type of normalization
number - normalization with respect to the total number of non-zero voxels
volume - normalization with respect to the total volume of non-zero voxels in physical dimensions
constant - normalization with respect to a chosen constant
others - no normalization
normalization_factor : float (default: 1)
Factor to be used in a constant normalization
Returns
------
tet_norm : lapy compatible object
Loaded vtk object corresponding to the normalized surface tetrahedral mesh
"""
nifti_input_file_head, nifti_input_file_tail = os.path.split(nifti_input_filename)
nifti_input_file_main, nifti_input_file_ext = os.path.splitext(nifti_input_file_tail)
ROI_number, ROI_volume = calc_volume(nifti_input_filename)
# normalization process
tet_norm = tet
if normalization_type == 'number':
tet_norm.v = tet.v/(ROI_number**(1/3))
elif normalization_type == 'volume':
tet_norm.v = tet.v/(ROI_volume**(1/3))
elif normalization_type == 'constant':
tet_norm.v = tet.v/(normalization_factor**(1/3))
else:
pass
# writing normalized surface to a vtk file
if normalization_type == 'number' or normalization_type == 'volume' or normalization_type == 'constant':
surface_output_filename = nifti_input_filename + '_norm=' + normalization_type + '.tetra.vtk'
f = open(surface_output_filename, 'w')
f.write('# vtk DataFile Version 2.0\n')
f.write(nifti_input_file_tail + '\n')
f.write('ASCII\n')
f.write('DATASET POLYDATA\n')
f.write('POINTS ' + str(np.shape(tet.v)[0]) + ' float\n')
for i in range(np.shape(tet.v)[0]):
f.write(' '.join(map(str, tet_norm.v[i, :])))
f.write('\n')
f.write('\n')
f.write('POLYGONS ' + str(np.shape(tet.t)[0]) + ' ' + str(5 * np.shape(tet.t)[0]) + '\n')
for i in range(np.shape(tet.t)[0]):
f.write(' '.join(map(str, np.append(4, tet.t[i, :]))))
f.write('\n')
f.close()
return tet_norm
def calc_eig(nifti_input_filename, output_eval_filename, output_emode_filename, num_modes, normalization_type='none', normalization_factor=1):
"""Calculate the eigenvalues and eigenmodes of the ROI volume in a nifti file.
Parameters
----------
nifti_input_filename : str
Filename of input volume where the relevant ROI have voxel values = 1
output_eval_filename : str
Filename of text file where the output eigenvalues will be stored
output_emode_filename : str
Filename of text file where the output eigenmodes (in tetrahedral surface space) will be stored
num_modes : int
Number of eigenmodes to be calculated
normalization_type : str (default: 'none')
Type of normalization
number - normalization with respect to the total number of non-zero voxels
volume - normalization with respect to the total volume of non-zero voxels in physical dimensions
constant - normalization with respect to a chosen constant
others - no normalization
normalization_factor : float (default: 1)
Factor to be used in a constant normalization
Returns
------
evals: array (num_modes x 1)
Eigenvalues
emodes: array (number of tetrahedral surface points x num_modes)
Eigenmodes
"""
# convert the ROI in the nifti file to a tetrahedral surface
tetra_file = make_tetra_file(nifti_input_filename)
# load tetrahedral surface (as a brainspace object)
tetra = TetIO.import_vtk(tetra_file)
# normalize tetrahedral surface
tetra_norm = normalize_vtk(tetra, nifti_input_filename, normalization_type, normalization_factor)
# calculate eigenvalues and eigenmodes
fem = Solver(tetra_norm)
evals, emodes = fem.eigs(k=num_modes)
output_eval_file_main, output_eval_file_ext = os.path.splitext(output_eval_filename)
output_emode_file_main, output_emode_file_ext = os.path.splitext(output_emode_filename)
if normalization_type == 'number' or normalization_type == 'volume' or normalization_type == 'constant':
np.savetxt(output_eval_file_main + '_norm=' + normalization_type + output_eval_file_ext, evals)
np.savetxt(output_emode_file_main + '_norm=' + normalization_type + output_emode_file_ext, emodes)
else:
np.savetxt(output_eval_filename, evals)
np.savetxt(output_emode_filename, emodes)
return evals, emodes
def calc_volume_eigenmodes(nifti_input_filename, nifti_output_filename, output_eval_filename, output_emode_filename, num_modes, normalization_type='none', normalization_factor=1):
"""Main function to calculate the eigenmodes of the ROI volume in a nifti file.
Parameters
----------
nifti_input_filename : str
Filename of input volume where the relevant ROI have voxel values = 1
nifti_output_filename : str
Filename of nifti file where the output eigenmdoes (in volume space) will be stored
output_eval_filename : str
Filename of text file where the output eigenvalues will be stored
output_emode_filename : str
Filename of text file where the output eigenmodes (in tetrahedral surface space) will be stored
num_modes : int
Number of eigenmodes to be calculated
normalization_type : str (default: 'none')
Type of normalization
number - normalization with respect to the total number of non-zero voxels
volume - normalization with respect to the total volume of non-zero voxels in physical dimensions
constant - normalization with respect to a chosen constant
others - no normalization
normalization_factor : float (default: 1)
Factor to be used in a constant normalization
"""
# calculate eigenvalues and eigenmodes
evals, emodes = calc_eig(nifti_input_filename, output_eval_filename, output_emode_filename, num_modes, normalization_type, normalization_factor)
# project eigenmodes in tetrahedral surface space into volume space
# prepare transformation
ROI_data = nib.load(nifti_input_filename)
roi_data = ROI_data.get_fdata()
inds_all = np.where(roi_data==1)
xx = inds_all[0]
yy = inds_all[1]
zz = inds_all[2]
points = np.zeros([xx.shape[0],4])
points[:,0] = xx
points[:,1] = yy
points[:,2] = zz
points[:,3] = 1
# calculate transformation matrix
T = get_tkrvox2ras(ROI_data.shape, ROI_data.header.get_zooms())
# apply transformation
points2 = np.matmul(T, np.transpose(points))
# load tetrahedral surface
tetra_file = nifti_input_filename + '.tetra.vtk'
tetra = TetIO.import_vtk(tetra_file)
points_surface = tetra.v
# initialize nifti output array
new_shape = np.array(roi_data.shape)
if roi_data.ndim>3:
new_shape[3] = num_modes
else:
new_shape = np.append(new_shape, num_modes)
new_data = np.zeros(new_shape)
# perform interpolation of eigenmodes from tetrahedral surface space to volume space
for mode in range(0, num_modes):
interpolated_data = griddata(points_surface, emodes[:,mode], np.transpose(points2[0:3,:]), method='linear')
for ind in range(0, len(interpolated_data)):
new_data[xx[ind],yy[ind],zz[ind],mode] = interpolated_data[ind]
# save to output nifti file
img = nib.Nifti1Image(new_data, ROI_data.affine, header=ROI_data.header)
nib.save(img, nifti_output_filename)
# remove all created temporary auxiliary files
geo_file = nifti_input_filename + '.geo'
tria_file = nifti_input_filename + '.vtk'
if os.path.exists(geo_file):
os.remove(geo_file)
if os.path.exists(tria_file):
os.remove(tria_file)
def main(raw_args=None):
parser = ArgumentParser(epilog="volume_eigenmodes.py -- A function to calculate the eigenmodes of an ROI volume. James Pang, Monash University, 2022 <james.pang1@monash.edu>")
parser.add_argument("nifti_input_filename", help="An input nifti with ROI voxel values=1", metavar="volume_input.nii.gz")
parser.add_argument("nifti_output_filename", help="An output nifti where the eigenmodes in volume space will be stored", metavar="emodes.nii.gz")
parser.add_argument("output_eval_filename", help="An output text file where the eigenvalues will be stored", metavar="evals.txt")
parser.add_argument("output_emode_filename", help="An output text file where the eigenmods in tetrahedral surface space will be stored", metavar="emodes.txt")
parser.add_argument("-N", dest="num_modes", default=20, help="Number of eigenmodes to be calculated, default=20", metavar="20")
parser.add_argument("-norm", dest="normalization_type", default='none', help="Type of normalization of tetrahedral surface", metavar="none")
parser.add_argument("-normfactor", dest="normalization_factor", default=1, help="Value of constant normalization factor of tetrahedral surface", metavar="1")
#-------------------- Parsing the inputs from terminal: -------------------
args = parser.parse_args()
nifti_input_filename = args.nifti_input_filename
nifti_output_filename = args.nifti_output_filename
output_eval_filename = args.output_eval_filename
output_emode_filename = args.output_emode_filename
num_modes = int(args.num_modes)
normalization_type = args.normalization_type
normalization_factor = float(args.normalization_factor)
#-------------------------------------------------------------------------------
calc_volume_eigenmodes(nifti_input_filename, nifti_output_filename, output_eval_filename, output_emode_filename, num_modes, normalization_type, normalization_factor)
if __name__ == '__main__':
# running via commandline
main()
# running within python
# structures = ['tha']
# hemispheres = ['lh', 'rh']
# num_modes = 30
# normalization_type = 'none'
# normalization_factor = 1
# for structure in structures:
# for hemisphere in hemispheres:
# nifti_input_filename = 'data/template_surfaces_volumes/hcp_' + structure + '-' + hemisphere + '_thr25.nii.gz'
# nifti_output_filename = 'data/template_eigenmodes/hcp_' structure + '-' + hemisphere + '_emode_' + str(num_modes) + '.nii.gz'
# output_eval_filename = 'data/template_eigenmodes/hcp' + structure + '-' + hemisphere + '_eval_' + str(num_modes) + '.txt'
# output_emode_filename = 'data/template_eigenmodes/hcp' + structure + '-' + hemisphere + '_emode_' + str(num_modes) + '.txt'
# calc_volume_eigenmodes(nifti_input_filename, nifti_output_filename, output_eval_filename, output_emode_filename, num_modes, normalization_type, normalization_factor):