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] serialize numpy arrays to list before saving to json #4325

Merged
merged 1 commit into from Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions doc/changes/latest.rst
Expand Up @@ -18,6 +18,9 @@ Fixes
- :bdg-dark:`Code` Masker reports tables do not overlap with report image (:gh:`4308` by `Rémi Gau`_).

- :bdg-dark:`Code` Fix color bar handling with color map with only 1 level (:gh:`4255` by `Rémi Gau`_).

- :bdg-dark:`Code` Ensure that :func:`nilearn.interfaces.bids.save_glm_to_bids` serializes numpy arrays to :obj:`list` used as GLM parameters before saving them to JSON (:gh:`4325` by `Rémi Gau`_).

- :bdg-dark:`Code` Check that the ``view`` parameter in surface plotting functions is a pair of ``int`` or ``float`` when it is not a ``string`` (:gh:`4297` by `Rémi Gau`_).
- :bdg-dark:`Code` Fix positions of the markers on the images on the sphere masker reports (:gh:`4285` by `Rémi Gau`_).
- :bdg-dark:`Code` Fix cut position in nifti maps maskers to match displayed map maximum (:gh:`4304` by `Rémi Gau`_).
Expand Down
4 changes: 4 additions & 0 deletions nilearn/interfaces/bids/glm.py
Expand Up @@ -114,6 +114,10 @@
ATTRIBUTE_RENAMING.get(k, k): v for k, v in model_attributes.items()
}

for key, value in model_attributes.items():
if isinstance(value, (np.ndarray)):
model_attributes[key] = value.tolist()

Check warning on line 119 in nilearn/interfaces/bids/glm.py

View check run for this annotation

Codecov / codecov/patch

nilearn/interfaces/bids/glm.py#L119

Added line #L119 was not covered by tests

model_metadata = {
"Description": "A statistical map generated by Nilearn.",
**data_attributes,
Expand Down
40 changes: 35 additions & 5 deletions nilearn/interfaces/tests/test_bids.py
Expand Up @@ -342,15 +342,11 @@ def test_save_glm_to_bids(tmp_path_factory):
]

shapes, rk = [(7, 8, 9, 15)], 3
mask, fmri_data, design_matrices = generate_fake_fmri_data_and_design(
_, fmri_data, design_matrices = generate_fake_fmri_data_and_design(
shapes,
rk,
)

masker = NiftiMasker(mask)
masker.fit()
Comment on lines -350 to -351
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this was not used


# Call with verbose (improve coverage)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

not true: probably left over from some copy pasta

single_run_model = FirstLevelModel(
mask_img=None,
minimize_memory=False,
Expand Down Expand Up @@ -378,6 +374,40 @@ def test_save_glm_to_bids(tmp_path_factory):
assert os.path.isfile(full_filename)


def test_save_glm_to_bids_serialize_affine(tmp_path):
"""Test that affines are turned into a serializable type.

Regression test for https://github.com/nilearn/nilearn/issues/4324.
"""
shapes, rk = [(7, 8, 9, 15)], 3
mask, fmri_data, design_matrices = generate_fake_fmri_data_and_design(
shapes,
rk,
)

target_affine = mask.affine

single_run_model = FirstLevelModel(
target_affine=target_affine,
minimize_memory=False,
).fit(
fmri_data[0],
design_matrices=design_matrices[0],
)

save_glm_to_bids(
model=single_run_model,
contrasts={
"effects of interest": np.eye(rk),
},
contrast_types={
"effects of interest": "F",
},
out_dir=tmp_path,
prefix="sub-01_ses-01_task-nback",
)


@pytest.fixture
def nb_cols_design_matrix():
return 3
Expand Down