Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasGensollen committed Nov 6, 2024
1 parent 1c9d3d5 commit eb5dba8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ def _build_input_node(self):
save_part_sess_long_ids_to_tsv,
)
from clinica.utils.exceptions import ClinicaException
from clinica.utils.input_files import T1_FS_DESTRIEUX, T1_FS_T_DESTRIEUX
from clinica.utils.input_files import (
Parcellation,
QueryPatternName,
query_pattern_factory,
)
from clinica.utils.inputs import (
clinica_file_reader,
format_clinica_file_reader_errors,
Expand Down Expand Up @@ -119,19 +123,26 @@ def _build_input_node(self):
) = extract_subject_session_longitudinal_ids_from_filename(
to_process_ids
)

pattern_segmentation = query_pattern_factory(
QueryPatternName.T1_FREESURFER_SEGMENTATION
)(Parcellation.DESTRIEUX)
_, errors_destrieux = clinica_file_reader(
self.subjects, self.sessions, self.caps_directory, T1_FS_DESTRIEUX
self.subjects, self.sessions, self.caps_directory, pattern_segmentation
)
pattern_template = query_pattern_factory(
QueryPatternName.T1_FREESURFER_TEMPLATE
)(Parcellation.DESTRIEUX)
_, errors_t_destrieux = clinica_file_reader(
self.subjects, list_long_id, self.caps_directory, T1_FS_T_DESTRIEUX
self.subjects, list_long_id, self.caps_directory, pattern_template
)
all_errors = [errors_destrieux, errors_t_destrieux]

if any(all_errors):
message = "Clinica faced errors while trying to read files in your CAPS directory.\n"
for error, info in zip(all_errors, [T1_FS_DESTRIEUX, T1_FS_T_DESTRIEUX]):
message += format_clinica_file_reader_errors(error, info)
for error, pattern in zip(
all_errors, [pattern_segmentation, pattern_template]
):
message += format_clinica_file_reader_errors(error, pattern)
raise ClinicaException(message)

