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: Revise the TOPUP workflow and related utilities #278

Merged
merged 14 commits into from
Sep 22, 2022

Conversation

oesteban
Copy link
Member

@oesteban oesteban commented Sep 2, 2022

Debugging towards fixing nipreps/fmriprep#2830.

I'm under the impression that the fieldmap estimated by topup is only correct in polarity if the input EPIs are RA{S,I} or AR{S,I} oriented.

The strategy here would be to force all axis to be filled from left to right.

EDIT (new when calling the draft finished):

This PR includes several improvements and changes towards making proper interpretation and reuse of TOPUP outputs:

  • Adds code comments at all workflows and functions with substantial edits, trying to help future maintenance.
  • Reorients all input images to have positive principal cosines (i.e., axes go L->R, P->A, I->S despite their ordering). The intent is to minimize the possible orientations fed into TOPUP (see 2444c11).
  • Rids TOPUP from calculating head motion, it is done outside, with the hope of a full integration with NiTransforms concatenation of transforms (see 2444c11).
  • Revises the fix_coeff node to consider the fact that the internal coordinate system of FSL is LAS (see e80e68d). I'm rather positive about the LR flip, since I've seen it on both @mgxd's and @Gilles86's data (e.g., TOPUP's field and warpfieldXX outputs showcase the flip). I'm less positive about the sign change of the field itself (only testable on @Gilles86's data because @mgxd's has j direction for PE).
  • Reorders the input EPI runs so that TOPUP always gets the same ordering, preferentially starting with a positive direction encoding (i.e., i, j, or k) - see 72640e7

This does not fully-cover nipreps/fmriprep#2830 (nor #218 EDIT - it does cover this issue, finally), but I have checked that at least the reference image is generated by SDCFlows, and the corresponding by TOPUP are in spatial agreement (i.e., correction is properly applied to the EPIs fed in the estimation) on both datasets (Mathias' and Gilles'). A subsequent PR will address the unwarping of IntendedFor EPI targets.

@oesteban oesteban force-pushed the fix/topup-field-flip branch 2 times, most recently from 90a137d to 0ab82fe Compare September 3, 2022 15:58
@codecov-commenter
Copy link

codecov-commenter commented Sep 3, 2022

Codecov Report

Base: 83.02% // Head: 82.54% // Decreases project coverage by -0.48% ⚠️

Coverage data is based on head (0ab82fe) compared to base (b1c2baf).
Patch coverage: 45.83% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #278      +/-   ##
==========================================
- Coverage   83.02%   82.54%   -0.49%     
==========================================
  Files          25       25              
  Lines        1850     1873      +23     
  Branches      278      281       +3     
==========================================
+ Hits         1536     1546      +10     
- Misses        282      295      +13     
  Partials       32       32              
Flag Coverage Δ
travis 82.54% <45.83%> (-0.49%) ⬇️

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

Impacted Files Coverage Δ
sdcflows/interfaces/utils.py 79.27% <40.90%> (-4.23%) ⬇️
sdcflows/workflows/fit/pepolar.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@oesteban oesteban force-pushed the fix/topup-field-flip branch 3 times, most recently from 748ba0a to 72640e7 Compare September 6, 2022 09:29
@oesteban oesteban changed the title FIX: Reorient images to have positive direction cosines (topup) FIX: Revise the TOPUP workflow and related utilities Sep 6, 2022
@oesteban oesteban marked this pull request as ready for review September 6, 2022 09:46
@oesteban oesteban force-pushed the fix/topup-field-flip branch from 72640e7 to 465c767 Compare September 6, 2022 09:48
@oesteban
Copy link
Member Author

oesteban commented Sep 7, 2022

Okay, for the most part, this seems to work on both @Gilles86 and @mgxd datasets - not sure about generalization to new datasets, but this is a start.

There's a remaining issue with @mgxd, being that the registration between the fieldmap reference and the EPI runs is not great. But I think that should be addressed on a separate issue.

Reviews at this point are welcome and important.

cc/ @effigies

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.

Did a quick pass, still having trouble conceptualizing the whole process. Do you happen to have some screenshots on how these results look on the troublesome datasets before / after this patch?

In trying to make a more reliable implementation of the workflow, this
commit continues with the following improvements:

- Added comments indicating the rationale of each step
- Removed the ``flatten`` node, as it will not be necessary anymore.
  Please note that this change breaks compatibility with fMRIPrep, as
  this node was the entry point for the ``max_trs`` hack (which is
  not justified anymore).
- Aligns the EPIs set prior to topup, and configures topup not to
  estimate head motion.
  The benefits are two-fold: we now control the alignment process, and
  may run it with ``3dVolReg``, which seems more reliable on average;
  and correction to generate a reference with topup and with SDCFlows
  (``--debug fieldmaps`` mode) are more consistent.
  This also saves some registration levels within topup (see changes
  to the config file), although I don't think this will make it run
  substantially faster.
I have empirically tested this with Gilles' dataset (oriented LAS, PE
along *i*) and with @mgxd's dataset (oriented LAS too, but PE is *j*),
so this seems to be accurate.

