-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwmc2trk.py
59 lines (48 loc) · 1.92 KB
/
wmc2trk.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
import os
import sys
import json
import argparse
import numpy as np
import nibabel as nib
from scipy.io import loadmat
def wmc2trk(trk_file, classification, tractID):
"""
Convert a single wmc classified tract into a single trk file.
"""
tractogram = nib.streamlines.load(trk_file)
aff_vox_to_ras = tractogram.affine
voxel_sizes = tractogram.header['voxel_sizes']
dimensions = tractogram.header['dimensions']
tractogram = tractogram.streamlines
wmc = loadmat(classification)
data = wmc["classification"][0][0]
t_name = data[0][0][tractID][0]
tract_name = t_name.replace(' ', '_')
indeces = data[1]
idx_tract = np.array(np.where(indeces==tractID))[0]
tract = tractogram[idx_tract]
with open('tract_name_list.txt', 'w') as filetowrite:
filetowrite.write('%s\n' %tract_name)
#creating empty hader
hdr = nib.streamlines.trk.TrkFile.create_empty_header()
hdr['voxel_sizes'] = voxel_sizes
hdr['dimensions'] = dimensions
hdr['voxel_order'] = 'LAS'
hdr['voxel_to_rasmm'] = aff_vox_to_ras
#saving tract
out_filename = '%s_tract.trk' %tract_name
t = nib.streamlines.tractogram.Tractogram(tract, affine_to_rasmm=np.eye(4))
nib.streamlines.save(t, out_filename, header=hdr)
print("Tract saved in %s" % out_filename)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-tractogram', nargs='?', const=1, default='',
help='The tractogram file')
parser.add_argument('-classification', nargs='?', const=1, default='',
help='The classification.mat file')
parser.add_argument('-tractID', nargs='?', const=1, default='',
help='The tract id')
args = parser.parse_args()
print("Convert a single wmc classified tract into a single trk file")
wmc2trk(args.tractogram, args.classification, eval(args.tractID))
sys.exit()