save_part_sess_long_ids_to_tsv(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
from typing import List

from clinica.pipelines.engine import Pipeline
from clinica.utils.input_files import (
Parcellation,
QueryPatternName,
query_pattern_factory,
)


class T1FreeSurferTemplate(Pipeline):
Expand All @@ -17,7 +22,6 @@ def get_processed_images(
) -> List[str]:
import re

from clinica.utils.input_files import T1_FS_T_DESTRIEUX
from clinica.utils.inputs import clinica_file_reader
from clinica.utils.longitudinal import get_long_id
from clinica.utils.participant import get_unique_subjects
Expand All @@ -28,11 +32,13 @@ def get_processed_images(
list_long_id = [
get_long_id(list_session_ids) for list_session_ids in list_list_session_ids
]

image_ids: List[str] = []
if caps_directory.is_dir():
pattern = query_pattern_factory(QueryPatternName.T1_FREESURFER_TEMPLATE)(
Parcellation.DESTRIEUX
)
t1_freesurfer_files, _ = clinica_file_reader(
list_participant_id, list_long_id, caps_directory, T1_FS_T_DESTRIEUX
list_participant_id, list_long_id, caps_directory, pattern
)
image_ids = [
re.search(r"(sub-[a-zA-Z0-9]+)_(long-[a-zA-Z0-9]+)", file).group()
Expand Down Expand Up @@ -88,9 +94,7 @@ def _build_input_node(self):
from clinica.pipelines.anatomical.freesurfer.longitudinal.utils import (
save_part_sess_long_ids_to_tsv,
)
from clinica.utils.exceptions import ClinicaCAPSError, ClinicaException
from clinica.utils.filemanip import extract_subjects_sessions_from_filename
from clinica.utils.input_files import T1_FS_DESTRIEUX
from clinica.utils.inputs import clinica_file_filter
from clinica.utils.longitudinal import (
get_long_id,
Expand Down Expand Up @@ -149,11 +153,12 @@ def _build_input_node(self):
self.subjects, self.sessions = extract_subjects_sessions_from_filename(
to_process_ids
)

pattern = query_pattern_factory(QueryPatternName.T1_FREESURFER_SEGMENTATION)(
Parcellation.DESTRIEUX
)
_, self.subjects, self.sessions = clinica_file_filter(
self.subjects, self.sessions, self.caps_directory, T1_FS_DESTRIEUX
self.subjects, self.sessions, self.caps_directory, pattern
)

long_ids = get_participants_long_id(self.subjects, self.sessions)
save_part_sess_long_ids_to_tsv(
self.subjects, self.sessions, long_ids, self.base_dir / self.name
Expand Down
10 changes: 6 additions & 4 deletions clinica/pipelines/machine_learning/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def get_images(self):
"""
Returns: a list of filenames
"""
from clinica.utils.input_files import pet_volume_normalized_suvr_pet
from clinica.utils.input_files import QueryPatternName, query_pattern_factory
from clinica.utils.inputs import clinica_file_reader

if self._images is not None:
Expand Down Expand Up @@ -183,8 +183,10 @@ def get_images(self):
raise Exception("File %s doesn't exists." % image)

elif self._input_params["image_type"] == "PET":
caps_files_information = pet_volume_normalized_suvr_pet(
acq_label=self._input_params["acq_label"],
pattern = query_pattern_factory(
QueryPatternName.PET_VOLUME_NORMALIZED_SUVR
)(
tracer=self._input_params["acq_label"],
group_label=self._input_params["group_label"],
suvr_reference_region=self._input_params["suvr_reference_region"],
use_brainmasked_image=True,
Expand All @@ -195,7 +197,7 @@ def get_images(self):
self._subjects,
self._sessions,
self._input_params["caps_directory"],
caps_files_information,
pattern,
)
else:
raise ValueError(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from pathlib import Path
from typing import List

from clinica.pipelines.engine import Pipeline
from clinica.utils.input_files import (
QueryPattern,
QueryPatternName,
query_pattern_factory,
)


class SpatialSVM(Pipeline):
Expand Down Expand Up @@ -53,16 +59,10 @@ def get_output_fields(self) -> List[str]:

def _build_input_node(self):
"""Build and connect an input node to the pipeline."""
import os

import nipype.interfaces.utility as nutil
import nipype.pipeline.engine as npe

from clinica.utils.exceptions import ClinicaCAPSError, ClinicaException
from clinica.utils.input_files import (
pet_volume_normalized_suvr_pet,
t1_volume_final_group_template,
)
from clinica.utils.inputs import (
clinica_file_reader,
clinica_group_reader,
Expand All @@ -86,19 +86,18 @@ def _build_input_node(self):
),
)
all_errors = []

if self.parameters["orig_input_data_ml"] == "t1-volume":
caps_files_information = {
"pattern": os.path.join(
"t1",
"spm",
"dartel",
"group-" + self.parameters["group_label"],
"*_T1w_segm-graymatter_space-Ixi549Space_modulated-on_probability.nii.gz",
pattern = QueryPattern(
str(
Path("t1")
/ "spm"
/ "dartel"
/ f"group-{self.parameters['group_label']}"
/ "*_T1w_segm-graymatter_space-Ixi549Space_modulated-on_probability.nii.gz"
),
"description": "graymatter tissue segmented in T1w MRI in Ixi549 space",
"needed_pipeline": "t1-volume-tissue-segmentation",
}
"graymatter tissue segmented in T1w MRI in Ixi549 space",
"t1-volume-tissue-segmentation",
)
elif self.parameters["orig_input_data_ml"] == "pet-volume":
if not (
self.parameters["acq_label"]
Expand All @@ -110,8 +109,11 @@ def _build_input_node(self):
f"- suvr_reference_region: {self.parameters['suvr_reference_region']}\n"
f"- use_pvc_data: {self.parameters['use_pvc_data']}\n"
)
caps_files_information = pet_volume_normalized_suvr_pet(
acq_label=self.parameters["acq_label"],
pattern = query_pattern_factory(
QueryPatternName.PET_VOLUME_NORMALIZED_SUVR
)(
tracer=self.parameters["acq_label"],
group_label=self.parameters["group_label"],
suvr_reference_region=self.parameters["suvr_reference_region"],
use_brainmasked_image=False,
use_pvc_data=self.parameters["use_pvc_data"],
Expand All @@ -121,27 +123,18 @@ def _build_input_node(self):
raise ValueError(
f"Image type {self.parameters['orig_input_data_ml']} unknown."
)

input_image, caps_error = clinica_file_reader(
self.subjects,
self.sessions,
self.caps_directory,
caps_files_information,
self.subjects, self.sessions, self.caps_directory, pattern
)
if caps_error:
all_errors.append(
format_clinica_file_reader_errors(caps_error, caps_files_information)
)

all_errors.append(format_clinica_file_reader_errors(caps_error, pattern))
try:
dartel_input = clinica_group_reader(
self.caps_directory,
t1_volume_final_group_template(self.parameters["group_label"]),
pattern = query_pattern_factory(QueryPatternName.T1_VOLUME_GROUP_TEMPLATE)(
self.parameters["group_label"]
)
dartel_input = clinica_group_reader(self.caps_directory, pattern)
except ClinicaException as e:
all_errors.append(e)

# Raise all errors if some happened
if any(all_errors):
error_message = "Clinica faced errors while trying to read files in your CAPS directories.\n"
for msg in all_errors:
Expand Down

0 comments on commit eb5dba8

Please sign in to comment.