`BSplineFieldTransform.to_field()` reuses the weights of its first reference
Assessment
- Difficulty
- 3/5
- Estimated time
- Half a day
- Newbie friendliness
- 55/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- python
Research direction
The bug is in BSplineFieldTransform.to_field() in nitransforms/nonlinear.py, which caches B-spline weights on the first call and reuses them. Start by reading that method and its cache, then run the reproduction script from the issue to confirm the stale deltas and the broadcast ValueError on the smaller reference. Done when to_field() recomputes weights for each reference and both the shifted and smaller-grid cases produce correct fields.
Written by the indexing model from the issue text.
Description
to_field() computes the B-spline weights for its reference on the first call and caches them. Later calls reuse those weights whatever reference they are given.
Reproduction
import nibabel as nb
import numpy as np
from nitransforms.base import ImageGrid
from nitransforms.nonlinear import BSplineFieldTransform
def centered_grid(linear, shape):
"""Affine for a grid of given shape, centered at the origin, with given linear part"""
affine = nb.affines.from_matvec(linear, -linear @ (np.array(shape) - 1) / 2)
return nb.Nifti1Image(np.zeros(shape), affine)
# A 10 mm lattice of 10 x 10 x 10 knots, centered at the origin
knots = ImageGrid(centered_grid(np.eye(3) * 10, (10, 10, 10)))
# Set coefficient to RAS position, which is linear and so exactly representable
coeffs = knots.ndcoords.reshape(10, 10, 10, 3).astype("float32")
xfm = BSplineFieldTransform(nb.Nifti1Image(coeffs, knots.affine))
first = centered_grid(np.eye(3) * 3, (11, 11, 11))
shifted = nb.Nifti1Image(first.dataobj, nb.affines.from_matvec(np.eye(3) * 3, [-10, -12, -14]))
xfm.to_field(reference=first)
field = xfm.to_field(reference=shifted)
assert np.allclose(field._deltas.reshape(-1, 3), ImageGrid(shifted).ndcoords, atol=1e-3)
A smaller reference fails outright:
xfm.to_field(reference=centered_grid(np.eye(3) * 3, (5, 5, 5)))
ValueError: could not broadcast input array from shape (1331,) into shape (125,)
- Dominant language
- Python
- Stars
- 36
- Forks
- 18
- Avg merge
- 1h 24m
- Merged PRs (30d)
- 1
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from nipy/nitransforms
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
nipy/nitransforms#307 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
nipy/nitransforms#306 ·
-
Difficulty 3/5 Half a day Newbie friendliness 25/100
nipy/nitransforms#305 ·
-
`BSplineFieldTransform` ignores `order`Possibly taken @effigies claimed this 1 day ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 48/100
nipy/nitransforms#302 ·
-
`BSplineFieldTransform.to_field()` ignores the orientation of the reference gridPossibly taken @effigies claimed this 1 day ago. Open
Difficulty 3/5 Half a day Newbie friendliness 45/100
nipy/nitransforms#300 ·
All issues in nipy/nitransforms
Similar issues
-
eegprep-lean: set channel types from the Zarr group's channel_typePossibly taken @claude claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
sccn/eegprep#668 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
blacklanternsecurity/bbot#3679 ·
Maintainers usually reply within 2 days
-
Link Checker ReportOpenBot Type: Bug Type: Documentation
Difficulty 1/5 Under an hour Newbie friendliness 85/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
speakeasy-api/speakeasy#2159 ·
Maintainers usually reply within 1 day