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: Refactoring the B0FieldTransform implementation #346

Merged
merged 44 commits into from
Aug 16, 2023

Conversation

oesteban
Copy link
Member

@oesteban oesteban commented Apr 7, 2023

This PR defines a new interface for the ApplyCoeffsField node, and dismisses TransformCoefficients because that projection is really not necessary.

Resolves: #345.

sdcflows/transform.py Outdated Show resolved Hide resolved
@oesteban
Copy link
Member Author

oesteban commented Apr 7, 2023

@effigies thanks much for looking at this draft -- this is a very good moment to give me feedback since I don't plan to work on this for a few hours.

@effigies
Copy link
Member

effigies commented Apr 7, 2023

@oesteban Everything here so far makes sense. I do think the idea of returning to Hz and then resampling that scalar map seems almost guaranteed to be fine. We could do a 2-4x upsampling when generating the $\mathbf{F}$-space fieldmap. Even a linear interpolation with the transform into $\mathbf{B}$ space would probably be a good enough approximation, then.

@oesteban oesteban force-pushed the fix/yoio-you-only-interpolate-once branch 4 times, most recently from ce25d24 to 5617345 Compare May 2, 2023 12:59
@oesteban oesteban force-pushed the fix/yoio-you-only-interpolate-once branch from 75be890 to fe4faf1 Compare June 30, 2023 07:48
@oesteban
Copy link
Member Author

@effigies @mgxd I'm finally unlocking this. I have made great progress this morning - it'd be nice to get a fast double-check of my latest commit 50a8e64.

As the TODO comment there anticipates, I'm now working toward integrating head motion correction within the interpolation framework.

@codecov-commenter
Copy link

codecov-commenter commented Jun 30, 2023

Codecov Report

Patch coverage: 77.16% and project coverage change: -1.46% ⚠️

Comparison is base (375dd7a) 81.99% compared to head (6e3de3a) 80.53%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #346      +/-   ##
==========================================
- Coverage   81.99%   80.53%   -1.46%     
==========================================
  Files          26       26              
  Lines        2271     2296      +25     
  Branches      281      286       +5     
==========================================
- Hits         1862     1849      -13     
- Misses        362      402      +40     
+ Partials       47       45       -2     
Flag Coverage Δ
unittests ∅ <ø> (∅)

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

Files Changed Coverage Δ
sdcflows/workflows/apply/correction.py 82.75% <ø> (-0.58%) ⬇️
sdcflows/workflows/fit/syn.py 48.50% <ø> (ø)
sdcflows/workflows/apply/registration.py 95.00% <66.66%> (+6.53%) ⬆️
sdcflows/transform.py 77.29% <73.19%> (-12.85%) ⬇️
sdcflows/interfaces/bspline.py 68.92% <92.00%> (-5.70%) ⬇️
sdcflows/workflows/fit/pepolar.py 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@effigies
Copy link
Member

Mathias is off. I'll review in an hour or so.

@oesteban
Copy link
Member Author

No rush, if not possible it's fine too - I'll keep going and we can have that look at the end. Thanks!

@oesteban oesteban force-pushed the fix/yoio-you-only-interpolate-once branch from 19513f6 to dea2408 Compare June 30, 2023 16:15
@oesteban
Copy link
Member Author

The last commit should be a candidate to give this an end... See the git commit message for more details. 🚀

@oesteban oesteban marked this pull request as ready for review June 30, 2023 16:16
@oesteban oesteban force-pushed the fix/yoio-you-only-interpolate-once branch 2 times, most recently from 182f3e5 to 001c3f9 Compare June 30, 2023 19:15
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.

Couple big issues. Still looking through this.

sdcflows/transform.py Outdated Show resolved Hide resolved
sdcflows/transform.py Outdated Show resolved Hide resolved
sdcflows/transform.py Outdated Show resolved Hide resolved
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.

Okay, I've read through this now. Only one additional comment, and it's more of a nit-pick as series with degenerate spatial dimensions are probably going to break in other ways...

sdcflows/transform.py Outdated Show resolved Hide resolved
sdcflows/transform.py Outdated Show resolved Hide resolved
@oesteban
Copy link
Member Author

oesteban commented Jul 3, 2023

Okay, I've been working on the docstring and on pacifying tests.

I have opened #369 for a test that will require further investigation.

This could be closed after beefing up tests a little. This is the plan:

1 - Add a recent EPI "pepolar" correction pair, and one fmap2epi affine. I have data I can distribute for this, with DWI with 6 b=0 acquired sufficient time apart to have visible head motion.
2 - Realign these b=0 and add them to the test data so that we can check the simultaneous hmc and sdc.

WDYT? @effigies @mgxd

@oesteban
Copy link
Member Author

oesteban commented Jul 3, 2023

BTW, these EPI are LAS I have them for both LR/RL and PA/AP PEs.

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.

A few passing comments

sdcflows/interfaces/bspline.py Outdated Show resolved Hide resolved
sdcflows/interfaces/bspline.py Outdated Show resolved Hide resolved
sdcflows/transform.py Outdated Show resolved Hide resolved
sdcflows/transform.py Outdated Show resolved Hide resolved
sdcflows/transform.py Outdated Show resolved Hide resolved
@oesteban oesteban force-pushed the fix/yoio-you-only-interpolate-once branch from da4c183 to 3266cf8 Compare August 11, 2023 14:05
@oesteban
Copy link
Member Author

@oesteban Those outputs look correct to me.

Just thinking about naming schemes (as we don't have fieldmap derivatives in any BEP), here's a thought for a naming scheme, preserving the order you have:

* `fmap/sub-101006_coeff-1_desc-<B0FieldIdentifier>_fieldmap.nii.gz`

* `fmap/sub-101006_desc-<B0FieldIdentifier>_fmapref.nii.gz`

* `fmap/sub-101006_desc-<B0FieldIdentifier>_mask.nii.gz`

* `func/sub-101006_task-rest_dir-LR_from-boldref_to-<B0FieldIdentifier>_mode-image_xfm.mat`

* `func/sub-101006_task-rest_dir-LR_desc-preproc_boldref.nii.gz`

* `func/sub-101006_task-rest_dir-LR_desc-brain_mask.nii.gz`

The idea is to try to be abstracted from the details of whether our boldref is derived from bold or sbref, and what type of fieldmap we use to generate fieldmap and fmapref. It's possible that we do want to specifically use different fmaprefs depending on the field source, as epi and magnitude suffixes will indicate a need for different registration parameters.

Can we maybe go ahead without tinkering with the test dataset and keep this in mind when we write the CLI of SDCFlows? I think we are not far from pinning that one down and your thoughts are very useful there.

@oesteban oesteban force-pushed the fix/yoio-you-only-interpolate-once branch from 3266cf8 to 1e5dbe4 Compare August 11, 2023 15:22
@oesteban
Copy link
Member Author

oesteban commented Aug 11, 2023

Okay, believe it or not, this is coming to an end :)

I have decided to leave the head motion correction transforms out of this PR (see 1e5dbe4, force-pushed 0f24b6b) because testing that will require some extra effort and we may want to give priority to the jacobians instead.

Should we try to merge this today?

I've checked and all tests are looking gorgeous (I'm even surprised how the phasediff correction is on pair with the quality level of pepolar maps).

I leave it in your hands @effigies and @mgxd .

@oesteban oesteban force-pushed the fix/yoio-you-only-interpolate-once branch from 1e5dbe4 to 0f24b6b Compare August 11, 2023 15:27
@effigies
Copy link
Member

Can we maybe go ahead without tinkering with the test dataset and keep this in mind when we write the CLI of SDCFlows? I think we are not far from pinning that one down and your thoughts are very useful there.

Agreed. Absolutely does not need doing here.

@oesteban
Copy link
Member Author

Thumbs up to merging? @mgxd @effigies

@effigies
Copy link
Member

effigies commented Aug 15, 2023

Sorry, I started a final review on Friday and wasn't able to get back to it yesterday. Will do it first thing today.

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.

gave another quick pass

("out_warp", "fieldwarp_ref")]),
(resample, outputnode, [("out_field", "fieldmap")]),
# (resample_ref, outputnode, [("out_field", "fieldmap_ref")]),
# TODO - take reference from brainextraction workflow
Copy link
Contributor

Choose a reason for hiding this comment

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

still todo?

sdcflows/interfaces/bspline.py Show resolved Hide resolved
sdcflows/interfaces/bspline.py Outdated Show resolved Hide resolved
sdcflows/interfaces/bspline.py Outdated Show resolved Hide resolved
sdcflows/interfaces/tests/test_bspline.py Show resolved Hide resolved
sdcflows/transform.py Outdated Show resolved Hide resolved
target_reference = deoblique_and_zooms(
listify(self.coeffs)[-1],
target_reference,
)

