diff --git a/clinica/iotools/converters/oasis_to_bids/oasis_to_bids.py b/clinica/iotools/converters/oasis_to_bids/oasis_to_bids.py index 5832966ff..206bbbc3f 100644 --- a/clinica/iotools/converters/oasis_to_bids/oasis_to_bids.py +++ b/clinica/iotools/converters/oasis_to_bids/oasis_to_bids.py @@ -69,8 +69,8 @@ def convert_clinical_data(self, clinical_data_dir: Path, bids_dir: Path): cprint("Converting clinical data...", lvl="info") bids_ids = get_bids_subjs_list(bids_dir) self._create_participants_tsv(clinical_data_dir, bids_dir, bids_ids) - sessions = self._create_sessions_tsv(clinical_data_dir, bids_dir, bids_ids) - self._create_scans_tsv(clinical_data_dir, bids_dir, bids_ids, sessions) + self._create_sessions_tsv(clinical_data_dir, bids_dir, bids_ids) + self._create_scans_tsv(bids_dir) self._create_modality_agnostic_files(bids_dir) def _create_participants_tsv( @@ -107,7 +107,7 @@ def _create_sessions_tsv( clinical_data_dir: Path, bids_dir: Path, bids_ids: list[str], - ) -> dict: + ) -> None: from .oasis_to_bids_utils import create_sessions_dict, write_sessions_tsv sessions_dict = create_sessions_dict( @@ -118,31 +118,10 @@ def _create_sessions_tsv( write_sessions_tsv(bids_dir, sessions_dict) - return sessions_dict + def _create_scans_tsv(self, bids_dir: Path) -> None: + from .oasis_to_bids_utils import write_scans_tsv - def _create_scans_tsv( - self, - clinical_data_dir: Path, - bids_dir: Path, - bids_ids: list[str], - sessions: dict, - ): - from clinica.iotools.bids_utils import ( - StudyName, - create_scans_dict, - write_scans_tsv, - ) - - scans_dict = create_scans_dict( - clinical_data_dir=clinical_data_dir, - study_name=StudyName.OASIS, - clinical_specifications_folder=Path(__file__).parents[1] / "specifications", - bids_ids=bids_ids, - name_column_ids="ID", - name_column_ses="", - ses_dict=sessions, - ) - write_scans_tsv(bids_dir, bids_ids, scans_dict) + write_scans_tsv(bids_dir) def _create_modality_agnostic_files(self, bids_dir: Path): from clinica.iotools.bids_utils import StudyName, write_modality_agnostic_files diff --git a/clinica/iotools/converters/oasis_to_bids/oasis_to_bids_utils.py b/clinica/iotools/converters/oasis_to_bids/oasis_to_bids_utils.py index 451d81261..7621f8146 100644 --- a/clinica/iotools/converters/oasis_to_bids/oasis_to_bids_utils.py +++ b/clinica/iotools/converters/oasis_to_bids/oasis_to_bids_utils.py @@ -5,7 +5,7 @@ from clinica.iotools.bids_utils import StudyName, bids_id_factory -__all__ = ["create_sessions_dict", "write_sessions_tsv"] +__all__ = ["create_sessions_dict", "write_sessions_tsv", "write_scans_tsv"] def create_sessions_dict( @@ -85,7 +85,6 @@ def write_sessions_tsv(bids_dir: Path, sessions_dict: dict) -> None: See also -------- create_sessions_dict - write_scans_tsv """ for subject_path in bids_dir.glob("sub-*"): if subject_path.name in sessions_dict: @@ -105,3 +104,29 @@ def write_sessions_tsv(bids_dir: Path, sessions_dict: dict) -> None: sep="\t", encoding="utf8", ) + + +def write_scans_tsv(bids_dir: Path) -> None: + """ + Write the scans.tsv file at the root of baseline sessions (ses-M000). + + Parameters + ---------- + bids_dir : Path to the BIDS output + """ + for subject_path in bids_dir.rglob("sub-*"): + if subject_path.is_dir(): + to_write = pd.DataFrame( + { + "filename": [ + f"{path.parent.name}/{path.name}" + for path in subject_path.rglob("*ses-M000*.nii.gz") + ] + } + ) + + to_write.to_csv( + subject_path / "ses-M000" / f"{subject_path.name}_ses-M000_scans.tsv", + sep="\t", + index=False, + ) diff --git a/test/unittests/iotools/converters/oasis_to_bids/test_oasis_to_bids_utils.py b/test/unittests/iotools/converters/oasis_to_bids/test_oasis_to_bids_utils.py index 671977eab..fe4ebff6d 100644 --- a/test/unittests/iotools/converters/oasis_to_bids/test_oasis_to_bids_utils.py +++ b/test/unittests/iotools/converters/oasis_to_bids/test_oasis_to_bids_utils.py @@ -1,3 +1,4 @@ +from os import write from pathlib import Path import numpy as np @@ -7,6 +8,7 @@ from clinica.iotools.converters.oasis_to_bids.oasis_to_bids_utils import ( create_sessions_dict, + write_scans_tsv, write_sessions_tsv, ) @@ -179,3 +181,33 @@ def test_write_sessions_tsv( check_dtype=False, ) assert file.name == f"{file.parent.name}_sessions.tsv" + + +def test_write_scans_tsv(tmp_path, bids_dir: Path) -> None: + image_path = ( + bids_dir + / "sub-OASIS10001" + / "ses-M000" + / "anat" + / "sub-OASIS10001_ses-M000_T1.nii.gz" + ) + image_path.parent.mkdir(parents=True) + image_path.touch() + + write_scans_tsv(bids_dir) + + for session_path in bids_dir.rglob("ses-M*"): + tsv_path = list(session_path.rglob("*scans.tsv")) + if session_path.name != "ses-M000": + assert not tsv_path + else: + assert len(tsv_path) == 1 + sub = session_path.parent.name + assert ( + tsv_path[0] == bids_dir / sub / "ses-M000" / f"{sub}_ses-M000_scans.tsv" + ) + file = pd.read_csv(tsv_path[0], sep="\t") + if sub == "sub-OASIS10001": + assert file["filename"].loc[0] == f"anat/{image_path.name}" + elif sub == "sub-OASIS10002": + assert file.empty