Skip to content

Commit 5890267

Browse files
authored
Merge pull request #400 from mgxd/fix/precomputed-mask-no-preproc
FIX: Do not force masking of anatomicals when using `--derivatives`
2 parents 25deabb + bc6fe17 commit 5890267

File tree

2 files changed

+23
-54
lines changed

2 files changed

+23
-54
lines changed

nibabies/workflows/anatomical/fit.py

Lines changed: 21 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ def init_infant_anat_fit_wf(
554554
workflow.connect([
555555
(t1w_buffer, ds_t1w_mask_wf, [('t1w_mask', 'inputnode.mask_file')]),
556556
]) # fmt:skip
557-
else:
557+
else: # T1w mask derivative has been found
558558
LOGGER.info('ANAT Found T1w brain mask')
559559
if reference_anat == 'T1w':
560560
desc += (
@@ -563,26 +563,19 @@ def init_infant_anat_fit_wf(
563563
)
564564
t1w_buffer.inputs.t1w_mask = t1w_mask
565565
apply_t1w_mask.inputs.in_mask = t1w_mask
566-
workflow.connect(t1w_validate, 'out_file', apply_t1w_mask, 'in_file')
566+
workflow.connect(apply_t1w_mask, 'out_file', t1w_buffer, 't1w_brain')
567567

568568
if not t1w_preproc:
569569
LOGGER.info('ANAT Skipping skull-strip, INU-correction only')
570-
t1w_n4_only_wf = init_n4_only_wf(
571-
omp_nthreads=omp_nthreads,
572-
atropos_use_random_seed=not skull_strip_fixed_seed,
573-
bids_suffix='T1w',
574-
name='t1w_n4_only_wf',
575-
)
570+
t1w_n4_wf = init_anat_preproc_wf(name='t1w_n4_wf')
576571
workflow.connect([
577-
(apply_t1w_mask, t1w_n4_only_wf, [('out_file', 'inputnode.in_files')]),
578-
(t1w_n4_only_wf, t1w_buffer, [
579-
(('outputnode.bias_corrected', pop_file), 't1w_preproc'),
580-
(('outputnode.out_file', pop_file), 't1w_brain'),
581-
]),
572+
(t1w_validate, t1w_n4_wf, [('out_file', 'inputnode.in_anat')]),
573+
(t1w_n4_wf, t1w_buffer, [('outputnode.anat_preproc', 't1w_preproc')]),
574+
(t1w_n4_wf, apply_t1w_mask, [('outputnode.anat_preproc', 'in_file')]),
582575
]) # fmt:skip
583576
else:
584577
LOGGER.info('ANAT Skipping T1w masking')
585-
workflow.connect(apply_t1w_mask, 'out_file', t1w_buffer, 't1w_brain')
578+
workflow.connect(t1w_validate, 'out_file', apply_t1w_mask, 'in_file')
586579

587580
# T2w masking logic:
588581
#
@@ -709,33 +702,24 @@ def init_infant_anat_fit_wf(
709702
LOGGER.info('ANAT Found T2w brain mask')
710703
if reference_anat == 'T2w':
711704
desc += (
712-
'A pre-computed T1w brain mask was provided as input and used throughout the '
705+
'A pre-computed T2w brain mask was provided as input and used throughout the '
713706
'workflow.'
714707
)
715708
t2w_buffer.inputs.t2w_mask = t2w_mask
716709
apply_t2w_mask.inputs.in_mask = t2w_mask
717-
workflow.connect([
718-
(t2w_validate, apply_t2w_mask, [('out_file', 'in_file')]),
719-
]) # fmt:skip
710+
workflow.connect(apply_t2w_mask, 'out_file', t2w_buffer, 't2w_brain')
720711

721712
if not t2w_preproc:
722713
LOGGER.info('ANAT Skipping skull-strip, INU-correction only')
723-
t2w_n4_only_wf = init_n4_only_wf(
724-
omp_nthreads=omp_nthreads,
725-
atropos_use_random_seed=not skull_strip_fixed_seed,
726-
bids_suffix='T2w',
727-
name='t2w_n4_only_wf',
728-
)
714+
t2w_n4_wf = init_anat_preproc_wf(name='t2w_n4_wf')
729715
workflow.connect([
730-
(apply_t2w_mask, t2w_n4_only_wf, [('out_file', 'inputnode.in_files')]),
731-
(t2w_n4_only_wf, t2w_buffer, [
732-
(('outputnode.bias_corrected', pop_file), 't2w_preproc'),
733-
(('outputnode.out_file', pop_file), 't2w_brain'),
734-
]),
716+
(t2w_validate, t2w_n4_wf, [('out_file', 'inputnode.in_anat')]),
717+
(t2w_n4_wf, t2w_buffer, [('outputnode.anat_preproc', 't2w_preproc')]),
718+
(t2w_n4_wf, apply_t2w_mask, [('outputnode.anat_preproc', 'in_file')]),
735719
]) # fmt:skip
736720
else:
737721
LOGGER.info('ANAT Skipping T2w masking')
738-
workflow.connect(apply_t2w_mask, 'out_file', t2w_buffer, 't2w_brain')
722+
workflow.connect(t2w_validate, 'out_file', apply_t2w_mask, 'in_file')
739723

740724
# Stage 3: Coregistration
741725
t1w2t2w_xfm = precomputed.get('t1w2t2w_xfm')
@@ -1643,28 +1627,19 @@ def init_infant_single_anat_fit_wf(
16431627
desc += 'A pre-computed brain mask was provided as input and used throughout the workflow.'
16441628
anat_buffer.inputs.anat_mask = anat_mask
16451629
apply_mask.inputs.in_mask = anat_mask
1646-
workflow.connect([
1647-
(anat_validate, apply_mask, [('out_file', 'in_file')]),
1648-
]) # fmt:skip
1630+
workflow.connect(apply_mask, 'out_file', anat_buffer, 'anat_brain')
16491631

16501632
if not anat_preproc:
16511633
LOGGER.info('ANAT Skipping skull-strip, INU-correction only')
1652-
n4_only_wf = init_n4_only_wf(
1653-
omp_nthreads=omp_nthreads,
1654-
atropos_use_random_seed=not skull_strip_fixed_seed,
1655-
bids_suffix=reference_anat,
1656-
name='n4_only_wf',
1657-
)
1634+
anat_n4_wf = init_anat_preproc_wf(name='anat_n4_wf')
16581635
workflow.connect([
1659-
(apply_mask, n4_only_wf, [('out_file', 'inputnode.in_files')]),
1660-
(n4_only_wf, anat_buffer, [
1661-
(('outputnode.bias_corrected', pop_file), 'anat_preproc'),
1662-
(('outputnode.out_file', pop_file), 'anat_brain'),
1663-
]),
1636+
(anat_validate, anat_n4_wf, [('out_file', 'inputnode.in_anat')]),
1637+
(anat_n4_wf, anat_buffer, [('outputnode.anat_preproc', 'anat_preproc')]),
1638+
(anat_n4_wf, apply_mask, [('outputnode.anat_preproc', 'in_file')]),
16641639
]) # fmt:skip
16651640
else:
1666-
LOGGER.info('ANAT Skipping T2w masking')
1667-
workflow.connect(apply_mask, 'out_file', anat_buffer, 'anat_brain')
1641+
LOGGER.info(f'ANAT Skipping {reference_anat} masking')
1642+
workflow.connect(anat_validate, 'out_file', apply_mask, 'in_file')
16681643

16691644
# Stage 3: Segmentation
16701645
seg_method = 'jlf' if config.execution.segmentation_atlases_dir else 'fast'

nibabies/workflows/anatomical/preproc.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ def init_anat_preproc_wf(
1212
1313
This workflow accepts T1w/T2w images as inputs (either raw or a merged template) and performs:
1414
- Intensity clipping
15-
- Denoising
1615
- N4 Bias Field Correction
1716
18-
The outputs of this workflow will be used to either create the brainmask,
19-
or reconstruct the cortical surfaces.
17+
The outputs of this workflow will be a structural reference used for subsequent processing.
2018
2119
Inputs
2220
------
@@ -45,7 +43,6 @@ def init_anat_preproc_wf(
4543
# validate image
4644
validate = pe.Node(ValidateImage(), name='anat_validate', run_without_submitting=True)
4745
clip = pe.Node(IntensityClip(p_min=10.0, p_max=99.5), name='clip')
48-
# denoise = pe.Node(DenoiseImage(dimension=3, noise_model="Rician"), name="denoise")
4946
n4_correct = pe.Node(
5047
N4BiasFieldCorrection(
5148
dimension=3,
@@ -61,14 +58,11 @@ def init_anat_preproc_wf(
6158
)
6259
final_clip = pe.Node(IntensityClip(p_min=5.0, p_max=99.5), name='final_clip')
6360

64-
# fmt:off
6561
wf.connect([
6662
(inputnode, validate, [('in_anat', 'in_file')]),
6763
(validate, clip, [('out_file', 'in_file')]),
6864
(clip, n4_correct, [('out_file', 'input_image')]),
69-
# (denoise, n4_correct, [("output_image", "input_image")]),
7065
(n4_correct, final_clip, [('output_image', 'in_file')]),
7166
(final_clip, outputnode, [('out_file', 'anat_preproc')]),
72-
])
73-
# fmt:on
67+
]) # fmt:skip
7468
return wf

0 commit comments

Comments
 (0)