From 2e2b6e31a592cbb31d4ba17b3ba3eec263e06bc7 Mon Sep 17 00:00:00 2001 From: NicolasGensollen Date: Fri, 29 Nov 2024 09:00:07 +0100 Subject: [PATCH] PetLinear considers visits processed if both image and transformation matrix are found --- clinica/pipelines/pet/linear/pipeline.py | 51 ++++++++++++++----- .../pipelines/pet/test_pet_linear_pipeline.py | 14 +++++ 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/clinica/pipelines/pet/linear/pipeline.py b/clinica/pipelines/pet/linear/pipeline.py index ffdf13525..a54f531d9 100644 --- a/clinica/pipelines/pet/linear/pipeline.py +++ b/clinica/pipelines/pet/linear/pipeline.py @@ -33,24 +33,47 @@ def _check_custom_dependencies(self) -> None: pass def get_processed_visits(self) -> list[Visit]: + """Return a list of visits for which the pipeline is assumed to have run already. + + Before running the pipeline, for a given visit, if both the PET SUVR registered image + and the rigid transformation files already exist, then the visit is added to this list. + The pipeline will further skip these visits and run processing only for the remaining + visits. + """ from clinica.utils.filemanip import extract_visits - from clinica.utils.input_files import pet_linear_nii + from clinica.utils.input_files import ( + pet_linear_nii, + pet_linear_transformation_matrix, + ) from clinica.utils.inputs import clinica_file_reader - processed_visits: list[Visit] = [] - if self.caps_directory.is_dir(): - cropped_files, _ = clinica_file_reader( - self.subjects, - self.sessions, - self.caps_directory, - pet_linear_nii( - acq_label=self.parameters["acq_label"], - suvr_reference_region=self.parameters["suvr_reference_region"], - uncropped_image=self.parameters.get("uncropped_image", False), - ), + if not self.caps_directory.is_dir(): + return [] + pet_registered_image, _ = clinica_file_reader( + self.subjects, + self.sessions, + self.caps_directory, + pet_linear_nii( + acq_label=self.parameters["acq_label"], + suvr_reference_region=self.parameters["suvr_reference_region"], + uncropped_image=self.parameters.get("uncropped_image", False), + ), + ) + visits_having_pet_image = extract_visits(pet_registered_image) + transformation, _ = clinica_file_reader( + self.subjects, + self.sessions, + self.caps_directory, + pet_linear_transformation_matrix(tracer=self.parameters["acq_label"]), + ) + visits_having_transformation = extract_visits(transformation) + return sorted( + list( + set(visits_having_pet_image).intersection( + set(visits_having_transformation) + ) ) - processed_visits.extend(extract_visits(cropped_files)) - return processed_visits + ) def get_input_fields(self) -> List[str]: """Specify the list of possible inputs of this pipeline. diff --git a/test/unittests/pipelines/pet/test_pet_linear_pipeline.py b/test/unittests/pipelines/pet/test_pet_linear_pipeline.py index 4587b3cbc..41e85af54 100644 --- a/test/unittests/pipelines/pet/test_pet_linear_pipeline.py +++ b/test/unittests/pipelines/pet/test_pet_linear_pipeline.py @@ -155,3 +155,17 @@ def test_pet_linear_get_processed_visits(tmp_path, mocker): Visit("sub-01", "ses-M006"), Visit("sub-02", "ses-M000"), ] + + # We remove the transformation matrix of the tracer of interest for sub-01 and session M006 + ( + caps + / "subjects" + / "sub-01" + / "ses-M006" + / "pet_linear" + / "sub-01_ses-M006_trc-18FFDG_pet_space-T1w_rigid.mat" + ).unlink() + + # The corresponding visit is not considered as "processed" anymore because the transformation is + # missing (even though the pet image is still here...) + assert pipeline.get_processed_visits() == [Visit("sub-02", "ses-M000")]