Skip to content

Commit

Permalink
CHange extract/preprocessing/modality names (#626)
Browse files Browse the repository at this point in the history
* change preprocessing to extraction and modality to preprocessing
  • Loading branch information
camillebrianceau authored Jun 19, 2024
1 parent 3547ca1 commit 5625612
Show file tree
Hide file tree
Showing 31 changed files with 557 additions and 568 deletions.
74 changes: 37 additions & 37 deletions clinicadl/caps_dataset/caps_dataset_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,60 @@

from clinicadl.caps_dataset.data_config import DataConfig
from clinicadl.caps_dataset.dataloader_config import DataLoaderConfig
from clinicadl.config.config.modality import (
CustomModalityConfig,
DTIModalityConfig,
FlairModalityConfig,
ModalityConfig,
PETModalityConfig,
T1ModalityConfig,
)
from clinicadl.preprocessing import config as preprocessing
from clinicadl.caps_dataset.extraction import config as extraction
from clinicadl.caps_dataset.preprocessing import config as preprocessing
from clinicadl.transforms.config import TransformsConfig
from clinicadl.utils.enum import ExtractionMethod, Preprocessing


def get_preprocessing(extract_method: ExtractionMethod):
def get_extraction(extract_method: ExtractionMethod):
if extract_method == ExtractionMethod.ROI:
return preprocessing.PreprocessingROIConfig
return extraction.ExtractionROIConfig
elif extract_method == ExtractionMethod.SLICE:
return preprocessing.PreprocessingSliceConfig
return extraction.ExtractionSliceConfig
elif extract_method == ExtractionMethod.IMAGE:
return preprocessing.PreprocessingImageConfig
return extraction.ExtractionImageConfig
elif extract_method == ExtractionMethod.PATCH:
return preprocessing.PreprocessingPatchConfig
return extraction.ExtractionPatchConfig
else:
raise ValueError(f"Modality {extract_method.value} is not implemented.")
raise ValueError(f"Preprocessing {extract_method.value} is not implemented.")


def get_modality(preprocessing: Preprocessing):
if (
preprocessing == Preprocessing.T1_EXTENSIVE
or preprocessing == Preprocessing.T1_LINEAR
):
return T1ModalityConfig
elif preprocessing == Preprocessing.PET_LINEAR:
return PETModalityConfig
elif preprocessing == Preprocessing.FLAIR_LINEAR:
return FlairModalityConfig
elif preprocessing == Preprocessing.CUSTOM:
return CustomModalityConfig
elif preprocessing == Preprocessing.DWI_DTI:
return DTIModalityConfig
def get_preprocessing(preprocessing_type: Preprocessing):
if preprocessing_type == Preprocessing.T1_LINEAR:
return preprocessing.T1PreprocessingConfig
elif preprocessing_type == Preprocessing.PET_LINEAR:
return preprocessing.PETPreprocessingConfig
elif preprocessing_type == Preprocessing.FLAIR_LINEAR:
return preprocessing.FlairPreprocessingConfig
elif preprocessing_type == Preprocessing.CUSTOM:
return preprocessing.CustomPreprocessingConfig
elif preprocessing_type == Preprocessing.DWI_DTI:
return preprocessing.DTIPreprocessingConfig
else:
raise ValueError(f"Preprocessing {preprocessing.value} is not implemented.")
raise ValueError(
f"Preprocessing {preprocessing_type.value} is not implemented."
)


class CapsDatasetConfig(BaseModel):
"""Config class for CapsDataset object.
caps_directory, preprocessing_json, extract_method, preprocessing
are arguments that must be passed by the user.
transforms isn't optional because there is always at least one transform (NanRemoval)
"""

class CapsDatasetBase(BaseModel):
data: DataConfig
dataloader: DataLoaderConfig
modality: ModalityConfig
extraction: extraction.ExtractionConfig
preprocessing: preprocessing.PreprocessingConfig
transforms: Optional[TransformsConfig]
transforms: TransformsConfig

# pydantic config
model_config = ConfigDict(validate_assignment=True, arbitrary_types_allowed=True)


class CapsDatasetConfig(CapsDatasetBase):
@classmethod
def from_preprocessing_and_extraction_method(
cls,
Expand All @@ -70,7 +68,9 @@ def from_preprocessing_and_extraction_method(
return cls(
data=DataConfig(**kwargs),
dataloader=DataLoaderConfig(**kwargs),
modality=get_modality(Preprocessing(preprocessing_type))(**kwargs),
preprocessing=get_preprocessing(ExtractionMethod(extraction))(**kwargs),
preprocessing=get_preprocessing(Preprocessing(preprocessing_type))(
**kwargs
),
extraction=get_extraction(ExtractionMethod(extraction))(**kwargs),
transforms=TransformsConfig(**kwargs),
)
47 changes: 25 additions & 22 deletions clinicadl/caps_dataset/caps_dataset_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from pathlib import Path
from typing import Dict, Optional, Tuple, Union

from pydantic import BaseModel, ConfigDict
from typing import Dict, Optional, Tuple

from clinicadl.caps_dataset.caps_dataset_config import CapsDatasetConfig
from clinicadl.caps_dataset.preprocessing.config import (
CustomPreprocessingConfig,
DTIPreprocessingConfig,
FlairPreprocessingConfig,
PETPreprocessingConfig,
T1PreprocessingConfig,
)
from clinicadl.utils.enum import LinearModality, Preprocessing


Expand All @@ -17,14 +22,12 @@ def compute_folder_and_file_type(
pet_linear_nii,
)

preprocessing = Preprocessing(
config.preprocessing.preprocessing
) # replace("-", "_")
preprocessing = config.preprocessing.preprocessing
if from_bids is not None:
if preprocessing == Preprocessing.CUSTOM:
if isinstance(config.preprocessing, CustomPreprocessingConfig):
mod_subfolder = Preprocessing.CUSTOM.value
file_type = {
"pattern": f"*{config.modality.custom_suffix}",
"pattern": f"*{config.preprocessing.custom_suffix}",
"description": "Custom suffix",
}
else:
Expand All @@ -33,34 +36,34 @@ def compute_folder_and_file_type(

elif preprocessing not in Preprocessing:
raise NotImplementedError(
f"Extraction of preprocessing {config.preprocessing.preprocessing.value} is not implemented from CAPS directory."
f"Extraction of preprocessing {preprocessing} is not implemented from CAPS directory."
)
else:
mod_subfolder = preprocessing.value.replace("-", "_")
if preprocessing == Preprocessing.T1_LINEAR:
if isinstance(config.preprocessing, T1PreprocessingConfig):
file_type = linear_nii(
LinearModality.T1W, config.preprocessing.use_uncropped_image
LinearModality.T1W, config.extraction.use_uncropped_image
)

elif preprocessing == Preprocessing.FLAIR_LINEAR:
elif isinstance(config.preprocessing, FlairPreprocessingConfig):
file_type = linear_nii(
LinearModality.FLAIR, config.preprocessing.use_uncropped_image
LinearModality.FLAIR, config.extraction.use_uncropped_image
)

elif preprocessing == Preprocessing.PET_LINEAR:
elif isinstance(config.preprocessing, PETPreprocessingConfig):
file_type = pet_linear_nii(
config.modality.tracer,
config.modality.suvr_reference_region,
config.preprocessing.use_uncropped_image,
config.preprocessing.tracer,
config.preprocessing.suvr_reference_region,
config.extraction.use_uncropped_image,
)
elif preprocessing == Preprocessing.DWI_DTI:
elif isinstance(config.preprocessing, DTIPreprocessingConfig):
file_type = dwi_dti(
config.modality.dti_measure,
config.modality.dti_space,
config.preprocessing.dti_measure,
config.preprocessing.dti_space,
)
elif preprocessing == Preprocessing.CUSTOM:
elif isinstance(config.preprocessing, CustomPreprocessingConfig):
file_type = {
"pattern": f"*{config.modality.custom_suffix}",
"pattern": f"*{config.preprocessing.custom_suffix}",
"description": "Custom suffix",
}
# custom_suffix["use_uncropped_image"] = None
Expand Down
Loading

0 comments on commit 5625612

Please sign in to comment.