Skip to content

Commit

Permalink
[REF] OASIS-to-BIDS create_scans_dict (#1330)
Browse files Browse the repository at this point in the history
* TODO

* Add new function

* Changes upon suggestions
  • Loading branch information
AliceJoubert authored Oct 15, 2024
1 parent 8abae06 commit c1aa34c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 29 deletions.
33 changes: 6 additions & 27 deletions clinica/iotools/converters/oasis_to_bids/oasis_to_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down
29 changes: 27 additions & 2 deletions clinica/iotools/converters/oasis_to_bids/oasis_to_bids_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand All @@ -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,
)
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from os import write
from pathlib import Path

import numpy as np
Expand All @@ -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,
)

Expand Down Expand Up @@ -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

0 comments on commit c1aa34c

Please sign in to comment.