-
Notifications
You must be signed in to change notification settings - Fork 5
/
pipeline.py
executable file
·277 lines (225 loc) · 8.61 KB
/
pipeline.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Module implementing the main sequence of pipeline analysis to process
diffusion MRI designed according to the requirements from APSS
Copyright (c) 2014, Fondazione Bruno Kessler
Distributed under the BSD 3-clause license. See COPYING.txt.
"""
"""
The available steps of pipeline analysis:
1. Structural Dicom to nifti
2. Structural brain extraction
3. Diffusion DICOM to nifti
4. Diffusion brain extraction
5. Eddy current correction
6. Rescaling isotropic voxel
7. Registration of structural data
8. Registration of atlas
9. Reconstruction of tensor model
10. Tracking of streamlines
11. Constraint Spherical Deconvolution
12. Tractome preprocessing
13. Registration of ROI from atlas
"""
import os
import sys
import platform
import numpy as np
import nibabel as nib
from parameters import *
from pipenode import dicom_to_nifti, brain_extraction, brain_dwi_extraction, eddy_current_correction, rescaling_isotropic_voxel, flirt_registration, atlas_registration, compute_reconstruction, compute_dti_tracking, compute_csd, compute_csd_det_tracking, compute_csd_prob_tracking, tractome_preprocessing, roi_registration
max_step = 16
do_step = [1] * max_step
def run_pipeline():
print("*** BEGINNING OF PIPELINE COMPUTATION ***")
if not os.path.isdir(main_data_directory):
print("FAIL: setting parameters - FILE: data directory not found!")
sys.exit()
subj = os.path.basename(main_data_directory)
step = 0
print("Subject: %s" % subj)
print("Step %i: Setting parameters....." % step)
## Directory of DICOM files
dir_dicom_dmri = os.path.join(main_data_directory, 'DICOM/Diffusion')
dir_dicom_mri = os.path.join(main_data_directory, 'DICOM/Structural')
if platform.system() == 'Darwin':
if os.path.isfile(os.path.join(dir_dicom_dmri, '.DS_Store')):
os.remove(os.path.join(dir_dicom_dmri, '.DS_Store'))
if os.path.isfile(os.path.join(dir_dicom_mri, '.DS_Store')):
os.remove(os.path.join(dir_dicom_mri, '.DS_Store'))
## Creating directory to save Niftii generated data
dir_nii = os.path.join(main_data_directory, 'Niftii')
if not os.path.exists(dir_nii):
os.makedirs(dir_nii)
dir_nii_mri = os.path.join(dir_nii, 'Structural/')
if not os.path.exists(dir_nii_mri):
os.makedirs(dir_nii_mri)
dir_nii_dmri = os.path.join(dir_nii, 'Diffusion/')
if not os.path.exists(dir_nii_dmri):
os.makedirs(dir_nii_dmri)
## Creating directory and filepaths for preprocessed data
dir_mri_pre = os.path.join(dir_nii_mri,'Preprocess/')
if not os.path.exists(dir_mri_pre):
os.makedirs(dir_mri_pre)
dir_dmri_pre = os.path.join(dir_nii_dmri,'Preprocess/')
if not os.path.exists(dir_dmri_pre):
os.makedirs(dir_dmri_pre)
## Creating directory for tractography
dir_trk_tractography = os.path.join(main_data_directory,'TRK/Tractography/')
if not os.path.exists(dir_trk_tractography):
os.makedirs(dir_trk_tractography)
dir_trk_dissection = os.path.join(main_data_directory,'TRK/Dissection/')
if not os.path.exists(dir_trk_dissection):
os.makedirs(dir_trk_dissection)
dir_trk_roi = os.path.join(main_data_directory,'TRK/ROI/')
if not os.path.exists(dir_trk_roi):
os.makedirs(dir_trk_roi)
print("DONE!")
step += 1
## Preprocessing of Structural MRI data
print("Step %i: Converting Structural DICOM files to Nifti..." % step)
if do_step[step]:
dicom_to_nifti(dir_dicom_mri, dir_nii_mri, subj, par_mri_tag)
print("DONE!")
else:
print("Skipped.")
step += 1
print("Step %i: Brain extraction of structural data..." % step)
if do_step[step]:
brain_extraction(dir_nii_mri, dir_mri_pre, subj, par_mri_tag)
print("DONE!")
else:
print("Skipped.")
step += 1
## Preprocessing of Diffusion MRI data
print("Step %i: Converting Diffusion DICOM files to Nifti..." % step)
if do_step[step]:
dicom_to_nifti(dir_dicom_dmri, dir_nii_dmri, subj, par_dmri_tag)
print('DONE!')
else:
print("Skipped.")
step += 1
print("Step %i: Brain extraction of diffusion data..." % step)
if do_step[step]:
brain_dwi_extraction(dir_nii_dmri, dir_dmri_pre, subj, par_dmri_tag)
print("DONE!")
else:
print("Skipped.")
step += 1
print("Step %i: Eddy current correction of diffusion data..." % step)
if do_step[step]:
eddy_current_correction(dir_dmri_pre, dir_dmri_pre, subj)
print("DONE!")
else:
print("Skipped.")
step += 1
print("Step %i: Rescaling isotropic voxels..." % step)
if do_step[step]:
rescaling_isotropic_voxel(dir_dmri_pre, dir_nii_dmri, subj)
print("DONE!")
else:
print("Skipped.")
step += 1
print("Step %i: Registration of structural data..." % step)
if do_step[step]:
flirt_registration(dir_mri_pre, dir_nii_dmri, dir_nii_mri, dir_mri_pre,subj)
print("DONE!")
else:
print("Skipped.")
step += 1
print("Step %i: Registration of atlas..." % step)
if do_step[step]:
atlas_registration(dir_nii_dmri, dir_nii_dmri, dir_dmri_pre, subj)
print("DONE!")
else:
print("Skipped.")
step += 1
print("Step %i: Reconstruction of tensor model..." % step)
if do_step[step]:
compute_reconstruction(dir_nii_dmri, subj)
print("DONE!")
else:
print("Skipped.")
step += 1
print("Step %i: Deterministic tracking with DTI model..." % step)
if do_step[step]:
compute_dti_tracking(dir_nii_dmri, dir_trk_tractography, subj)
print("DONE!")
else:
print("Skipped.")
step += 1
print("Step %i: Reconstruction CSD model..." % step)
if do_step[step]:
compute_csd(dir_nii_dmri, dir_nii_dmri, subj)
print("DONE!")
else:
print("Skipped.")
step += 1
print("Step %i: Deterministic tracking with CSD model..." % step)
if do_step[step]:
compute_csd_det_tracking(dir_nii_dmri, dir_trk_tractography, subj)
print("DONE!")
else:
print("Skipped.")
step += 1
print("Step %i: Probabilistic tracking with CSD model..." % step)
if do_step[step]:
compute_csd_prob_tracking(dir_nii_dmri, dir_trk_tractography, subj)
print("DONE!")
else:
print("Skipped.")
step += 1
print("Step %i: Tractome preprocessing..." % step)
if do_step[step]:
tractome_preprocessing(dir_trk_tractography, subj)
print("DONE!")
else:
print("Skipped.")
step += 1
print("Step %i: ROI Registration..." % step)
if do_step[step]:
roi_registration(dir_nii_dmri, dir_trk_roi, subj)
print("DONE!")
else:
print("Skipped.")
step += 1
print("*** END OF PIPELINE ***")
if __name__ == '__main__':
for arg in sys.argv[1:]:
if arg == '-h':
print("Usage:")
print(" pipeline.py <args>* ")
print("Arguments:")
print(" path: <pathname>")
print(" Data file directory")
print(" step: 'number number ...'")
print(" 1. Structural Dicom to nifti")
print(" 2. Structural brain extraction")
print(" 3. Diffusion DICOM to nifti")
print(" 4. Diffusion brain extraction")
print(" 5. Eddy current correction")
print(" 6. Rescaling isotropic voxel")
print(" 7. Registration of structural data")
print(" 8. Registration of atlas")
print(" 9. Reconstruction of tensor model")
print(" 10. Deterministic tracking with DTI")
print(" 11. Constraint Spherical Deconvolution")
print(" 12. Deterministic tracking with CSD")
print(" 13. Probabilistic tracking with CSD")
print(" 14. Tractome preprocessing")
print(" 15. Registration of ROI from atlas")
print(" help: -h")
print(" this help")
print("Examples:")
print(" pipeline.py /path/to/my/data")
print(" pipeline.py /path/to/my/data '1 2 7'")
print(" pipeline.py '1 2 7'")
sys.exit()
if not os.path.isdir(arg):
do_step = [0] * max_step
arg_step = map(int, arg.split())
for s in arg_step: do_step[s]=1
if os.path.isdir(arg):
main_data_directory = os.path.abspath(sys.argv[1])
run_pipeline()