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] improve support for F statistics in compute_fixed_effects #3203

Merged
merged 38 commits into from Sep 28, 2023

Conversation

bthirion
Copy link
Member

@bthirion bthirion commented Mar 30, 2022

closes #3194.

Still need more tests (for F tests).

Detecting some inconsistencies in dimension: se sometimes require effect sizes for t contrasts to have shape (1, n_voxels) while it would be more natural to have shape (n_voxels). I think that we should actually support both cases seamlessly.

@github-actions
Copy link
Contributor

👋 @bthirion 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.

@codecov
Copy link

codecov bot commented Mar 30, 2022

Codecov Report

Merging #3203 (961bd81) into main (93c49f1) will increase coverage by 0.02%.
Report is 25 commits behind head on main.
The diff coverage is 91.42%.

@@            Coverage Diff             @@
##             main    #3203      +/-   ##
==========================================
+ Coverage   91.79%   91.82%   +0.02%     
==========================================
  Files         134      134              
  Lines       15772    15816      +44     
  Branches     3284     3306      +22     
==========================================
+ Hits        14478    14523      +45     
  Misses        751      751              
+ Partials      543      542       -1     
Flag Coverage Δ
macos-latest_3.10 91.74% <91.42%> (+0.03%) ⬆️
macos-latest_3.11 91.74% <91.42%> (+0.03%) ⬆️
macos-latest_3.8 91.70% <91.42%> (+0.02%) ⬆️
macos-latest_3.9 91.70% <91.42%> (+0.03%) ⬆️
ubuntu-latest_3.10 91.74% <91.42%> (+0.03%) ⬆️
ubuntu-latest_3.11 91.74% <91.42%> (+0.03%) ⬆️
ubuntu-latest_3.8 91.70% <91.42%> (+0.02%) ⬆️
ubuntu-latest_3.9 91.70% <91.42%> (+0.03%) ⬆️
windows-latest_3.10 91.68% <91.42%> (+0.03%) ⬆️
windows-latest_3.11 91.68% <91.42%> (+0.03%) ⬆️
windows-latest_3.8 91.64% <91.42%> (+0.02%) ⬆️
windows-latest_3.9 91.65% <91.42%> (+0.03%) ⬆️

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

Files Coverage Δ
nilearn/glm/contrasts.py 92.34% <91.42%> (+4.34%) ⬆️

... and 11 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

nilearn/glm/contrasts.py Outdated Show resolved Hide resolved
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.

@bthirion thx for the updates! A quick review with a few comments. I still need to look at the tests.

nilearn/glm/contrasts.py Show resolved Hide resolved
nilearn/glm/contrasts.py Outdated Show resolved Hide resolved
nilearn/glm/contrasts.py Outdated Show resolved Hide resolved
nilearn/glm/contrasts.py Outdated Show resolved Hide resolved
nilearn/glm/contrasts.py Outdated Show resolved Hide resolved
@bthirion
Copy link
Member Author

This one has been ready for some time. Can't it be counted in the upcoming release ?

@ymzayek
Copy link
Member

ymzayek commented Apr 26, 2023

@bthirion sure we can try to make it work. Can you rebase on main, resolve the conflicts, and add a whatsnew entry? Then we can give it a final review and merge.

@Remi-Gau
Copy link
Collaborator

@bthirion
do you want me to fix the merge conflicts or should I do it?

@ymzayek
Copy link
Member

ymzayek commented Apr 26, 2023

@Remi-Gau I was thinking to do the same since they look quite straightforward. I'll let you do it and I'd say to add a whatsnew entry as well. I don't think @bthirion would mind :)

@ymzayek
Copy link
Member

ymzayek commented Apr 26, 2023

@bthirion and @Remi-Gau I just realized this PR breaks backwards compatibility so I'm not sure we should rush it. The default behavior of compute_fixed_effects is to return 3 objects and this fix it returns 4. This is a public function so we need a deprecation cycle. I think what you can do is keep default behavior with a warning added and if dof is defined then use that to apply the new behavior and returns

nilearn/glm/contrasts.py Outdated Show resolved Hide resolved
ymzayek
ymzayek previously approved these changes Apr 26, 2023
nilearn/glm/contrasts.py Outdated Show resolved Hide resolved
Comment on lines 179 to 189
# F test
XX1 = np.vstack((X1, X1))
XX2 = np.vstack((X2, X2))

Xw, Vw, tw, zw = _compute_fixed_effects_params(
[XX1, XX2], [V1, V2], dofs=[200, 200],
precision_weighted=False)
assert_almost_equal(Xw, 1.5 * XX1)
assert_almost_equal(Vw, 1.25 * V1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

note to self when refactoring the tests: split into smaller independent tests



def _compute_fixed_effects_params(contrasts, variances, precision_weighted):
def _compute_fixed_effects_params(contrasts, variances, precision_weighted,
dofs):
"""Compute the fixed effects t-statistic, contrast, variance, \
Copy link
Collaborator

Choose a reason for hiding this comment

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

if this can now handle F contrast, the doc string should be updated, no?

@Remi-Gau Remi-Gau changed the title WIP: Fix fixed effects [FIX] improve support for F statistics in compute_fixed_effects Apr 26, 2023
@Remi-Gau
Copy link
Collaborator

@bthirion updated the name of the PR. May need a double check.

nilearn/glm/contrasts.py Outdated Show resolved Hide resolved
nilearn/glm/contrasts.py Outdated Show resolved Hide resolved
nilearn/glm/contrasts.py Outdated Show resolved Hide resolved
nilearn/glm/contrasts.py Outdated Show resolved Hide resolved
nilearn/glm/contrasts.py Outdated Show resolved Hide resolved
@ymzayek
Copy link
Member

ymzayek commented Sep 22, 2023

Thx @bthirion for the updates! I'll give another review once the CI finishes. Note that the test coverage report might fail due to black being applied to whole files that we still didn't cover in our reformatting journey. I would have rather done that in a separate PR but I think in this case since you are the only one touching these files (at least no other PR by a non coredev is working on these files) we can let it be. WDYT @Remi-Gau ? Anyways long story short, in the case of codecov/project failing I wouldn't worry about it as long as the new lines of code you wrote are covered.

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.

Some suboptimal black formatting behavior

nilearn/glm/tests/test_first_level.py Outdated Show resolved Hide resolved
nilearn/glm/tests/test_first_level.py Outdated Show resolved Hide resolved
nilearn/glm/tests/test_first_level.py Outdated Show resolved Hide resolved
Co-authored-by: Yasmin <63292494+ymzayek@users.noreply.github.com>
Copy link
Collaborator

@Remi-Gau Remi-Gau left a comment

Choose a reason for hiding this comment

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

I would have rather done that in a separate PR but I think in this case since you are the only one touching these files (at least no other PR by a non coredev is working on these files) we can let it be. WDYT @Remi-Gau ?

Good with me to do the fade to black in this PR.

But if we do that then we must update the black and pre-commit config by deleting from them the files involved in this PR:

https://github.com/nilearn/nilearn/blob/3f85e187a6784652a3e4835ed026c02cab958141/pyproject.toml#L111C1-L113C39

https://github.com/nilearn/nilearn/blob/3f85e187a6784652a3e4835ed026c02cab958141/.pre-commit-config.yaml#L23C2-L25C15

@Remi-Gau
Copy link
Collaborator

in the case of codecov/project failing I wouldn't worry about it as long as the new lines of code you wrote are covered

Agreed that this should probably be tackled by checking what our blind spots are by looking at codecov reports: e.g. https://app.codecov.io/gh/nilearn/nilearn/blob/main/nilearn%2Fglm%2Fcontrasts.py

@bthirion
Copy link
Member Author

SHould be good now. Thx for the feedback.

@bthirion
Copy link
Member Author

The black failure is weird.

@Remi-Gau
Copy link
Collaborator

When in doubt, my first line of defense is usually.

pre-commit install
pre-commit run -a

@Remi-Gau
Copy link
Collaborator

It may be useful adding the --diff to Black in CI because the current reports are so terse they are almost useless.

nilearn/glm/contrasts.py Outdated Show resolved Hide resolved
nilearn/glm/contrasts.py Outdated Show resolved Hide resolved
Co-authored-by: Remi Gau <remi_gau@hotmail.com>
Copy link
Collaborator

@Remi-Gau Remi-Gau 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 have run out of things to nitpick about. Thanks @bthirion

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.

I changed the warning message to make it less redundant and to make it clear to the user what they should do to access the new behavior. Otherwise I think this is good to go

nilearn/glm/contrasts.py Outdated Show resolved Hide resolved
nilearn/glm/contrasts.py Outdated Show resolved Hide resolved
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.

Thanks @bthirion and @Remi-Gau! Will merge now

@ymzayek ymzayek merged commit 0300d74 into nilearn:main Sep 28, 2023
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

compute_fixed_effects currently does not support F statistics (multi-dimensional)
3 participants