Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Move crop_nifti function #1156

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions clinica/pipelines/pet_linear/pet_linear_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ def _build_core_nodes(self):
from nipype.interfaces import ants

import clinica.pipelines.pet_linear.pet_linear_utils as utils
from clinica.pipelines.tasks import crop_nifti_task

# Utilitary nodes
init_node = npe.Node(
Expand Down Expand Up @@ -379,12 +380,12 @@ def _build_core_nodes(self):
crop_nifti_node = npe.Node(
name="cropNifti",
interface=nutil.Function(
function=utils.crop_nifti,
input_names=["input_img", "ref_img"],
function=crop_nifti_task,
input_names=["input_image", "reference_image"],
output_names=["output_img"],
),
)
crop_nifti_node.inputs.ref_img = self.ref_crop
crop_nifti_node.inputs.reference_image = self.ref_crop

# 5. Print end message
print_end_message = npe.Node(
Expand Down Expand Up @@ -470,7 +471,7 @@ def _build_core_nodes(self):
(
normalize_intensity_node,
crop_nifti_node,
[("output_img", "input_img")],
[("output_img", "input_image")],
),
(
crop_nifti_node,
Expand Down
31 changes: 0 additions & 31 deletions clinica/pipelines/pet_linear/pet_linear_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,37 +102,6 @@ def suvr_normalization(
return output_img


def crop_nifti(input_img: str, ref_img: str) -> str:
"""Crop input image based on the reference.

It uses nilearn `resample_to_img` function.

Parameters
----------
input_img : str
Path to the input image.

ref_img : str
Path to the reference image used for cropping.

Returns
-------
output_img : str
Path to the cropped image.
"""
from pathlib import Path

from nilearn.image import resample_to_img

basedir = Path.cwd()
# resample the individual MRI into the cropped template image
crop_img = resample_to_img(input_img, ref_img, force_resample=True)
crop_filename = Path(input_img.split(".nii")[0] + "_cropped.nii.gz")
output_img = basedir / crop_filename
crop_img.to_filename(str(output_img))
return str(output_img)


def rename_into_caps(
pet_filename_bids: str,
pet_filename_raw: str,
Expand Down
13 changes: 7 additions & 6 deletions clinica/pipelines/t1_linear/anat_linear_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,10 @@ def _build_core_nodes(self):
import nipype.pipeline.engine as npe
from nipype.interfaces import ants

from clinica.pipelines.tasks import crop_nifti_task
from clinica.utils.filemanip import get_filename_no_ext

from .anat_linear_utils import crop_nifti, print_end_pipeline
from .anat_linear_utils import print_end_pipeline

image_id_node = npe.Node(
interface=nutil.Function(
Expand Down Expand Up @@ -304,12 +305,12 @@ def _build_core_nodes(self):
cropnifti = npe.Node(
name="cropnifti",
interface=nutil.Function(
function=crop_nifti,
input_names=["input_img", "ref_crop"],
output_names=["output_img", "crop_template"],
function=crop_nifti_task,
input_names=["input_image", "reference_image"],
output_names=["output_img"],
),
)
cropnifti.inputs.ref_crop = self.ref_crop
cropnifti.inputs.reference_image = self.ref_crop

# 4. Print end message
print_end_message = npe.Node(
Expand Down Expand Up @@ -353,7 +354,7 @@ def _build_core_nodes(self):
(
ants_registration_node,
cropnifti,
[("warped_image", "input_img")],
[("warped_image", "input_image")],
),
(cropnifti, self.output_node, [("output_img", "outfile_crop")]),
(cropnifti, print_end_message, [("output_img", "final_file")]),
Expand Down
40 changes: 0 additions & 40 deletions clinica/pipelines/t1_linear/anat_linear_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,46 +54,6 @@ def _get_substitutions_datasink(bids_image_id: str, suffix: str) -> list:
]


# Function used by the nipype interface.
# It crops an image based on the reference.
def crop_nifti(input_img, ref_crop):
"""Crop input image based on the reference.

It uses nilearn `resample_to_img` function.

Args:
input_img (str): image to be processed
ref_crop (str): template used to crop the image

Returns:
output_img (NIfTI image): crop image on disk.
crop_template: (NIfTI image): output template on disk.
"""
import os

from nilearn.image import crop_img, resample_to_img

basedir = os.getcwd()
# crop_ref = crop_img(ref_img, rtol=0.5)
# crop_ref.to_filename(os.path.join(basedir, os.path.basename(input_img).split('.nii')[0] + '_cropped_template.nii.gz'))
# crop_template = os.path.join(basedir, os.path.basename(input_img).split('.nii')[0] + '_cropped_template.nii.gz')

# resample the individual MRI into the cropped template image
crop_img = resample_to_img(input_img, ref_crop, force_resample=True)
crop_img.to_filename(
os.path.join(
basedir, os.path.basename(input_img).split(".nii")[0] + "_cropped.nii.gz"
)
)

output_img = os.path.join(
basedir, os.path.basename(input_img).split(".nii")[0] + "_cropped.nii.gz"
)
crop_template = ref_crop

return output_img, crop_template


def print_end_pipeline(anat, final_file):
"""Display end message for <subject_id> when <final_file> is connected."""
from clinica.utils.filemanip import get_subject_id
Expand Down
9 changes: 9 additions & 0 deletions clinica/pipelines/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""This module contains Nipype tasks used in several pipelines."""


def crop_nifti_task(input_image: str, reference_image: str) -> str:
from pathlib import Path

from clinica.utils.image import crop_nifti

return str(crop_nifti(Path(input_image), Path(reference_image)))
4 changes: 2 additions & 2 deletions clinica/pydra/pet_linear/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ def crop_nifti_task(input_img: os.PathLike, ref_img: os.PathLike) -> os.PathLike
"""
from pathlib import Path

from clinica.pipelines.pet_linear.pet_linear_utils import crop_nifti
from clinica.utils.image import crop_nifti

return Path(crop_nifti(str(input_img), str(ref_img)))
return crop_nifti(input_img, ref_img)
31 changes: 31 additions & 0 deletions clinica/utils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,34 @@ def remove_dummy_dimension_from_image(image: str, output: str) -> str:
nib.save(img, output)

return output


def crop_nifti(input_image: Path, reference_image: Path) -> Path:
"""Crop input image based on the reference.

It uses nilearn `resample_to_img` function.

Parameters
----------
input_image : Path
Path to the input image.

reference_image : Path
Path to the reference image used for cropping.

Returns
-------
output_img : Path
Path to the cropped image.
"""
from pathlib import Path

from nilearn.image import resample_to_img

basedir = Path.cwd()
# resample the individual MRI into the cropped template image
crop_img = resample_to_img(input_image, reference_image, force_resample=True)
crop_filename = str(input_image).split(".nii")[0] + "_cropped.nii.gz"
output_img = basedir / crop_filename
crop_img.to_filename(output_img)
return output_img
Loading