# Generate tensor-product B-Spline weights
for level in listify(self.coeffs):
Copy link
Contributor

Choose a reason for hiding this comment

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

listify is potentially called 3 times on self.coeffs - perhaps we save the cycles and just set to a variable?

sdcflows/transform.py Outdated Show resolved Hide resolved
sdcflows/transform.py Outdated Show resolved Hide resolved
sdcflows/workflows/apply/registration.py Outdated Show resolved Hide resolved
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.

Okay. Overall looks good. Most suggestions are for maintainability, not bugs caught.

sdcflows/transform.py Outdated Show resolved Hide resolved
sdcflows/interfaces/bspline.py Show resolved Hide resolved
sdcflows/transform.py Outdated Show resolved Hide resolved
sdcflows/transform.py Outdated Show resolved Hide resolved
sdcflows/transform.py Outdated Show resolved Hide resolved
sdcflows/workflows/apply/correction.py Show resolved Hide resolved
sdcflows/transform.py Outdated Show resolved Hide resolved
sdcflows/interfaces/bspline.py Outdated Show resolved Hide resolved
sdcflows/workflows/tests/test_integration.py Outdated Show resolved Hide resolved
sdcflows/workflows/tests/test_integration.py Outdated Show resolved Hide resolved
sdcflows/transform.py Outdated Show resolved Hide resolved
sdcflows/transform.py Outdated Show resolved Hide resolved
oesteban and others added 2 commits August 16, 2023 10:41
Co-authored-by: Mathias Goncalves <mathiasg@stanford.edu>
Co-authored-by: Chris Markiewicz <markiewicz@stanford.edu>
@oesteban oesteban force-pushed the fix/yoio-you-only-interpolate-once branch from c78c74e to 107f56e Compare August 16, 2023 08:57
@oesteban
Copy link
Member Author

oesteban commented Aug 16, 2023

@effigies it looks our tests dislike the new version of pybids released two days ago:

ImportError while loading conftest '/home/runner/work/sdcflows/sdcflows/sdcflows/conftest.py'.
sdcflows/conftest.py:39: in <module>
    layouts = {
sdcflows/conftest.py:40: in <dictcomp>
    p.name: BIDSLayout(str(p), validate=False, derivatives=True)
/usr/share/miniconda/envs/test/lib/python3.10/site-packages/bids/layout/layout.py:177: in __init__
    _indexer(self)
/usr/share/miniconda/envs/test/lib/python3.10/site-packages/bids/layout/index.py:154: in __call__
    self._index_metadata()
/usr/share/miniconda/envs/test/lib/python3.10/site-packages/bids/layout/index.py:484: in _index_metadata
    tag = _create_tag_dict(bf, all_entities[md_key], md_val, is_metadata=True)
/usr/share/miniconda/envs/test/lib/python3.10/site-packages/bids/layout/models.py:720: in _create_tag_dict
    raise ValueError(
E   ValueError: Passed value has an invalid dtype (NoneType). Must be one of int, float, bool, or str.

Do you have any quick ideas of why this is happening?

EDIT: seems related to bids-standard/pybids#1009

setup.cfg Outdated Show resolved Hide resolved
@oesteban oesteban force-pushed the fix/yoio-you-only-interpolate-once branch from 515d8c9 to 3c024ea Compare August 16, 2023 13:23
@effigies
Copy link
Member

According to git bisect, bids-standard/pybids#1013 is where this started to fail.

Co-authored-by: Mathias Goncalves <mathiasg@stanford.edu>
@oesteban oesteban force-pushed the fix/yoio-you-only-interpolate-once branch from 779c239 to ea12e7f Compare August 16, 2023 14:14
Co-authored-by: Mathias Goncalves <mathiasg@stanford.edu>
@oesteban oesteban merged commit 11fadb2 into master Aug 16, 2023
16 of 18 checks passed
@oesteban oesteban deleted the fix/yoio-you-only-interpolate-once branch August 16, 2023 20:14
@effigies
Copy link
Member

🎉

effigies added a commit to nipreps/fmriprep that referenced this pull request Aug 23, 2023
…3077)

## Changes proposed in this pull request

* Accommodate nipreps/sdcflows#346
* Convert ITK transforms from `.mat` (MATLAB) to text format
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.

*The problem* of SDCFlows (and how to fix it)
7 participants