docs(mri): add build() docstring; fail-fast on unknown site label
Browse files
tests/fixtures/build_mri_fixture.py
CHANGED
|
@@ -25,6 +25,7 @@ import numpy as np
|
|
| 25 |
|
| 26 |
SITE_A_BIAS = 0.0
|
| 27 |
SITE_B_BIAS = 5.0
|
|
|
|
| 28 |
VOLUME_SHAPE = (8, 8, 8)
|
| 29 |
SUBJECTS = (
|
| 30 |
("subject_0", "A"),
|
|
@@ -51,6 +52,26 @@ def _spherical_brain(rng: np.random.Generator, bias: float) -> np.ndarray:
|
|
| 51 |
|
| 52 |
|
| 53 |
def build(out_dir: Path | None = None) -> Path:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
out = out_dir if out_dir is not None else Path(__file__).parent / "mri_sample"
|
| 55 |
out.mkdir(parents=True, exist_ok=True)
|
| 56 |
|
|
@@ -59,7 +80,7 @@ def build(out_dir: Path | None = None) -> Path:
|
|
| 59 |
|
| 60 |
sites_rows: list[tuple[str, str]] = []
|
| 61 |
for subject_id, site in SUBJECTS:
|
| 62 |
-
bias =
|
| 63 |
volume = _spherical_brain(rng, bias=bias)
|
| 64 |
img = nib.Nifti1Image(volume, affine=affine)
|
| 65 |
nib.save(img, out / f"{subject_id}.nii.gz")
|
|
|
|
| 25 |
|
| 26 |
SITE_A_BIAS = 0.0
|
| 27 |
SITE_B_BIAS = 5.0
|
| 28 |
+
_SITE_BIAS: dict[str, float] = {"A": SITE_A_BIAS, "B": SITE_B_BIAS}
|
| 29 |
VOLUME_SHAPE = (8, 8, 8)
|
| 30 |
SUBJECTS = (
|
| 31 |
("subject_0", "A"),
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
def build(out_dir: Path | None = None) -> Path:
|
| 55 |
+
"""Generate the MRI fixture and write all 7 artifacts to ``out_dir``.
|
| 56 |
+
|
| 57 |
+
The output directory will contain six 8×8×8 NIfTI volumes
|
| 58 |
+
(``subject_0.nii.gz`` through ``subject_5.nii.gz``) and a ``sites.csv``
|
| 59 |
+
file with columns ``subject_id, site``. Files are byte-deterministic
|
| 60 |
+
given a fixed nibabel version (see module-level NOTE).
|
| 61 |
+
|
| 62 |
+
Args:
|
| 63 |
+
out_dir: Target directory. Defaults to ``tests/fixtures/mri_sample/``
|
| 64 |
+
resolved relative to this script (so CWD-independent).
|
| 65 |
+
|
| 66 |
+
Returns:
|
| 67 |
+
The resolved output directory path.
|
| 68 |
+
|
| 69 |
+
Raises:
|
| 70 |
+
KeyError: if ``SUBJECTS`` lists a site label not present in
|
| 71 |
+
``_SITE_BIAS``. This is a fail-fast guard that prevents a
|
| 72 |
+
silent fall-through when adding a new site without updating
|
| 73 |
+
``_SITE_BIAS``.
|
| 74 |
+
"""
|
| 75 |
out = out_dir if out_dir is not None else Path(__file__).parent / "mri_sample"
|
| 76 |
out.mkdir(parents=True, exist_ok=True)
|
| 77 |
|
|
|
|
| 80 |
|
| 81 |
sites_rows: list[tuple[str, str]] = []
|
| 82 |
for subject_id, site in SUBJECTS:
|
| 83 |
+
bias = _SITE_BIAS[site]
|
| 84 |
volume = _spherical_brain(rng, bias=bias)
|
| 85 |
img = nib.Nifti1Image(volume, affine=affine)
|
| 86 |
nib.save(img, out / f"{subject_id}.nii.gz")
|