From 8b64e5ffe4beebb6fb8c97ded650e077184f01fb Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Fri, 7 Jul 2023 15:24:12 +0200 Subject: [PATCH 1/8] chore: improve docstrings in junifer.data.parcellations --- junifer/data/parcellations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/junifer/data/parcellations.py b/junifer/data/parcellations.py index aa67a1f3e..331e09823 100644 --- a/junifer/data/parcellations.py +++ b/junifer/data/parcellations.py @@ -308,7 +308,7 @@ def _retrieve_parcellation( ``n_rois`` : {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000} Granularity of parcellation to be used. ``yeo_network`` : {7, 17}, optional - Number of yeo networks to use (default 7). + Number of Yeo networks to use (default 7). * Tian : ``scale`` : {1, 2, 3, 4} Scale of parcellation (defines granularity). @@ -430,7 +430,7 @@ def _retrieve_schaefer( n_rois : {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000}, optional Granularity of the parceallation to be used (default None). yeo_networks : {7, 17}, optional - Number of yeo networks to use (default 7). + Number of Yeo networks to use (default 7). Returns ------- -- 2.52.0 From f4186e6d2286eaa267411b61c80fcc993c919d51 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Fri, 7 Jul 2023 15:24:32 +0200 Subject: [PATCH 2/8] update: improve logging in junifer.data.parcellations --- junifer/data/parcellations.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/junifer/data/parcellations.py b/junifer/data/parcellations.py index 331e09823..d8ab8e06d 100644 --- a/junifer/data/parcellations.py +++ b/junifer/data/parcellations.py @@ -447,9 +447,9 @@ def _retrieve_schaefer( """ logger.info("Parcellation parameters:") + logger.info(f"\tresolution: {resolution}") logger.info(f"\tn_rois: {n_rois}") logger.info(f"\tyeo_networks: {yeo_networks}") - logger.info(f"\tresolution: {resolution}") _valid_n_rois = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000] _valid_networks = [7, 17] @@ -555,10 +555,11 @@ def _retrieve_tian( """ # show parameters to user logger.info("Parcellation parameters:") + logger.info(f"\tresolution: {resolution}") logger.info(f"\tscale: {scale}") logger.info(f"\tspace: {space}") logger.info(f"\tmagneticfield: {magneticfield}") - logger.info(f"\tresolution: {resolution}") + # check validity of parameters _valid_scales = [1, 2, 3, 4] if scale not in _valid_scales: @@ -714,6 +715,7 @@ def _retrieve_suit( """ logger.info("Parcellation parameters:") + logger.info(f"\tresolution: {resolution}") logger.info(f"\tspace: {space}") _valid_spaces = ["MNI", "SUIT"] @@ -825,6 +827,7 @@ def _retrieve_aicha( """ # show parameters to user logger.info("Parcellation parameters:") + logger.info(f"\tresolution: {resolution}") logger.info(f"\tversion: {version}") # Check version value @@ -963,6 +966,7 @@ def _retrieve_shen( # noqa: C901 """ # show parameters to user logger.info("Parcellation parameters:") + logger.info(f"\tresolution: {resolution}") logger.info(f"\tyear: {year}") logger.info(f"\tn_rois: {n_rois}") -- 2.52.0 From 768a74106f36120064ff2d56b00c7ac0bb291635 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Fri, 7 Jul 2023 15:25:53 +0200 Subject: [PATCH 3/8] refactor: parametrize Schaefer parcellation loading test --- junifer/data/tests/test_parcellations.py | 137 ++++++++++++----------- 1 file changed, 72 insertions(+), 65 deletions(-) diff --git a/junifer/data/tests/test_parcellations.py b/junifer/data/tests/test_parcellations.py index e1a4a2e46..b9d853cf9 100644 --- a/junifer/data/tests/test_parcellations.py +++ b/junifer/data/tests/test_parcellations.py @@ -207,84 +207,91 @@ def test_retrieve_parcellation_incorrect() -> None: _retrieve_parcellation("wrongparcellation") -# TODO: paramdtrize test -def test_schaefer_parcellation(tmp_path: Path) -> None: +@pytest.mark.parametrize( + "resolution, n_rois, yeo_networks", + [ + (1.0, 100, 7), + (1.0, 200, 7), + (1.0, 300, 7), + (1.0, 400, 7), + (1.0, 500, 7), + (1.0, 600, 7), + (1.0, 700, 7), + (1.0, 800, 7), + (1.0, 900, 7), + (1.0, 1000, 7), + (2.0, 100, 7), + (2.0, 200, 7), + (2.0, 300, 7), + (2.0, 400, 7), + (2.0, 500, 7), + (2.0, 600, 7), + (2.0, 700, 7), + (2.0, 800, 7), + (2.0, 900, 7), + (2.0, 1000, 7), + (1.0, 100, 17), + (1.0, 200, 17), + (1.0, 300, 17), + (1.0, 400, 17), + (1.0, 500, 17), + (1.0, 600, 17), + (1.0, 700, 17), + (1.0, 800, 17), + (1.0, 900, 17), + (1.0, 1000, 17), + (2.0, 100, 17), + (2.0, 200, 17), + (2.0, 300, 17), + (2.0, 400, 17), + (2.0, 500, 17), + (2.0, 600, 17), + (2.0, 700, 17), + (2.0, 800, 17), + (2.0, 900, 17), + (2.0, 1000, 17), + ], +) +def test_schaefer( + tmp_path: Path, + resolution: float, + n_rois: int, + yeo_networks: int, +) -> None: """Test Schaefer parcellation. Parameters ---------- tmp_path : pathlib.Path The path to the test directory. + resolution : float + The parametrized resolution values. + n_rois : int + The parametrized ROI count values. + yeo_networks : int + The parametrized Yeo networks values. """ parcellations = list_parcellations() - for n_rois in range(100, 1001, 100): - for t_net in [7, 17]: - t_name = f"Schaefer{n_rois}x{t_net}" - assert t_name in parcellations + parcellation_name = f"Schaefer{n_rois}x{yeo_networks}" + assert parcellation_name in parcellations - # Define parcellation file names - fname1 = "Schaefer2018_100Parcels_7Networks_order_FSLMNI152_1mm.nii.gz" - fname2 = "Schaefer2018_100Parcels_7Networks_order_FSLMNI152_2mm.nii.gz" - - # Load parcellation - img, lbl, fname = load_parcellation( - name="Schaefer100x7", parcellations_dir=str(tmp_path.absolute()) + parcellation_file = ( + f"Schaefer2018_{n_rois}Parcels_{yeo_networks}Networks_order_FSLMNI152_" + f"{int(resolution)}mm.nii.gz" + ) + # Load parcellation + img, label, img_path = load_parcellation( + name=parcellation_name, + parcellations_dir=tmp_path, + resolution=resolution, ) - # Check parcellation values assert img is not None - assert fname.name == fname1 - assert len(lbl) == 100 - assert_array_equal(img.header["pixdim"][1:4], [1, 1, 1]) # type: ignore - - # Test with Path - img, lbl, fname = load_parcellation( - name="Schaefer100x7", parcellations_dir=tmp_path + assert img_path.name == parcellation_file + assert len(label) == n_rois + assert_array_equal( + img.header["pixdim"][1:4], 3 * [resolution] # type: ignore ) - # Load parcellation - img2, lbl, fname = load_parcellation( - name="Schaefer100x7", parcellations_dir=tmp_path, resolution=3 - ) - # Check parcellation values - assert fname.name == fname2 - assert len(lbl) == 100 - assert img2 is not None - assert_array_equal(img2.header["pixdim"][1:4], [2, 2, 2]) # type: ignore - # Load parcellation - img2, lbl, fname = load_parcellation( - "Schaefer100x7", parcellations_dir=tmp_path, resolution=2.1 - ) - # Check parcellation values - assert fname.name == fname2 - assert len(lbl) == 100 - assert img2 is not None - assert_array_equal(img2.header["pixdim"][1:4], [2, 2, 2]) # type: ignore - # Load parcellation - img2, lbl, fname = load_parcellation( - "Schaefer100x7", parcellations_dir=tmp_path, resolution=1.99 - ) - # Check parcellation values - assert fname.name == fname1 - assert len(lbl) == 100 - assert img2 is not None - assert_array_equal(img2.header["pixdim"][1:4], [1, 1, 1]) # type: ignore - # Load parcellation - img2, lbl, fname = load_parcellation( - "Schaefer100x7", parcellations_dir=tmp_path, resolution=0.5 - ) - # Check parcellation values - assert fname.name == fname1 - assert len(lbl) == 100 - assert img2 is not None - assert_array_equal(img2.header["pixdim"][1:4], [1, 1, 1]) # type: ignore - - -def test_load_parcellation_schaefer() -> None: - """Test Schaefer parcellation loading.""" - img, lbl, fname = load_parcellation(name="Schaefer100x7") - assert img is not None - home_dir = Path().home() / "junifer" / "data" / "parcellations" - assert home_dir in fname.parents def test_retrieve_schaefer_incorrect_n_rois(tmp_path: Path) -> None: -- 2.52.0 From c0ef13b439b2f378737539366561b3d4298ca8b5 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Fri, 7 Jul 2023 15:26:36 +0200 Subject: [PATCH 4/8] chore: improve docstrings in junifer.data.parcellations tests --- junifer/data/tests/test_parcellations.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/junifer/data/tests/test_parcellations.py b/junifer/data/tests/test_parcellations.py index b9d853cf9..fdc396d7c 100644 --- a/junifer/data/tests/test_parcellations.py +++ b/junifer/data/tests/test_parcellations.py @@ -295,7 +295,7 @@ def test_schaefer( def test_retrieve_schaefer_incorrect_n_rois(tmp_path: Path) -> None: - """Test retrieve schaefer with incorrect n_rois. + """Test retrieve Schaefer with incorrect ROIs. Parameters ---------- @@ -313,7 +313,7 @@ def test_retrieve_schaefer_incorrect_n_rois(tmp_path: Path) -> None: def test_retrieve_schaefer_incorrect_yeo_networks(tmp_path: Path) -> None: - """Test retrieve schaefer with incorrect yeo_networks. + """Test retrieve Schaefer with incorrect Yeo networks. Parameters ---------- @@ -376,7 +376,7 @@ def test_suit(tmp_path: Path) -> None: def test_retrieve_suit_incorrect_space(tmp_path: Path) -> None: - """Test retrieve suit with incorrect space. + """Test retrieve SUIT with incorrect space. Parameters ---------- -- 2.52.0 From c257896bdf53ae353a8f23611068885eb61d3a69 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Fri, 7 Jul 2023 15:27:37 +0200 Subject: [PATCH 5/8] refactor: parametrize SUIT parcellation loading test --- junifer/data/tests/test_parcellations.py | 43 +++++++----------------- 1 file changed, 13 insertions(+), 30 deletions(-) diff --git a/junifer/data/tests/test_parcellations.py b/junifer/data/tests/test_parcellations.py index fdc396d7c..b3b9e165f 100644 --- a/junifer/data/tests/test_parcellations.py +++ b/junifer/data/tests/test_parcellations.py @@ -330,48 +330,31 @@ def test_retrieve_schaefer_incorrect_yeo_networks(tmp_path: Path) -> None: ) -# TODO: parametrize test -def test_suit(tmp_path: Path) -> None: +@pytest.mark.parametrize( + "space", + ["SUIT", "MNI"], +) +def test_suit(tmp_path: Path, space: str) -> None: """Test SUIT parcellation. Parameters ---------- tmp_path : pathlib.Path The path to the test directory. + space : str + The parametrized space values. """ parcellations = list_parcellations() - assert "SUITxSUIT" in parcellations - assert "SUITxMNI" in parcellations - + assert f"SUITx{space}" in parcellations # Load parcellation - img, lbl, fname = load_parcellation( - name="SUITxSUIT", parcellations_dir=tmp_path + img, label, img_path = load_parcellation( + name=f"SUITx{space}", + parcellations_dir=tmp_path, ) - fname1 = "SUIT_SUITSpace_1mm.nii" assert img is not None - assert fname.name == fname1 - assert len(lbl) == 34 - assert_array_equal(img.header["pixdim"][1:4], [1, 1, 1]) # type: ignore - - # Load parcellation - img, lbl, fname = load_parcellation( - name="SUITxSUIT", parcellations_dir=tmp_path - ) - fname1 = "SUIT_SUITSpace_1mm.nii" - assert img is not None - assert fname.name == fname1 - assert len(lbl) == 34 - assert_array_equal(img.header["pixdim"][1:4], [1, 1, 1]) # type: ignore - - # Load parcellation - img, lbl, fname = load_parcellation( - name="SUITxMNI", parcellations_dir=tmp_path - ) - fname1 = "SUIT_MNISpace_1mm.nii" - assert img is not None - assert fname.name == fname1 - assert len(lbl) == 34 + assert img_path.name == f"SUIT_{space}Space_1mm.nii" + assert len(label) == 34 assert_array_equal(img.header["pixdim"][1:4], [1, 1, 1]) # type: ignore -- 2.52.0 From 9ecffbf68860a60fb7eaadb4600f4c1487cd709e Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Fri, 7 Jul 2023 15:27:59 +0200 Subject: [PATCH 6/8] chore: code improvements in junifer.data.parcellations tests --- junifer/data/tests/test_parcellations.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/junifer/data/tests/test_parcellations.py b/junifer/data/tests/test_parcellations.py index b3b9e165f..542a9e1fb 100644 --- a/junifer/data/tests/test_parcellations.py +++ b/junifer/data/tests/test_parcellations.py @@ -368,7 +368,9 @@ def test_retrieve_suit_incorrect_space(tmp_path: Path) -> None: """ with pytest.raises(ValueError, match=r"The parameter `space`"): - _retrieve_suit(parcellations_dir=tmp_path, resolution=1, space="wrong") + _retrieve_suit( + parcellations_dir=tmp_path, resolution=1.0, space="wrong" + ) @pytest.mark.parametrize( @@ -631,7 +633,9 @@ def test_shen( assert f"Shen_{year}_{n_rois}" in parcellations # Load parcellation img, label, img_path = load_parcellation( - name=f"Shen_{year}_{n_rois}", parcellations_dir=tmp_path + name=f"Shen_{year}_{n_rois}", + parcellations_dir=tmp_path, + resolution=resolution, ) assert img is not None assert img_name in img_path.name -- 2.52.0 From f9896ffe969deb89e576b03d3056f39e41beab37 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Fri, 7 Jul 2023 15:28:21 +0200 Subject: [PATCH 7/8] docs: improve builtin.rst --- docs/builtin.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/builtin.rst b/docs/builtin.rst index 9657f49de..df6323a75 100644 --- a/docs/builtin.rst +++ b/docs/builtin.rst @@ -269,7 +269,7 @@ Available | A spatially unbiased atlas template of the human cerebellum. | NeuroImage, Volume 33(1), Pages 127–138 (2006). | https://doi.org/10.1016/j.neuroimage.2006.05.056 - * - TIAN + * - Tian - ``scale``, ``space``, ``magneticfield`` - | ``TianxS1x3TxMNI6thgeneration``, ``TianxS1x7TxMNI6thgeneration``, | ``TianxS2x3TxMNI6thgeneration``, ``TianxS2x7TxMNI6thgeneration``, @@ -301,7 +301,7 @@ Available - | Shen, X., Tokoglu, F., Papademetris, X., Constable, R.T. | Groupwise whole-brain parcellation from resting-state fMRI data | for network node identification. - | Neuroimage, Volume 82 (2013). + | NeuroImage, Volume 82 (2013). | https://doi.org/10.1016/j.neuroimage.2013.05.081. | Finn, E.S., Shen, X., Scheinost, D., et al. | Functional connectome fingerprinting: identifying individuals using @@ -353,7 +353,7 @@ Planned * - AAL - | Rolls, E.T., Huang, C.C., Lin, C.P., et al. | Automated anatomical labelling atlas 3. - | Neuroimage, Volume 206 (2020). + | NeuroImage, Volume 206 (2020). | https://doi.org/10.1016/j.neuroimage.2019.116189 * - Mindboggle 101 - | Klein, A., & Tourville, J. -- 2.52.0 From 4fd281fb8efdfdf6c444c7fad1c92aed736e3fdd Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Fri, 7 Jul 2023 15:39:55 +0200 Subject: [PATCH 8/8] chore: add changelog 238.misc --- docs/changes/newsfragments/238.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/changes/newsfragments/238.misc diff --git a/docs/changes/newsfragments/238.misc b/docs/changes/newsfragments/238.misc new file mode 100644 index 000000000..7e598e512 --- /dev/null +++ b/docs/changes/newsfragments/238.misc @@ -0,0 +1 @@ +Improve docstrings and code style and parametrize remaining tests for ``junifer.data.parcellations`` by `Synchon Mandal`_ -- 2.52.0