diff --git a/docs/changes/newsfragments/274.bugfix b/docs/changes/newsfragments/274.bugfix new file mode 100644 index 000000000..1ae09259f --- /dev/null +++ b/docs/changes/newsfragments/274.bugfix @@ -0,0 +1 @@ +Store native warped parcellations, coordinates and masks in element-scoped tempdirs for the pipeline to work by `Synchon Mandal`_ diff --git a/junifer/data/coordinates.py b/junifer/data/coordinates.py index 9bd903fa2..f27b5a8e9 100644 --- a/junifer/data/coordinates.py +++ b/junifer/data/coordinates.py @@ -240,24 +240,31 @@ def get_coordinates( f"{target_data['space']} space for further computation." ) - # Create tempdir + # Create component-scoped tempdir tempdir = WorkDirManager().get_tempdir(prefix="coordinates") - # Save existing coordinates + # Save existing coordinates to a component-scoped tempfile pretransform_coordinates_path = ( tempdir / "pretransform_coordinates.txt" ) np.savetxt(pretransform_coordinates_path, seeds) - # Create a tempfile for transformed coordinates output - std2imgcoord_out_path = tempdir / "coordinates_transformed.txt" + # Create element-scoped tempdir so that transformed coordinates is + # available later as numpy stores file path reference for + # loading on computation + element_tempdir = WorkDirManager().get_element_tempdir( + prefix="coordinates" + ) + + # Create an element-scoped tempfile for transformed coordinates output + std2imgcoord_out_path = element_tempdir / "coordinates_transformed.txt" # Set std2imgcoord command std2imgcoord_cmd = [ "std2imgcoord", f"-img {target_data['reference_path'].resolve()}", f"-warp {extra_input['Warp']['path'].resolve()}", - f"{pretransform_coordinates_path}", - f"> {std2imgcoord_out_path}", + f"{pretransform_coordinates_path.resolve()}", + f"> {std2imgcoord_out_path.resolve()}", ] # Call std2imgcoord std2imgcoord_cmd_str = " ".join(std2imgcoord_cmd) @@ -272,8 +279,6 @@ def get_coordinates( shell=True, # needed for respecting $PATH check=False, ) - # Delete saved coordinates file - pretransform_coordinates_path.unlink() # Check for success or failure if std2imgcoord_process.returncode == 0: logger.info( diff --git a/junifer/data/masks.py b/junifer/data/masks.py index 2837130cc..b96e18b04 100644 --- a/junifer/data/masks.py +++ b/junifer/data/masks.py @@ -344,15 +344,20 @@ def get_mask( # noqa: C901 f"{target_data['space']} space for further computation." ) - # Create tempdir + # Create component-scoped tempdir tempdir = WorkDirManager().get_tempdir(prefix="masks") - # Save mask image + # Save mask image to a component-scoped tempfile prewarp_mask_path = tempdir / "prewarp_mask.nii.gz" nib.save(mask_img, prewarp_mask_path) - # Create a tempfile for warped output - applywarp_out_path = tempdir / "mask_warped.nii.gz" + # Create element-scoped tempdir so that warped mask is + # available later as nibabel stores file path reference for + # loading on computation + element_tempdir = WorkDirManager().get_element_tempdir(prefix="masks") + + # Create an element-scoped tempfile for warped output + applywarp_out_path = element_tempdir / "mask_warped.nii.gz" # Set applywarp command applywarp_cmd = [ "applywarp", diff --git a/junifer/data/parcellations.py b/junifer/data/parcellations.py index 25b690691..d34ac08a9 100644 --- a/junifer/data/parcellations.py +++ b/junifer/data/parcellations.py @@ -289,15 +289,22 @@ def get_parcellation( f"{target_data['space']} space for further computation." ) - # Create tempdir + # Create component-scoped tempdir tempdir = WorkDirManager().get_tempdir(prefix="parcellations") - # Save parcellation image + # Save parcellation image to a component-scoped tempfile prewarp_parcellation_path = tempdir / "prewarp_parcellation.nii.gz" nib.save(resampled_parcellation_img, prewarp_parcellation_path) - # Create a tempfile for warped output - applywarp_out_path = tempdir / "parcellation_warped.nii.gz" + # Create element-scoped tempdir so that warped parcellation is + # available later as nibabel stores file path reference for + # loading on computation + element_tempdir = WorkDirManager().get_element_tempdir( + prefix="parcellations" + ) + + # Create an element-scoped tempfile for warped output + applywarp_out_path = element_tempdir / "parcellation_warped.nii.gz" # Set applywarp command applywarp_cmd = [ "applywarp", diff --git a/junifer/preprocess/fsl/bold_warper.py b/junifer/preprocess/fsl/bold_warper.py index 63d1c20a4..e5993c099 100644 --- a/junifer/preprocess/fsl/bold_warper.py +++ b/junifer/preprocess/fsl/bold_warper.py @@ -35,7 +35,7 @@ class BOLDWarper(BasePreprocessor): ] = [ { "name": "fsl", - "optional": True, + "optional": False, "commands": ["applywarp"], }, ]