From 6b284c73ab1c8cd4a9f85f95f010d3d0747d630d Mon Sep 17 00:00:00 2001 From: NicolasGensollen Date: Fri, 29 Nov 2024 09:23:17 +0100 Subject: [PATCH] T1Linear considers visits processed if both image and transformation matrix are found --- .../t1_linear/anat_linear_pipeline.py | 47 ++++++++++++++----- clinica/utils/testing_utils.py | 13 ++--- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/clinica/pipelines/t1_linear/anat_linear_pipeline.py b/clinica/pipelines/t1_linear/anat_linear_pipeline.py index cb79cb22d..e7e2808e3 100644 --- a/clinica/pipelines/t1_linear/anat_linear_pipeline.py +++ b/clinica/pipelines/t1_linear/anat_linear_pipeline.py @@ -69,22 +69,45 @@ def __init__( ) 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 T1w image registered + to the MNI152NLin2009cSym template and the affine transformation estimated with ANTs + 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 T1W_LINEAR, T1W_LINEAR_CROPPED + from clinica.utils.input_files import ( + T1W_LINEAR, + T1W_LINEAR_CROPPED, + T1W_TO_MNI_TRANSFORM, + ) 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, - T1W_LINEAR - if self.parameters.get("uncropped_image", False) - else T1W_LINEAR_CROPPED, + if not self.caps_directory.is_dir(): + return [] + images, _ = clinica_file_reader( + self.subjects, + self.sessions, + self.caps_directory, + T1W_LINEAR + if self.parameters.get("uncropped_image", False) + else T1W_LINEAR_CROPPED, + ) + visits_having_image = extract_visits(images) + transformation, _ = clinica_file_reader( + self.subjects, + self.sessions, + self.caps_directory, + T1W_TO_MNI_TRANSFORM, + ) + visits_having_transformation = extract_visits(transformation) + return sorted( + list( + set(visits_having_image).intersection(set(visits_having_transformation)) ) - processed_visits.extend(extract_visits(cropped_files)) - return processed_visits + ) def _check_custom_dependencies(self) -> None: """Check dependencies that can not be listed in the `info.json` file.""" diff --git a/clinica/utils/testing_utils.py b/clinica/utils/testing_utils.py index 92daa2052..03c978285 100644 --- a/clinica/utils/testing_utils.py +++ b/clinica/utils/testing_utils.py @@ -223,14 +223,11 @@ def _build_subjects(directory: Path, configuration: dict) -> None: def _build_t1_linear(directory: Path, sub: str, ses: str, config: dict) -> None: """Build a fake t1-linear file structure in a CAPS directory.""" uncropped = config.get("uncropped_image", False) - ( - directory - / "subjects" - / sub - / ses - / "t1_linear" - / f"{sub}_{ses}_space-MNI152NLin2009cSym{'' if uncropped else '_desc-Crop'}_res-1x1x1_T1w.nii.gz" - ).touch() + for filename in ( + f"{sub}_{ses}_space-MNI152NLin2009cSym{'' if uncropped else '_desc-Crop'}_res-1x1x1_T1w.nii.gz", + f"{sub}_{ses}_space-MNI152NLin2009cSym_res-1x1x1_affine.mat", + ): + (directory / "subjects" / sub / ses / "t1_linear" / filename).touch() def _build_pet_linear(directory: Path, sub: str, ses: str, config: dict) -> None: