forked from NIST-MNI/nist_mni_pipelines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iplT1Preprocessing.py
314 lines (249 loc) · 12 KB
/
iplT1Preprocessing.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @author Daniel, Vladimir S. FONOV
# @date 10/07/2011
version = '1.0'
#
# Preprocessing functions
# - mri 2 tal
# - n3 correction
# - denoising
# - vol pol
from optparse import OptionParser # to change when python updates in the machines for argparse
from optparse import OptionGroup # to change when python updates in the machines for argparse
from ipl.minc_tools import mincTools,mincError
from iplGeneral import *
from iplPatient import *
import shutil
# Run preprocessing using patient info
# - Function to read info from the pipeline patient
# - pipeline_version is employed to select the correct version of the pipeline
def pipeline_t1preprocessing(patient, tp):
# checking if processing was performed
if os.path.exists(patient[tp].qc_jpg['stx_t1']) \
and os.path.exists(patient[tp].clp['t1']) \
and os.path.exists(patient[tp].stx_xfm['t1']) \
and os.path.exists(patient[tp].stx_mnc['t1']) \
and os.path.exists(patient[tp].stx_ns_xfm['t1']) \
and os.path.exists(patient[tp].stx_ns_mnc['t1']):
print ' -- pipeline_t1preprocessing was already performed'
else:
# # Run the appropiate version
t1preprocessing_v10(patient, tp)
# Writing QC images
# #####################
# qc stx registration
modeloutline = patient.modeldir + os.sep + patient.modelname + '_outline.mnc'
with mincTools( ) as minc:
minc.qc(
patient[tp].stx_mnc['t1'],
patient[tp].qc_jpg['stx_t1'],
title=patient[tp].qc_title,
image_range=[0, 120],
mask=modeloutline,
big=True,
clamp=True,
)
return True
def t1preprocessing_v10(patient, tp):
# # processing data
# ##################
with mincTools() as minc:
tmpt1 = minc.tmp('float_t1.mnc')
tmpmask = minc.tmp('mask_t1.mnc')
tmpn3 = minc.tmp('n3_t1.mnc')
tmpstats = minc.tmp('volpol_t1.stats')
tmpxfm = minc.tmp('stx_t1.xfm')
tmpnlm = minc.tmp('nlm_t1.mnc')
modelt1 = patient.modeldir + os.sep + patient.modelname + '.mnc'
modelmask = patient.modeldir + os.sep + patient.modelname + '_mask.mnc'
init_xfm = None
if 'stx_t1' in patient[tp].manual \
and os.path.exists(patient[tp].manual['stx_t1']):
init_xfm = patient[tp].manual['stx_t1']
# Manual clp t1
if 'clp_t1' in patient[tp].manual \
and os.path.exists(patient[tp].manual['clp_t1']):
shutil.copyfile(patient[tp].manual['clp_t1'], patient[tp].clp['t1'])
tmpt1 = patient[tp].clp['t1'] # In order to make the registration if needed
if not os.path.exists( patient[tp].clp['t1'] ):
minc.reshape( patient[tp].native['t1'], tmpt1 )
for s in ['xspace', 'yspace', 'zspace']:
spacing = minc.query_attribute( tmpt1, s + ':spacing' )
if spacing.count( 'irregular' ):
minc.set_attribute( tmpt1, s + ':spacing', 'regular__' )
# 3. denoise
if patient.denoise:
minc.nlm( tmpt1, tmpnlm, beta=0.7 ) # TODO: maybe USE anlm sometimes?
else:
tmpnlm = tmpt1
if not os.path.exists( patient[tp].clp['t1'] ) \
or not os.path.exists( patient[tp].nuc['t1']):
if patient.n4:
minc.winsorize_intensity(tmpt1,minc.tmp('trunc_t1.mnc'))
minc.binary_morphology(minc.tmp('trunc_t1.mnc'),'',minc.tmp('otsu_t1.mnc'),binarize_bimodal=True)
minc.defrag(minc.tmp('otsu_t1.mnc'),minc.tmp('otsu_defrag_t1.mnc'))
minc.autocrop(minc.tmp('otsu_defrag_t1.mnc'),minc.tmp('otsu_defrag_expanded_t1.mnc'),isoexpand='50mm')
minc.binary_morphology(minc.tmp('otsu_defrag_expanded_t1.mnc'),'D[25] E[25]',minc.tmp('otsu_expanded_closed_t1.mnc'))
minc.resample_labels(minc.tmp('otsu_expanded_closed_t1.mnc'),minc.tmp('otsu_closed_t1.mnc'),like=minc.tmp('trunc_t1.mnc'))
minc.calc([minc.tmp('trunc_t1.mnc'),minc.tmp('otsu_closed_t1.mnc')], 'A[0]*A[1]', minc.tmp('trunc_masked_t1.mnc'))
minc.calc([tmpt1,minc.tmp('otsu_closed_t1.mnc')],'A[0]*A[1]' ,minc.tmp('masked_t1.mnc'))
minc.linear_register( minc.tmp('trunc_masked_t1.mnc'), modelt1, tmpxfm,
init_xfm=init_xfm,
objective='-nmi', conf=patient.linreg )
minc.resample_labels( modelmask, minc.tmp('brainmask_t1.mnc'),
transform=tmpxfm, invert_transform=True,
like=minc.tmp('otsu_defrag_t1.mnc') )
minc.calc([minc.tmp('otsu_defrag_t1.mnc'),minc.tmp('brainmask_t1.mnc')],'A[0]*A[1]',minc.tmp('weightmask_t1.mnc'))
dist=200
if patient.mri3T: dist=50 # ??
minc.n4(minc.tmp('masked_t1.mnc'),
output_field=patient[tp].nuc['t1'],
output_corr=tmpn3,
iter='200x200x200x200',
weight_mask=minc.tmp('weightmask_t1.mnc'),
mask=minc.tmp('otsu_closed_t1.mnc'),
distance=dist,
downsample_field=4,
datatype='short'
)
# shrink?
minc.volume_pol(
tmpn3,
modelt1,
patient[tp].clp['t1'],
source_mask=minc.tmp('weightmask_t1.mnc'),
target_mask=modelmask,
datatype='-short',
)
elif patient.mask_n3:
# 2. Reformat mask
minc.linear_register( tmpt1, modelt1, tmpxfm,
init_xfm=init_xfm,
objective='-nmi',
conf=patient.linreg )
minc.resample_labels( modelmask, tmpmask,
transform=tmpxfm, invert_transform=True,
like=tmpnlm )
minc.nu_correct( tmpnlm, output_image=tmpn3,
mask=tmpmask,
mri3t=patient.mri3T,
output_field=patient[tp].nuc['t1'],
downsample_field=4,
datatype='short')
minc.volume_pol(
tmpn3,
modelt1,
patient[tp].clp['t1'],
source_mask=tmpmask,
target_mask=modelmask,
datatype='-short',
)
else:
minc.nu_correct( tmpnlm,
output_image=tmpn3,
mri3t=patient.mri3T,
output_field=patient[tp].nuc['t1'],
downsample_field=4,
datatype='short')
minc.volume_pol( tmpn3, modelt1, patient[tp].clp['t1'],
datatype='-short' )
# register to the stx space
t1_corr = patient[tp].clp['t1']
if 't1' in patient[tp].geo and patient.geo_corr:
t1_corr = patient[tp].corr['t1'] #TODO: avoid double resampling for the output!
minc.resample_smooth( patient[tp].clp['t1'],
t1_corr,
transform=patient[tp].geo['t1'] )
if not os.path.exists( patient[tp].stx_xfm['t1']):
minc.linear_register( t1_corr, modelt1,
patient[tp].stx_xfm['t1'],
init_xfm=init_xfm,
objective='-nmi',
conf=patient.linreg)
# target_mask=modelmask
minc.resample_smooth( t1_corr,
patient[tp].stx_mnc['t1'], like=modelt1,
transform=patient[tp].stx_xfm['t1'] )
# stx no scale
minc.xfm_noscale( patient[tp].stx_xfm['t1'],
patient[tp].stx_ns_xfm['t1'])
minc.resample_smooth(t1_corr,
patient[tp].stx_ns_mnc['t1'],
like=modelt1,
transform=patient[tp].stx_ns_xfm['t1'])
if __name__ == '__main__':
# We can create this as a stand alone script
# 1. Create a patient
# 2. Fill necessary images from the options (inputs and outputs)
# 3. Call the function -> preprocessing_v10
# 4. Exit
# Here a
# Using script as a stand-alone script
# copy output files into the structure!
usage = \
"""usage: %prog <patient id> <patinet visit> <t1.mnc> -o <outputdir>
or: %prog -h
The list have this structure:
anatomical_scan.mnc[,mask.mnc]
"""
parser = OptionParser(usage=usage, version=version)
group = OptionGroup(parser, ' -- Mandatory options ', ' Necessary')
group.add_option('-o', '--output-dir', dest='output',
help='Output dir')
parser.add_option_group(group)
group = OptionGroup(parser, ' -- Pipeline options ',
' Options to start processing')
group.add_option(
'-D',
'--denoise',
dest='denoise',
help='Denoise first images',
action='store_true',
default=False,
)
group.add_option('-3', '--3T', dest='mri3T',
help='Parameters for 3T scans', action='store_true',default=False
)
group.add_option('','--n4', dest='n4',
help='Use Devenyi strategy for preprocessing with N4', action='store_true', default=False
)
group.add_option('', '--model-dir', dest='modeldir',
help='Directory with the model [%default]',
default='/ipl/quarantine/models/icbm152_model_09c/'
)
group.add_option('', '--model-name', dest='modelname',
help='Model name',
default='mni_icbm152_t1_tal_nlin_sym_09c')
group.add_option('-f', '--fast', dest='fast',
help='Fast mode : quick & dirty mostly for testing pipeline'
, action='store_true',default=False)
group.add_option('--resample', dest='resample',
help='Resample algorithm: itk (b-spline 4th order), sinc, linear [%default]'
, default='itk')
parser.add_option_group(group)
(opts, args) = parser.parse_args()
if opts.output is None:
print ' -- Please specify and output dir (-o)'
sys.exit(1)
(id, visit, t1w) = args
print ' -- Copying data to patient structure!'
patient = LngPatient(id)
patient.pipeline_version = version
patient.denoise = opts.denoise
patient.n4 = opts.n4
patient.mri3T = opts.mri3T
patient.fast = opts.fast
patient.modeldir = opts.modeldir
patient.modelname = opts.modelname
patient.patientdir = opts.output + os.sep + id + os.sep
patient.logfile = patient.patientdir + id + '.log'
patient.cmdfile = patient.patientdir + id + '.commands'
patient[visit] = TP(visit)
patient[visit].tpdir = patient.patientdir + visit + os.sep
patient[visit].qc_title = id + '_' + visit
patient[visit].native['t1'] = t1w
setFilenames(patient)
pipeline_t1preprocessing(patient, visit)
# kate: space-indent on; indent-width 4; indent-mode python;replace-tabs on;word-wrap-column 80;show-tabs on