-
Notifications
You must be signed in to change notification settings - Fork 0
/
external.py
326 lines (205 loc) · 8.73 KB
/
external.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Author: Eleftherios Garyfallidis
Description: Script that use External command line tools for analysis of Diffusion Data
This script assumes that you are using a modern unix system Debian or Ubuntu
This script was tested on Ubuntu Intrepid 8.10 64bit
External tools used at the moment diffusion toolkit, fsl, mricron, camino
All external tools must be added to the path after installed
'''
import os
import sys
import platform
from subprocess import Popen,PIPE
#subfolders for results
fsl_out_dir='fsl_out'
dtk_dti_out_dir='dtk_dti_out'
dtk_hardi_out_dir='dtk_hardi_out'
camino_out_dir='cami_out'
mricron_out_dir='mricron_out'
freesurf_out_dir='freesurf_out_dir'
#Siemens default 64 Gradient directions for diffusion imaging
dtk_gm_fpath='/home/eg01/tract/gradientSiemens64.txt'
def find_files_unix(initial_dir,filt):
#Example
#initial_dir='/backup/Data/Alexandra/'
#filt='*DTI_64'
cmd='find '+initial_dir+' -name '+ filt
p = Popen(cmd, shell=True,stdout=PIPE,stderr=PIPE)
sto=p.stdout.readlines()
ste=p.stderr.readlines()
return sto,ste
def Alex_dirs():
dpath_list=[
'/backup/Data/Alexandra/Patients/CBU060972/Series_010_CBU_DTI_64',
'/backup/Data/Alexandra/Patients/CBU071017/Series_011_CBU_DTI_64',
'/backup/Data/Alexandra/Patients/CBU070012/Series_011_CBU_DTI_64',
'/backup/Data/Alexandra/Patients/CBU070010/Series_011_CBU_DTI_64',
'/backup/Data/Alexandra/Patients/CBU070096/Series_011_CBU_DTI_64',
'/backup/Data/Alexandra/Controls/CBU070013/Series_009_CBU_DTI_64',
'/backup/Data/Alexandra/Controls/CBU060960/Series_010_CBU_DTI_64',
'/backup/Data/Alexandra/Controls/CBU070011/Series_010_CBU_DTI_64',
'/backup/Data/Alexandra/Controls/CBU070002/Series_009_CBU_DTI_64',
'/backup/Data/Alexandra/Controls/CBU070006/Series_010_CBU_DTI_64',
'/backup/Data/Alexandra/Controls/CBU060967/Series_010_CBU_DTI_64',
'/backup/Data/Alexandra/Controls/CBU060966/Series_009_CBU_DTI_64',
'/backup/Data/Alexandra/Controls/CBU070001/Series_010_CBU_DTI_64',
'/backup/Data/Alexandra/Controls/CBU070092/Series_009_CBU_DTI_64',
'/backup/Data/Alexandra/Controls/CBU070018/Series_010_CBU_DTI_64',
'/backup/Data/Alexandra/Controls/CBU070007/Series_010_CBU_DTI_64']
return dpath_list
def Elef_dirs():
dpath_list=[
'/backup/Data/Eleftherios/CBU090134_METHODS/20090227_154122/Series_003_CBU_DTI_64D_iso_1000',
'/backup/Data/Eleftherios/CBU090133_METHODS/20090227_145404/Series_003_CBU_DTI_64D_iso_1000']
return dpath_list
def Fibre_Cup_files():
fpath1_list=[
'/home/eg01/Data/Fibre_Cup/3x3x3/dwi-b0650.nii',
'/home/eg01/Data/Fibre_Cup/3x3x3/dwi-b1500.nii',
'/home/eg01/Data/Fibre_Cup/3x3x3/dwi-b2650.nii',
]
fpath2_list=[
'/home/eg01/Data/Fibre_Cup/6x6x6/dwi-b0650.nii',
'/home/eg01/Data/Fibre_Cup/6x6x6/dwi-b1500.nii',
'/home/eg01/Data/Fibre_Cup/6x6x6/dwi-b2000.nii',
]
fdirections1='/home/eg01/Data/Fibre_Cup/3x3x3/diffusion_directions.txt'
fdirections2='/home/eg01/Data/Fibre_Cup/6x6x6/diffusion_directions.txt'
return fpath1_list, fpath2_list, fdirections1, fdirections2
def list_files(dpath,filt):
cmd='ls ' + dpath + '/' + filt
p = Popen(cmd, shell=True,stdout=PIPE,stderr=PIPE)
#stdout, stderr = p.communicate()
sto=p.stdout.readlines()
#print sto
stdout, stderr = p.communicate()
return sto
def size_files(dpath,filt):
cmd='du -h ' + dpath + '/' + filt
p = Popen(cmd, shell=True,stdout=PIPE,stderr=PIPE)
#stdout, stderr = p.communicate()
sto=p.stdout.readlines()
#print sto
stdout, stderr = p.communicate()
return sto
def find_first_dcm(dpath,filt='*.dcm'):
dicom_file=list_files(dpath,filt)[0]
return dicom_file[:-1]
def dicom2nifti_dtk(dcmfile,outdir):
cmd='diff_unpack "' + dcmfile + '"' + ' "'+ outdir + '/dwi_all" -ot nii'
p = Popen(cmd, shell=True,stdout=PIPE,stderr=PIPE)
stdout, stderr = p.communicate()
return outdir + '/dwi_all.nii'
def dti_reconstruct_dtk(dwi_all_fpath,dtk_dti_out_dpath,gm_fpath):
cmd = 'dti_recon "' + dwi_all_fpath + '"' + ' "' + dtk_dti_out_dpath + '/dti' + '"' + ' -gm "' + gm_fpath + '" -b 1000 -b0 1 -oc -p 3 -sn 1 -ot nii'
#print cmd
p = Popen(cmd,shell=True,stdout=PIPE,stderr=PIPE)
stdout, stderr = p.communicate()
def dti_tracker_dtk(dtk_dti_out_dpath,mask_fpath='Default',tracking_method='FACT'):
#dti_tracker "/backup/Data/Alexandra/Controls/CBU060960/dti" "/backup/Data/Alexandra/Controls/CBU060960/dti.trk" -at 35 -m "/backup/Data/Alexandra/Controls/CBU060960/dti_dwi.nii" -it nii
if mask_fpath=='Default':
mask_fpath= dtk_dti_out_dpath + '/'+ 'dti_dwi.nii'
if tracking_method=='RK2':
cmd = 'dti_tracker "' + dtk_dti_out_dpath + '/dti' + '"' + ' "' + dtk_dti_out_dpath + '/dti_' + tracking_method + '.trk' + '" ' + '-rk2 -at 35 -m "' + mask_fpath +'" -it nii'
elif tracking_method=='TENSOR_LINE':
cmd = 'dti_tracker "' + dtk_dti_out_dpath + '/dti' + '"' + ' "' + dtk_dti_out_dpath + '/dti_' + tracking_method + '.trk' + '" ' + '-tl -at 35 -m "' + mask_fpath + '" -it nii'
else: #use FACT
cmd = 'dti_tracker "' + dtk_dti_out_dpath + '/dti' + '"' + ' "' + dtk_dti_out_dpath + '/dti_' + tracking_method + '.trk' + '" ' + '-fact -at 35 -m "' + mask_fpath + '" -it nii'
p = Popen(cmd,shell=True,stdout=PIPE,stderr=PIPE)
stdout, stderr = p.communicate()
def mksubdir(dpath,newsubdir):
cmd='mkdir ' + dpath + '/' + newsubdir
p = Popen(cmd, shell=True,stdout=PIPE,stderr=PIPE)
stdout, stderr = p.communicate()
#print(p.stdout.readlines())
#print(p.stderr.readlines())
return dpath + '/' + newsubdir
def rmfiles(dpath,filt):
cmd='rm ' + dpath + '/' + filt
p = Popen(cmd, shell=True,stdout=PIPE,stderr=PIPE)
stdout, stderr = p.communicate()
#print(p.stdout.readlines())
#print(p.stderr.readlines())
def tractography_dtk_dti(dtk_dti_out_dpath,dwi_all_fpath):
#print dwi_all_fpath
dti_reconstruct_dtk(dwi_all_fpath,dtk_dti_out_dpath,dtk_gm_fpath)
dti_tracker_dtk(dtk_dti_out_dpath, tracking_method='FACT')
dti_tracker_dtk(dtk_dti_out_dpath, tracking_method='RK2')
def tractography_dtk_hardi(dtk_hardi_out_dpath,dwi_all_fpath):
pass
def mri_convert(fname,suffix='.nii',out_dir='cur'):
'''
Convert image files to different file formats using freesurfer mri_convert
e.g. image.mgz will be converted to image.nii if suffix is .nii
Todo check else option for out_dir
'''
filenameonly=os.path.splitext(os.path.basename(fname))[0]
if out_dir=='cur':
#Remove suffix
cmd='mri_convert '+ fname + ' ' + filenameonly + suffix
else :
cmd='mri_convert '+ fname + ' ' + os.path.dirname(fname) +'/' + filenameonly + suffix
p = Popen(cmd, shell=True,stdout=PIPE,stderr=PIPE)
stdout, stderr = p.communicate()
print stdout
print stderr
def eddy_correct(fname,suffix='_eddy.nii'):
'''
buggy needs work
'''
filenameonly=os.path.splitext(os.path.basename(fname))[0]
cmd='eddy_correct '+fname + ' ' + os.path.dirname(fname) + '/'+ filenameonly+ suffix
print cmd
'''
p = Popen(cmd, shell=True,stdout=PIPE,stderr=PIPE)
stdout, stderr = p.communicate()
print stdout
print stderr
'''
def mpi_kmeans(finput,foutput,fassign,k,option='assign'):
'''
http://mloss.org/software/view/48/
http://www.kyb.mpg.de/bs/people/pgehler/code/index.html
'''
fkmeans='/home/eg01/Devel/mpi_kmeans-1.5/mpi_kmeans64'
fkmeans2='/home/eg01/Devel/mpi_kmeans-1.5/mpi_assign64'
if os.path.isfile(fkmeans)==False:
print('mpi_kmeans64 does not exist in the specified folder')
return
if option=='assign':
cmd=fkmeans2 + ' --k '+str(k)+' --data '+finput+' --output '+ foutput + ' --assignment ' + fassign
else:
cmd=fkmeans + ' --k '+str(k)+' --data '+finput+' --output '+ foutput
p = Popen(cmd, shell=True,stdout=PIPE,stderr=PIPE)
stdout, stderr = p.communicate()
print stdout
print stderr
def Pipeline(dicom_dirs):
logf = open('log.txt','w')
for folder in dicom_dirs:
dcmfile=find_first_dcm(folder)
'''
Make subdirectories and generate nii
'''
dtk_dti_out_dpath=mksubdir(folder,dtk_dti_out_dir)
dwi_all_fpath=dicom2nifti_dtk(dcmfile,dtk_dti_out_dpath)
'''
DTK DTI Tractography
'''
tractography_dtk_dti(dtk_dti_out_dpath,dwi_all_fpath)
#res=list_files(dtk_dti_out_dpath,'*.trk')
res=size_files(dtk_dti_out_dpath,'*.trk')
'''
Print trks' paths
'''
for r in res:
print r
logf.writelines(res)
'''
DTK HARDI Tractography
'''
logf.close()
if __name__ == "__main__":
Pipeline(Alex_dirs())