-
Notifications
You must be signed in to change notification settings - Fork 3
/
connectome_trk.py
executable file
·214 lines (173 loc) · 6.66 KB
/
connectome_trk.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
#!/usr/bin/env python
#
#
# NAME
#
# connectome_trk.py
#
# DESCRIPTION
#
# This is a wrapper for the Connectome Mapping Toolkit such that
# it can be executed from the CHB web front-end.
#
# Debug test version
#
# AUTHORS
#
# Daniel Ginsburg
# Rudolph Pienaar
# Children's Hospital Boston, 2010
#
# HISTORY
# 17 August 2011
# o Added '--notalairach' flag and handling.
#
import cmp,cmp.connectome,cmp.gui,cmp.configuration,cmp.pipeline,cmp.logme
import sys
import os
import shutil, glob
from optparse import OptionParser
def parseCommandLine(conf):
"""Setup and parse command-line options"""
parser = OptionParser(usage="%prog [options]")
parser.add_option("-p", "--projectName",
dest="projectName",
help="Project name")
parser.add_option("-v", "--verbose",
action="store_true", dest="verbose")
parser.add_option("-d","--workingDir",
dest="workingDir",
help="Subject Working Directory")
parser.add_option("--b0",
dest="b0",
type="int",
help="Number of B0 volumes")
parser.add_option("--bValue",
dest="bValue",
type="int",
help="B Value")
parser.add_option("--gm",
dest="gradientMatrix",
help="Gradient file")
parser.add_option("--dtiDir",
dest="dtiDir",
help="DTI DICOM Input directory")
parser.add_option("--t1Dir",
dest="t1Dir",
help="T1 DICOM Input directory")
parser.add_option("--skipCompletedStages",
dest="skipCompletedStages",
action="store_true",
help="Skip previously completed stages.")
parser.add_option("--notalairach",
dest="notalairach",
action="store_true",
help="Do not perform talairach registration.")
parser.add_option("--writePickle",
dest="writePickle",
help="Filename to write pickle for use with CMT GUI. Exit after writing pickle file.")
(options, args) = parser.parse_args()
if len(args) != 0:
parser.error("Wrong number of arguments")
# Parse command-line options
if options.workingDir == None:
parser.error('You must specify --workingDir')
else:
conf.project_dir = os.path.dirname(options.workingDir)
conf.subject_name = os.path.basename(options.workingDir)
if options.projectName:
conf.project_name = options.projectName
else:
conf.project_name = 'connectome_web'
if options.gradientMatrix:
conf.gradient_table_file = options.gradientMatrix
conf.gradient_table = 'custom'
if options.b0:
conf.nr_of_b0 = options.b0
if options.bValue:
conf.max_b0_val = options.bValue
if options.skipCompletedStages:
conf.skip_completed_stages = True
# This must be the last step, write the configuration object
# out to a pickle file for use in the CMT GUI
if options.writePickle:
conf.save_state(os.path.abspath(options.writePickle))
return options
def prepForExecution(conf, options):
"""Prepare the files for execution of the cmp pipeline"""
# Must specify the T1 and DTI input directories
if options.t1Dir == None:
sys.exit('You must specify --t1Dir')
if options.dtiDir == None:
sys.exit('You must specify --dtiDir')
# First, setup the pipeline status so we can determine the inputs
cmp.connectome.setup_pipeline_status(conf)
# Get the first stage by number
stage = conf.pipeline_status.GetStage(num=1)
# Get the DTI and T1 DICOM input folders
dtiInput = conf.pipeline_status.GetStageInput(stage, 'dti-dcm')
t1Input = conf.pipeline_status.GetStageInput(stage, 't1-dcm')
# Create the input folders
if not os.path.exists(dtiInput.rootDir):
os.makedirs(dtiInput.rootDir)
if not os.path.exists(t1Input.rootDir):
os.makedirs(t1Input.rootDir)
# Copy the DICOM's
for file in glob.glob(os.path.join(options.dtiDir, dtiInput.filePath)):
shutil.copy(file, dtiInput.rootDir)
for file in glob.glob(os.path.join(options.t1Dir, t1Input.filePath)):
shutil.copy(file, t1Input.rootDir)
def main():
"""Main entrypoint for program"""
# Create configuration object (the GUI object
# is subclassed from PipelineConfiguration and
# we use this so we can serialize it as a pickle
# if we want to)
conf = cmp.gui.CMPGUI()
# Default Options
conf.freesurfer_home = os.environ['FREESURFER_HOME']
conf.fsl_home = os.environ['FSLDIR']
conf.dtk_matrices = os.environ['DSI_PATH']
conf.dtk_home = os.path.dirname(conf.dtk_matrices) # DTK home is one up from the matrices
conf.subject_raw_glob_diffusion = '*.dcm'
conf.subject_raw_glob_T1 = '*.dcm'
conf.subject_raw_glob_T2 = '*.dcm'
conf.do_convert_T2 = False
conf.diffusion_imaging_model = "DTI"
conf.streamline_param = ''
# Enable all stages
conf.active_dicomconverter = False
conf.active_registration = False
conf.active_segmentation = False
conf.active_parcellation = False
conf.active_applyregistration = False
conf.active_reconstruction = False
conf.active_tractography = False
conf.active_fiberfilter = False
conf.active_connectome = True
conf.active_statistics = True
conf.active_cffconverter = True
conf.skip_completed_stages = False
# Setup and parse command-line options
options = parseCommandLine(conf)
# XXX: These are hardcoded for now until I figure out how they
# should be set
conf.creator = 'Neuroimaging Web Pipeline'
conf.publisher = 'CHB'
conf.legalnotice = 'institution-specific'
conf.email = 'default@default.edu'
if options.notalairach:
conf.recon_all_param = '-all -no-isrunning -notalairach'
#print conf.recon_all_param
# If writing pickle, return
if options.writePickle:
return
# Prepare the directory structure for execution
prepForExecution(conf, options)
# Before running, reset the pipeline status because it will
# get created in mapit()
conf.pipeline_status = cmp.pipeline_status.PipelineStatus()
# Execute the 'cmp' pipeline!
cmp.connectome.mapit(conf)
if __name__ == '__main__':
main()