Skip to content

Commit

Permalink
WIP 1
Browse files Browse the repository at this point in the history
  • Loading branch information
AliceJoubert committed Oct 7, 2024
1 parent cdc5a46 commit 29de1b2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 42 deletions.
8 changes: 3 additions & 5 deletions clinica/iotools/converters/oasis_to_bids/oasis_to_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,16 @@ def _create_sessions_tsv(
bids_dir: Path,
bids_ids: list[str],
) -> dict:
from oasis_to_bids_utils import create_sessions_dict, write_sessions_tsv

from clinica.iotools.bids_utils import StudyName
from .oasis_to_bids_utils import create_sessions_dict, write_sessions_tsv

sessions_dict = create_sessions_dict(
clinical_data_dir=clinical_data_dir,
bids_dir=bids_dir,
study_name=StudyName.OASIS,
clinical_specifications_folder=Path(__file__).parents[1] / "specifications",
bids_ids=bids_ids,
name_column_ids="ID",
)

# todo : when tested add to create_sessions_dict bc specific to oasis1
for bids_id in bids_ids:
sessions_dict[bids_id]["M000"]["diagnosis"] = (
"AD" if sessions_dict[bids_id]["M000"]["diagnosis"] > 0 else "CN"
Expand Down
42 changes: 5 additions & 37 deletions clinica/iotools/converters/oasis_to_bids/oasis_to_bids_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
def create_sessions_dict(
clinical_data_dir: Path,
bids_dir: Path,
study_name: StudyName,
clinical_specifications_folder: Path,
bids_ids: list[str],
name_column_ids: str,
subj_to_remove: Optional[list[str]] = None,
participants_df: Optional[pd.DataFrame] = None,
) -> dict:
"""Extract the information regarding the sessions and store them in a dictionary (session M00 only).
"""Extract the information regarding the sessions and store them in a dictionary (session M000 only).
Parameters
----------
Expand All @@ -31,34 +28,25 @@ def create_sessions_dict(
bids_dir : Path
The path to the BIDS directory.
study_name : StudyName
The name of the study (Ex: ADNI).
clinical_specifications_folder : Path
The path to the clinical file.
bids_ids : list of str
The list of bids ids.
name_column_ids : str
The name of the column where the subject ids are stored.
subj_to_remove : list of str, optional
The list of subject IDs to remove.
participants_df : pd.DataFrame, optional
A pandas dataframe that contains the participants data (required for OASIS3 only).
Returns
-------
dict :
Session dict.
"""

subj_to_remove = subj_to_remove or []
location = f"{study_name.value} location"
location = f"{StudyName.OASIS.value} location"
sessions = pd.read_csv(clinical_specifications_folder / "sessions.tsv", sep="\t")
sessions_fields = sessions[study_name.value]
sessions_fields = sessions[StudyName.OASIS.value]
field_location = sessions[location]
sessions_fields_bids = sessions["BIDS CLINICA"]
fields_dataset = []
Expand Down Expand Up @@ -94,7 +82,7 @@ def create_sessions_dict(

for r in range(0, len(file_to_read.values)):
# Extracts the subject ids columns from the dataframe
subj_id = file_to_read.iloc[r][name_column_ids]
subj_id = file_to_read.iloc[r]["ID"]
if hasattr(subj_id, "dtype"):
if subj_id.dtype == np.int64:
subj_id = str(subj_id)
Expand All @@ -116,34 +104,14 @@ def create_sessions_dict(
session_names = get_bids_sess_list(subj_dir)
for s in session_names:
s_name = s.replace("ses-", "")
if study_name == StudyName.OASIS3:
row = file_to_read[
file_to_read["MR ID"].str.startswith(subj_id)
& file_to_read["MR ID"].str.endswith(s_name)
].iloc[0]
else:
row = file_to_read.iloc[r]
row = file_to_read.iloc[r]
if subj_bids not in sessions_dict:
sessions_dict.update({subj_bids: {}})
if s_name not in sessions_dict[subj_bids].keys():
sessions_dict[subj_bids].update({s_name: {"session_id": s}})
(sessions_dict[subj_bids][s_name]).update(
{sessions_fields_bids[i]: row[sessions_fields[i]]}
)
# Calculate the difference in months for OASIS3 only
if (
study_name == StudyName.OASIS3
and sessions_fields_bids[i] == "age"
):
diff_years = (
float(sessions_dict[subj_bids][s_name]["age"])
- participants_df[
participants_df["participant_id"] == subj_bids
]["age_bl"]
)
(sessions_dict[subj_bids][s_name]).update(
{"diff_months": round(float(diff_years) * 12)}
)

return sessions_dict

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
from pathlib import Path

import pandas as pd
import pytest

from clinica.iotools.converters.oasis_to_bids.oasis_to_bids_utils import (
create_sessions_dict,
write_sessions_tsv,
)


def build_clinical_data(tmp_path: Path) -> None:
df = pd.DataFrame(
{
"ID": ["OAS1_0001_MR1", "OAS1_0002_MR1"],
"M/F": ["F", "M"],
"Hand": ["R", "L"],
"Age": [74, 67],
"Educ": [2, 2],
"SES": [3, 3],
"MMSE": [29, 29],
"CDR": [0, 0],
"eTIV": [1344, 1344],
"nWBV": [0.704, 0.645],
"ASF": [1.306, 1.100],
"Delay": [float("nan"), float("nan")],
}
)
df.to_csv(tmp_path / "oasis_cross-sectional.csv", index=False)

# todo : do excel for future
bids_ids = ["sub-OASIS10001", "sub-OASIS10002"]


def test_create_sessions_dict():
# todo
clinical_specifications_folder = Path("clinica/iotools/converters/specifications")
pass


def test_write_sessions_tsv():
# todo
pass

0 comments on commit 29de1b2

Please sign in to comment.