forked from BRAINSia/BRAINSStandAlone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkupT1T2TissueClassify.py
224 lines (192 loc) · 12.3 KB
/
WorkupT1T2TissueClassify.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
#!/usr/bin/env python
from nipype.interfaces.base import CommandLine, CommandLineInputSpec, TraitedSpec, File, Directory
from nipype.interfaces.base import traits, isdefined, BaseInterface
from nipype.interfaces.utility import Merge, Split, Function, Rename, IdentityInterface
import nipype.interfaces.io as nio # Data i/o
import nipype.pipeline.engine as pe # pypeline engine
from BRAINSABCext import *
"""
from WorkupT1T2TissueClassify import CreateTissueClassifyWorkflow
myLocalTCWF= CreateTissueClassifyWorkflow("TissueClassify")
tissueClassifyWF.connect( [ (uidSource, myLocalTCWF, [(('uid', getT1s, subjectDatabaseFile ), 'T1List')] ), ])
tissueClassifyWF.connect( [ (uidSource, myLocalTCWF, [(('uid', getT2s, subjectDatabaseFile ), 'T2List')] ), ])
tissueClassifyWF.connect( [ (uidSource, myLocalTCWF, [(('uid', getT1sLength, subjectDatabaseFile ), 'T1_count')] ), ])
tissueClassifyWF.connect( BCD, 'outputResampledVolume', myLocalTCWF, 'PrimaryT1' )
tissueClassifyWF.connect(BAtlas,'ExtendedAtlasDefinition.xml',myLocalTCWF,'atlasDefinition')
tissueClassifyWF.connect(BLI,'outputTransformFilename',myLocalTCWF,'atlasToSubjectInitialTransform')
"""
def MakeOneFileList(T1List, T2List, PDList, FLList, OtherList, PrimaryT1):
""" This funciton uses PrimaryT1 for the first T1, and the append the rest of the T1's and T2's """
imagePathList = list()
imagePathList.append(PrimaryT1) # Force replacement of the first element
for i in T1List[1:]:
imagePathList.append(i) # The reset of the elements
for i in T2List[0:]:
imagePathList.append(i)
for i in PDList[0:]:
imagePathList.append(i)
for i in FLList[0:]:
imagePathList.append(i)
for i in OtherList[0:]:
imagePathList.append(i)
return imagePathList
def MakeOneFileTypeList(T1List, T2List, PDList, FLList, OtherList):
input_types = ["T1"] * len(T1List)
input_types.extend(["T2"] * len(T2List))
input_types.extend(["PD"] * len(PDList))
input_types.extend(["FL"] * len(FLList))
input_types.extend(["OTHER"] * len(OtherList))
return input_types
def MakeOutFileList(T1List, T2List, PDList, FLList, OtherList):
def GetExtBaseName(filename):
'''
Get the filename without the extension. Works for .ext and .ext.gz
'''
import os
currBaseName = os.path.basename(filename)
currExt = os.path.splitext(currBaseName)[1]
currBaseName = os.path.splitext(currBaseName)[0]
if currExt == ".gz":
currBaseName = os.path.splitext(currBaseName)[0]
currExt = os.path.splitext(currBaseName)[1]
return currBaseName
all_files = T1List
all_files.extend(T2List)
all_files.extend(PDList)
all_files.extend(FLList)
all_files.extend(OtherList)
out_corrected_names = []
for i in all_files:
out_name = GetExtBaseName(i) + "_corrected.nii.gz"
out_corrected_names.append(out_name)
return out_corrected_names
def getListIndexOrNoneIfOutOfRange(imageList, index):
if index < len(imageList):
return imageList[index]
else:
return None
def MakePosteriorDictionaryFunc(posteriorImages):
from PipeLineFunctionHelpers import POSTERIORS
if len(POSTERIORS) != len(posteriorImages):
print "ERROR: ", posteriorNames
print "ERROR: ", POSTERIORS
return -1
temp_dictionary = dict(zip(POSTERIORS, posteriorImages))
return temp_dictionary
def CreateTissueClassifyWorkflow(WFname, CLUSTER_QUEUE, CLUSTER_QUEUE_LONG, InterpolationMode):
tissueClassifyWF = pe.Workflow(name=WFname)
inputsSpec = pe.Node(interface=IdentityInterface(fields=['T1List', 'T2List', 'PDList', 'FLList',
'OtherList', 'T1_count', 'PrimaryT1',
'atlasDefinition',
'atlasToSubjectInitialTransform']),
run_without_submitting=True,
name='inputspec')
outputsSpec = pe.Node(interface=IdentityInterface(fields=['atlasToSubjectTransform',
'atlasToSubjectInverseTransform',
'outputLabels',
'outputHeadLabels', # ???
#'t1_corrected', 't2_corrected',
't1_average',
't2_average',
'pd_average',
'fl_average',
'posteriorImages']),
run_without_submitting=True,
name='outputspec')
########################################################
# Run BABCext on Multi-modal images
########################################################
makeImagePathList = pe.Node(Function(function=MakeOneFileList,
input_names=['T1List', 'T2List', 'PDList', 'FLList', 'OtherList', 'PrimaryT1'],
output_names=['imagePathList']), run_without_submitting=True, name="99_makeImagePathList")
tissueClassifyWF.connect(inputsSpec, 'T1List', makeImagePathList, 'T1List')
tissueClassifyWF.connect(inputsSpec, 'T2List', makeImagePathList, 'T2List')
tissueClassifyWF.connect(inputsSpec, 'PDList', makeImagePathList, 'PDList')
tissueClassifyWF.connect(inputsSpec, 'FLList', makeImagePathList, 'FLList')
tissueClassifyWF.connect(inputsSpec, 'OtherList', makeImagePathList, 'OtherList')
# -- Standard mode to make 256^3 images
tissueClassifyWF.connect(inputsSpec, 'PrimaryT1', makeImagePathList, 'PrimaryT1')
makeImageTypeList = pe.Node(Function(function=MakeOneFileTypeList,
input_names=['T1List', 'T2List', 'PDList', 'FLList', 'OtherList'],
output_names=['imageTypeList']), run_without_submitting=True, name="99_makeImageTypeList")
tissueClassifyWF.connect(inputsSpec, 'T1List', makeImageTypeList, 'T1List')
tissueClassifyWF.connect(inputsSpec, 'T2List', makeImageTypeList, 'T2List')
tissueClassifyWF.connect(inputsSpec, 'PDList', makeImageTypeList, 'PDList')
tissueClassifyWF.connect(inputsSpec, 'FLList', makeImageTypeList, 'FLList')
tissueClassifyWF.connect(inputsSpec, 'OtherList', makeImageTypeList, 'OtherList')
makeOutImageList = pe.Node(Function(function=MakeOutFileList,
input_names=['T1List', 'T2List', 'PDList', 'FLList', 'OtherList'],
output_names=['outImageList']), run_without_submitting=True, name="99_makeOutImageList")
tissueClassifyWF.connect(inputsSpec, 'T1List', makeOutImageList, 'T1List')
tissueClassifyWF.connect(inputsSpec, 'T2List', makeOutImageList, 'T2List')
tissueClassifyWF.connect(inputsSpec, 'PDList', makeOutImageList, 'PDList')
makeOutImageList.inputs.FLList = [] # an emptyList HACK
# HACK tissueClassifyWF.connect( inputsSpec, 'FLList', makeOutImageList, 'FLList' )
tissueClassifyWF.connect(inputsSpec, 'OtherList', makeOutImageList, 'OtherList')
BABCext = pe.Node(interface=BRAINSABCext(), name="BABC")
many_cpu_BABC_options_dictionary = {'qsub_args': '-S /bin/bash -pe smp1 4-4 -l h_vmem=23G,mem_free=8G -o /dev/null -e /dev/null ' + CLUSTER_QUEUE, 'overwrite': True}
BABCext.plugin_args = many_cpu_BABC_options_dictionary
tissueClassifyWF.connect(makeImagePathList, 'imagePathList', BABCext, 'inputVolumes')
tissueClassifyWF.connect(makeImageTypeList, 'imageTypeList', BABCext, 'inputVolumeTypes')
tissueClassifyWF.connect(makeOutImageList, 'outImageList', BABCext, 'outputVolumes')
BABCext.inputs.debuglevel = 0
BABCext.inputs.maxIterations = 3
BABCext.inputs.maxBiasDegree = 4
BABCext.inputs.filterIteration = 3
BABCext.inputs.filterMethod = 'GradientAnisotropicDiffusion'
BABCext.inputs.atlasToSubjectTransformType = 'SyN'
# BABCext.inputs.atlasToSubjectTransformType = 'BSpline'
# BABCext.inputs.gridSize = [28,20,24]
BABCext.inputs.gridSize = [10, 10, 10]
BABCext.inputs.outputFormat = "NIFTI"
BABCext.inputs.outputLabels = "brain_label_seg.nii.gz"
BABCext.inputs.outputDirtyLabels = "volume_label_seg.nii.gz"
BABCext.inputs.posteriorTemplate = "POSTERIOR_%s.nii.gz"
BABCext.inputs.atlasToSubjectTransform = "atlas_to_subject.h5"
# BABCext.inputs.implicitOutputs = ['t1_average_BRAINSABC.nii.gz', 't2_average_BRAINSABC.nii.gz']
BABCext.inputs.interpolationMode = InterpolationMode
BABCext.inputs.outputDir = './'
tissueClassifyWF.connect(inputsSpec, 'atlasDefinition', BABCext, 'atlasDefinition')
tissueClassifyWF.connect(inputsSpec, 'atlasToSubjectInitialTransform', BABCext, 'atlasToSubjectInitialTransform')
"""
Get the first T1 and T2 corrected images from BABCext
"""
""" HACK: THIS IS NOT NEEDED! We should use the averged t1 and averaged t2 images instead!
def get_first_T1_and_T2(in_files,T1_count):
'''
Returns the first T1 and T2 file in in_files, based on offset in T1_count.
'''
return in_files[0],in_files[T1_count]
bfc_files = pe.Node(Function(input_names=['in_files','T1_count'],
output_names=['t1_corrected','t2_corrected'],
function=get_first_T1_and_T2), run_without_submitting=True, name='99_bfc_files' )
tissueClassifyWF.connect( inputsSpec, 'T1_count', bfc_files, 'T1_count')
tissueClassifyWF.connect(BABCext,'outputVolumes',bfc_files, 'in_files')
tissueClassifyWF.connect(bfc_files,'t1_corrected',outputsSpec,'t1_corrected')
tissueClassifyWF.connect(bfc_files,'t2_corrected',outputsSpec,'t2_corrected')
#tissueClassifyWF.connect(bfc_files,'pd_corrected',outputsSpec,'pd_corrected')
#tissueClassifyWF.connect(bfc_files,'fl_corrected',outputsSpec,'fl_corrected')
"""
#############
tissueClassifyWF.connect(BABCext, 'atlasToSubjectTransform', outputsSpec, 'atlasToSubjectTransform')
def MakeInverseTransformFileName(TransformFileName):
"""### HACK: This function is to work around a deficiency in BRAINSABCext where the inverse transform name is not being computed properly
in the list outputs"""
fixed_inverse_name = TransformFileName.replace(".h5", "_Inverse.h5")
return [ fixed_inverse_name ]
tissueClassifyWF.connect([(BABCext, outputsSpec, [(('atlasToSubjectTransform', MakeInverseTransformFileName), "atlasToSubjectInverseTransform")]), ])
tissueClassifyWF.connect(BABCext, 'outputLabels', outputsSpec, 'outputLabels')
tissueClassifyWF.connect(BABCext, 'outputDirtyLabels', outputsSpec, 'outputHeadLabels')
tissueClassifyWF.connect(BABCext, 'outputT1AverageImage', outputsSpec, 't1_average')
tissueClassifyWF.connect(BABCext, 'outputT2AverageImage', outputsSpec, 't2_average')
tissueClassifyWF.connect(BABCext, 'outputPDAverageImage', outputsSpec, 'pd_average')
tissueClassifyWF.connect(BABCext, 'outputFLAverageImage', outputsSpec, 'fl_average')
## remove tissueClassifyWF.connect( [ ( BABCext, outputsSpec, [ (( 'outputAverageImages', getListIndexOrNoneIfOutOfRange, 0 ), "t1_average")] ), ] )
## remove tissueClassifyWF.connect( [ ( BABCext, outputsSpec, [ (( 'outputAverageImages', getListIndexOrNoneIfOutOfRange, 1 ), "t2_average")] ), ] )
## remove tissueClassifyWF.connect( [ ( BABCext, outputsSpec, [ (( 'outputAverageImages', getListIndexOrNoneIfOutOfRange, 2 ), "pd_average")] ), ] )
MakePosteriorDictionaryNode = pe.Node(Function(function=MakePosteriorDictionaryFunc,
input_names=['posteriorImages'],
output_names=['posteriorDictionary']), run_without_submitting=True, name="99_makePosteriorDictionary")
tissueClassifyWF.connect(BABCext, 'posteriorImages', MakePosteriorDictionaryNode, 'posteriorImages')
tissueClassifyWF.connect(MakePosteriorDictionaryNode, 'posteriorDictionary', outputsSpec, 'posteriorImages')
return tissueClassifyWF