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 #711

Closed
wants to merge 7 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
7 changes: 1 addition & 6 deletions .zenodo.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

{
"title": "NIWorkflows: NeuroImaging Workflows",
"description": "<p>A collection of Nipype interfaces, patches and workflows for reuse in NIPreps (NeuroImaging PREProcesssing tools) such as fMRIPrep.</p>",
Expand Down Expand Up @@ -85,12 +86,6 @@
"name": "de Hollander, Gilles",
"orcid": "0000-0003-1988-5091",
"type": "Researcher"
},
{
"affiliation": "Sagol School of Neuroscience, Tel Aviv University",
"name": "Ben-Zvi, Gal",
"orcid": "0000-0002-5655-9423",
"type": "Researcher"
}
],
"keywords": [
Expand Down
56 changes: 0 additions & 56 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,59 +1,3 @@
1.5.4 (April 08, 2022)
======================
Bug-fix release in the 1.5.x series.

This change enables a bug-fix in sdcflows where images with slightly
different affines were unable to be concatenated for use with TOPUP.

* ENH: Add ``affine_tolerance`` flag to ``MergeSeries`` (#706)

1.5.3 (April 05, 2022)
======================

A bugfix release incorporating changes from the 1.3.x and 1.4.x
maintenance series.

* FIX: Alternate query for template brain mask (#704)
* FIX: Use copy function that does not preserve mtime when creating fsaverage directories (#703)
* FIX: Test failures (#701)
* ENH: Add optional session filter when collecting data (#678)
* ENH: Specify path pattern for transformation files in dwi datatype (#699)
* REF: Move BIDS skeleton generation into new ``testing`` module (#682)
* MNT: Select magnitude images in collect_data for BIDS 1.5.0 (#594)
* CI: Install package across Python versions and run pytest (#697)

1.5.2 (March 23, 2022)
======================
A bugfix release ensuring compatibility with Python 3.7.

* FIX: Add ``unlink()`` with ``missing_ok`` for Python 3.7 (#695)

1.5.1 (March 11, 2022)
======================
A bugfix release addressing several issues and improving the carpet plot.

* FIX: Set ``pixdim[4]`` to match ``RepetitionTime`` (#679)
* FIX: Unlink destination files before saving derivative (#683)
* FIX: Smarter decision to remap input segmentation to carpetplot (#691)
* FIX: Error handling the segmentation in NIfTI extraction (#689)
* ENH: Add a legend to carpet plots with more than one segment (#690)
* ENH: Miscellaneous improvements to carpetplot (#688)
* ENH: Refactor carpetplot reasigning responsibilities (#687)
* ENH: Initiate a module of nipype interfaces for mathematical morphology (#651)
* ENH: Reorder within-segment rows of carpetplot with hierarchical clustering (#686)
* DOC: Fix doc builds in ``maint/1.3.x`` (#692)

1.5.0 (February 07, 2022)
=========================
First release in the 1.5.x series.
This release upgrades the Dockerfile to use FSL6, and includes some new interfaces.

* ENH: Additional nibabel interfaces (#680)
* ENH: Add utility function to generate sample BIDS directory (#677)
* ENH: Update to FSL6 (#675)
* FIX: Scipy docs path (#681)
* TEST: Drop excessively long interface equivalence tests (#674)

1.4.8 (April 08, 2022)
======================
Bug-fix release in the 1.4.x series.
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ RUN curl -sSL "https://dl.dropbox.com/s/gwf51ykkk5bifyj/ants-Linux-centos6_x86_6
| tar -xzC $ANTSPATH --strip-components 1
ENV PATH="$ANTSPATH:$PATH"

# FSL 6.0.5.1
RUN curl -sSL https://fsl.fmrib.ox.ac.uk/fsldownloads/fsl-6.0.5.1-centos7_64.tar.gz | tar zxv --no-same-owner -C /opt \
# FSL 5.0.11
RUN curl -sSL https://fsl.fmrib.ox.ac.uk/fsldownloads/fsl-5.0.11-centos6_64.tar.gz | tar zxv --no-same-owner -C /opt \
--exclude='fsl/doc' \
--exclude='fsl/refdoc' \
--exclude='fsl/python/oxford_asl' \
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@
"nibabel": ("https://nipy.org/nibabel/", None),
"nipype": ("https://nipype.readthedocs.io/en/latest/", None),
"numpy": ("https://numpy.org/doc/stable/", None),
"pandas": ("https://pandas.pydata.org/pandas-docs/dev", None),
"pandas": ("http://pandas.pydata.org/pandas-docs/dev", None),
"python": ("https://docs.python.org/3/", None),
"scipy": ("https://docs.scipy.org/doc/scipy-1.8.0/html-scipyorg/", None),
"scipy": ("https://docs.scipy.org/doc/scipy/reference", None),
"smriprep": ("https://www.nipreps.org/smriprep/", None),
"surfplot": ("https://surfplot.readthedocs.io/en/latest/", None),
"templateflow": ("https://www.templateflow.org/python-client", None),
Expand Down
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
1 change: 0 additions & 1 deletion niworkflows/data/nipreps.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@
"sub-{subject}[/ses-{session}]/{datatype<dwi>|dwi}/sub-{subject}[_ses-{session}][_acq-{acquisition}][_rec-{reconstruction}][_dir-{direction}][_run-{run}][_space-{space}][_cohort-{cohort}][_res-{resolution}][_desc-{desc}]_{suffix<dwi|epiref|lowb|dseg>}{extension<.json|.nii.gz|.nii>|.nii.gz}",
"sub-{subject}[/ses-{session}]/{datatype<dwi>|dwi}/sub-{subject}[_ses-{session}][_acq-{acquisition}][_rec-{reconstruction}][_dir-{direction}][_run-{run}][_space-{space}][_cohort-{cohort}][_res-{resolution}]_desc-{desc}_{suffix<mask>}{extension<.json|.nii.gz|.nii>|.nii.gz}",
"sub-{subject}[/ses-{session}]/{datatype<dwi>|dwi}/sub-{subject}[_ses-{session}][_acq-{acquisition}][_rec-{reconstruction}][_dir-{direction}][_run-{run}][_space-{space}][_cohort-{cohort}][_res-{resolution}][_desc-{desc}]_{suffix<dwi>}{extension<.tsv|.bval|.bvec>|.tsv}",
"sub-{subject}[/ses-{session}]/{datatype<dwi>|dwi}/sub-{subject}[_ses-{session}][_acq-{acquisition}][_rec-{reconstruction}][_dir-{direction}][_run-{run}]_from-{from}_to-{to}_mode-{mode<image|points>|image}_{suffix<xfm>|xfm}{extension<.txt|.h5>}",
"sub-{subject}[/ses-{session}]/{datatype<fmap>|fmap}/sub-{subject}[_ses-{session}][_acq-{acquisition}][_dir-{direction}][_run-{run}][_part-{part}][_space-{space}][_cohort-{cohort}][_res-{resolution}][_fmapid-{fmapid}][_desc-{desc}]_{suffix<fieldmap>}{extension<.nii|.nii.gz|.json>|.nii.gz}",
"sub-{subject}/{datatype<figures>}/sub-{subject}[_ses-{session}][_acq-{acquisition}][_ce-{ceagent}][_rec-{reconstruction}][_run-{run}][_space-{space}][_cohort-{cohort}][_desc-{desc}]_{suffix<T1w|T2w|T1rho|T1map|T2map|T2star|FLAIR|FLASH|PDmap|PD|PDT2|inplaneT[12]|angio|dseg|mask|dwi|epiref|T2starw|MTw|TSE>}{extension<.html|.svg>|.svg}",
"sub-{subject}/{datatype<figures>}/sub-{subject}[_ses-{session}][_acq-{acquisition}][_ce-{ceagent}][_rec-{reconstruction}][_run-{run}][_space-{space}][_cohort-{cohort}][_fmapid-{fmapid}][_desc-{desc}]_{suffix<fieldmap>}{extension<.html|.svg>|.svg}",
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
12 changes: 6 additions & 6 deletions niworkflows/interfaces/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class BIDSInfo(SimpleInterface):

>>> bids_info = BIDSInfo(bids_dir=str(datadir / 'ds054'), bids_validate=False)
>>> bids_info.inputs.in_file = '''\
sub-01/func/ses-retest/sub-01_ses-retest_task-covertverbgeneration_rec-MB_acq-AP_run-01_bold.nii.gz'''
sub-01/func/ses-retest/sub-01_ses-retest_task-covertverbgeneration_rec-MB_acq-AP_run-1_bold.nii.gz'''
>>> res = bids_info.run()
>>> res.outputs
<BLANKLINE>
Expand Down Expand Up @@ -416,12 +416,12 @@ class DerivativesDataSink(SimpleInterface):
>>> dsink.inputs.desc = 'preproc'
>>> res = dsink.run()
>>> res.outputs.out_file # doctest: +ELLIPSIS
'.../niworkflows/sub-02/ses-noanat/func/sub-02_ses-noanat_task-rest_run-01_\
'.../niworkflows/sub-02/ses-noanat/func/sub-02_ses-noanat_task-rest_run-1_\
desc-preproc_bold.nii'

>>> bids_dir = tmpdir / 'bidsroot' / 'sub-02' / 'ses-noanat' / 'func'
>>> bids_dir.mkdir(parents=True, exist_ok=True)
>>> tricky_source = bids_dir / 'sub-02_ses-noanat_task-rest_run-01_bold.nii.gz'
>>> tricky_source = bids_dir / 'sub-02_ses-noanat_task-rest_run-1_bold.nii.gz'
>>> tricky_source.open('w').close()
>>> dsink = DerivativesDataSink(base_directory=str(tmpdir), check_hdr=False)
>>> dsink.inputs.in_file = str(tmpfile)
Expand All @@ -430,7 +430,7 @@ class DerivativesDataSink(SimpleInterface):
>>> dsink.inputs.RepetitionTime = 0.75
>>> res = dsink.run()
>>> res.outputs.out_meta # doctest: +ELLIPSIS
'.../niworkflows/sub-02/ses-noanat/func/sub-02_ses-noanat_task-rest_run-01_\
'.../niworkflows/sub-02/ses-noanat/func/sub-02_ses-noanat_task-rest_run-1_\
desc-preproc_bold.json'

>>> Path(res.outputs.out_meta).read_text().splitlines()[1]
Expand All @@ -450,7 +450,7 @@ class DerivativesDataSink(SimpleInterface):
>>> dsink.inputs.RepetitionTime = 0.75
>>> res = dsink.run()
>>> res.outputs.out_meta # doctest: +ELLIPSIS
'.../niworkflows/sub-02/ses-noanat/func/sub-02_ses-noanat_task-rest_run-01_\
'.../niworkflows/sub-02/ses-noanat/func/sub-02_ses-noanat_task-rest_run-1_\
space-MNI152NLin6Asym_res-01_desc-preproc_bold.json'

>>> lines = Path(res.outputs.out_meta).read_text().splitlines()
Expand All @@ -475,7 +475,7 @@ class DerivativesDataSink(SimpleInterface):
>>> dsink.inputs.meta_dict = {'RepetitionTime': 1.75, 'SkullStripped': False, 'Z': 'val'}
>>> res = dsink.run()
>>> res.outputs.out_meta # doctest: +ELLIPSIS
'.../niworkflows/sub-02/ses-noanat/func/sub-02_ses-noanat_task-rest_run-01_\
'.../niworkflows/sub-02/ses-noanat/func/sub-02_ses-noanat_task-rest_run-1_\
space-MNI152NLin6Asym_desc-preproc_bold.json'

>>> lines = Path(res.outputs.out_meta).read_text().splitlines()
Expand Down
Loading