Skip to content

Commit

Permalink
Added node to clip the image
Browse files Browse the repository at this point in the history
  • Loading branch information
ravih18 committed Oct 24, 2024
1 parent 5e2270d commit dbaf33b
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
38 changes: 34 additions & 4 deletions clinica/pipelines/pet/linear/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,11 @@ def _build_core_nodes(self):

from clinica.pipelines.tasks import crop_nifti_task

from .tasks import perform_suvr_normalization_task, remove_mni_background_task
from .tasks import (
clip_task,
perform_suvr_normalization_task,
remove_mni_background_task,
)
from .utils import concatenate_transforms, init_input_node, print_end_pipeline

init_node = npe.Node(
Expand All @@ -281,6 +285,16 @@ def _build_core_nodes(self):

# The core (processing) nodes

# 0. Optional, clipping node
clipping_node = npe.Node(
name="clipping",
interface=nutil.Function(
function=clip_task,
input_names=["input_pet"],
output_names=["output_image"],
),
)

# 1. `RegistrationSynQuick` by *ANTS*. It uses nipype interface.
ants_registration_node = npe.Node(
name="antsRegistration", interface=ants.RegistrationSynQuick()
Expand Down Expand Up @@ -354,11 +368,12 @@ def _build_core_nodes(self):

# 5. Remove background
remove_background_node = npe.Node(
name="removeBackground",
interface=nutil.Function(
function=remove_mni_background_task,
input_names=["input_image"],
output_names=["output_image"],
)
),
)
remove_background_node.mni_mask_path = self.mni_mask

Expand All @@ -376,12 +391,27 @@ def _build_core_nodes(self):
)
ants_applytransform_optional_node.inputs.dimension = 3

self.connect([(self.input_node, init_node, [("pet", "pet")])])
# STEP 0: Optional
if self.parameters.get("clip_min_0"):
self.connect(
[
(init_node, clipping_node, ["pet", "input_pet"]),
(
clipping_node,
ants_registration_node,
["output_image", "moving_image"],
),
]
)
else:
self.connect(
[(init_node, ants_registration_node, [("pet", "moving_image")])]
)
self.connect(
[
(self.input_node, init_node, [("pet", "pet")]),
# STEP 1
(self.input_node, ants_registration_node, [("t1w", "fixed_image")]),
(init_node, ants_registration_node, [("pet", "moving_image")]),
# STEP 2
(
ants_registration_node,
Expand Down
10 changes: 10 additions & 0 deletions clinica/pipelines/pet/linear/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ def remove_mni_background_task(
)


def clip_task(
input_pet: str,
) -> str:
from pathlib import Path

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

return str(clip_img(Path(input_pet)))


def rename_into_caps_task(
pet_bids_image_filename: str,
pet_preprocessed_image_filename: str,
Expand Down
20 changes: 20 additions & 0 deletions clinica/pipelines/pet/linear/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@ def remove_mni_background(
return output_image


def clip_img(
pet_image_path: Path,
) -> Path:
""" """
import nibabel as nib
import numpy as np

from clinica.utils.filemanip import get_filename_no_ext

pet_image = nib.load(pet_image_path)

data = np.clip(pet_image.get_fdata(dtype="float32"), a_min=0)

output_image = Path.cwd() / f"{get_filename_no_ext(pet_image_path)}_clipped.nii.gz"
clipped_pet = nib.Nifti1Image(data, pet_image.affine, header=pet_image.header)
clipped_pet.to_filename(output_image)

return output_image


def rename_into_caps(
pet_bids_image_filename: Path,
pet_preprocessed_image_filename: Path,
Expand Down

0 comments on commit dbaf33b

Please sign in to comment.