I tested it by manually opening topup's and our fieldmap with Mango and
removing the affine's orientation (although switching it off should not
and does not alter the result), and checking that data are in the same
LR ordering.
Instead of relying on the user's implicit ordering by BIDS' entities,
this makes ordering by PE and then readout time higher priority.

This means that we will always find *i* encoding blips before *i-* ones,
and *j* before *j-*.

I suspect that topup will give opposite sign fieldmaps if *i-* is set
before *i*.
@oesteban oesteban force-pushed the fix/topup-field-flip branch from cbad583 to d82eb92 Compare September 8, 2022 15:33
@oesteban
Copy link
Member Author

oesteban commented Sep 8, 2022

Okay, now with tests revised for the new set of reorientation operations

oesteban and others added 2 commits September 9, 2022 09:23
Co-authored-by: Mathias Goncalves <mathiasg@stanford.edu>
Co-authored-by: Mathias Goncalves <mathiasg@stanford.edu>
@oesteban oesteban force-pushed the fix/topup-field-flip branch from cf8447e to af4ca90 Compare September 9, 2022 07:24
@effigies effigies added the impact: high Estimated high impact task label Sep 13, 2022
Co-authored-by: Mathias Goncalves <mathiasg@stanford.edu>
@oesteban
Copy link
Member Author

still having trouble conceptualizing the whole process. Do you happen to have some screenshots on how these results look on the troublesome datasets before / after this patch?

I had @Gilles86's reports - maybe he could confirm whether things are looking better now? In his case (nipreps/fmriprep#2830), the displacements were flipped (i.e., in the opposed direction).

On your data, @mgxd, the diagnosis was similar. However, the results are still a bit off because (I suspect but haven't confirmed) of the registration between the distorted EPI and fieldmap reference.

Still hoping to put together more visual documentation to make it easier to understand both the problem and the solution.

@oesteban
Copy link
Member Author

I'll merge after @eilidhmacnicol's blessings.

Copy link
Contributor

@eilidhmacnicol eilidhmacnicol left a comment

Choose a reason for hiding this comment

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

LGTM. Left a few comments, but nothing critical to address before merging.

What was the rationale behind the b02b0.cnf changes? I understand the estmov change to stop the estimation of motion (since it is done elsewhere) but what was the reasoning for the reduction of levels to calculate the field?

@oesteban
Copy link
Member Author

What was the rationale behind the b02b0.cnf changes? I understand the estmov change to stop the estimation of motion (since it is done elsewhere) but what was the reasoning for the reduction of levels to calculate the field?

I could have kept the number of levels, but it didn't make much sense as the blips come already aligned. Keeping them was a waste of resources and time for no improvement of the nonlinear component of the estimation (which is done in the latter levels of registration).

@Gilles86
Copy link

Gilles86 commented Oct 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
impact: high Estimated high impact task
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants