Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions nitransforms/tests/test_resampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,30 @@ def test_apply_transformchain(tmp_path, testdata_path):
assert (np.abs(diff) > 1e-3).sum() / diff.size < RMSE_TOL_LINEAR


@pytest.mark.xfail(reason="gh-281: applying a single 3D transform to 4D data")
def test_apply_single_3d_on_4d():
"""Apply one 3D transform across all timepoints of a 4D dataset."""
nvols = 5
data = np.zeros((10, 5, 5, nvols), dtype=np.float32)
for i in range(nvols):
data[i + 1, 2, 2, i] = i + 1

img = nb.Nifti1Image(data, np.eye(4))

mat = np.eye(4)
mat[0, 3] = -1.0
ref = nb.Nifti1Image(np.zeros((10, 5, 5), dtype=np.uint8), np.eye(4))
xfm = nitl.Affine(mat, reference=ref)

moved = apply(xfm, img, order=0)
moved_data = np.asanyarray(moved.dataobj)

assert moved_data.shape == data.shape
for i in range(nvols):
assert moved_data[i + 2, 2, 2, i] == i + 1
assert moved_data[i + 1, 2, 2, i] == 0
Comment on lines +333 to +354
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At present, this could fail because checking the values after applying the transform is wrong. I think a much simpler test is:

Suggested change
@pytest.mark.xfail(reason="gh-281: applying a single 3D transform to 4D data")
def test_apply_single_3d_on_4d():
"""Apply one 3D transform across all timepoints of a 4D dataset."""
nvols = 5
data = np.zeros((10, 5, 5, nvols), dtype=np.float32)
for i in range(nvols):
data[i + 1, 2, 2, i] = i + 1
img = nb.Nifti1Image(data, np.eye(4))
mat = np.eye(4)
mat[0, 3] = -1.0
ref = nb.Nifti1Image(np.zeros((10, 5, 5), dtype=np.uint8), np.eye(4))
xfm = nitl.Affine(mat, reference=ref)
moved = apply(xfm, img, order=0)
moved_data = np.asanyarray(moved.dataobj)
assert moved_data.shape == data.shape
for i in range(nvols):
assert moved_data[i + 2, 2, 2, i] == i + 1
assert moved_data[i + 1, 2, 2, i] == 0
@pytest.mark.xfail(reason="gh-281: applying a single 3D transform to 4D data", strict=True)
def test_apply_single_3d_on_4d():
"""Apply one 3D transform across all timepoints of a 4D dataset."""
img4d = nb.Nifti1Image(np.zeros((2, 3, 4, 5), np.eye(4))
ref3d = img4d.slicer[..., 0]
xfm = nitl.Affine(np.eye(4))
# Just a smoke test. When this passes, we should validate the values.
apply(xfm, img, reference=ref3d)

I'm adding strict=True so that the test doesn't just sit around XPASSing when we should start validating values after that point.



@pytest.mark.parametrize("serialize_4d", [True, False])
def test_LinearTransformsMapping_apply(
tmp_path, data_path, testdata_path, serialize_4d
Expand Down
Loading