Skip to content

Commit

Permalink
[CONV] IXI-to-BIDS modifications to fit BIDS specifications 1.10 (ara…
Browse files Browse the repository at this point in the history
…mis-lab#1310)

* First pass

* Add dwi files

* Fix number of dwi volumes

* Fix tsv

* Fix broken unit test
  • Loading branch information
AliceJoubert authored Oct 3, 2024
1 parent 95bcd3c commit 0d4309b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
3 changes: 3 additions & 0 deletions clinica/iotools/converters/ixi_to_bids/dwi_b_files/bvecs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0.0 1.0 0.0 0.0 -0.179 -0.064 0.711 0.619 0.242 -0.259 -0.817 -0.844 -0.263 0.0 0.745 0.973
0.0 0.0 -1.0 0.0 0.111 -0.377 -0.052 0.438 -0.784 0.618 -0.170 -0.526 -0.955 -0.969 -0.666 -0.232
1.0 0.0 0.0 -1.0 0.978 0.924 0.701 0.651 0.571 0.742 0.551 0.106 0.139 -0.248 -0.024 -0.021
4 changes: 4 additions & 0 deletions clinica/iotools/converters/ixi_to_bids/ixi_to_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
check_modalities,
define_participants,
read_clinical_data,
write_dwi_b_values,
write_participants,
write_scans,
write_sessions,
Expand Down Expand Up @@ -65,6 +66,9 @@ def convert(
)
write_scans(bids_dir=bids_dir, participant=participant)

if list(bids_dir.rglob("dwi")):
write_dwi_b_values(bids_dir=bids_dir)

readme_data = {
"link": "https://brain-development.org/ixi-dataset/",
"desc": (
Expand Down
12 changes: 10 additions & 2 deletions clinica/iotools/converters/ixi_to_bids/ixi_to_bids_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@
"write_scans",
"write_participants",
"check_modalities",
"write_dwi_b_values",
]


def write_dwi_b_values(bids_dir: Path) -> None:
"""Writes DWI bval and bvec files at the root of the BIDS directory"""
b_folder = Path(__file__).parents[0] / "dwi_b_files"
shutil.copy(b_folder / "bvals.txt", bids_dir / "dwi.bval")
shutil.copy(b_folder / "bvecs.txt", bids_dir / "dwi.bvec")


def _get_subjects_list_from_data(data_directory: Path) -> List[str]:
return list(
dict.fromkeys(
Expand Down Expand Up @@ -308,7 +316,7 @@ def _write_subject_dti_if_exists(


def _find_subject_dti_data(data_directory: Path, subject: str) -> List[Path]:
pattern = subject + r"(-\w*){2}-DTI(-\w*){1}.nii.gz$"
pattern = subject + r"(-\w*){2}-DTI(-(0[1-9]|[1-9][0-9])){1}.nii.gz$"
return [
path
for path in data_directory.rglob(pattern="IXI*.nii.gz")
Expand Down Expand Up @@ -377,7 +385,7 @@ def write_participants(
participants : List of converted subjects study source ids.
"""
clinical_data.set_index("source_id", inplace=True, drop=False)
clinical_data.assign(
clinical_data = clinical_data.assign(
participant_id=clinical_data.source_id.apply(
lambda x: bids_id_factory(StudyName.IXI).from_original_study_id(x)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,21 @@
check_modalities,
define_participants,
read_clinical_data,
write_dwi_b_values,
write_participants,
write_scans,
write_sessions,
)


def test_write_dwi_b_values(tmp_path):
write_dwi_b_values(tmp_path)
bvec_files = list(tmp_path.rglob("*.bvec"))
bval_files = list(tmp_path.rglob("*.bval"))
assert len(bvec_files) == 1 and bvec_files[0].name == "dwi.bvec"
assert len(bval_files) == 1 and bval_files[0].name == "dwi.bval"


def test_get_subjects_list_from_data(tmp_path):
for filename in ("IXI1", "IXI123", "IXIaaa", "foo"):
(tmp_path / f"{filename}_T1w.nii.gz").touch()
Expand Down Expand Up @@ -219,7 +228,10 @@ def clinical_data_builder(tmp_path: Path) -> None:
def test_read_clinical_data_success(tmp_path):
clinical_data_builder(tmp_path)
assert (
read_clinical_data(tmp_path).eq(formatted_clinical_data_builder()).all().all()
read_clinical_data(tmp_path)
.eq(formatted_clinical_data_builder().drop("participant_id", axis=1))
.all()
.all()
)


Expand Down Expand Up @@ -531,7 +543,7 @@ def test_find_subject_dti_data(tmp_path):
(tmp_path / "IXI001-Guys-1234-T1.nii.gz").touch()
(tmp_path / "IXI001-Guys-1234-DTI").touch()
(tmp_path / "IXI001-Guys-DTI.nii.gz").touch()
tmp_image = tmp_path / "IXI001-Guys-1234-DTI-00.nii.gz"
tmp_image = tmp_path / "IXI001-Guys-1234-DTI-01.nii.gz"
tmp_image.touch()
list_dti = _find_subject_dti_data(data_directory=tmp_path, subject="IXI001")
assert len(list_dti) == 1 and list_dti[0] == tmp_image
Expand Down Expand Up @@ -564,6 +576,7 @@ def formatted_clinical_data_builder() -> pd.DataFrame:
return pd.DataFrame(
{
"source_id": ["IXI001"],
"participant_id": ["sub-IXI001"],
"session_id": ["ses-M000"],
"acq_time": ["2024-08-23"],
"sex": ["female"],
Expand Down

0 comments on commit 0d4309b

Please sign in to comment.