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: Revision of antsBrainExtraction, better handling edge cases #567

Merged
merged 10 commits into from
Sep 23, 2020

Conversation

oesteban
Copy link
Member

This PR introduces changes to better handle the copying of headers within the workflow, which is a potential source for nipreps/smriprep#127, and addresses some problems that surface when the ATROPOS refinement step fails (nipreps/smriprep#125).

This refactor could be considered a preliminary work on #531.

  • Updates the nodes with pure python interfaces based on nibabel, minimizing the need for the new copy_header of ANTs' nipype interfaces.
  • Reorganizes the workflow so that the Atropos refinement is completely self-contained.
  • If a WM prior is found within the template's structure, then it is mapped on to the individual's brain and fed as weight_image in the N4 correction.
  • Adds regularization of the WM posterior estimated with ATROPOS, by multiplying with the WM prior mapped from the template. In the future, an easy check could be implemented here to dismiss the ATROPOS refinement if the prior and the posterior do not overlap sufficiently (i.e., the WM was not correctly labeled or segmented)

This commit:

  - [x] Updates the nodes with pure python interfaces based on nibabel,
    minimizing the need for the new ``copy_header`` of ANTs' nipype
    interfaces.
  - [x] Reorganizes the workflow so that the Atropos refinement is
    completely self contained.

These are the first two steps to address nipreps/smriprep#125.
@oesteban oesteban requested a review from mgxd September 16, 2020 18:16
@pull-assistant
Copy link

pull-assistant bot commented Sep 16, 2020

@oesteban oesteban changed the base branch from master to maint/1.2.x September 16, 2020 18:17
@oesteban oesteban changed the base branch from maint/1.2.x to master September 16, 2020 18:17
This commit introduces two changes:

  - [x] If a WM prior is found within the template's structure, then it
    is mapped on to the individual's brain and fed as ``weight_image``
    in the N4 correction.
  - [x] Adds regularization of the WM posterior estimated with ATROPOS,
    by multiplying with the WM prior mapped from the template.
    In the future, an easy check could be implemented here to dismiss
    the ATROPOS refinement if the prior and the posterior do not overlap
    sufficiently (i.e., the WM was not correctly labeled or segmented)

Resolves: nipreps/smriprep#125 (at least for ds005/sub-08).
@codecov
Copy link

codecov bot commented Sep 16, 2020

Codecov Report

Merging #567 into master will decrease coverage by 5.38%.
The diff coverage is 6.25%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #567      +/-   ##
==========================================
- Coverage   64.62%   59.23%   -5.39%     
==========================================
  Files          43       42       -1     
  Lines        5215     5264      +49     
  Branches      760      765       +5     
==========================================
- Hits         3370     3118     -252     
- Misses       1688     2009     +321     
+ Partials      157      137      -20     
Flag Coverage Δ
#documentation ?
#reportlettests ?
#travis 59.23% <6.25%> (-0.65%) ⬇️
#unittests ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
niworkflows/anat/ants.py 9.70% <6.25%> (-60.92%) ⬇️
niworkflows/func/util.py 21.17% <0.00%> (-58.83%) ⬇️
niworkflows/anat/freesurfer.py 39.13% <0.00%> (-52.18%) ⬇️
niworkflows/anat/skullstrip.py 30.00% <0.00%> (-50.00%) ⬇️
niworkflows/interfaces/itk.py 26.31% <0.00%> (-12.04%) ⬇️
niworkflows/interfaces/fixes.py 44.44% <0.00%> (-11.12%) ⬇️
niworkflows/interfaces/bids.py 87.58% <0.00%> (-8.97%) ⬇️
niworkflows/interfaces/utils.py 37.76% <0.00%> (-4.99%) ⬇️
niworkflows/__init__.py

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9c3eb81...ed4d06e. Read the comment docs.

(init_aff, norm, [("output_transform", "initial_moving_transform")]),
(norm, map_brainmask, [
("reverse_transforms", "transforms"),
("reverse_invert_flags", "invert_transform_flags"),
]),
(map_brainmask, thr_brainmask, [("output_image", "input_image")]),
(thr_brainmask, dil_brainmask, [("output_image", "op1")]),
(dil_brainmask, get_brainmask, [("output_image", "op1")]),
(map_brainmask, inu_n4_final, [("output_image", "weight_image")]),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having trouble tracing how this originally connected through...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so it looks like we're no longer using Atropos to select white matter at all. What purpose does it now serve?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we still use ATROPOS:

  • To refine the binary mask (as originally done by antsBrainExtraction.sh)
  • To obtain the WM posterior (i.e., select the WM).

In some sense, now we use it even more :D

