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] Load compcor related confounds generated from fmriprep 21.x series #3285

Merged
merged 16 commits into from Dec 14, 2023

Conversation

htwangtw
Copy link
Member

@htwangtw htwangtw commented Jun 18, 2022

Closes #3268.

  • refactor the test file loader to allow adding new major release
  • add test data for 21.x.x and make sure the correct files are loaded
  • apply compcor loading suggestions from @askieslinger and iterate on it
  • simply _check_compcor_method by using all five options as prefix checking keys
  • add tests to test_load_confounds_strategy.py::test_strategy_compcor and test_load_confounds.py::test_n_compcor to make sure the correct components are loaded for 21.x and older

- refactor the test file loader
- add test data and make sure the corret files are loaded
- apply compcor loading suggestions from @askieslinger

Still need to investigate if the actual behaviour is inline with the
literature
@github-actions
Copy link
Contributor

👋 @htwangtw Thanks for creating a PR!

Until this PR is ready for review, you can include the [WIP] tag in its title, or leave it as a github draft.

Please make sure it is compliant with our contributing guidelines. In particular, be sure it checks the boxes listed below.

  • PR has an interpretable title.
  • PR links to Github issue with mention "Closes #XXXX"
  • Code is PEP8-compliant.
  • (Bug fixes) There is at least one test that would fail under the original bug conditions.
  • (New features) There is at least one unit test per new function / class.
  • (New features) The new feature is demoed in at least one relevant example.

We will review it as quick as possible, feel free to ping us with questions if needed.

the code should now work for both before and after 21.x update.

* simply _check_compcor_method by using all five options as prefix
checking keys

* add tests to test_load_confounds_strategy.py::test_strategy_compcor and test_load_confounds.py::test_n_compcor to make sure the correct componenets are loaded
@codecov
Copy link

codecov bot commented Jul 4, 2022

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (68cfa06) 91.92% compared to head (cd02a21) 91.92%.
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3285   +/-   ##
=======================================
  Coverage   91.92%   91.92%           
=======================================
  Files         145      145           
  Lines       16337    16354   +17     
  Branches     3407     3424   +17     
=======================================
+ Hits        15017    15034   +17     
  Misses        777      777           
  Partials      543      543           
Flag Coverage Δ
macos-latest_3.10_test_plotting 91.70% <100.00%> (+<0.01%) ⬆️
macos-latest_3.11_test_plotting 91.70% <100.00%> (+<0.01%) ⬆️
macos-latest_3.12_test_plotting 91.70% <100.00%> (+<0.01%) ⬆️
macos-latest_3.8_test_plotting 91.66% <100.00%> (+<0.01%) ⬆️
macos-latest_3.9_test_plotting 91.67% <100.00%> (+<0.01%) ⬆️
ubuntu-latest_3.10_test_plotting 91.70% <100.00%> (+<0.01%) ⬆️
ubuntu-latest_3.11_test_plotting 91.70% <100.00%> (+<0.01%) ⬆️
ubuntu-latest_3.12_test_plotting 91.70% <100.00%> (+<0.01%) ⬆️
ubuntu-latest_3.12_test_pre 91.70% <100.00%> (+<0.01%) ⬆️
ubuntu-latest_3.8_test_min 68.89% <100.00%> (+0.03%) ⬆️
ubuntu-latest_3.8_test_plot_min 91.43% <100.00%> (+<0.01%) ⬆️
ubuntu-latest_3.8_test_plotting 91.66% <100.00%> (+<0.01%) ⬆️
ubuntu-latest_3.9_test_plotting 91.67% <100.00%> (+<0.01%) ⬆️
windows-latest_3.10_test_plotting 91.68% <100.00%> (+<0.01%) ⬆️
windows-latest_3.11_test_plotting 91.68% <100.00%> (+<0.01%) ⬆️
windows-latest_3.12_test_plotting 91.68% <100.00%> (+<0.01%) ⬆️
windows-latest_3.8_test_plotting 91.64% <100.00%> (+<0.01%) ⬆️
windows-latest_3.9_test_plotting 91.65% <100.00%> (+<0.01%) ⬆️

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

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

@htwangtw htwangtw marked this pull request as ready for review July 4, 2022 20:35
Copy link
Member

@NicolasGensollen NicolasGensollen left a comment

Choose a reason for hiding this comment

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

Thanks @htwangtw !
I made a quick first pass and it LGTM. I just have a small suggestion to refactor a test.
Also, could you add a whatsnew ?

* shorten the `test_strategy_compcor` by factorising the check on
different fmriprep versions
* add an entry in `latest.rst`
Copy link
Member

@bthirion bthirion 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 that the PR is fine, but this leads us exactly where we didn't want to go: have an increasingly complex infrastructure to be compatible with fmriprep.
I would be in favor of deprecating old fmriprep versions asap.

@htwangtw
Copy link
Member Author

htwangtw commented Jul 6, 2022

I think that the PR is fine, but this leads us exactly where we didn't want to go: have an increasingly complex infrastructure to be compatible with fmriprep. I would be in favor of deprecating old fmriprep versions asap.

That's a great point. I think at this point we should only support LTS. I will have a look and perhaps a different PR to add deprecation warning about dropping non-LTS versions.

Copy link
Member

@NicolasGensollen NicolasGensollen left a comment

Choose a reason for hiding this comment

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

Just a small typo

