From 0eaac69eb0c9b42811449b034c862b81eefefe36 Mon Sep 17 00:00:00 2001 From: AliceJoubert Date: Fri, 11 Oct 2024 18:11:44 +0200 Subject: [PATCH 1/3] TODO --- .../converters/oasis_to_bids/oasis_to_bids_utils.py | 8 ++++++++ 1 file changed, 8 insertions(+) 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..87b1ad436 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 @@ -105,3 +105,11 @@ def write_sessions_tsv(bids_dir: Path, sessions_dict: dict) -> None: sep="\t", encoding="utf8", ) + + +def create_scans_dict( + clinical_data_dir: Path, +) -> dict: + # todo : dict, can be specific to oasis1 ; + + return dict() From aae943388ffe26738c23d1da6377ae8dd058e218 Mon Sep 17 00:00:00 2001 From: AliceJoubert Date: Mon, 14 Oct 2024 16:10:25 +0200 Subject: [PATCH 2/3] Add new function --- .../converters/oasis_to_bids/oasis_to_bids.py | 30 ++++------------ .../oasis_to_bids/oasis_to_bids_utils.py | 35 +++++++++++++++---- .../oasis_to_bids/test_oasis_to_bids_utils.py | 32 +++++++++++++++++ 3 files changed, 67 insertions(+), 30 deletions(-) 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..d9008c795 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,15 @@ def _create_sessions_tsv( write_sessions_tsv(bids_dir, sessions_dict) - return sessions_dict - 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, + ) -> None: + from clinica.iotools.converters.oasis_to_bids.oasis_to_bids_utils import ( + write_scans, ) - 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(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 87b1ad436..ae4d9b2f5 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"] 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: @@ -107,9 +106,31 @@ def write_sessions_tsv(bids_dir: Path, sessions_dict: dict) -> None: ) -def create_scans_dict( - clinical_data_dir: Path, -) -> dict: - # todo : dict, can be specific to oasis1 ; +def write_scans( + 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 - return dict() + """ + + 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..7373e7dcd 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, 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(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(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 From 9550e1e70763aebd172bb67cc3d8f815462d5ae3 Mon Sep 17 00:00:00 2001 From: AliceJoubert Date: Tue, 15 Oct 2024 10:19:30 +0200 Subject: [PATCH 3/3] Changes upon suggestions --- .../iotools/converters/oasis_to_bids/oasis_to_bids.py | 11 +++-------- .../converters/oasis_to_bids/oasis_to_bids_utils.py | 8 ++------ .../oasis_to_bids/test_oasis_to_bids_utils.py | 6 +++--- 3 files changed, 8 insertions(+), 17 deletions(-) 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 d9008c795..206bbbc3f 100644 --- a/clinica/iotools/converters/oasis_to_bids/oasis_to_bids.py +++ b/clinica/iotools/converters/oasis_to_bids/oasis_to_bids.py @@ -118,15 +118,10 @@ def _create_sessions_tsv( write_sessions_tsv(bids_dir, sessions_dict) - def _create_scans_tsv( - self, - bids_dir: Path, - ) -> None: - from clinica.iotools.converters.oasis_to_bids.oasis_to_bids_utils import ( - write_scans, - ) + def _create_scans_tsv(self, bids_dir: Path) -> None: + from .oasis_to_bids_utils import write_scans_tsv - write_scans(bids_dir) + 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 ae4d9b2f5..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", "write_scans"] +__all__ = ["create_sessions_dict", "write_sessions_tsv", "write_scans_tsv"] def create_sessions_dict( @@ -106,18 +106,14 @@ def write_sessions_tsv(bids_dir: Path, sessions_dict: dict) -> None: ) -def write_scans( - bids_dir: Path, -) -> None: +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( 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 7373e7dcd..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 @@ -8,7 +8,7 @@ from clinica.iotools.converters.oasis_to_bids.oasis_to_bids_utils import ( create_sessions_dict, - write_scans, + write_scans_tsv, write_sessions_tsv, ) @@ -183,7 +183,7 @@ def test_write_sessions_tsv( assert file.name == f"{file.parent.name}_sessions.tsv" -def test_write_scans(tmp_path, bids_dir: Path) -> None: +def test_write_scans_tsv(tmp_path, bids_dir: Path) -> None: image_path = ( bids_dir / "sub-OASIS10001" @@ -194,7 +194,7 @@ def test_write_scans(tmp_path, bids_dir: Path) -> None: image_path.parent.mkdir(parents=True) image_path.touch() - write_scans(bids_dir) + write_scans_tsv(bids_dir) for session_path in bids_dir.rglob("ses-M*"): tsv_path = list(session_path.rglob("*scans.tsv"))