Skip to content

BF: Resolve slice starts against axis length in slice_affine - #1534

Merged
effigies merged 3 commits into
nipy:masterfrom
wterrr:fix/1533-slicer-negative-starts
Nov 7, 2026
Merged

effigies merged 3 commits into
nipy:masterfrom
wterrr:fix/1533-slicer-negative-starts

Conversation

@wterrr

@wterrr wterrr commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

SpatialFirstSlicer.slice_affine copied subslicer.start straight into the affine translation. Two cases were therefore wrong:

  • A negative start (img.slicer[-2:]) put the negative value into the affine instead of the equivalent positive index.
  • A negative step with an implicit start (img.slicer[::-1]) used 0 as the origin, but the first voxel of the result is the last voxel of the axis.

In both cases the returned data was correct while the affine was not, so the image was silently shifted in RAS+ space. img.slicer[-2:] and img.slicer[8:] select the same voxels but produced affines 20 mm apart. nib-roi is affected too, since it accepts negative starts and :-1 steps and calls slice_affine directly.

Resolve each spatial subslicer with slice.indices() so the transform is built from the concrete start and step that dataobj[slicer] uses.

Closes gh-1533.

@Arthur031221

Copy link
Copy Markdown
Contributor

@wterrr The three follow-on pieces are up as wterrr#1 against this branch, opened on 27 July: the doctest update, an independent affine assertion in test_nib_roi, and one line covering slice_affine directly.

Flagging them here because two of them affect what this PR leaves behind:

doc/source/nibabel_images.rst line 352 prints the X translation for img.slicer[::-1] as 117.86, which is the pre-fix value. Once this merges the correct value is -136.14 and that doctest starts failing. The doctest job in test.yml runs with continue-on-error: true, so CI would stay green and the wrong affine would sit in the docs.

test_nib_roi asserts np.allclose(in_sliced.affine, out_img.affine), which compares the slicer against itself. It already exercises -j -1:1:-1, a negative start with a negative step, and it passes either way. That is why nothing caught this. Computing the expected affine independently makes it catch it: with the fix here reverted, the old assertion still gives 12 passed and the new one gives 2 failed.

The third is one line asserting slice_affine directly rather than only through img.slicer, since the affine calculator and the slicer were asked for separately in the issue.

Whichever is least work for you. If you would rather not take a PR into your branch, I can open the doctest and test_roi changes as a separate PR against master to merge after this one. They cannot go in before, since the doctest only becomes correct once this fix is in. The slice_affine line sits inside the test added here, so it belongs either on this PR or on a follow-up.

No rush from my side. I just did not want the doctest to slip through quietly.

@codecov

codecov Bot commented Aug 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.59259% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.45%. Comparing base (e3026a8) to head (6b00715).

Files with missing lines Patch % Lines
nibabel/tests/test_spatialimages.py 88.88% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1534      +/-   ##
==========================================
- Coverage   95.45%   95.45%   -0.01%     
==========================================
  Files         209      209              
  Lines       30089    30113      +24     
  Branches     5183     5187       +4     
==========================================
+ Hits        28722    28744      +22     
- Misses        937      938       +1     
- Partials      430      431       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

`SpatialFirstSlicer.slice_affine` copied `subslicer.start` straight into the
affine translation. Two cases were therefore wrong:

* A negative start (``img.slicer[-2:]``) put the negative value into the
  affine instead of the equivalent positive index.
* A negative step with an implicit start (``img.slicer[::-1]``) used 0 as the
  origin, but the first voxel of the result is the *last* voxel of the axis.

In both cases the returned data was correct while the affine was not, so the
image was silently shifted in RAS+ space. ``img.slicer[-2:]`` and
``img.slicer[8:]`` select the same voxels but produced affines 20 mm apart.
`nib-roi` is affected too, since it accepts negative starts and ``:-1`` steps
and calls `slice_affine` directly.

Resolve each spatial subslicer with `slice.indices()` so the transform is
built from the concrete start and step that `dataobj[slicer]` uses.

Closes nipygh-1533.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@effigies
effigies force-pushed the fix/1533-slicer-negative-starts branch from c2ac2ce to 637dc0b Compare October 15, 2026 18:18
@read-the-docs-community

read-the-docs-community Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

@effigies

Copy link
Copy Markdown
Member

Apologies for taking so long to get around to this.

@Arthur031221 Do you by chance still have that repository around locally? I could cherry-pick your fixes before merge.

@effigies effigies added the bug label Oct 15, 2026
@Arthur031221

Copy link
Copy Markdown
Contributor

@effigies I don't have a local checkout anymore, but the commit is still on GitHub as the single commit of wterrr#1 (closed unmerged):

git fetch https://github.com/wterrr/nibabel pull/1/head
git cherry-pick FETCH_HEAD

I ran exactly that on this PR's current head (42a0585) and it applies cleanly. It touches three files: the slice_affine line in test_slicer_preserves_ras_position, the independent affine check in test_nib_roi, and the 117.86 to -136.14 value in doc/source/nibabel_images.rst.

With it applied, test_roi.py and test_spatialimages.py pass. With spatialimages.py back at the base version, test_nib_roi[True], test_nib_roi[False] and test_slicer_preserves_ras_position fail. The img.slicer[::-1] example from the doc gives -136.14 for the X translation with the fix and 117.86 without.

Three things the fix needs alongside it.

The flip example in doc/source/nibabel_images.rst prints an X translation of
117.86, which is the value from before the fix. With the fix in place that
doctest fails, and the job that would catch it is continue-on-error, so it
would land quietly.

test_nib_roi asserted np.allclose(in_sliced.affine, out_img.affine), which
compares the slicer against itself and passes whatever the slicer does. It
already exercises -j -1:1:-1, so that assertion is why this was never caught
there. It now compares both against an affine calculated independently.

test_slicer_preserves_ras_position went through img.slicer only. One line
asserts slice_affine directly as well, so the affine calculator is covered on
its own rather than through its caller.
@effigies
effigies merged commit 1bf9c36 into nipy:master Nov 7, 2026
36 of 37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

img.slicer produces a wrong affine for negative starts and reversed slices

3 participants