Skip to content

Commit 6bec246

Browse files
committed
rf(brain_extraction): Conditionally run inu_n4_final
1 parent 1f44132 commit 6bec246

File tree

1 file changed

+47
-52
lines changed

1 file changed

+47
-52
lines changed

niworkflows/anat/ants.py

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -334,37 +334,8 @@ def init_brain_extraction_wf(
334334
name='thr_brainmask',
335335
)
336336

337-
# Refine INU correction
338-
inu_n4_final = pe.MapNode(
339-
N4BiasFieldCorrection(
340-
dimension=3,
341-
save_bias=True,
342-
copy_header=True,
343-
n_iterations=[50] * 5,
344-
convergence_threshold=1e-7,
345-
shrink_factor=4,
346-
bspline_fitting_distance=bspline_fitting_distance,
347-
),
348-
n_procs=omp_nthreads,
349-
name='inu_n4_final',
350-
iterfield=['input_image'],
351-
)
352-
try:
353-
inu_n4_final.inputs.rescale_intensities = True
354-
except ValueError:
355-
warn(
356-
"N4BiasFieldCorrection's --rescale-intensities option was added in ANTS 2.1.0 "
357-
f'({inu_n4_final.interface.version} found.) Please consider upgrading.',
358-
UserWarning,
359-
stacklevel=1,
360-
)
361-
362-
# Apply mask
363-
apply_mask = pe.MapNode(ApplyMask(), iterfield=['in_file'], name='apply_mask')
364-
365337
wf.connect([
366338
(inputnode, trunc, [('in_files', 'op1')]),
367-
(inputnode, inu_n4_final, [('in_files', 'input_image')]),
368339
(inputnode, init_aff, [('in_mask', 'fixed_image_mask')]),
369340
(inputnode, norm, [('in_mask', fixed_mask_trait)]),
370341
(inputnode, map_brainmask, [(('in_files', _pop), 'reference_image')]),
@@ -378,13 +349,6 @@ def init_brain_extraction_wf(
378349
('reverse_invert_flags', 'invert_transform_flags'),
379350
]),
380351
(map_brainmask, thr_brainmask, [('output_image', 'input_image')]),
381-
(map_brainmask, inu_n4_final, [('output_image', 'weight_image')]),
382-
(inu_n4_final, apply_mask, [('output_image', 'in_file')]),
383-
(thr_brainmask, apply_mask, [('output_image', 'in_mask')]),
384-
(thr_brainmask, outputnode, [('output_image', 'out_mask')]),
385-
(inu_n4_final, outputnode, [('output_image', 'bias_corrected'),
386-
('bias_image', 'bias_image')]),
387-
(apply_mask, outputnode, [('out_file', 'out_file')]),
388352
]) # fmt:skip
389353

390354
wm_tpm = get_template(in_template, label='WM', suffix='probseg', **common_spec) or None
@@ -401,21 +365,16 @@ def init_brain_extraction_wf(
401365
full_wm = pe.Node(niu.Function(function=_imsum), name='full_wm')
402366
full_wm.inputs.op1 = str(wm_tpm)
403367
full_wm.inputs.op2 = str(bstem_tpm)
404-
wf.connect([
405-
(full_wm, map_wmmask, [('out', 'input_image')])
406-
]) # fmt:skip
368+
wf.connect([(full_wm, map_wmmask, [('out', 'input_image')])])
407369
else:
408370
map_wmmask.inputs.input_image = str(wm_tpm)
409-
wf.disconnect([
410-
(map_brainmask, inu_n4_final, [('output_image', 'weight_image')]),
411-
]) # fmt:skip
371+
412372
wf.connect([
413373
(inputnode, map_wmmask, [(('in_files', _pop), 'reference_image')]),
414374
(norm, map_wmmask, [
415375
('reverse_transforms', 'transforms'),
416376
('reverse_invert_flags', 'invert_transform_flags'),
417377
]),
418-
(map_wmmask, inu_n4_final, [('output_image', 'weight_image')]),
419378
]) # fmt:skip
420379

421380
if use_laplacian:
@@ -457,12 +416,6 @@ def init_brain_extraction_wf(
457416
wm_prior=bool(wm_tpm),
458417
)
459418

460-
wf.disconnect([
461-
(thr_brainmask, outputnode, [('output_image', 'out_mask')]),
462-
(inu_n4_final, outputnode, [('output_image', 'bias_corrected'),
463-
('bias_image', 'bias_image')]),
464-
(apply_mask, outputnode, [('out_file', 'out_file')]),
465-
]) # fmt:skip
466419
wf.connect([
467420
(inputnode, atropos_wf, [('in_files', 'inputnode.in_files')]),
468421
(inu_n4, atropos_wf, [('output_image', 'inputnode.in_corrected')]),
@@ -477,9 +430,51 @@ def init_brain_extraction_wf(
477430
]),
478431
]) # fmt:skip
479432
if wm_tpm:
480-
wf.connect([
481-
(map_wmmask, atropos_wf, [('output_image', 'inputnode.wm_prior')]),
482-
]) # fmt:skip
433+
wf.connect([(map_wmmask, atropos_wf, [('output_image', 'inputnode.wm_prior')])])
434+
else:
435+
# If no Atropos refinement, rerun N4 with the brain or white matter mask
436+
inu_n4_final = pe.MapNode(
437+
N4BiasFieldCorrection(
438+
dimension=3,
439+
save_bias=True,
440+
copy_header=True,
441+
n_iterations=[50] * 5,
442+
convergence_threshold=1e-7,
443+
shrink_factor=4,
444+
bspline_fitting_distance=bspline_fitting_distance,
445+
),
446+
n_procs=omp_nthreads,
447+
name='inu_n4_final',
448+
iterfield=['input_image'],
449+
)
450+
451+
# Check ANTs version
452+
try:
453+
inu_n4_final.inputs.rescale_intensities = True
454+
except ValueError:
455+
warn(
456+
"N4BiasFieldCorrection's --rescale-intensities option was added in ANTS 2.1.0 "
457+
f'({inu_n4.interface.version} found.) Please consider upgrading.',
458+
UserWarning,
459+
stacklevel=1,
460+
)
461+
462+
# Apply mask
463+
apply_mask = pe.MapNode(ApplyMask(), iterfield=['in_file'], name='apply_mask')
464+
465+
wf.connect([
466+
(inputnode, inu_n4_final, [('in_files', 'input_image')]),
467+
(map_wmmask if wm_tpm else map_brainmask, inu_n4_final, [
468+
('output_image', 'weight_image'),
469+
]),
470+
(inu_n4_final, apply_mask, [('output_image', 'in_file')]),
471+
(thr_brainmask, apply_mask, [('output_image', 'in_mask')]),
472+
(thr_brainmask, outputnode, [('output_image', 'out_mask')]),
473+
(inu_n4_final, outputnode, [('output_image', 'bias_corrected'),
474+
('bias_image', 'bias_image')]),
475+
(apply_mask, outputnode, [('out_file', 'out_file')]),
476+
]) # fmt:skip
477+
483478
return wf
484479

485480

0 commit comments

Comments
 (0)