Skip to content

Commit

Permalink
Merge pull request #1 from oesteban/sandbox
Browse files Browse the repository at this point in the history
fix: update many apply() calls
  • Loading branch information
jmarabotto authored Apr 5, 2024
2 parents e299dc1 + ffd8a6d commit aaa4637
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
4 changes: 3 additions & 1 deletion nitransforms/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from .linear import load as linload
from .nonlinear import load as nlinload
from .resampling import apply


def cli_apply(pargs):
Expand Down Expand Up @@ -38,7 +39,8 @@ def cli_apply(pargs):
# ensure a reference is set
xfm.reference = pargs.ref or pargs.moving

moved = xfm.apply(
moved = apply(
xfm,
pargs.moving,
order=pargs.order,
mode=pargs.mode,
Expand Down
7 changes: 4 additions & 3 deletions nitransforms/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from ..base import SpatialReference, SampledSpatialData, ImageGrid, TransformBase
from .. import linear as nitl
from ..resampling import apply


def test_SpatialReference(testdata_path):
Expand Down Expand Up @@ -95,7 +96,7 @@ def _to_hdf5(klass, x5_root):
xfm = TransformBase()
xfm.reference = fname
assert xfm.ndim == 3
moved = xfm.apply(fname, order=0)
moved = apply(xfm, fname, order=0)
assert np.all(
imgdata == np.asanyarray(moved.dataobj, dtype=moved.get_data_dtype())
)
Expand All @@ -104,7 +105,7 @@ def _to_hdf5(klass, x5_root):
xfm = TransformBase()
xfm.reference = fname
assert xfm.ndim == 3
moved = xfm.apply(str(fname), reference=fname, order=0)
moved = apply(xfm, str(fname), reference=fname, order=0)
assert np.all(
imgdata == np.asanyarray(moved.dataobj, dtype=moved.get_data_dtype())
)
Expand All @@ -118,7 +119,7 @@ def _to_hdf5(klass, x5_root):
)
]
)
giimoved = xfm.apply(fname, reference=gii, order=0)
giimoved = apply(xfm, fname, reference=gii, order=0)
assert np.allclose(giimoved.reshape(xfm.reference.shape), moved.get_fdata())

# Test to_filename
Expand Down
41 changes: 27 additions & 14 deletions nitransforms/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
)
from nitransforms.io.base import LinearParameters, TransformIOError, TransformFileError
from nitransforms.conftest import _datadir, _testdir
from nitransforms.resampling import apply


