Skip to content

Commit

Permalink
[REF] Move PETLinear pipeline to new clinica.pipelines.pet module (
Browse files Browse the repository at this point in the history
…#1224)

* move PETLinear pipeline to new pet module

* update tests

* remove unused crop_nifti_image
  • Loading branch information
NicolasGensollen authored Jul 9, 2024
1 parent 70afb52 commit 68364a5
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 122 deletions.
2 changes: 1 addition & 1 deletion clinica/pipelines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
dwi,
machine_learning,
machine_learning_spatial_svm,
pet_linear,
pet,
pet_surface,
pet_volume,
statistics_surface,
Expand Down
1 change: 1 addition & 0 deletions clinica/pipelines/pet/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import linear
1 change: 1 addition & 0 deletions clinica/pipelines/pet/linear/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import cli
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def cli(

from clinica.utils.ux import print_end_pipeline

from .pet_linear_pipeline import PETLinear
from .pipeline import PETLinear

parameters = {
"acq_label": acq_label,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
"version": ">=2.2.0"
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,16 @@ def _build_output_node(self):

from clinica.utils.nipype import container_from_filename, fix_join

from .pet_linear_utils import rename_into_caps
from .tasks import rename_into_caps_task

write_node = npe.Node(name="WriteCaps", interface=DataSink())
write_node.inputs.base_directory = str(self.caps_directory)
write_node.inputs.parameterization = False

rename_file_node_inputs = [
"pet_filename_bids",
"pet_filename_raw",
"transformation_filename_raw",
"pet_bids_image_filename",
"pet_preprocessed_image_filename",
"pet_to_mri_transformation_filename",
"suvr_reference_region",
"uncropped_image",
]
Expand All @@ -167,7 +167,7 @@ def _build_output_node(self):
"transformation_filename_caps",
"pet_filename_in_t1w_caps",
],
function=rename_into_caps,
function=rename_into_caps_task,
),
name="renameFileCAPS",
)
Expand All @@ -193,11 +193,11 @@ def _build_output_node(self):
write_node,
[(("container", fix_join, "pet_linear"), "container")],
),
(self.input_node, rename_files, [("pet", "pet_filename_bids")]),
(self.input_node, rename_files, [("pet", "pet_bids_image_filename")]),
(
self.output_node,
rename_files,
[("affine_mat", "transformation_filename_raw")],
[("affine_mat", "pet_to_mri_transformation_filename")],
),
(
rename_files,
Expand All @@ -212,7 +212,7 @@ def _build_output_node(self):
(
self.output_node,
rename_files,
[("outfile_crop", "pet_filename_raw")],
[("outfile_crop", "pet_preprocessed_image_filename")],
),
(
rename_files,
Expand All @@ -227,7 +227,7 @@ def _build_output_node(self):
(
self.output_node,
rename_files,
[("suvr_pet", "pet_filename_raw")],
[("suvr_pet", "pet_preprocessed_image_filename")],
),
(
rename_files,
Expand Down Expand Up @@ -258,23 +258,24 @@ def _build_core_nodes(self):
import nipype.pipeline.engine as npe
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
from .tasks import perform_suvr_normalization_task
from .utils import concatenate_transforms, init_input_node, print_end_pipeline

init_node = npe.Node(
interface=nutil.Function(
input_names=["pet"],
output_names=["pet"],
function=utils.init_input_node,
function=init_input_node,
),
name="initPipeline",
)
concatenate_node = npe.Node(
interface=nutil.Function(
input_names=["pet_to_t1w_transform", "t1w_to_mni_transform"],
output_names=["transforms_list"],
function=utils.concatenate_transforms,
function=concatenate_transforms,
),
name="concatenateTransforms",
)
Expand Down Expand Up @@ -330,28 +331,32 @@ def _build_core_nodes(self):
normalize_intensity_node = npe.Node(
name="intensityNormalization",
interface=nutil.Function(
function=utils.suvr_normalization,
input_names=["input_img", "norm_img", "ref_mask"],
function=perform_suvr_normalization_task,
input_names=[
"pet_image_path",
"normalizing_image_path",
"reference_mask_path",
],
output_names=["output_img"],
),
)
normalize_intensity_node.inputs.ref_mask = self.ref_mask
normalize_intensity_node.inputs.reference_mask_path = self.ref_mask

# 4. Crop image (using nifti). It uses custom interface, from utils file
crop_nifti_node = npe.Node(
name="cropNifti",
interface=nutil.Function(
function=crop_nifti_task,
input_names=["input_image", "output_path"],
output_names=["output_img"],
output_names=["output_image"],
),
)
crop_nifti_node.inputs.output_path = self.base_dir

# 5. Print end message
print_end_message = npe.Node(
interface=nutil.Function(
input_names=["pet", "final_file"], function=utils.print_end_pipeline
input_names=["pet", "final_file"], function=print_end_pipeline
),
name="WriteEndMessage",
)
Expand Down Expand Up @@ -404,12 +409,12 @@ def _build_core_nodes(self):
(
ants_applytransform_node,
normalize_intensity_node,
[("output_image", "input_img")],
[("output_image", "pet_image_path")],
),
(
ants_applytransform_nonlinear_node,
normalize_intensity_node,
[("output_image", "norm_img")],
[("output_image", "normalizing_image_path")],
),
# Connect to DataSink
(
Expand Down Expand Up @@ -437,12 +442,12 @@ def _build_core_nodes(self):
(
crop_nifti_node,
self.output_node,
[("output_img", "outfile_crop")],
[("output_image", "outfile_crop")],
),
(
crop_nifti_node,
print_end_message,
[("output_img", "final_file")],
[("output_image", "final_file")],
),
]
)
Expand Down
55 changes: 55 additions & 0 deletions clinica/pipelines/pet/linear/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
def perform_suvr_normalization_task(
pet_image_path: str,
normalizing_image_path: str,
reference_mask_path: str,
) -> str:
from pathlib import Path

from clinica.pipelines.pet.linear.utils import perform_suvr_normalization

return str(
perform_suvr_normalization(
Path(pet_image_path),
Path(normalizing_image_path),
Path(reference_mask_path),
)
)


def rename_into_caps_task(
pet_bids_image_filename: str,
pet_preprocessed_image_filename: str,
pet_to_mri_transformation_filename: str,
suvr_reference_region: str,
uncropped_image: bool,
pet_filename_in_t1w_raw: str = None,
output_dir: str = None,
) -> tuple:
from pathlib import Path

from clinica.pipelines.pet.linear.utils import rename_into_caps

if pet_filename_in_t1w_raw:
pet_filename_in_t1w_raw = Path(pet_filename_in_t1w_raw)
if output_dir:
output_dir = Path(output_dir)
(
pet_filename_caps,
transformation_filename_caps,
pet_filename_in_t1w_caps,
) = rename_into_caps(
Path(pet_bids_image_filename),
Path(pet_preprocessed_image_filename),
Path(pet_to_mri_transformation_filename),
suvr_reference_region,
uncropped_image,
pet_filename_in_t1w_raw,
output_dir,
)
if pet_filename_in_t1w_caps:
pet_filename_in_t1w_caps = str(pet_filename_in_t1w_caps)
return (
str(pet_filename_caps),
str(transformation_filename_caps),
pet_filename_in_t1w_caps,
)
Loading

0 comments on commit 68364a5

Please sign in to comment.