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 pathlib.Path not being counted as Niimg-like in new_image_like #3723

Merged
merged 9 commits into from Aug 10, 2023
Merged
3 changes: 3 additions & 0 deletions CITATION.cff
Expand Up @@ -295,6 +295,9 @@ authors:
- given-names: Matthieu
family-names: Joulot
website: https://github.com/MatthieuJoulot
- given-names: Maximilian Cosmo
family-names: Sitter
website: https://github.com/mcsitter
- given-names: Mehdi
family-names: Rahim
website: https://github.com/mrahim
Expand Down
3 changes: 3 additions & 0 deletions doc/changes/latest.rst
Expand Up @@ -30,6 +30,9 @@ Fixes

- Relax the :func:`~nilearn.interfaces.fmriprep.load_confounds` confounds selection on `cosine` as not all confound files contained the variables (:gh:`3816` by `Hao-Ting Wang`_).

- Fix pathlib.Path not being counted as Niimg-like object in :func:`~image.new_img_like` (:gh:`3723` by `Maximilian Cosmo Sitter`_).


Enhancements
------------

Expand Down
2 changes: 2 additions & 0 deletions nilearn/image/image.py
Expand Up @@ -751,13 +751,15 @@ def new_img_like(ref_niimg, data, affine=None, copy_header=False):
"""
# Hand-written loading code to avoid too much memory consumption
orig_ref_niimg = ref_niimg
ref_niimg = stringify_path(ref_niimg)
is_str = isinstance(ref_niimg, str)
has_get_data = hasattr(ref_niimg, "get_data")
has_get_fdata = hasattr(ref_niimg, "get_fdata")
has_iter = hasattr(ref_niimg, "__iter__")
has_affine = hasattr(ref_niimg, "affine")
if has_iter and not any([is_str, has_get_data, has_get_fdata]):
ref_niimg = ref_niimg[0]
ref_niimg = stringify_path(ref_niimg)
is_str = isinstance(ref_niimg, str)
has_get_data = hasattr(ref_niimg, "get_data")
has_get_fdata = hasattr(ref_niimg, "get_fdata")
Expand Down
18 changes: 18 additions & 0 deletions nilearn/image/tests/test_image.py
Expand Up @@ -654,6 +654,24 @@ def test_new_img_like():
assert_array_equal(get_data(img_nifti2), get_data(img2_nifti2))


def test_new_img_like_accepts_paths(tmp_path):
"""Check that new_img_like can accept instances of pathlib.Path."""
nifti_path = tmp_path / "sample.nii"
assert isinstance(nifti_path, Path)

data = np.random.rand(10, 10, 10)
img = Nifti1Image(data, np.eye(4))
Comment on lines +662 to +663
Copy link
Collaborator

Choose a reason for hiding this comment

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

not needed for now but I think there may be a lot random img creation in the whole test suite that could be fixturised

Copy link
Member

Choose a reason for hiding this comment

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

Linking #3725 and #3331

nibabel.save(img, nifti_path)

new_data = np.random.rand(10, 10, 10)
new_img = new_img_like(nifti_path, new_data)
assert new_img.shape == (10, 10, 10)
ymzayek marked this conversation as resolved.
Show resolved Hide resolved

# Check that list of pathlib.Path also accepted
new_img = new_img_like([nifti_path], new_data)
assert new_img.shape == (10, 10, 10)


def test_new_img_like_non_iterable_header():
"""
Tests that when an niimg's header is not iterable
Expand Down