From a0f883bbfb2911db61ab9c6f3874eecd217f71b1 Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Wed, 13 Nov 2024 13:49:15 +0100 Subject: [PATCH 1/3] refactor: update compute_brain_mask with improved space detection when working in native space --- junifer/data/masks/_masks.py | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/junifer/data/masks/_masks.py b/junifer/data/masks/_masks.py index a868ccf30..a81affc1e 100644 --- a/junifer/data/masks/_masks.py +++ b/junifer/data/masks/_masks.py @@ -47,7 +47,6 @@ _masks_path = Path(__file__).parent def compute_brain_mask( target_data: Dict[str, Any], - extra_input: Optional[Dict[str, Any]] = None, mask_type: str = "brain", threshold: float = 0.5, ) -> "Nifti1Image": @@ -61,9 +60,6 @@ def compute_brain_mask( target_data : dict The corresponding item of the data object for which mask will be loaded. - extra_input : dict, optional - The other fields in the data object. Useful for accessing other data - types (default None). mask_type : {"brain", "gm", "wm"}, optional Type of mask to be computed: @@ -94,26 +90,19 @@ def compute_brain_mask( # Check pre-requirements for space manipulation target_space = target_data["space"] - # Set target standard space to target space - target_std_space = target_space - # Extra data type requirement check if target space is native + # Conditional for native space-warped target data if target_space == "native": - # Check for extra inputs - if extra_input is None: - raise_error( - "No extra input provided, requires `Warp` " - "data type to infer target template space." - ) - # Set target standard space to warp file space source - for entry in extra_input["Warp"]: - if entry["dst"] == "native": - target_std_space = entry["src"] + # prewarp_space is added in SpaceWarper and should be there + target_std_space = target_data["prewarp_space"] + else: + # Set target standard space to target space + target_std_space = target_space # Fetch template in closest resolution template = get_template( space=target_std_space, target_data=target_data, - extra_input=extra_input, + extra_input=None, template_type=mask_type, ) # Resample template to target image -- 2.52.0 From afc8c9a6245cc4ef09b17027fad9f852aa0d611d Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Wed, 13 Nov 2024 15:28:18 +0100 Subject: [PATCH 2/3] chore: fix tests for compute_brain_mask --- junifer/data/masks/tests/test_masks.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/junifer/data/masks/tests/test_masks.py b/junifer/data/masks/tests/test_masks.py index a6d6ef49b..7b37b3c93 100644 --- a/junifer/data/masks/tests/test_masks.py +++ b/junifer/data/masks/tests/test_masks.py @@ -64,8 +64,8 @@ def test_compute_brain_mask(mask_type: str, threshold: float) -> None: element_data = DefaultDataReader().fit_transform(dg["sub-01"]) mask = compute_brain_mask( target_data=element_data["BOLD"], - extra_input=None, mask_type=mask_type, + threshold=threshold, ) assert isinstance(mask, nib.nifti1.Nifti1Image) @@ -104,7 +104,6 @@ def test_compute_brain_mask_for_native(mask_type: str) -> None: ) mask = compute_brain_mask( target_data=element_data["BOLD"], - extra_input=None, mask_type=mask_type, ) assert isinstance(mask, nib.nifti1.Nifti1Image) -- 2.52.0 From 90dd3958945779cde9b1fb4c16940295f0c1c19e Mon Sep 17 00:00:00 2001 From: Synchon Mandal Date: Wed, 13 Nov 2024 16:19:25 +0100 Subject: [PATCH 3/3] chore: add changelog 394.bugfix --- docs/changes/newsfragments/394.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/changes/newsfragments/394.bugfix diff --git a/docs/changes/newsfragments/394.bugfix b/docs/changes/newsfragments/394.bugfix new file mode 100644 index 000000000..898446427 --- /dev/null +++ b/docs/changes/newsfragments/394.bugfix @@ -0,0 +1 @@ +Refactor ``compute_brain_mask``'s interface and internals to not fail during native space warping by `Synchon Mandal`_ -- 2.52.0