@@ -60,8 +60,8 @@ def init_infant_brain_extraction_wf(
60
60
61
61
Inputs
62
62
------
63
- in_t2w : :obj:`str`
64
- The unprocessed input T2w image.
63
+ t2w_preproc : :obj:`str`
64
+ The preprocessed T2w image (Denoising/INU/N4)
65
65
66
66
Outputs
67
67
-------
@@ -87,7 +87,6 @@ def init_infant_brain_extraction_wf(
87
87
BinaryDilation ,
88
88
IntensityClip ,
89
89
)
90
- from templateflow .api import get as get_template
91
90
92
91
from ...utils .misc import cohort_by_months
93
92
@@ -101,41 +100,20 @@ def init_infant_brain_extraction_wf(
101
100
raise KeyError (f"Age or cohort for { skull_strip_template } must be provided!" )
102
101
template_specs ["cohort" ] = cohort_by_months (skull_strip_template , age_months )
103
102
104
- tpl_target_path = get_template (
105
- skull_strip_template ,
106
- suffix = "T1w" , # no T2w template
107
- desc = None ,
108
- ** template_specs ,
109
- )
110
- if not tpl_target_path :
111
- raise RuntimeError (
112
- f"An instance of template <tpl-{ skull_strip_template } > with T1w suffix "
113
- "could not be found."
114
- )
115
-
116
- tpl_brainmask_path = get_template (
117
- skull_strip_template , label = "brain" , suffix = "probseg" , ** template_specs
118
- ) or get_template (skull_strip_template , desc = "brain" , suffix = "mask" , ** template_specs )
119
-
120
- tpl_regmask_path = get_template (
121
- skull_strip_template ,
122
- label = "BrainCerebellumExtraction" ,
123
- suffix = "mask" ,
124
- ** template_specs ,
125
- )
103
+ template_files = fetch_templates (skull_strip_template , template_specs )
126
104
127
105
# main workflow
128
106
workflow = pe .Workflow (name )
129
107
130
- inputnode = pe .Node (niu .IdentityInterface (fields = ["in_t2w " ]), name = "inputnode" )
108
+ inputnode = pe .Node (niu .IdentityInterface (fields = ["t2w_preproc " ]), name = "inputnode" )
131
109
outputnode = pe .Node (
132
- niu .IdentityInterface (fields = ["t2w_preproc" , " t2w_brain" , "out_mask" , "out_probmap" ]),
110
+ niu .IdentityInterface (fields = ["t2w_brain" , "out_mask" , "out_probmap" ]),
133
111
name = "outputnode" ,
134
112
)
135
113
136
114
# Ensure template comes with a range of intensities ANTs will like
137
115
clip_tmpl = pe .Node (IntensityClip (p_max = 99 ), name = "clip_tmpl" )
138
- clip_tmpl .inputs .in_file = _pop (tpl_target_path )
116
+ clip_tmpl .inputs .in_file = _pop (template_files [ 'anat' ] )
139
117
140
118
# Generate laplacian registration targets
141
119
lap_tmpl = pe .Node (ImageMath (operation = "Laplacian" , op2 = "0.4 1" ), name = "lap_tmpl" )
@@ -147,13 +125,15 @@ def init_infant_brain_extraction_wf(
147
125
mrg_tmpl = pe .Node (niu .Merge (2 ), name = "mrg_tmpl" , run_without_submitting = True )
148
126
mrg_t2w = pe .Node (niu .Merge (2 ), name = "mrg_t2w" , run_without_submitting = True )
149
127
bin_regmask = pe .Node (Binarize (thresh_low = 0.20 ), name = "bin_regmask" )
150
- bin_regmask .inputs .in_file = str (tpl_brainmask_path )
128
+ bin_regmask .inputs .in_file = str (template_files [ 'mask' ] )
151
129
refine_mask = pe .Node (BinaryDilation (radius = 3 , iterations = 2 ), name = "refine_mask" )
152
130
153
131
fixed_masks = pe .Node (niu .Merge (4 ), name = "fixed_masks" , run_without_submitting = True )
154
132
fixed_masks .inputs .in1 = "NULL"
155
133
fixed_masks .inputs .in2 = "NULL"
156
- fixed_masks .inputs .in3 = "NULL" if not tpl_regmask_path else _pop (tpl_regmask_path )
134
+ fixed_masks .inputs .in3 = (
135
+ "NULL" if not template_files ['regmask' ] else _pop (template_files ['regmask' ])
136
+ )
157
137
158
138
# Set up initial spatial normalization
159
139
ants_params = "testing" if sloppy else "precise"
@@ -176,7 +156,7 @@ def init_infant_brain_extraction_wf(
176
156
)
177
157
178
158
# map template brainmask to t2w space
179
- map_mask_t2w .inputs .input_image = str (tpl_brainmask_path )
159
+ map_mask_t2w .inputs .input_image = str (template_files [ 'mask' ] )
180
160
181
161
thr_t2w_mask = pe .Node (Binarize (thresh_low = 0.80 ), name = "thr_t2w_mask" )
182
162
@@ -200,7 +180,7 @@ def init_infant_brain_extraction_wf(
200
180
201
181
# fmt:off
202
182
workflow .connect ([
203
- (inputnode , final_n4 , [("in_t2w " , "input_image" )]),
183
+ (inputnode , final_n4 , [("t2w_preproc " , "input_image" )]),
204
184
# 1. Massage T2w
205
185
(inputnode , mrg_t2w , [("in_t2w" , "in1" )]),
206
186
(inputnode , lap_t2w , [("in_t2w" , "op1" )]),
@@ -229,7 +209,7 @@ def init_infant_brain_extraction_wf(
229
209
# 5. Refine T2w INU correction with brain mask
230
210
(map_mask_t2w , final_n4 , [("output_image" , "weight_image" )]),
231
211
(final_n4 , final_clip , [("output_image" , "in_file" )]),
232
- # 9 . Outputs
212
+ # 6 . Outputs
233
213
(final_clip , outputnode , [("out_file" , "t2w_preproc" )]),
234
214
(map_mask_t2w , outputnode , [("output_image" , "out_probmap" )]),
235
215
(thr_t2w_mask , outputnode , [("out_mask" , "out_mask" )]),
@@ -255,8 +235,8 @@ def init_infant_brain_extraction_wf(
255
235
name = "init_aff" ,
256
236
n_procs = omp_nthreads ,
257
237
)
258
- if tpl_regmask_path :
259
- init_aff .inputs .fixed_image_mask = _pop (tpl_regmask_path )
238
+ if template_files [ 'regmask' ] :
239
+ init_aff .inputs .fixed_image_mask = _pop (template_files [ 'regmask' ] )
260
240
261
241
# fmt:off
262
242
workflow .connect ([
@@ -349,3 +329,20 @@ def _norm_lap(in_file):
349
329
hdr .set_data_dtype ("float32" )
350
330
img .__class__ (data .astype ("float32" ), img .affine , hdr ).to_filename (out_file )
351
331
return out_file
332
+
333
+
334
+ def fetch_templates (template : str , specs : dict ) -> dict :
335
+ from templateflow .api import get
336
+
337
+ template_files = {}
338
+ # Anatomical reference
339
+ template_files ['anat' ] = get (template , suffix = "T1w" , desc = None , raise_empty = True , ** specs )
340
+ # Anatomical mask, prefer probseg if available
341
+ template_files ['mask' ] = get (template , label = "brain" , suffix = "probseg" , ** specs ) or get (
342
+ template , desc = "brain" , suffix = "mask" , ** specs
343
+ )
344
+ # More dilated mask to facilitate registration
345
+ template_files ['regmask' ] = get (
346
+ template , label = "BrainCerebellumExtraction" , suffix = "mask" , ** specs
347
+ )
348
+ return template_files
0 commit comments