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] ensure that dataframes can be used as input for second level GLM (estimation and contrast) #3879

Merged
merged 7 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/changes/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Fixes

- Fix bug that would prevent loading the confounds of a gifti file in actual fmriprep datasets (:gh:`3819` by `Rémi Gau`_).

- Fix bug that prevented using that dataframes as input for second level GLM when computing contrasts (:gh:`3879` by `Rémi Gau`_).
Remi-Gau marked this conversation as resolved.
Show resolved Hide resolved

- Fix bug in :func:`~glm.first_level.first_level_from_bids` that returned no confound files if the corresponding bold files contained derivatives BIDS entities (:gh:`3742` by `Rémi Gau`_).

- Fix bug in :func:`~glm.first_level.first_level_from_bids` that would throw a warning about ``slice_time_ref`` not being provided even when it was (:gh:`3811` by `Rémi Gau`_).
Expand Down
20 changes: 11 additions & 9 deletions nilearn/glm/second_level/second_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,15 +179,17 @@ def _check_confounds(confounds):


def _check_first_level_contrast(second_level_input, first_level_contrast):
if isinstance(second_level_input[0], FirstLevelModel):
if first_level_contrast is None:
raise ValueError(
"If second_level_input was a list of "
"FirstLevelModel, then first_level_contrast "
"is mandatory. It corresponds to the "
"second_level_contrast argument of the "
"compute_contrast method of FirstLevelModel"
)
if (
isinstance(second_level_input, list)
and isinstance(second_level_input[0], FirstLevelModel)
and first_level_contrast is None
):
raise ValueError(
"If second_level_input was a list of FirstLevelModel,"
" then first_level_contrast is mandatory. "
"It corresponds to the second_level_contrast argument "
"of the compute_contrast method of FirstLevelModel."
)


def _check_output_type(output_type, valid_types):
Expand Down
23 changes: 23 additions & 0 deletions nilearn/glm/tests/test_second_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -1178,3 +1178,26 @@ def test_second_level_contrast_computation_with_memory_caching():
# Delete objects attached to files to avoid WindowsError when deleting
# temporary directory (in Windows)
del func_img, model, X, Y


def test_second_lvl_dataframe_computation():
"""Check that contrast can be computed when using dataframes as input.

See bug https://github.com/nilearn/nilearn/issues/3871
"""
# 'abusing' write_fake_fmri_data_and_design
# to just generate a 3D image
shapes = ((7, 8, 9, 1),)
_, FUNCFILE, _ = write_fake_fmri_data_and_design(shapes)
Remi-Gau marked this conversation as resolved.
Show resolved Hide resolved
FUNCFILE = FUNCFILE[0]

dfcols = ["subject_label", "map_name", "effects_map_path"]
dfrows = [
["01", "a", FUNCFILE],
["02", "a", FUNCFILE],
["03", "a", FUNCFILE],
]
niidf = pd.DataFrame(dfrows, columns=dfcols)

model = SecondLevelModel().fit(niidf)
model.compute_contrast(first_level_contrast="a")