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] fix SimpleRegressionResults #4071

Merged
merged 8 commits into from Dec 16, 2023
Merged

Conversation

Remi-Gau
Copy link
Collaborator

@Remi-Gau Remi-Gau commented Oct 19, 2023

@github-actions
Copy link
Contributor

👋 @Remi-Gau 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 (see our documentation on PR structure)
  • Code is PEP8-compliant (see our documentation on coding style)
  • Changelog or what's new entry in doc/changes/latest.rst (see our documentation on PR structure)

For new features:

  • There is at least one unit test per new function / class (see our documentation on testing)
  • The new feature is demoed in at least one relevant example.

For bug fixes:

  • There is at least one test that would fail under the original bug conditions.

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

@codecov
Copy link

codecov bot commented Oct 19, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (a1810e3) 91.85% compared to head (a9fadff) 91.96%.

Files Patch % Lines
nilearn/glm/regression.py 85.71% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4071      +/-   ##
==========================================
+ Coverage   91.85%   91.96%   +0.11%     
==========================================
  Files         145      145              
  Lines       16360    16358       -2     
  Branches     3424     3423       -1     
==========================================
+ Hits        15027    15044      +17     
+ Misses        792      771      -21     
- Partials      541      543       +2     
Flag Coverage Δ
macos-latest_3.10_test_plotting ?
macos-latest_3.11_test_plotting 91.74% <85.71%> (+0.03%) ⬆️
macos-latest_3.12_test_plotting 91.74% <85.71%> (+0.03%) ⬆️
macos-latest_3.8_test_plotting 91.70% <85.71%> (+0.03%) ⬆️
macos-latest_3.9_test_plotting 91.71% <85.71%> (+0.03%) ⬆️
ubuntu-latest_3.10_test_plotting ?
ubuntu-latest_3.11_test_plotting ?
ubuntu-latest_3.12_test_plotting ?
ubuntu-latest_3.12_test_pre 91.74% <85.71%> (+0.03%) ⬆️
ubuntu-latest_3.8_test_min 68.93% <85.71%> (?)
ubuntu-latest_3.8_test_plot_min 91.46% <85.71%> (+0.03%) ⬆️
ubuntu-latest_3.8_test_plotting 91.70% <85.71%> (+0.03%) ⬆️
ubuntu-latest_3.9_test_plotting ?
windows-latest_3.10_test_plotting 91.72% <85.71%> (?)
windows-latest_3.11_test_plotting 91.72% <85.71%> (?)
windows-latest_3.12_test_plotting 91.72% <85.71%> (?)
windows-latest_3.8_test_plotting ?
windows-latest_3.9_test_plotting ?

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.

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.

LGTM overall.

@@ -8,7 +7,7 @@ class BaseGLM(BaseEstimator, TransformerMixin, CacheMixin):
"""Implement a base class \
for the :term:`General Linear Model<GLM>`."""

@auto_attr
@property
Copy link
Member

Choose a reason for hiding this comment

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

I have no issue with the change, but could you explain what it means/implies ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As far as I can tell these decorators do the same thing but one comes from the standard library, so feels like it is more appropriate to use it.

Do you want me to mention it in the changelog?

Copy link
Member

Choose a reason for hiding this comment

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

IIUC it's not exactly the same, as property doesn't store the value as an object attribute after initial call (see https://nipy.org/nibabel/reference/nibabel.onetime.html#auto-attr). While using property works as well it seems it could add function call overhead that could affect performance. You could time some tests that we already have to quickly compare one versus the other.

Copy link
Collaborator Author

@Remi-Gau Remi-Gau Dec 15, 2023

Choose a reason for hiding this comment

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

IIUC it's not exactly the same, as property doesn't store the value as an object attribute after initial call (see https://nipy.org/nibabel/reference/nibabel.onetime.html#auto-attr). While using property works as well it seems it could add function call overhead that could affect performance. You could time some tests that we already have to quickly compare one versus the other.

You were right @ymzayek it would make the code quite a bit slower.

Ran the following script on this branch and on main.

On main I am at about 0.197 seconds per iteration.

On this branch it increases up to 0.943 seconds per iteration if I try to get the residuals 10 times.

So yeah will revert those decorators.

"""Benchmark for getting GLM residuals

Runs a first level GLM N_ITER time on dummy data
and gets the residuals n times (between 0 and N_ITER_RESIDUALS times).

Gets the time taken to for 1 iteration (fit GLM and get residual n times).

"""python
from nilearn._utils.data_gen import (
    generate_fake_fmri_data_and_design,
)
from nilearn.glm.first_level import (
    FirstLevelModel,
)

import time

def compute_residuals(mask, fmri_data, design_matrices):
    model = FirstLevelModel(
        mask_img=mask, minimize_memory=False, noise_model="ols"
    )
    model.fit(fmri_data, design_matrices=design_matrices)
    return model.residuals

N_ITER = 20
N_ITER_RESIDUALS = 11

def main():

    shapes, rk = [(20, 20, 20, 100)], 3
    mask, fmri_data, design_matrices = generate_fake_fmri_data_and_design(
        shapes, rk
    )

    for i in range(len(design_matrices)):
        design_matrices[i][design_matrices[i].columns[0]] = 1

    for n_iter_residuals in range(1, N_ITER_RESIDUALS):

        t0 = time.monotonic_ns()
        for _ in range(N_ITER):
            model = FirstLevelModel(
                mask_img=mask, minimize_memory=False, noise_model="ols"
            )
            model.fit(fmri_data, design_matrices=design_matrices)
            # get the residuals several times to see if this makes a difference
            for __ in range(n_iter_residuals):
                model.residuals
        t1 = time.monotonic_ns()

        print(f"Get residuals {n_iter_residuals} times")
        print(f"{(t1-t0)/10**9/N_ITER:0.3f} seconds per iteration")


if __name__ == "__main__":
    main()

@Remi-Gau Remi-Gau marked this pull request as ready for review October 23, 2023 09:55
@Remi-Gau
Copy link
Collaborator Author

I think that this PR could also address #2971

@Remi-Gau Remi-Gau closed this Nov 9, 2023
@Remi-Gau Remi-Gau deleted the simple_reg branch November 9, 2023 09:43
@Remi-Gau Remi-Gau restored the simple_reg branch November 9, 2023 09:43
@Remi-Gau
Copy link
Collaborator Author

Remi-Gau commented Nov 9, 2023

closed by mistake

@Remi-Gau Remi-Gau reopened this Nov 9, 2023
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.

Sounds good, thx.

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.

Still not quite sure we need to change the decorator but LGTM!

doc/changes/latest.rst Outdated Show resolved Hide resolved
Co-authored-by: Yasmin <63292494+ymzayek@users.noreply.github.com>
@Remi-Gau
Copy link
Collaborator Author

will run some benchmark to check

@Remi-Gau Remi-Gau modified the milestone: release 0.11.0 Dec 13, 2023
@bthirion
Copy link
Member

Can be merged I think ?

@Remi-Gau Remi-Gau merged commit b90604c into nilearn:main Dec 16, 2023
31 of 32 checks passed
@Remi-Gau Remi-Gau deleted the simple_reg branch December 16, 2023 00:08
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.

SimpleRegressionResults.__init__() Small bugs in SimpleRegressionResults
3 participants