From 674f927d890a6026b6136c8b9837ebd26ef18456 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Wed, 12 Jun 2024 17:02:30 +0200 Subject: [PATCH 1/7] chore: remove copyright year from nilearn license as is upstream --- junifer/external/nilearn/junifer_nifti_spheres_masker.py | 2 +- .../external/nilearn/tests/test_junifer_nifti_spheres_masker.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/junifer/external/nilearn/junifer_nifti_spheres_masker.py b/junifer/external/nilearn/junifer_nifti_spheres_masker.py index 36fd7d9b8..c200154c4 100644 --- a/junifer/external/nilearn/junifer_nifti_spheres_masker.py +++ b/junifer/external/nilearn/junifer_nifti_spheres_masker.py @@ -31,7 +31,7 @@ if TYPE_CHECKING: # New BSD License -# Copyright (c) 2007 - 2022 The nilearn developers. +# Copyright (c) The nilearn developers. # All rights reserved. diff --git a/junifer/external/nilearn/tests/test_junifer_nifti_spheres_masker.py b/junifer/external/nilearn/tests/test_junifer_nifti_spheres_masker.py index a36c8f2ff..92b5429b6 100644 --- a/junifer/external/nilearn/tests/test_junifer_nifti_spheres_masker.py +++ b/junifer/external/nilearn/tests/test_junifer_nifti_spheres_masker.py @@ -17,7 +17,7 @@ from junifer.external.nilearn import JuniferNiftiSpheresMasker # New BSD License -# Copyright (c) 2007 - 2022 The nilearn developers. +# Copyright (c) The nilearn developers. # All rights reserved. -- 2.52.0 From 05b65df121a1b8cad4c183c02e328e5b08b87d75 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Wed, 12 Jun 2024 17:03:13 +0200 Subject: [PATCH 2/7] chore: improve docstring in junifer_nifti_spheres_masker.py --- .../external/nilearn/junifer_nifti_spheres_masker.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/junifer/external/nilearn/junifer_nifti_spheres_masker.py b/junifer/external/nilearn/junifer_nifti_spheres_masker.py index c200154c4..2973e6e9a 100644 --- a/junifer/external/nilearn/junifer_nifti_spheres_masker.py +++ b/junifer/external/nilearn/junifer_nifti_spheres_masker.py @@ -99,6 +99,17 @@ def _apply_mask_and_get_affinity( Contains the boolean indices for each sphere. shape: (number of seeds, number of voxels) + Raises + ------ + ValueError + If ``niimg`` and ``mask_img`` are both provided or + if overlap is detected between spheres. + + Warns + ----- + RuntimeWarning + If the provided images contain NaN, they will be converted to zeroes. + """ seeds = list(seeds) -- 2.52.0 From 15369f11c90608bb4d66c47f00b62e2cb55b32f4 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Wed, 12 Jun 2024 17:03:49 +0200 Subject: [PATCH 3/7] update: make necessary change in junifer_nifti_spheres_masker.py as per upstream --- junifer/external/nilearn/junifer_nifti_spheres_masker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/junifer/external/nilearn/junifer_nifti_spheres_masker.py b/junifer/external/nilearn/junifer_nifti_spheres_masker.py index 2973e6e9a..e6ea7e831 100644 --- a/junifer/external/nilearn/junifer_nifti_spheres_masker.py +++ b/junifer/external/nilearn/junifer_nifti_spheres_masker.py @@ -221,7 +221,7 @@ def _iter_signals_from_spheres( X, A = _apply_mask_and_get_affinity( seeds, niimg, radius, allow_overlap, mask_img=mask_img ) - for _, row in enumerate(A.rows): + for row in A.rows: yield X[:, row] -- 2.52.0 From d348599a75ab5f0a6a21fdf0080198b17952ee3a Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Wed, 12 Jun 2024 17:04:22 +0200 Subject: [PATCH 4/7] chore: add __all__ in junifer_nifti_spheres_masker.py --- junifer/external/nilearn/junifer_nifti_spheres_masker.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/junifer/external/nilearn/junifer_nifti_spheres_masker.py b/junifer/external/nilearn/junifer_nifti_spheres_masker.py index e6ea7e831..699f37e62 100644 --- a/junifer/external/nilearn/junifer_nifti_spheres_masker.py +++ b/junifer/external/nilearn/junifer_nifti_spheres_masker.py @@ -29,6 +29,9 @@ if TYPE_CHECKING: from pandas import DataFrame +__all__ = ["JuniferNiftiSpheresMasker"] + + # New BSD License # Copyright (c) The nilearn developers. -- 2.52.0 From 01be82e08b9eb3c83fb21b26e6c38de1d4593cd0 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Wed, 12 Jun 2024 17:06:51 +0200 Subject: [PATCH 5/7] chore: add __all__ in external/nilearn/__init__.py --- junifer/external/nilearn/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/junifer/external/nilearn/__init__.py b/junifer/external/nilearn/__init__.py index 7575c3212..4fdb3c40c 100644 --- a/junifer/external/nilearn/__init__.py +++ b/junifer/external/nilearn/__init__.py @@ -4,3 +4,6 @@ # License: AGPL from .junifer_nifti_spheres_masker import JuniferNiftiSpheresMasker + + +__all__ = ["JuniferNiftiSpheresMasker"] -- 2.52.0 From 2c92aaf3b9866eda40799bb4beb596078d515948 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Wed, 12 Jun 2024 17:48:33 +0200 Subject: [PATCH 6/7] update: add test to compare junifer and nilearn spheres masker mean aggregation --- .../test_junifer_nifti_spheres_masker.py | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/junifer/external/nilearn/tests/test_junifer_nifti_spheres_masker.py b/junifer/external/nilearn/tests/test_junifer_nifti_spheres_masker.py index 92b5429b6..fbd71cb46 100644 --- a/junifer/external/nilearn/tests/test_junifer_nifti_spheres_masker.py +++ b/junifer/external/nilearn/tests/test_junifer_nifti_spheres_masker.py @@ -4,12 +4,14 @@ # License: AGPL import warnings +from typing import List, Tuple import nibabel import numpy as np import pytest from nilearn._utils import data_gen from nilearn.image import get_data +from nilearn.maskers import NiftiSpheresMasker from numpy.testing import assert_array_equal from junifer.external.nilearn import JuniferNiftiSpheresMasker @@ -331,3 +333,76 @@ def test_nifti_spheres_masker_io_shapes() -> None: ) test_data = masker.transform(img_4d) assert test_data.shape == (n_volumes, n_regions) + + +@pytest.mark.parametrize( + "shape", + [ + (10, 11, 12), + (10, 11, 12, 5), + ], +) +@pytest.mark.parametrize( + "radius, allow_overlap", + [ + (2.0, True), + (2.0, False), + (3.0, True), + (4.0, True), + (5.0, True), + ], +) +@pytest.mark.parametrize( + "coords", + [ + [(1, 1, 1)], + [(1, 1, 1), (4, 4, 4)], + [(1, 1, 1), (4, 4, 4), (10, 10, 10)], + ], +) +def test_junifer_and_nilearn_mean_agg_are_equal( + shape: Tuple[int, ...], + radius: float, + allow_overlap: bool, + coords: List[Tuple[int, int, int]], +) -> None: + """Test junifer's masker behaves same as nilearn's when agg is mean. + + Parameters + ---------- + shape : tuple of int + The parametrized shape of the input image. + radius : float + The parametrized radius of the spheres. + allow_overlap : bool + The parametrized option to overlap spheres or not. + coords : list of tuple of int, int and int + The parametrized seeds. + + """ + # Set affine + affine = np.eye(4) + # Generate random image + input_img, mask_img = data_gen.generate_random_img( + shape=shape, + affine=affine, + ) + # Compute junifer's version + junifer_masker = JuniferNiftiSpheresMasker( + seeds=coords, + radius=radius, + allow_overlap=allow_overlap, + mask_img=mask_img, + ) + junifer_output = junifer_masker.fit_transform(input_img) + # Compute nilearn's version + nilearn_masker = NiftiSpheresMasker( + seeds=coords, + radius=radius, + allow_overlap=allow_overlap, + mask_img=mask_img, + ) + nilearn_output = nilearn_masker.fit_transform(input_img) + # Checks + assert junifer_output.shape == nilearn_output.shape + np.testing.assert_almost_equal(junifer_output, nilearn_output) -- 2.52.0 From 4c6a5a6c8a6cb5a6e787a85275f0ae3d39598c89 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Wed, 12 Jun 2024 18:23:38 +0200 Subject: [PATCH 7/7] chore: add changelog 136.enh --- docs/changes/newsfragments/136.enh | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/changes/newsfragments/136.enh diff --git a/docs/changes/newsfragments/136.enh b/docs/changes/newsfragments/136.enh new file mode 100644 index 000000000..d0a495563 --- /dev/null +++ b/docs/changes/newsfragments/136.enh @@ -0,0 +1 @@ +Add test to be sure that :class:`.JuniferNiftiSpheresMasker` with mean aggregation function behaves exactly as :class:`nilearn.maskers.NiftiSpheresMasker` by `Synchon Mandal`_ -- 2.52.0