Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
fix: update integration test (fails with new nitransforms)
Browse files Browse the repository at this point in the history
  • Loading branch information
oesteban committed Dec 19, 2024
1 parent e903e40 commit 5123f50
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions test/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@

from eddymotion.data.dmri import DWI
from eddymotion.estimator import EddyMotionEstimator
from eddymotion.registration.utils import displacements_within_mask


def test_proximity_estimator_trivial_model(datadir):
def test_proximity_estimator_trivial_model(datadir, tmp_path):
"""Check the proximity of transforms estimated by the estimator with a trivial B0 model."""

dwdata = DWI.from_filename(datadir / "dwi.h5")
b0nii = nb.Nifti1Image(dwdata.bzero, dwdata.affine, None)
masknii = nb.Nifti1Image(dwdata.brainmask.astype(np.uint8), dwdata.affine, None)

# Generate a list of large-yet-plausible bulk-head motion.
xfms = nt.linear.LinearTransformsMapping(
Expand All @@ -56,8 +58,8 @@ def test_proximity_estimator_trivial_model(datadir):
moved_nii = (~xfms).apply(b0nii, reference=b0nii)

# Uncomment to see the moved dataset
# moved_nii.to_filename(tmp_path / "test.nii.gz")
# xfms.apply(moved_nii).to_filename(tmp_path / "ground_truth.nii.gz")
moved_nii.to_filename(tmp_path / "test.nii.gz")
xfms.apply(moved_nii).to_filename(tmp_path / "ground_truth.nii.gz")

# Wrap into dataset object
dwi_motion = DWI(
Expand All @@ -70,7 +72,7 @@ def test_proximity_estimator_trivial_model(datadir):

estimator = EddyMotionEstimator()
em_affines = estimator.estimate(
dwdata=dwi_motion,
data=dwi_motion,
models=("b0",),
seed=None,
align_kwargs={
Expand All @@ -81,14 +83,18 @@ def test_proximity_estimator_trivial_model(datadir):
)

# Uncomment to see the realigned dataset
# nt.linear.LinearTransformsMapping(
# em_affines,
# reference=b0nii,
# ).apply(moved_nii).to_filename(tmp_path / "realigned.nii.gz")
nt.linear.LinearTransformsMapping(
em_affines,
reference=b0nii,
).apply(moved_nii).to_filename(tmp_path / "realigned.nii.gz")

# For each moved b0 volume
coords = xfms.reference.ndcoords.T
for i, est in enumerate(em_affines):
xfm = nt.linear.Affine(xfms.matrix[i], reference=b0nii)
est = nt.linear.Affine(est, reference=b0nii)
assert np.sqrt(((xfm.map(coords) - est.map(coords)) ** 2).sum(1)).mean() < 0.2
assert (
displacements_within_mask(
masknii,
nt.linear.Affine(est),
xfms[i],
).max()
< 0.2
)

0 comments on commit 5123f50

Please sign in to comment.