Skip to content

Commit

Permalink
Merge branch 'clinicadl_v2' into caps_dataset_transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultdvx committed Dec 18, 2024
2 parents 61458a0 + e650b57 commit bac2315
Show file tree
Hide file tree
Showing 162 changed files with 3,821 additions and 3,332 deletions.
173 changes: 66 additions & 107 deletions clinicadl/API/complicated_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,120 +2,89 @@

import torchio.transforms as transforms

from clinicadl.dataset.caps_dataset import (
CapsDatasetPatch,
CapsDatasetRoi,
CapsDatasetSlice,
from clinicadl.data.dataloader import DataLoaderConfig
from clinicadl.data.datasets.caps_dataset import CapsDataset
from clinicadl.data.datasets.concat import ConcatDataset
from clinicadl.data.preprocessing import (
PreprocessingPET,
PreprocessingT1,
)
from clinicadl.dataset.caps_reader import CapsReader
from clinicadl.dataset.concat import ConcatDataset
from clinicadl.dataset.config.extraction import ExtractionConfig
from clinicadl.dataset.config.preprocessing import (
PreprocessingConfig,
T1PreprocessingConfig,
)
from clinicadl.dataset.dataloader_config import DataLoaderConfig
from clinicadl.experiment_manager.experiment_manager import ExperimentManager
from clinicadl.losses.config import CrossEntropyLossConfig
from clinicadl.losses.factory import get_loss_function
from clinicadl.model.clinicadl_model import ClinicaDLModel
from clinicadl.networks.config import ImplementedNetworks
from clinicadl.networks.factory import (
ConvEncoderOptions,
create_network_config,
get_network_from_config,
)
from clinicadl.optimization.optimizer.config import AdamConfig, OptimizerConfig
from clinicadl.optimization.optimizer.factory import get_optimizer
from clinicadl.predictor.predictor import Predictor
from clinicadl.splitter.kfold import KFolder
from clinicadl.splitter.split import get_single_split, split_tsv
from clinicadl.networks.config.resnet import ResNetConfig
from clinicadl.optim.optimizers.config import AdamConfig
from clinicadl.splitter import KFold, make_kfold, make_split
from clinicadl.trainer.trainer import Trainer
from clinicadl.transforms.extraction import Extraction, Image, Patch, Slice
from clinicadl.transforms.transforms import Transforms

# Create the Maps Manager / Read/write manager /
maps_path = Path("/")
manager = ExperimentManager(
maps_path, overwrite=False
) # a ajouter dans le manager: mlflow/ profiler/ etc ...

caps_directory = Path("caps_directory") # output of clinica pipelines
caps_reader = CapsReader(caps_directory, manager=manager)

preprocessing_1 = caps_reader.get_preprocessing("t1-linear")
caps_reader.prepare_data(
preprocessing=preprocessing_1, data_tsv=Path(""), n_proc=2
) # don't return anything -> just extract the image tensor and compute some information for each images

transforms_1 = Transforms(
object_augmentation=[transforms.Crop, transforms.Transform],
image_augmentation=[transforms.Crop, transforms.Transform],
extraction=ExtractionPatchConfig(patch_size=3),
image_transforms=[transforms.Blur, transforms.Ghosting],
object_transforms=[transforms.BiasField, transforms.Motion],
) # not mandatory

preprocessing_2 = caps_reader.get_preprocessing("pet-linear")
caps_reader.prepare_data(
preprocessing=preprocessing_1, data_tsv=Path(""), n_proc=2
) # to extract the tensor of the PET file this time
transforms_2 = Transforms(
object_augmentation=[transforms.Crop, transforms.Transform],
image_augmentation=[transforms.Crop, transforms.Transform],
extraction=ExtractionSliceConfig(),
image_transforms=[transforms.Blur, transforms.Ghosting],
object_transforms=[transforms.BiasField, transforms.Motion],
caps_directory = Path(
"/Users/camille.brianceau/aramis/CLINICADL/caps"
) # output of clinica pipelines

sub_ses_t1 = Path("/Users/camille.brianceau/aramis/CLINICADL/caps/subjects_t1.tsv")
preprocessing_t1 = PreprocessingT1()
transforms_image = Transforms(
image_augmentation=[transforms.RandomMotion()],
extraction=Image(),
image_transforms=[transforms.Blur((0.5, 0.6, 0.3))],
)

sub_ses_tsv_1 = Path("")
split_dir_1 = split_tsv(sub_ses_tsv_1) # -> creer un test.tsv et un train.tsv

sub_ses_tsv_2 = Path("")
split_dir_2 = split_tsv(sub_ses_tsv_2)

dataset_t1_roi = caps_reader.get_dataset(
preprocessing=preprocessing_1,
sub_ses_tsv=split_dir_1 / "train.tsv",
transforms=transforms_1,
) # do we give config or object for transforms ?
dataset_pet_patch = caps_reader.get_dataset(
preprocessing=preprocessing_2,
sub_ses_tsv=split_dir_2 / "train.tsv",
transforms=transforms_2,
print("T1 and image ")

dataset_t1_image = CapsDataset(
caps_directory=caps_directory,
data=sub_ses_t1,
preprocessing=preprocessing_t1,
transforms=transforms_image,
)
dataset_t1_image.prepare_data(n_proc=2) # to extract the tensor of the T1 file


sub_ses_pet_45 = Path(
"/Users/camille.brianceau/aramis/CLINICADL/caps/subjects_pet_18FAV45.tsv"
)
preprocessing_pet_45 = PreprocessingPET(tracer="18FAV45", suvr_reference_region="pons2") # type: ignore

dataset_pet_image = CapsDataset(
caps_directory=caps_directory,
data=sub_ses_pet_45,
preprocessing=preprocessing_pet_45,
transforms=transforms_image,
)
dataset_t1_image.prepare_data(n_proc=2) # to extract the tensor of the PET file


dataset_multi_modality_multi_extract = ConcatDataset(
dataset_multi_modality = ConcatDataset(
[
dataset_t1_roi,
dataset_pet_patch,
caps_reader.get_dataset_from_json(json_path=Path("dataset.json")),
dataset_t1_image,
dataset_pet_image,
]
) # 3 train.tsv en entrée qu'il faut concat et pareil pour les transforms à faire attention

# TODO : think about adding transforms in extract_json

# CAS CROSS-VALIDATION

split_dir = make_split(sub_ses_t1, n_test=0.2) # Optional data tsv and output_dir
fold_dir = make_kfold(split_dir / "train.tsv", n_splits=2)

splitter = KFold(fold_dir)


maps_path = Path("/")
manager = ExperimentManager(maps_path, overwrite=False)

config_file = Path("config_file")
trainer = Trainer.from_json(config_file=config_file, manager=manager)

# CAS CROSS-VALIDATION
splitter = KFolder(caps_dataset=dataset_multi_modality_multi_extract, manager=manager)
split_dir = splitter.make_splits(
n_splits=3, output_dir=Path(""), subset_name="validation", stratification=""
) # Optional data tsv and output_dir

dataloader_config = DataLoaderConfig(n_procs=3, batch_size=10)

for split in splitter.get_splits(splits=(0, 3, 4), dataloader_config=dataloader_config):
# bien définir ce qu'il y a dans l'objet split
for split in splitter.get_splits(dataset=dataset_t1_image):
train_loader = split.build_train_loader(batch_size=2)
val_loader = split.build_val_loader(DataLoaderConfig())

network_config = create_network_config(ImplementedNetworks.CNN)(
in_shape=[2, 2, 2],
num_outputs=1,
conv_args=ConvEncoderOptions(channels=[3, 2, 2]),
)
model = ClinicaDLModelClassif.from_config(
network_config=network_config,
model = ClinicaDLModel.from_config(
network_config=ResNetConfig(num_outputs=1, spatial_dims=1, in_channels=1),
loss_config=CrossEntropyLossConfig(),
optimizer_config=AdamConfig(),
)
Expand All @@ -125,20 +94,10 @@

# TEST

preprocessing_test = caps_reader.get_preprocessing("pet-linear")
transforms_test = Transforms(
object_augmentation=[transforms.Crop, transforms.Transform],
image_augmentation=[transforms.Crop, transforms.Transform],
extraction=ExtractioImageConfig(),
image_transforms=[transforms.Blur, transforms.Ghosting],
object_transforms=[transforms.BiasField, transforms.Motion],
)

dataset_test = caps_reader.get_dataset(
preprocessing=preprocessing_test,
sub_ses_tsv=split_dir_1 / "test.tsv", # test only on data from the first dataset
transforms=transforms_test,
dataset_test = CapsDataset(
caps_directory=caps_directory,
preprocessing=preprocessing_t1,
data=Path("test.tsv"), # test only on data from the first dataset
transforms=transforms_image,
)

predictor = Predictor(model=model, manager=manager)
predictor.predict(dataset_test=dataset_test, split_number=2)
77 changes: 16 additions & 61 deletions clinicadl/API/cross_val.py
Original file line number Diff line number Diff line change
@@ -1,80 +1,35 @@
from pathlib import Path

import torchio.transforms as transforms

from clinicadl.dataset.caps_dataset import (
CapsDatasetPatch,
CapsDatasetRoi,
CapsDatasetSlice,
)
from clinicadl.dataset.caps_reader import CapsReader
from clinicadl.dataset.concat import ConcatDataset
from clinicadl.dataset.config.extraction import ExtractionConfig
from clinicadl.dataset.config.preprocessing import (
PreprocessingConfig,
T1PreprocessingConfig,
)
from clinicadl.dataset.dataloader_config import DataLoaderConfig
from clinicadl.experiment_manager.experiment_manager import ExperimentManager
from clinicadl.losses.config import CrossEntropyLossConfig
from clinicadl.losses.factory import get_loss_function
from clinicadl.model.clinicadl_model import ClinicaDLModel
from clinicadl.networks.config import ImplementedNetworks
from clinicadl.networks.factory import (
ConvEncoderOptions,
create_network_config,
get_network_from_config,
)
from clinicadl.optimization.optimizer.config import AdamConfig, OptimizerConfig
from clinicadl.optimization.optimizer.factory import get_optimizer
from clinicadl.predictor.predictor import Predictor
from clinicadl.splitter.kfold import KFolder
from clinicadl.splitter.split import get_single_split, split_tsv
from clinicadl.trainer.trainer import Trainer
from clinicadl.transforms.config import TransformsConfig
from clinicadl.data.dataloader import DataLoaderConfig
from clinicadl.data.datasets import CapsDataset
from clinicadl.experiment_manager import ExperimentManager
from clinicadl.splitter import KFold, make_kfold, make_split
from clinicadl.trainer import Trainer

# SIMPLE EXPERIMENT WITH A CAPS ALREADY EXISTING

maps_path = Path("/")
manager = ExperimentManager(maps_path, overwrite=False)

dataset_t1_image = CapsDatasetPatch.from_json(
extraction=Path("test.json"),
sub_ses_tsv=Path("split_dir") / "train.tsv",
)
dataset_t1_image = CapsDataset.from_json(Path("json_path.json"))

config_file = Path("config_file")
trainer = Trainer.from_json(
config_file=config_file, manager=manager
) # gpu, amp, fsdp, seed

# CAS CROSS-VALIDATION
splitter = KFolder(caps_dataset=dataset_t1_image, manager=manager)
split_dir = splitter.make_splits(
n_splits=3,
output_dir=Path(""),
data_tsv=Path("labels.tsv"),
subset_name="validation",
stratification="",
split_dir = make_split(
dataset_t1_image.df, n_test=0.2, subset_name="validation", output_dir="test"
) # Optional data tsv and output_dir
# n_splits must be >1
# for the single split case, this method output a path to the directory containing the train and test tsv files so we should have the same output here
fold_dir = make_kfold(split_dir / "train.tsv", n_splits=2)

# CAS EXISTING CROSS-VALIDATION
splitter = KFolder.from_split_dir(caps_dataset=dataset_t1_image, manager=manager)
splitter = KFold(fold_dir)

# define the needed parameters for the dataloader
dataloader_config = DataLoaderConfig(n_procs=3, batch_size=10)

for split in splitter.get_splits(splits=(0, 3, 4), dataloader_config=dataloader_config):
# bien définir ce qu'il y a dans l'objet split
# define the needed parameters for the dataloader
dataloader_config = DataLoaderConfig(num_workers=3, batch_size=10)

network_config = create_network_config(ImplementedNetworks.CNN)(
in_shape=[2, 2, 2],
num_outputs=1,
conv_args=ConvEncoderOptions(channels=[3, 2, 2]),
)
optimizer, _ = get_optimizer(network, AdamConfig())
model = ClinicaDLModel(network=network_config, loss=nn.MSE(), optimizer=optimizer)

trainer.train(model, split)
# le trainer va instancier un predictor/valdiator dans le train ou dans le init
for split in splitter.get_splits(dataset=dataset_t1_image):
split.build_train_loader(dataloader_config)
split.build_val_loader(num_workers=3, batch_size=10)
Loading

0 comments on commit bac2315

Please sign in to comment.