Skip to content

Commit

Permalink
TST: Add test for new workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
mgxd committed Dec 12, 2024
1 parent 6dbf4bc commit c364839
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Empty file.
55 changes: 55 additions & 0 deletions nibabies/workflows/anatomical/tests/test_preproc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import typing as ty
from pathlib import Path

Check warning on line 2 in nibabies/workflows/anatomical/tests/test_preproc.py

View check run for this annotation

Codecov / codecov/patch

nibabies/workflows/anatomical/tests/test_preproc.py#L1-L2

Added lines #L1 - L2 were not covered by tests

import nibabel as nb
import numpy as np
import pytest

Check warning on line 6 in nibabies/workflows/anatomical/tests/test_preproc.py

View check run for this annotation

Codecov / codecov/patch

nibabies/workflows/anatomical/tests/test_preproc.py#L4-L6

Added lines #L4 - L6 were not covered by tests

from nibabies.workflows.anatomical.preproc import _normalize_roi, init_csf_norm_wf

Check warning on line 8 in nibabies/workflows/anatomical/tests/test_preproc.py

View check run for this annotation

Codecov / codecov/patch

nibabies/workflows/anatomical/tests/test_preproc.py#L8

Added line #L8 was not covered by tests

EXPECTED_CSF_NORM = np.array([[[10, 73], [73, 29]], [[77, 80], [6, 16]]], dtype='uint8')

Check warning on line 10 in nibabies/workflows/anatomical/tests/test_preproc.py

View check run for this annotation

Codecov / codecov/patch

nibabies/workflows/anatomical/tests/test_preproc.py#L10

Added line #L10 was not covered by tests


@pytest.fixture
def csf_norm_data(tmp_path) -> ty.Generator[tuple[Path, list[Path]], None, None]:
np.random.seed(10)

Check warning on line 15 in nibabies/workflows/anatomical/tests/test_preproc.py

View check run for this annotation

Codecov / codecov/patch

nibabies/workflows/anatomical/tests/test_preproc.py#L13-L15

Added lines #L13 - L15 were not covered by tests

in_file = tmp_path / 'input.nii.gz'
data = np.random.randint(1, 101, size=(2, 2, 2), dtype='uint8')
img = nb.Nifti1Image(data, np.eye(4))
img.to_filename(in_file)

Check warning on line 20 in nibabies/workflows/anatomical/tests/test_preproc.py

View check run for this annotation

Codecov / codecov/patch

nibabies/workflows/anatomical/tests/test_preproc.py#L17-L20

Added lines #L17 - L20 were not covered by tests

masks = []

Check warning on line 22 in nibabies/workflows/anatomical/tests/test_preproc.py

View check run for this annotation

Codecov / codecov/patch

nibabies/workflows/anatomical/tests/test_preproc.py#L22

Added line #L22 was not covered by tests
for tpm in ('gm', 'wm', 'csf'):
name = tmp_path / f'{tpm}.nii.gz'
binmask = data > np.random.randint(10, 90)
masked = (binmask * 1).astype('uint8')
mask = nb.Nifti1Image(masked, img.affine)
mask.to_filename(name)
masks.append(name)

Check warning on line 29 in nibabies/workflows/anatomical/tests/test_preproc.py

View check run for this annotation

Codecov / codecov/patch

nibabies/workflows/anatomical/tests/test_preproc.py#L24-L29

Added lines #L24 - L29 were not covered by tests

yield in_file, masks

Check warning on line 31 in nibabies/workflows/anatomical/tests/test_preproc.py

View check run for this annotation

Codecov / codecov/patch

nibabies/workflows/anatomical/tests/test_preproc.py#L31

Added line #L31 was not covered by tests

in_file.unlink()

Check warning on line 33 in nibabies/workflows/anatomical/tests/test_preproc.py

View check run for this annotation

Codecov / codecov/patch

nibabies/workflows/anatomical/tests/test_preproc.py#L33

Added line #L33 was not covered by tests
for m in masks:
m.unlink()

Check warning on line 35 in nibabies/workflows/anatomical/tests/test_preproc.py

View check run for this annotation

Codecov / codecov/patch

nibabies/workflows/anatomical/tests/test_preproc.py#L35

Added line #L35 was not covered by tests


def test_csf_norm_wf(tmp_path, csf_norm_data):
anat, tpms = csf_norm_data
wf = init_csf_norm_wf()
wf.base_dir = tmp_path

Check warning on line 41 in nibabies/workflows/anatomical/tests/test_preproc.py

View check run for this annotation

Codecov / codecov/patch

nibabies/workflows/anatomical/tests/test_preproc.py#L38-L41

Added lines #L38 - L41 were not covered by tests

wf.inputs.inputnode.anat_preproc = anat
wf.inputs.inputnode.anat_tpms = tpms

Check warning on line 44 in nibabies/workflows/anatomical/tests/test_preproc.py

View check run for this annotation

Codecov / codecov/patch

nibabies/workflows/anatomical/tests/test_preproc.py#L43-L44

Added lines #L43 - L44 were not covered by tests

# verify workflow runs
wf.run()

Check warning on line 47 in nibabies/workflows/anatomical/tests/test_preproc.py

View check run for this annotation

Codecov / codecov/patch

nibabies/workflows/anatomical/tests/test_preproc.py#L47

Added line #L47 was not covered by tests

# verify function works as expected
outfile = _normalize_roi(anat, tpms[2])
assert np.array_equal(

Check warning on line 51 in nibabies/workflows/anatomical/tests/test_preproc.py

View check run for this annotation

Codecov / codecov/patch

nibabies/workflows/anatomical/tests/test_preproc.py#L50-L51

Added lines #L50 - L51 were not covered by tests
np.asanyarray(nb.load(outfile).dataobj),
EXPECTED_CSF_NORM,
)
Path(outfile).unlink()

Check warning on line 55 in nibabies/workflows/anatomical/tests/test_preproc.py

View check run for this annotation

Codecov / codecov/patch

nibabies/workflows/anatomical/tests/test_preproc.py#L55

Added line #L55 was not covered by tests

0 comments on commit c364839

Please sign in to comment.