Support (N, N) affines in rescale_affine - #1553
Merged
Merged
Conversation
rescale_affine is documented for an (N, N) affine describing an (N - 1)-dimensional space, but the body slices affine[:3, :3] while voxel_sizes() correctly returns N - 1 values. Anything other than a 4x4 affine therefore fails with a broadcasting error. Derive the dimensionality from the affine instead of assuming 3.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1553 +/- ##
=======================================
Coverage 95.48% 95.48%
=======================================
Files 209 209
Lines 30045 30065 +20
Branches 4492 4492
=======================================
+ Hits 28689 28709 +20
Misses 925 925
Partials 431 431 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
effigies
reviewed
Sep 29, 2026
effigies
left a comment
Member
There was a problem hiding this comment.
Thanks for this! A couple small comments, but this is definitely makes sense given that we mostly do write these without limiting to 3-dimensional data.
Slice the affine with [:-1, :-1] rather than deriving a dimensionality from its first axis, so the rotation/zoom/shear block is taken without assuming the affine is square, and drop the now-redundant np.asarray(). Add a 4D case alongside the 2D one.
effigies
approved these changes
Sep 29, 2026
effigies
left a comment
Member
There was a problem hiding this comment.
Thanks! Good to merge when tests pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
rescale_affine()documents itsaffineparameter asbut the implementation slices
affine[:3, :3], so only a 4x4 affine actually works.voxel_sizes(), whichrescale_affine()calls, generalises correctly and returns N - 1 values, so the two disagree and the multiply raises:The same happens for a 5x5 affine.
Change
Take the dimensionality from the affine rather than assuming 3.
apply_affine()andfrom_matvec()are already dimension-agnostic, so nothing else needed adjusting. The 4x4 path is unchanged.np.asarray()is applied toaffinefirst so that array-likes work, matching what the docstring promises.Tests
Added
test_rescale_affine_2d, which checks the returned affine has the right shape and zooms and that the documented invariant - the RAS location of the central voxel is preserved - still holds. It fails with aValueErrorbefore the change.pytest nibabel/tests/test_affines.pypasses (8 passed).