`DenseFieldTransform.map()` distorts displacements near the edges of the field

Open Beginner friendly
#307 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
data

Research direction

Start in nitransforms/nonlinear.py at the DenseFieldTransform.map() interpolation around lines 193-209, where absolute mapped positions are splined instead of the displacements themselves. Run the reproduction snippet from the issue to see the edge error, then check how the grid coordinates are added back after interpolation. Done means the constant 6 mm shift is recovered within the stated tolerance at every sample point, including the outer ones.

Written by the indexing model from the issue text.

Description

Off the voxel grid, map() interpolates the absolute mapped positions (grid coordinates plus displacements) with a cubic spline:

https://github.com/nipy/nitransforms/blob/06537caee1e5908e7faada275187a0568982b549/nitransforms/nonlinear.py#L193-L209

Reproduction

A constant 6mm shift should be recoverable to a reasonable tolerance:

import nibabel as nb
import numpy as np
from nitransforms.nonlinear import DenseFieldTransform

# 21^3 grid of 4 mm voxels spanning -40..40 mm; constant +6 mm x-shift
deltas = np.zeros((21, 21, 21, 3), dtype="float32")
deltas[..., 0] = 6.0
affine = nb.affines.from_matvec(np.eye(3) * 4, [-40] * 3)
xfm = DenseFieldTransform(nb.Nifti1Image(deltas, affine))

x = np.array([-39.5, -38.5, -30.5, -20.5, 0.5, 20.5, 30.5, 38.5, 39.5])
points = np.stack([x, np.full_like(x, 0.5), np.full_like(x, 0.5)], axis=1)

assert np.allclose(xfm.map(points) - points, [6, 0, 0], atol=1e-4)

Actual x shift:

>>> (xfm.map(points) - points)[:, 0]
array([5.60253403, 5.31986161, 5.95116826, 5.99918935, 5.99999812,
       6.00081065, 6.04883174, 6.68013839, 6.39746597])
Dominant language
Python
Stars
36
Forks
18
Avg merge
1h 24m
Merged PRs (30d)
1

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nipy/nitransforms

All issues in nipy/nitransforms

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.