However, it is a bit implicit - see the disconnect() call below, to replace the final outputs of the brain-extraction workflow with those coming from the ATROPOS workflow.

@oesteban
Copy link
Member Author

oesteban commented Sep 19, 2020

It seems this refactor still has some problems:

ds054/sub-100003:
Screen Shot 2020-09-19 at 9 28 47 AM

I might have to roll back the WM prior utilization and change it to using the brain mask from the template. Look at the remaining inhomogeneity around the cerebellum in ds054/sub-100057:
Screen Shot 2020-09-19 at 9 32 08 AM

BTW, this seems pretty idiosyncratic of ds054. But in hindsight, I think I can propose a much cleverer usage of priors that works for ds054 and all others I'm testing on (ds003, ds005, ds051, ds114).

oesteban added a commit to oesteban/niworkflows that referenced this pull request Sep 21, 2020
oesteban added a commit to nipreps/fmriprep that referenced this pull request Sep 21, 2020
@oesteban
Copy link
Member Author

The latest commit includes changes to make the workflow more reliable overall, fixing the problems (at least for sub-100003) I detected on ds054.

I will re-run a good bunch of datasets, including the two originally identified as failing (ds005/sub-08 and ds051/sub-06) and ds054.

@oesteban
Copy link
Member Author

I have visually confirmed that after a05dddc, the posterior most overlapping with the WM prior is correctly selected for ds051/sub-06.

@pep8speaks
Copy link

pep8speaks commented Sep 22, 2020

Hello @oesteban, Thank you for updating!

Cheers! There are no style issues detected in this Pull Request. 🍻 To test for issues locally, pip install flake8 and then run flake8 niworkflows.

Comment last updated at 2020-09-22 18:51:16 UTC

@oesteban
Copy link
Member Author

Okay, it seems to be working correctly now.

ds005

Checked visually and all subjects (incl. 08) look great now.

ds051

Checked visually and all subjects (incl. 06) look great now.

ds054/sub-100003

Before:
Screen Shot 2020-09-19 at 9 28 47 AM

After:
Screen Shot 2020-09-22 at 8 43 10 AM

ds054/sub-100057

Before:
Screen Shot 2020-09-19 at 9 32 08 AM

After:
Screen Shot 2020-09-22 at 8 48 33 AM

ds054/sub-100042

Before:
Screen Shot 2020-09-22 at 8 51 45 AM

After:
Screen Shot 2020-09-22 at 8 52 09 AM

@oesteban
Copy link
Member Author

This is ready for merge - final comments @effigies @mgxd ?

niworkflows/anat/ants.py Outdated Show resolved Hide resolved
@oesteban oesteban force-pushed the fix/smriprep-125 branch 2 times, most recently from e5b4a80 to 70fbadf Compare September 22, 2020 15:37
Copy link
Contributor

@mgxd mgxd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty good (looking at the before/after pictures)

niworkflows/anat/ants.py Outdated Show resolved Hide resolved
niworkflows/anat/ants.py Outdated Show resolved Hide resolved
niworkflows/anat/ants.py Outdated Show resolved Hide resolved
niworkflows/anat/ants.py Outdated Show resolved Hide resolved
niworkflows/anat/ants.py Outdated Show resolved Hide resolved
oesteban and others added 2 commits September 22, 2020 10:58
Co-authored-by: Mathias Goncalves <goncalves.mathias@gmail.com>
@oesteban
Copy link
Member Author

Good to go?

@oesteban oesteban mentioned this pull request Sep 22, 2020
@oesteban oesteban requested a review from bpinsard September 22, 2020 19:23
@oesteban
Copy link
Member Author

Hey @bpinsard, I'd like to squish this into an RC2 before the LTS is cut (and preferably, cut the RC as soon as possible). Do you want to take a look into it before we go ahead? We would like to hear your opinion before merging since you'll be maintaining the LTS.

Copy link
Member

@effigies effigies left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I roughly follow what happened here. No obvious problems.

@oesteban
Copy link
Member Author

I'll take @bpinsard's silence as no-opposition. The sooner we start testing this, the better.

@oesteban oesteban merged commit ffe8eb1 into nipreps:master Sep 23, 2020
@oesteban oesteban deleted the fix/smriprep-125 branch September 23, 2020 07:46
HippocampusGirl added a commit to HippocampusGirl/niworkflows that referenced this pull request Sep 29, 2020
1.3.1

Bug-fix release in the 1.3.x series.
Addresses longstanding issues in the anatomical MRI brain extraction workflow.

* FIX: Revision of ``antsBrainExtraction``, better handling edge cases (nipreps#567)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants