Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Address some reliability issues of the functional masking workflow #707

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion niworkflows/data/epi_atlasbased_brainmask.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"winsorize_upper_quantile": 0.98,
"winsorize_lower_quantile": 0.05,
"float": true,
"metric": ["Mattes"],
"metric_weight": [1],
"radius_or_number_of_bins": [64],
Expand Down
63 changes: 21 additions & 42 deletions niworkflows/func/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ def init_enhance_and_skullstrip_bold_wf(
reportlet for the skull-stripping

"""
from niworkflows.interfaces.nibabel import ApplyMask, BinaryDilation

workflow = Workflow(name=name)
inputnode = pe.Node(
niu.IdentityInterface(fields=["in_file", "pre_mask"]), name="inputnode"
Expand All @@ -374,20 +376,6 @@ def init_enhance_and_skullstrip_bold_wf(
name="outputnode",
)

# Dilate pre_mask
pre_dilate = pe.Node(
fsl.DilateImage(
operation="max",
kernel_shape="sphere",
kernel_size=3.0,
internal_datatype="char",
),
name="pre_mask_dilate",
)

# Ensure mask's header matches reference's
check_hdr = pe.Node(MatchHeader(), name="check_hdr", run_without_submitting=True)

# Run N4 normally, force num_threads=1 for stability (images are small, no need for >1)
n4_correct = pe.Node(
N4BiasFieldCorrection(
Expand All @@ -403,16 +391,8 @@ def init_enhance_and_skullstrip_bold_wf(
skullstrip_first_pass = pe.Node(
fsl.BET(frac=0.2, mask=True), name="skullstrip_first_pass"
)
bet_dilate = pe.Node(
fsl.DilateImage(
operation="max",
kernel_shape="sphere",
kernel_size=6.0,
internal_datatype="char",
),
name="skullstrip_first_dilate",
)
bet_mask = pe.Node(fsl.ApplyMask(), name="skullstrip_first_mask")
first_dilate = pe.Node(BinaryDilation(radius=6), name="first_dilate")
first_mask = pe.Node(ApplyMask(), name="first_mask")

# Use AFNI's unifize for T2 constrast & fix header
unifize = pe.Node(
Expand All @@ -438,11 +418,10 @@ def init_enhance_and_skullstrip_bold_wf(
combine_masks = pe.Node(fsl.BinaryMaths(operation="mul"), name="combine_masks")

# Compute masked brain
apply_mask = pe.Node(fsl.ApplyMask(), name="apply_mask")
apply_mask = pe.Node(ApplyMask(), name="apply_mask")

if not pre_mask:
from nipype.interfaces.ants.utils import AI
from ..interfaces.nibabel import Binarize

bold_template = get_template(
"MNI152NLin2009cAsym", resolution=2, desc="fMRIPrep", suffix="boldref"
Expand Down Expand Up @@ -482,8 +461,7 @@ def init_enhance_and_skullstrip_bold_wf(
norm.inputs.fixed_image = str(bold_template)
map_brainmask = pe.Node(
ApplyTransforms(
interpolation="BSpline",
float=True,
interpolation="Linear",
# Use the higher resolution and probseg for numerical stability in rounding
input_image=str(
get_template(
Expand All @@ -496,10 +474,12 @@ def init_enhance_and_skullstrip_bold_wf(
),
name="map_brainmask",
)
binarize_mask = pe.Node(Binarize(thresh_low=brainmask_thresh), name="binarize_mask")
# Ensure mask's header matches reference's
check_hdr = pe.Node(MatchHeader(), name="check_hdr", run_without_submitting=True)

# fmt: off
workflow.connect([
(inputnode, check_hdr, [("in_file", "reference")]),
(inputnode, init_aff, [("in_file", "moving_image")]),
(inputnode, map_brainmask, [("in_file", "reference_image")]),
(inputnode, norm, [("in_file", "moving_image")]),
Expand All @@ -508,37 +488,34 @@ def init_enhance_and_skullstrip_bold_wf(
("reverse_invert_flags", "invert_transform_flags"),
("reverse_transforms", "transforms"),
]),
(map_brainmask, binarize_mask, [("output_image", "in_file")]),
(binarize_mask, pre_dilate, [("out_mask", "in_file")]),
(map_brainmask, check_hdr, [("output_image", "in_file")]),
(check_hdr, n4_correct, [("out_file", "weight_image")]),
])
# fmt: on
else:
# fmt: off
workflow.connect([
(inputnode, pre_dilate, [("pre_mask", "in_file")]),
(inputnode, n4_correct, [("pre_mask", "weight_image")]),
])
# fmt: on

# fmt: off
workflow.connect([
(inputnode, check_hdr, [("in_file", "reference")]),
(pre_dilate, check_hdr, [("out_file", "in_file")]),
(check_hdr, n4_correct, [("out_file", "mask_image")]),
(inputnode, n4_correct, [("in_file", "input_image")]),
(inputnode, fixhdr_unifize, [("in_file", "hdr_file")]),
(inputnode, fixhdr_skullstrip2, [("in_file", "hdr_file")]),
(n4_correct, skullstrip_first_pass, [("output_image", "in_file")]),
(skullstrip_first_pass, bet_dilate, [("mask_file", "in_file")]),
(bet_dilate, bet_mask, [("out_file", "mask_file")]),
(skullstrip_first_pass, bet_mask, [("out_file", "in_file")]),
(bet_mask, unifize, [("out_file", "in_file")]),
(skullstrip_first_pass, first_dilate, [("mask_file", "in_file")]),
(first_dilate, first_mask, [("out_file", "in_mask")]),
(skullstrip_first_pass, first_mask, [("out_file", "in_file")]),
(first_mask, unifize, [("out_file", "in_file")]),
(unifize, fixhdr_unifize, [("out_file", "in_file")]),
(fixhdr_unifize, skullstrip_second_pass, [("out_file", "in_file")]),
(skullstrip_first_pass, combine_masks, [("mask_file", "in_file")]),
(skullstrip_second_pass, fixhdr_skullstrip2, [("out_file", "in_file")]),
(fixhdr_skullstrip2, combine_masks, [("out_file", "operand_file")]),
(fixhdr_unifize, apply_mask, [("out_file", "in_file")]),
(combine_masks, apply_mask, [("out_file", "mask_file")]),
(combine_masks, apply_mask, [("out_file", "in_mask")]),
(combine_masks, outputnode, [("out_file", "mask_file")]),
(apply_mask, outputnode, [("out_file", "skull_stripped_file")]),
(n4_correct, outputnode, [("output_image", "bias_corrected_file")]),
Expand Down Expand Up @@ -580,6 +557,8 @@ def init_skullstrip_bold_wf(name="skullstrip_bold_wf"):
reportlet for the skull-stripping

"""
from niworkflows.interfaces.nibabel import ApplyMask

workflow = Workflow(name=name)
inputnode = pe.Node(niu.IdentityInterface(fields=["in_file"]), name="inputnode")
outputnode = pe.Node(
Expand All @@ -595,7 +574,7 @@ def init_skullstrip_bold_wf(name="skullstrip_bold_wf"):
afni.Automask(dilate=1, outputtype="NIFTI_GZ"), name="skullstrip_second_pass"
)
combine_masks = pe.Node(fsl.BinaryMaths(operation="mul"), name="combine_masks")
apply_mask = pe.Node(fsl.ApplyMask(), name="apply_mask")
apply_mask = pe.Node(ApplyMask(), name="apply_mask")
mask_reportlet = pe.Node(SimpleShowMaskRPT(), name="mask_reportlet")

# fmt: off
Expand All @@ -607,7 +586,7 @@ def init_skullstrip_bold_wf(name="skullstrip_bold_wf"):
(combine_masks, outputnode, [("out_file", "mask_file")]),
# Masked file
(inputnode, apply_mask, [("in_file", "in_file")]),
(combine_masks, apply_mask, [("out_file", "mask_file")]),
(combine_masks, apply_mask, [("out_file", "in_mask")]),
(apply_mask, outputnode, [("out_file", "skull_stripped_file")]),
# Reportlet
(inputnode, mask_reportlet, [("in_file", "background_file")]),
Expand Down