From 7731f6c4a6262049daa67474c15ec727a4b0fc63 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Thu, 5 Dec 2024 17:23:25 +0100 Subject: [PATCH 1/4] update!: provide target image to get_template --- junifer/data/template_spaces.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/junifer/data/template_spaces.py b/junifer/data/template_spaces.py index 1ec253580..42b826727 100644 --- a/junifer/data/template_spaces.py +++ b/junifer/data/template_spaces.py @@ -122,7 +122,7 @@ def get_xfm( def get_template( space: str, - target_data: dict[str, Any], + target_img: nib.Nifti1Image, extra_input: Optional[dict[str, Any]] = None, template_type: str = "T1w", ) -> nib.Nifti1Image: @@ -132,9 +132,9 @@ def get_template( ---------- space : str The name of the template space. - target_data : dict - The corresponding item of the data object for which the template space - will be loaded. + target_img : Nifti1Image + The corresponding image for which the template space will be loaded. + This is used to obtain the best matching resolution. extra_input : dict, optional The other fields in the data object. Useful for accessing other data types (default None). @@ -163,7 +163,6 @@ def get_template( raise_error(f"Unknown template type: {template_type}") # Get the min of the voxels sizes and use it as the resolution - target_img = target_data["data"] resolution = np.min(target_img.header.get_zooms()[:3]).astype(int) # Fetch available resolutions for the template -- 2.52.0 From c5cfeebfb65af4fda699bace0ae420b8a65a3cf0 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Thu, 5 Dec 2024 17:25:29 +0100 Subject: [PATCH 2/4] chore: update tests for get_template --- junifer/data/tests/test_template_spaces.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/junifer/data/tests/test_template_spaces.py b/junifer/data/tests/test_template_spaces.py index d52964259..1b0fe211a 100644 --- a/junifer/data/tests/test_template_spaces.py +++ b/junifer/data/tests/test_template_spaces.py @@ -61,7 +61,9 @@ def test_get_template(template_type: str) -> None: bold = element_data["BOLD"] # Get tailored parcellation tailored_template = get_template( - space=bold["space"], target_data=bold, template_type=template_type + space=bold["space"], + target_img=bold["data"], + template_type=template_type, ) assert isinstance(tailored_template, nib.Nifti1Image) @@ -74,7 +76,7 @@ def test_get_template_invalid_space() -> None: vbm_gm = element_data["VBM_GM"] # Get tailored parcellation with pytest.raises(ValueError, match="Unknown template space:"): - get_template(space="andromeda", target_data=vbm_gm) + get_template(space="andromeda", target_img=vbm_gm["data"]) def test_get_template_invalid_template_type() -> None: @@ -87,7 +89,7 @@ def test_get_template_invalid_template_type() -> None: with pytest.raises(ValueError, match="Unknown template type:"): get_template( space=vbm_gm["space"], - target_data=vbm_gm, + target_img=vbm_gm["data"], template_type="xenon", ) @@ -100,5 +102,7 @@ def test_get_template_closest_resolution() -> None: vbm_gm = element_data["VBM_GM"] # Change header resolution to fetch closest resolution element_data["VBM_GM"]["data"].header.set_zooms((3, 3, 3)) - template = get_template(space=vbm_gm["space"], target_data=vbm_gm) + template = get_template( + space=vbm_gm["space"], target_img=vbm_gm["data"] + ) assert isinstance(template, nib.Nifti1Image) -- 2.52.0 From c2ac5194609b9a1b26b95b0cf384eabcc675804c Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Thu, 5 Dec 2024 17:28:29 +0100 Subject: [PATCH 3/4] chore: update usage of get_template --- junifer/data/masks/_ants_mask_warper.py | 2 +- junifer/data/masks/_masks.py | 4 ++-- junifer/data/parcellations/_ants_parcellation_warper.py | 2 +- junifer/preprocess/warping/_ants_warper.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/junifer/data/masks/_ants_mask_warper.py b/junifer/data/masks/_ants_mask_warper.py index 466e9cd5a..5b91cd381 100644 --- a/junifer/data/masks/_ants_mask_warper.py +++ b/junifer/data/masks/_ants_mask_warper.py @@ -125,7 +125,7 @@ class ANTsMaskWarper: # Get template space image template_space_img = get_template( space=dst, - target_data=target_data, + target_img=mask_img, extra_input=None, ) # Save template to a tempfile diff --git a/junifer/data/masks/_masks.py b/junifer/data/masks/_masks.py index 779aa3eab..86bd9cb10 100644 --- a/junifer/data/masks/_masks.py +++ b/junifer/data/masks/_masks.py @@ -106,15 +106,15 @@ def compute_brain_mask( if entry["dst"] == "native": target_std_space = entry["src"] + target_img = target_data["data"] # Fetch template in closest resolution template = get_template( space=target_std_space, - target_data=target_data, + target_img=target_img, extra_input=extra_input, template_type=mask_type, ) # Resample template to target image - target_img = target_data["data"] resampled_template = resample_to_img( source_img=template, target_img=target_img ) diff --git a/junifer/data/parcellations/_ants_parcellation_warper.py b/junifer/data/parcellations/_ants_parcellation_warper.py index e30c184bd..d9a429d09 100644 --- a/junifer/data/parcellations/_ants_parcellation_warper.py +++ b/junifer/data/parcellations/_ants_parcellation_warper.py @@ -131,7 +131,7 @@ class ANTsParcellationWarper: # Get template space image template_space_img = get_template( space=dst, - target_data=target_data, + target_img=parcellation_img, extra_input=None, ) # Save template to a tempfile diff --git a/junifer/preprocess/warping/_ants_warper.py b/junifer/preprocess/warping/_ants_warper.py index ed9ab4ca2..98112f51a 100644 --- a/junifer/preprocess/warping/_ants_warper.py +++ b/junifer/preprocess/warping/_ants_warper.py @@ -189,7 +189,7 @@ class ANTsWarper: # Get template space image template_space_img = get_template( space=reference, - target_data=input, + target_img=input["data"], extra_input=None, ) # Save template -- 2.52.0 From 3a2feabc46f908c164bd4d97ec08a81734fbeded Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Thu, 5 Dec 2024 17:32:43 +0100 Subject: [PATCH 4/4] chore: add changelog 413.change --- docs/changes/newsfragments/413.change | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/changes/newsfragments/413.change diff --git a/docs/changes/newsfragments/413.change b/docs/changes/newsfragments/413.change new file mode 100644 index 000000000..8538387d2 --- /dev/null +++ b/docs/changes/newsfragments/413.change @@ -0,0 +1 @@ +``target_data`` parameter of :func:`.get_template` has been replaced with ``target_img`` by `Fede Raimondo`_ -- 2.52.0