Skip to content

Commit

Permalink
Extract tensor from BIDS (aramis-lab#521)
Browse files Browse the repository at this point in the history
* add prepare-data-from-bids command
  • Loading branch information
camillebrianceau authored Feb 13, 2024
1 parent 06dd001 commit bc4d3a6
Show file tree
Hide file tree
Showing 9 changed files with 497 additions and 40 deletions.
4 changes: 4 additions & 0 deletions clinicadl/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from clinicadl.interpret.interpret_cli import cli as interpret_cli
from clinicadl.predict.predict_cli import cli as predict_cli
from clinicadl.prepare_data.prepare_data_cli import cli as prepare_data_cli
from clinicadl.prepare_data.prepare_data_from_bids_cli import (
cli as prepare_data_from_bids_cli,
)
from clinicadl.quality_check.qc_cli import cli as qc_cli
from clinicadl.random_search.random_search_cli import cli as random_search_cli
from clinicadl.train.train_cli import cli as train_cli
Expand Down Expand Up @@ -42,6 +45,7 @@ def cli(verbose):
cli.add_command(train_cli)
cli.add_command(generate_cli)
cli.add_command(prepare_data_cli)
cli.add_command(prepare_data_from_bids_cli)
cli.add_command(predict_cli)
cli.add_command(interpret_cli)
cli.add_command(qc_cli)
Expand Down
36 changes: 27 additions & 9 deletions clinicadl/prepare_data/prepare_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
from pathlib import Path


def DeepLearningPrepareData(caps_directory: Path, tsv_file: Path, n_proc, parameters):
def DeepLearningPrepareData(
caps_directory: Path,
tsv_file: Path,
n_proc: int,
parameters: dict,
from_bids: str = None,
):

from joblib import Parallel, delayed
from torch import save as save_tensor

Expand All @@ -20,12 +27,23 @@ def DeepLearningPrepareData(caps_directory: Path, tsv_file: Path, n_proc, parame
logger = getLogger("clinicadl.prepare_data")

# Get subject and session list
check_caps_folder(caps_directory)
logger.debug(f"CAPS directory: {caps_directory}.")
is_bids_dir = False
if from_bids is not None:
try:
input_directory = Path(from_bids)
except ClinicaDLArgumentError:
logger.warning("Your BIDS directory doesn't exist.")
logger.debug(f"BIDS directory: {input_directory}.")
is_bids_dir = True
else:
input_directory = caps_directory
check_caps_folder(input_directory)
logger.debug(f"CAPS directory: {input_directory}.")
is_bids_dir = False

subjects, sessions = get_subject_session_list(
caps_directory, tsv_file, is_bids_dir, False, None
input_directory, tsv_file, is_bids_dir, False, None
)

if parameters["prepare_dl"]:
logger.info(
f"{parameters['mode']}s will be extracted in Pytorch tensor from {len(sessions)} images."
Expand All @@ -46,11 +64,11 @@ def DeepLearningPrepareData(caps_directory: Path, tsv_file: Path, n_proc, parame
logger.debug(
f"Selected images are preprocessed with {parameters['preprocessing']} pipeline`."
)
mod_subfolder, file_type = compute_folder_and_file_type(parameters)
parameters["file_type"] = file_type

mod_subfolder, file_type = compute_folder_and_file_type(parameters, from_bids)
parameters["file_type"] = file_type
# Input file:
input_files = clinicadl_file_reader(subjects, sessions, caps_directory, file_type)[
input_files = clinicadl_file_reader(subjects, sessions, input_directory, file_type)[
0
]
logger.debug(f"Selected image file name list: {input_files}.")
Expand Down Expand Up @@ -145,7 +163,7 @@ def prepare_roi(file):
]

parameters["masks_location"] = (
caps_directory / "masks" / f"tpl-{parameters['roi_template']}"
input_directory / "masks" / f"tpl-{parameters['roi_template']}"
)

if len(parameters["roi_list"]) == 0:
Expand Down
Loading

0 comments on commit bc4d3a6

Please sign in to comment.