LPS = np.diag([-1, -1, 1, 1])
ITK_MAT = LPS.dot(np.ones((4, 4)).dot(LPS))
Expand Down Expand Up @@ -497,10 +499,13 @@ def test_afni_oblique(tmpdir, parameters, swapaxes, testdata_path, dir_x, dir_y,
assert np.allclose(card_aff, nb.load("deob_3drefit.nii.gz").affine)

# Check that nitransforms can emulate 3drefit -deoblique
nt3drefit = Affine(
afni._cardinal_rotation(img.affine, False),
reference="deob_3drefit.nii.gz",
).apply("orig.nii.gz")
nt3drefit = apply(
Affine(
afni._cardinal_rotation(img.affine, False),
reference="deob_3drefit.nii.gz",
),
"orig.nii.gz",
)

diff = (
np.asanyarray(img.dataobj, dtype="uint8")
Expand All @@ -509,10 +514,13 @@ def test_afni_oblique(tmpdir, parameters, swapaxes, testdata_path, dir_x, dir_y,
assert np.sqrt((diff[10:-10, 10:-10, 10:-10] ** 2).mean()) < 0.1

# Check that nitransforms can revert 3drefit -deoblique
nt_undo3drefit = Affine(
afni._cardinal_rotation(img.affine, True),
reference="orig.nii.gz",
).apply("deob_3drefit.nii.gz")
nt_undo3drefit = apply(
Affine(
afni._cardinal_rotation(img.affine, True),
reference="orig.nii.gz",
),
"deob_3drefit.nii.gz",
)

diff = (
np.asanyarray(img.dataobj, dtype="uint8")
Expand All @@ -531,16 +539,21 @@ def test_afni_oblique(tmpdir, parameters, swapaxes, testdata_path, dir_x, dir_y,
assert np.allclose(deobaff, deobnii.affine)

# Check resampling in deobliqued grid
ntdeobnii = Affine(np.eye(4), reference=deobnii.__class__(
np.zeros(deobshape, dtype="uint8"),
deobaff,
deobnii.header
)).apply(img, order=0)
ntdeobnii = apply(
Affine(np.eye(4), reference=deobnii.__class__(
np.zeros(deobshape, dtype="uint8"),
deobaff,
deobnii.header
)),
img,
order=0,
)

# Generate an internal box to exclude border effects
box = np.zeros(img.shape, dtype="uint8")
box[10:-10, 10:-10, 10:-10] = 1
ntdeobmask = Affine(np.eye(4), reference=ntdeobnii).apply(
ntdeobmask = apply(
Affine(np.eye(4), reference=ntdeobnii),
nb.Nifti1Image(box, img.affine, img.header),
order=0,
)
Expand Down
16 changes: 9 additions & 7 deletions nitransforms/tests/test_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from nibabel.affines import from_matvec
from nitransforms import linear as nitl
from nitransforms import io
from nitransforms.resampling import apply
from .utils import assert_affines_by_filename

RMSE_TOL = 0.1
Expand Down Expand Up @@ -285,7 +286,7 @@ def test_apply_linear_transform(tmpdir, get_testdata, get_testmask, image_orient
assert exit_code == 0
sw_moved_mask = nb.load("resampled_brainmask.nii.gz")

nt_moved_mask = xfm.apply(msk, order=0)
nt_moved_mask = apply(xfm, msk, order=0)
nt_moved_mask.set_data_dtype(msk.get_data_dtype())
nt_moved_mask.to_filename("ntmask.nii.gz")
diff = np.asanyarray(sw_moved_mask.dataobj) - np.asanyarray(nt_moved_mask.dataobj)
Expand All @@ -305,7 +306,7 @@ def test_apply_linear_transform(tmpdir, get_testdata, get_testmask, image_orient
sw_moved = nb.load("resampled.nii.gz")
sw_moved.set_data_dtype(img.get_data_dtype())

nt_moved = xfm.apply(img, order=0)
nt_moved = apply(xfm, img, order=0)
diff = (
np.asanyarray(sw_moved.dataobj, dtype=sw_moved.get_data_dtype())
- np.asanyarray(nt_moved.dataobj, dtype=nt_moved.get_data_dtype())
Expand All @@ -314,7 +315,7 @@ def test_apply_linear_transform(tmpdir, get_testdata, get_testmask, image_orient
# A certain tolerance is necessary because of resampling at borders
assert np.sqrt((diff[brainmask] ** 2).mean()) < RMSE_TOL

nt_moved = xfm.apply("img.nii.gz", order=0)
nt_moved = apply(xfm, "img.nii.gz", order=0)
diff = (
np.asanyarray(sw_moved.dataobj, dtype=sw_moved.get_data_dtype())
- np.asanyarray(nt_moved.dataobj, dtype=nt_moved.get_data_dtype())
Expand Down Expand Up @@ -343,22 +344,23 @@ def test_LinearTransformsMapping_apply(tmp_path, data_path, testdata_path):
assert isinstance(hmc, nitl.LinearTransformsMapping)

# Test-case: realign functional data on to sbref
nii = hmc.apply(
testdata_path / "func.nii.gz", order=1, reference=testdata_path / "sbref.nii.gz"
nii = apply(
hmc, testdata_path / "func.nii.gz", order=1, reference=testdata_path / "sbref.nii.gz"
)
assert nii.dataobj.shape[-1] == len(hmc)

# Test-case: write out a fieldmap moved with head
hmcinv = nitl.LinearTransformsMapping(
np.linalg.inv(hmc.matrix), reference=testdata_path / "func.nii.gz"
)
nii = hmcinv.apply(testdata_path / "fmap.nii.gz", order=1)
nii = apply(hmcinv, testdata_path / "fmap.nii.gz", order=1)
assert nii.dataobj.shape[-1] == len(hmc)

# Ensure a ValueError is issued when trying to do weird stuff
hmc = nitl.LinearTransformsMapping(hmc.matrix[:1, ...])
with pytest.raises(ValueError):
hmc.apply(
apply(
hmc,
testdata_path / "func.nii.gz",
order=1,
reference=testdata_path / "sbref.nii.gz",
Expand Down

0 comments on commit aaa4637

Please sign in to comment.