[go: up one dir, main page]

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

[BUG] problem with compute_fixed_effects when using precision_weighted=True #4454

Closed
3 of 9 tasks
jmumford opened this issue Jun 12, 2024 · 1 comment · Fixed by #4439
Closed
3 of 9 tasks

[BUG] problem with compute_fixed_effects when using precision_weighted=True #4454

jmumford opened this issue Jun 12, 2024 · 1 comment · Fixed by #4439
Assignees
Labels
Bug for bug reports GLM Issues/PRs related to the nilearn.glm module.
Milestone

Comments

@jmumford
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Operating system

  • Linux
  • Mac
  • Windows

Operating system version

Mac 14.4.1 (23E224)

Python version

  • 3.12
  • 3.11
  • 3.10
  • 3.9
  • 3.8

nilearn version

0.10.3
tested using code directly from github as well.

Expected behavior

I'm working off of the sample analysis here.

The only change I make is adding precision_weighted=True. I would expect fixed_fx_contrast to be a 3D image, but it is a 4D image where the 4th dimension = # of contrasts

contrast_imgs = [
summary_statistics_run_1["effect_size"],
summary_statistics_run_2["effect_size"],
]
variance_imgs = [
summary_statistics_run_1["effect_variance"],
summary_statistics_run_2["effect_variance"],
]

fixed_fx_contrast, fixed_fx_variance, fixed_fx_stat =compute_fixed_effects(
contrast_imgs,
variance_imgs,
data["mask"],
precision_weighted=True
)

I'm fairly certain the error is occurring here, where the contrast images are read in. For these data,

    contrasts = np.array(
        [masker.transform(contrast_img) for contrast_img in contrast_imgs]
    )

yields a numpy array with size (2, 1, 28008) and that second dimension of 1 is an issue. Then problems compound here
since contrasts * weights will be (2, 2, 28008) and so the sum won't work as expected.

Hope that helps!
Jeanette

Current behavior & error messages

There are no errors, but the output data dimension is not correct. Code is provided that shows this.

Steps and code to reproduce bug

from pathlib import Path
import numpy as np
import pandas as pd
from nilearn.datasets import func
from nilearn.glm.first_level import FirstLevelModel
from nilearn.glm.contrasts import compute_fixed_effects


output_dir = Path.cwd() / "results" / "plot_two_runs_model"
output_dir.mkdir(exist_ok=True, parents=True)
print(f"Output will be saved to: {output_dir}")

data = func.fetch_fiac_first_level()
fmri_imgs = [data["func1"], data["func2"]]

from nilearn.image import mean_img

mean_img_ = mean_img(fmri_imgs[0])

design_files = [data["design_matrix1"], data["design_matrix2"]]
design_matrices = [pd.DataFrame(np.load(df)["X"]) for df in design_files]

fmri_glm = FirstLevelModel(
    mask_img=data["mask"],
    smoothing_fwhm=5,
    minimize_memory=True,
)

contrast_val = [[-1, -1, 1, 1]]
fmri_glm_run_1 = fmri_glm.fit(fmri_imgs[0], design_matrices=design_matrices[0])
summary_statistics_run_1 = fmri_glm_run_1.compute_contrast(
    contrast_val,
    output_type="all",
)

fmri_glm_run_2 = fmri_glm.fit(fmri_imgs[1], design_matrices=design_matrices[1])
contrast_val = np.array([[-1, -1, 1, 1]])
summary_statistics_run_2 = fmri_glm_run_2.compute_contrast(
    contrast_val,
    output_type="all",
)

# Run fixed effects analysis

contrast_imgs = [
    summary_statistics_run_1["effect_size"],
    summary_statistics_run_2["effect_size"],
]
variance_imgs = [
    summary_statistics_run_1["effect_variance"],
    summary_statistics_run_2["effect_variance"],
]

fixed_fx_contrast, fixed_fx_variance, fixed_fx_stat = compute_fixed_effects(
    contrast_imgs,
    variance_imgs,
    data["mask"],
    precision_weighted=True
)

print('This should be a 3D image and it is not')
print(fixed_fx_contrast.shape)
@jmumford jmumford added the Bug for bug reports label Jun 12, 2024
@Remi-Gau
Copy link
Collaborator

I have a PR to fix it: #4439

It is work in progress that I have put on the back burner but we will get back to it.

But definitely I will add it to our TODO list that the example should give sensible outputs.

@Remi-Gau Remi-Gau added this to the Release 0.11.0 milestone Aug 27, 2024
@Remi-Gau Remi-Gau added the GLM Issues/PRs related to the nilearn.glm module. label Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug for bug reports GLM Issues/PRs related to the nilearn.glm module.
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants