From 7c7ed532fd5442748598a2b5212d21aec3ebf83a Mon Sep 17 00:00:00 2001 From: Remi Gau Date: Thu, 28 Mar 2024 12:51:33 -0400 Subject: [PATCH] add dummy content in niftis --- fids/fids.py | 49 ++++++++++++++++++++++++++++++++++++++++++++-- pyproject.toml | 4 +++- tests/test_fids.py | 2 +- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/fids/fids.py b/fids/fids.py index f762406..6f62a8d 100644 --- a/fids/fids.py +++ b/fids/fids.py @@ -4,7 +4,10 @@ import json from pathlib import Path +import nibabel as nib +import numpy as np from bids import BIDSLayout +from nibabel import Nifti1Image DEFAUTL_NIFTI_EXT = ".nii.gz" @@ -30,7 +33,8 @@ def bids_fitler_file() -> dict[str, dict[str, list[str]]]: return { "fmap": {}, "func": {"suffix": ["bold", "events"]}, - "anat": {"suffix": ["T1w"]}, + "dwi": {"suffix": ["dwi"]}, + "anat": {"suffix": ["T1w", "T2w"]}, } @@ -95,7 +99,13 @@ def create_empty_file(layout: BIDSLayout, entities: dict[str, str | int]) -> Non ) filepath = Path(filepath) filepath.parent.mkdir(parents=True, exist_ok=True) - filepath.touch() + if entities["extension"] in [".nii", ".nii.gz"]: + image = _img_3d_rand_eye() + if entities["datatype"] in ["func", "dwi"]: + image = _img_4d_rand_eye() + nib.save(image, filepath) + else: + filepath.touch() def create_sidecar( @@ -115,5 +125,40 @@ def create_sidecar( json.dump(metadata, f, indent=4) +def _rng(seed=42): + return np.random.default_rng(seed) + + +def _affine_eye(): + """Return an identity matrix affine.""" + return np.eye(4) + + +def _shape_3d_default(): + """Return default shape for a 3D image.""" + return (10, 10, 10) + + +def _length_default(): + return 10 + + +def _shape_4d_default(): + """Return default shape for a 4D image.""" + return (10, 10, 10, _length_default()) + + +def _img_3d_rand_eye(affine=_affine_eye()): + """Return random 3D Nifti1Image in MNI space.""" + data = _rng().random(_shape_3d_default()) + return Nifti1Image(data, affine) + + +def _img_4d_rand_eye(affine=_affine_eye()): + """Return random 3D Nifti1Image in MNI space.""" + data = _rng().random(_shape_4d_default()) + return Nifti1Image(data, affine) + + if __name__ == "__main__": create_fake_bids_dataset() diff --git a/pyproject.toml b/pyproject.toml index f77069e..84b753f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,7 +23,9 @@ classifiers = [ "Programming Language :: Python :: 3.12" ] dependencies = [ - "pybids" + "pybids", + "numpy", + "nibabel" ] description = "fake bids dataset generator" # Version from setuptools_scm diff --git a/tests/test_fids.py b/tests/test_fids.py index 3108cc2..9bb59cb 100644 --- a/tests/test_fids.py +++ b/tests/test_fids.py @@ -7,7 +7,7 @@ @pytest.mark.parametrize("subjects", ["01", 1, ["1", "baz"], [1, 2], ["boo", 2]]) @pytest.mark.parametrize("sessions", [None, "01", 1, ["foo", "2"], [1, 2], ["bar", 2]]) -@pytest.mark.parametrize("datatypes", ["anat", "func", ["anat", "func"]]) +@pytest.mark.parametrize("datatypes", ["anat", "func", "dwi", ["anat", "func"]]) def test_fids_smoke(tmp_path, subjects, sessions, datatypes): """Smoke test.""" create_fake_bids_dataset(