doc/changes/latest.rst Outdated Show resolved Hide resolved
@htwangtw
Copy link
Member Author

@bthirion @NicolasGensollen Let me know if there's more we want to discuss about this PR!

Copy link
Member

@bthirion bthirion left a comment

Choose a reason for hiding this comment

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

I don't see any outstanding issue. But take my review with a grain of salt, I'm not familar at all with this part of the codebase.
Thx !

@@ -42,22 +44,17 @@ def _select_compcor(compcor_cols, n_compcor):
# only select if not "all", or less components are requested than there
# actually is
if (n_compcor != "all") and (n_compcor < len(compcor_cols)):
compcor_cols = compcor_cols[0:n_compcor]
compcor_cols = compcor_cols[:n_compcor]
Copy link
Member

Choose a reason for hiding this comment

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

@htwangtw would it make sense to add a check around here? Something like a warning if no CompCor regressors are found?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point - I have yet encountered this case but is it possible with how compcor is done?
I know it's possible for fMRIPrep to output confounds with no cosineXX regresssors

Copy link
Member

Choose a reason for hiding this comment

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

I hadn't really considered that. I don't know if fMRIPrep would ever not write out compcor regressors, short of a failure within the workflow. I was more thinking that, if fMRIPrep changes things again, having warnings in these functions would make it easier for users to tell something's gone wrong.

Copy link
Member Author

Choose a reason for hiding this comment

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

Right now the behaviour is the function will just fail, and a check is implemented in _load_single_confounds_file, so we don't need to write things in each function

def _load_single_confounds_file(confounds_file, strategy, demean=True,

The only variable that could be missing when fMRIPrep can run correctly is cosineXX, when the timeseries is too short.

This deserves it's own issue

Copy link
Member

Choose a reason for hiding this comment

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

Sorry for the delay in responding. I don't think _check_error catches the missing CompCor regressors. I tried running load_confounds on an fMRIPrepped file from 22.0.0 with the following kwargs:

{'strategy': ['motion', 'high_pass', 'compcor'],
 'motion': 'derivatives',
 'compcor': 'anat_separated',
 'n_compcor': 5}

It successfully ran, but the only regressors it returned were the cosine terms, motion parameters, and the motion derivatives. I wouldn't expect 22.x acompcor confounds to be loaded correctly per #3268, but I would have expected an error.

Copy link
Member Author

Choose a reason for hiding this comment

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

No worries! I really appreciate that you took time to look into this!
I will have more time to work on this next week..

Copy link
Collaborator

Choose a reason for hiding this comment

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

checked on a 23.0.2 dataset

from nilearn.interfaces.fmriprep import load_confounds

nii = "/home/remi/datalad/datasets.datalad.org/openneuro-derivatives/ds003768-fmriprep/sub-01/func/sub-01_task-rest_run-1_desc-preproc_bold.nii.gz"

confounds_out, sample_mask_out = load_confounds(
    nii,
    strategy=["motion", "high_pass", "compcor"],
    motion="derivatives",
    compcor="anat_separated",
    n_compcor=5,
)

print(sorted(confounds_out.columns))

and got this:

['c_comp_cor_00', 'c_comp_cor_01', 'c_comp_cor_02', 'c_comp_cor_03', 'c_comp_cor_04', 'cosine00', 'cosine01', 'cosine02', 'cosine03', 'cosine04', 'cosine05', 'cosine06', 'cosine07', 'rot_x', 'rot_x_derivative1', 'rot_y', 'rot_y_derivative1', 'rot_z', 'rot_z_derivative1', 'trans_x', 'trans_x_derivative1', 'trans_y', 'trans_y_derivative1', 'trans_z', 'trans_z_derivative1', 'w_comp_cor_00', 'w_comp_cor_01', 'w_comp_cor_02', 'w_comp_cor_03', 'w_comp_cor_04']

AFAICT this test also catches missing regressors:

def test_missing_keywords(tmp_path, strategy_keywords, expected_parameters):

@Remi-Gau
Copy link
Collaborator

Remi-Gau commented Nov 9, 2023

@htwangtw
what's the status on this PR? The number of merge conflicts is starting to worry me...

@htwangtw
Copy link
Member Author

htwangtw commented Nov 9, 2023

@htwangtw what's the status on this PR? The number of merge conflicts is starting to worry me...

It's pretty much ready and then I forgot to remind people to review it.... I can fix it up this week.

@Remi-Gau
Copy link
Collaborator

Remi-Gau commented Dec 13, 2023

@htwangtw
You said you were drowning in notifications and similar things.
Do you want me to take this one off your plate?

@Remi-Gau
Copy link
Collaborator

@htwangtw

  • reduced the number of rows in the new confound files to 30 (like the one that was already there)
  • fixed merge conflicts
  • parametrized several tests to be run on both fmriprep data versions
  • refactored some tests a bit

@htwangtw
Copy link
Member Author

Thanks a bunch @Remi-Gau !

Copy link
Member

@ymzayek ymzayek left a comment

Choose a reason for hiding this comment

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

LGTM thanks!

@Remi-Gau
Copy link
Collaborator

OK will merge this.

@tsalo
if your comment from above still stands, let's open an issue and tackle that in another PR.

@Remi-Gau Remi-Gau merged commit 2d6ff1c into nilearn:main Dec 14, 2023
32 checks passed
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.

nilearn.interfaces.fmriprep.load_confounds not able to load compcor regressors
6 participants