Skip to content

Commit

Permalink
add dummy content in niftis
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Mar 28, 2024
1 parent 52f186d commit 7c7ed53
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
49 changes: 47 additions & 2 deletions fids/fids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"]},
}


Expand Down Expand Up @@ -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(
Expand All @@ -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()
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ classifiers = [
"Programming Language :: Python :: 3.12"
]
dependencies = [
"pybids"
"pybids",
"numpy",
"nibabel"
]
description = "fake bids dataset generator"
# Version from setuptools_scm
Expand Down
2 changes: 1 addition & 1 deletion tests/test_fids.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 7c7ed53

Please sign in to comment.