forked from DaveOC90/dki_preproc
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dki_preproc.py
359 lines (261 loc) · 12.4 KB
/
dki_preproc.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
import nipype.pipeline.engine as pe
from nipype.interfaces import afni,fsl,ants,dipy
import nipype.interfaces.io as nio
import nipype.interfaces.utility as util
import nipype
import os,glob,sys
from nipype.workflows.dmri.fsl.dti import create_eddy_correct_pipeline
def hex2float(inmat):
'''
Function to convert matrices in hexadecimal format to floating point format
Input: double space delimited .mat file from FSL
Ouput: double space delimited .mat file
'''
# Read in data as list of lists, seperating strings based on double spaces and newline characters
data=[l.strip().split(' ') for l in open(inmat,'rU')]
# convert each element to float, if all all elements have the hex character for scientific notation (p)
# Assuming the p being present in all means it is hex, also assuming you wont have a mix of hex and float
datanew=[map(lambda x: float.fromhex(x),d) if all('p' in e for e in d) else d for d in data]
# turn list of lists back into one long string
opdatanew='\n'.join([' '.join(map(str,d)) for d in datanew])
# write mat to same directory as input mat
opname=inmat.replace('.mat','_float.mat')
# write string
fo=open(opname,'w')
fo.write(opdatanew)
fo.close()
return opname
hexmat2fltmat = util.Function(input_names=["inmat"], output_names=["opmat"],function=hex2float)
## Intialize variables and workflow
globaldir='/home/davidoconner/dki_preproc/'
workdir='/home/davidoconner/dki_preproc/working'
preproc = pe.Workflow(name='preprocflow')
preproc.base_dir = workdir
ipdir='/home/davidoconner/hbnssi_rawdata/'
sublist=[g.split('/')[-2] for g in glob.glob(ipdir+'*/')]
seslist=['ses-SSV1']#list(set([g.split('/')[-2] for g in glob.glob(ipdir+'*/*/')]))
# Topup Acquisition Parameters
#acqparm='0 -1 0 0.0684\n0 1 0 0.0684'
#index=' '.join([1 for x in range(0,numvols)])
# BBR Schedule
bbrsched='/usr/share/fsl/5.0/etc/flirtsch/bbr.sch'
## Setup data managment nodes
infosource = pe.Node(util.IdentityInterface(fields=['subject_id','session_id']),name="infosource")
infosource.iterables = [('subject_id', sublist),('session_id', seslist)]
templates={
'dki' : ipdir+'{subject_id}/{session_id}/dwi/{subject_id}_{session_id}_acq-DKI64DIRECTIONSAP3WEIGHTSAX_dwi.nii.gz', \
'bvals' : ipdir+'{subject_id}/{session_id}/dwi/{subject_id}_{session_id}_acq-DKI64DIRECTIONSAP3WEIGHTSAX_dwi.bval', \
'bvecs' : ipdir+'{subject_id}/{session_id}/dwi/{subject_id}_{session_id}_acq-DKI64DIRECTIONSAP3WEIGHTSAX_dwi.bvec', \
'b0ap' : ipdir+'{subject_id}/{session_id}/dwi/{subject_id}_{session_id}_acq-DWIB0APAX_dwi.nii.gz', \
'b0pa' : ipdir+'{subject_id}/{session_id}/dwi/{subject_id}_{session_id}_acq-DWIB0PAAX_dwi.nii.gz', \
'anat' : ipdir+'{subject_id}/{session_id}/anat/{subject_id}_{session_id}_acq-MEMPRAGE_T1w.nii.gz'
}
selectfiles = pe.Node(nio.SelectFiles(templates,base_directory=ipdir),name="selectfiles")
datasink = pe.Node(nio.DataSink(base_directory=globaldir, container=workdir),name="datasink")
## Anat Preproc
# Skullstrip MPRAGE
anat_skullstrip = pe.Node(interface=afni.preprocess.SkullStrip(),
name='anat_skullstrip')
anat_skullstrip.inputs.args = '-o_ply'
anat_skullstrip.inputs.outputtype = 'NIFTI_GZ'
# Mask MPRAGE
anat_brain_only = pe.Node(interface=afni.preprocess.Calc(),
name='anat_brain_only')
anat_brain_only.inputs.expr = 'a*step(b)'
anat_brain_only.inputs.outputtype = 'NIFTI_GZ'
# FAST Node
segment=pe.Node(interface = fsl.FAST(), name='segment')
segment.inputs.img_type = 1
segment.inputs.segments = True
segment.inputs.probability_maps = True
segment.inputs.out_basename = 'segment'
# Split fast prob maps, picking wm
wmprob_split = pe.Node(interface=util.Select(), name = 'wmprob_split')
wmprob_split.inputs.index=2
wmmapbin = pe.Node(interface=fsl.maths.MathsCommand(),name='wmmapbin')
#fslmaths opname_fast_pve_2 -thr 0.5 -bin opname_fast_wmseg
# in_file -> out_file
wmmapbin.inputs.args='-thr 0.5 -bin'
# Calculate ANTs Warp
calculate_ants_warp = pe.Node(interface=ants.Registration(),
name='calculate_ants_warp')
calculate_ants_warp.inputs. \
fixed_image = '/usr/share/fsl/5.0/data/standard/MNI152_T1_3mm_brain.nii.gz'
calculate_ants_warp.inputs. \
dimension = 3
calculate_ants_warp.inputs. \
use_histogram_matching=[ True, True, True ]
calculate_ants_warp.inputs. \
winsorize_lower_quantile = 0.01
calculate_ants_warp.inputs. \
winsorize_upper_quantile = 0.99
calculate_ants_warp.inputs. \
metric = ['MI','MI','CC']
calculate_ants_warp.inputs. \
metric_weight = [1,1,1]
calculate_ants_warp.inputs. \
radius_or_number_of_bins = [32,32,4]
calculate_ants_warp.inputs. \
sampling_strategy = ['Regular','Regular',None]
calculate_ants_warp.inputs. \
sampling_percentage = [0.25,0.25,None]
calculate_ants_warp.inputs. \
number_of_iterations = [[1000,500,250,100], \
[1000,500,250,100], [100,100,70,20]]
calculate_ants_warp.inputs. \
convergence_threshold = [1e-8,1e-8,1e-9]
calculate_ants_warp.inputs. \
convergence_window_size = [10,10,15]
calculate_ants_warp.inputs. \
transforms = ['Rigid','Affine','SyN']
calculate_ants_warp.inputs. \
transform_parameters = [[0.1],[0.1],[0.1,3,0]]
calculate_ants_warp.inputs. \
shrink_factors = [[8,4,2,1],[8,4,2,1],[6,4,2,1]]
calculate_ants_warp.inputs. \
smoothing_sigmas = [[3,2,1,0],[3,2,1,0],[3,2,1,0]]
calculate_ants_warp.inputs. \
sigma_units = ['vox','vox','vox']
calculate_ants_warp.inputs. \
output_warped_image = True
calculate_ants_warp.inputs. \
output_inverse_warped_image = True
calculate_ants_warp.inputs. \
output_transform_prefix = 'xfm'
calculate_ants_warp.inputs. \
write_composite_transform = True
calculate_ants_warp.inputs. \
collapse_output_transforms = False
## Diffusion Tensor Computation
fslroi = pe.Node(interface=fsl.ExtractROI(), name='fslroi')
fslroi.inputs.t_min = 0
fslroi.inputs.t_size = 1
bet = pe.Node(interface=fsl.BET(), name='bet')
bet.inputs.mask = True
bet.inputs.frac = 0.34
mergelistb0 = pe.Node(interface=util.Merge(2), name='mergelistb0')
b0merge = pe.Node(interface=fsl.Merge(), name='b0merge')
b0merge.inputs.dimension='t'
# input 'in_files' list of items
# output merged_file
topup = pe.Node(interface=fsl.TOPUP(), name='topup')
topup.inputs.config='b02b0.cnf'
topup.inputs.encoding_file='/home/davidoconner/git/dki_preproc/acqparams.txt'
b0_corrected_mean = pe.Node(interface=fsl.maths.MeanImage(), name='b0_corrected_mean')
b0_corrected_mean.inputs.dimension='T'
#fslmaths my_hifi_b0 -Tmean my_hifi_b0
#output is 'file'
bet_b0corr = pe.Node(interface=fsl.BET(), name='bet_b0corr')
bet_b0corr.inputs.mask = True
bet_b0corr.inputs.frac = 0.34
bet_b0 = pe.Node(interface=fsl.BET(), name='bet_b0')
bet_b0.inputs.mask = True
#bet my_hifi_b0 my_hifi_b0_brain -m
eddycorrect = pe.Node(interface=fsl.Eddy(), name='eddycorrect')
eddycorrect.inputs.in_index = '/home/davidoconner/git/dki_preproc/index.txt'
eddycorrect.inputs.in_acqp = '/home/davidoconner/git/dki_preproc/acqparams.txt'
eddycorrect.threads = 2
#eddy --imain=data --mask=my_hifi_b0_brain_mask --acqp=acqparafslviewms.txt --index=index.txt --bvecs=bvecs --bvals=bvals --topup=my_topup_results --out=eddy_corrected_data
fslroi_b0corr = pe.Node(interface=fsl.ExtractROI(), name='fslroi_b0corr')
fslroi_b0corr.inputs.t_min = 0
fslroi_b0corr.inputs.t_size = 1
## COnvert eddy mat to float
h2f = pe.Node(interface=hexmat2fltmat,name='h2f')
## COnvert b0 flirt to anat initialization mat to float
h2f2 = pe.Node(interface=hexmat2fltmat,name='h2f2')
## B0 to Anat Initial mat
linear_reg_b0_init = pe.Node(interface=fsl.FLIRT(), name='linear_reg_b0_init')
linear_reg_b0_init.inputs.cost = 'corratio'
linear_reg_b0_init.inputs.dof = 6
linear_reg_b0_init.inputs.interp = 'trilinear'
## B0 to Anat
linear_reg_b0 = pe.Node(interface=fsl.FLIRT(), name='linear_reg_b0')
linear_reg_b0.inputs.cost = 'bbr'
linear_reg_b0.inputs.dof = 6
linear_reg_b0.inputs.interp = 'nearestneighbour'
## Apply XFM
app_xfm_lin = pe.Node(interface=fsl.ApplyXfm(),
name='app_xfm_lin')
app_xfm_lin.inputs.apply_xfm = True
## Func to MNI
b0_t1_to_mni = pe.Node(interface=ants.ApplyTransforms(), name='b0_t1_to_mni')
b0_t1_to_mni.inputs.dimension = 3
b0_t1_to_mni.inputs.reference_image='/usr/share/fsl/5.0/data/standard/MNI152_T1_3mm_brain.nii.gz'
b0_t1_to_mni.inputs.invert_transform_flags = [False]
b0_t1_to_mni.inputs.interpolation = 'NearestNeighbor'
b0_t1_to_mni.inputs.input_image_type = 3
dtifit = pe.Node(interface=fsl.DTIFit(), name='dtifit')
dtifit_norm = pe.Node(interface=fsl.DTIFit(), name='dtifit_norm')
dkifit = pe.Node(interface=dipy.DKI(), name='dkifit')
preproc.connect([
(infosource,selectfiles,[('subject_id', 'subject_id'),('session_id', 'session_id')]),
#(selectfiles,fslroi,[('dki','in_file')]),
#(fslroi, bet, [('roi_file', 'in_file')]),
(selectfiles,mergelistb0,[('b0ap','in1')]),
(selectfiles,mergelistb0,[('b0pa','in2')]),
(mergelistb0,b0merge,[('out','in_files')]),
(b0merge,topup,[('merged_file','in_file')]),
(topup,b0_corrected_mean,[('out_corrected','in_file')]),
(b0_corrected_mean,bet_b0,[('out_file','in_file')]),
(selectfiles, eddycorrect, [('bvals', 'in_bval')]),
(selectfiles, eddycorrect, [('bvecs', 'in_bvec')]),
(selectfiles, eddycorrect, [('dki','in_file')]),
(bet_b0, eddycorrect, [('mask_file', 'in_mask')]),
(topup,eddycorrect, [('out_fieldcoef','in_topup_fieldcoef')]),
(topup,eddycorrect, [('out_movpar','in_topup_movpar')]),
(eddycorrect,fslroi_b0corr,[('out_corrected','in_file')]),
(fslroi_b0corr,bet_b0corr,[('roi_file','in_file')])
#(eddycorrect, dtifit, [('out_corrected', 'dwi')]),
#(infosource, dtifit, [('subject_id', 'base_name')]),
#(bet_b0corr, dtifit, [('mask_file', 'mask')]),
#(selectfiles, dtifit, [('bvals', 'bvals')]),
#(selectfiles, dtifit, [('bvecs', 'bvecs')]),
#(dtifit,datasink,[('FA','@dtifitFA')]),
#(dtifit,datasink,[('L1','@dtifitL1')]),
#(dtifit,datasink,[('L2','@dtifitL2')]),
#(dtifit,datasink,[('L3','@dtifitL3')]),
#(dtifit,datasink,[('MD','@dtifitMD')]),
#(dtifit,datasink,[('MO','@dtifitMO')]),
#(dtifit,datasink,[('S0','@dtifitS0')]),
#(dtifit,datasink,[('V1','@dtifitV1')]),
#(dtifit,datasink,[('V2','@dtifitV2')]),
#(dtifit,datasink,[('V3','@dtifitV3')]),
#(dtifit,datasink,[('tensor','@dtifittensor')])
#(selectfiles, anat_skullstrip, [('anat','in_file')]),
#(selectfiles, anat_brain_only, [('anat','in_file_a')]),
#(anat_skullstrip, anat_brain_only, [('out_file', 'in_file_b')]),
#(anat_brain_only, calculate_ants_warp, [('out_file', 'moving_image')]),
#(anat_brain_only, segment, [('out_file', 'in_files')]),
#(segment,wmprob_split, [('probability_maps','inlist')]),
#(wmprob_split,wmmapbin, [('out','in_file')]),
#(fslroi_b0corr, linear_reg_b0_init, [('roi_file', 'in_file')]),
#(anat_brain_only, linear_reg_b0_init,[('out_file', 'reference')]),
#(linear_reg_b0_init, h2f2, [('out_matrix_file','inmat')]),
#(fslroi_b0corr, linear_reg_b0, [('roi_file', 'in_file')]),
#(anat_brain_only, linear_reg_b0,[('out_file', 'reference')]),
#(h2f2, linear_reg_b0,[('opmat', 'in_matrix_file')]),
#(wmmapbin, linear_reg_b0, [('out_file', 'wm_seg')]),
#(eddycorrect, app_xfm_lin, [('out_corrected','in_file')]),
#(anat_brain_only, app_xfm_lin, [('out_file','reference')]),
#(linear_reg_b0, h2f, [('out_matrix_file','inmat')]),
#(h2f, app_xfm_lin, [('opmat','in_matrix_file')]),
#(calculate_ants_warp, b0_t1_to_mni, [('composite_transform', 'transforms')]),
#(app_xfm_lin, b0_t1_to_mni, [('out_file','input_image')]),
#b0_t1_to_mni,datasink, [('output_image','@dkimni')]),
#(bet, dtifit, [('mask_file', 'mask')]),
#(selectfiles, dtifit, [('bvals', 'bvals')]),
#(selectfiles, dtifit, [('bvecs', 'bvecs')]),
#(eddycorrect, dkifit, [('out_corrected', 'in_file')]),
#(selectfiles, dkifit, [('bvals', 'in_bval')]),
#(selectfiles, dkifit, [('bvecs', 'in_bvec')]),
#(dkifit,datasink,[('fa','@.dkimodel.FA')]),
#(dkifit,datasink,[('md','@.dkimodel.MD')]),
#(dkifit,datasink,[('rd','@.dkimodel.RD')]),
#(dkifit,datasink,[('ad','@.dkimodel.AD')]),
#(dkifit,datasink,[('mk','@.dkimodel.MK')]),
#(dkifit,datasink,[('ak','@.dkimodel.AK')]),
#(dkifit,datasink,[('rk','@.dkimodel.RK')])
])
if __name__ == '__main__':
preproc.run('MultiProc',plugin_args={'n_procs':4})
preproc.write_graph()