You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SlicedLowLevelWCS does not handle negative indexing correctly #15557
SlicedLowLevelWCS is a WCS wrapper for slicing WCSs. It takes a WCS object and a sequence of slice items that can be applied to a data array, and returns a WCS object that ensures the same pixels correspond to the same world coordinates before and after slicing.
In slice items, negative indices are used to express indexing from the end of the array back towards the beginning. However, SlicedLowLevelWCS interprets negative indices in the standard WCS way, i.e. indices to the left of 0.
Expected behavior
Since the inputs are slice items for slicing the data array, SlicedLowLevelWCS should interpret negative indices along the same array slicing paradigm. If negative indices are supplied, it should therefore either:
Error with a message saying negative indices are not supported, or;
Convert the negative indices to their positive equivalent using the axis length information if available.
Nested slicing hits the same problem through combine_slices, which adds a negative integer to the parent slice's start instead of counting from the end of the parent. Seen on astropy 7.2.0; combine_slices is the same on main.
fromastropy.wcsimportWCSfromastropy.wcs.wcsapiimportSlicedLowLevelWCSwcs=WCS(naxis=2)
wcs.wcs.ctype= ["WAVE", "TIME"]
wcs.wcs.cunit= ["m", "s"]
wcs.wcs.crpix= [1, 1] # array index i along axis 0 has time i swcs.pixel_shape= (3, 6)
inner=SlicedLowLevelWCS(wcs, [slice(1, 5)]) # array indices 1 to 4print(SlicedLowLevelWCS(inner, [3]).dropped_world_dimensions["value"]) # correct: 4print(SlicedLowLevelWCS(inner, [-1]).dropped_world_dimensions["value"]) # expected 4
[array(4.)]
[array(0.)]
inner[-1] should be array index 4, but combine_slices returns 1 + (-1) = 0. I think resolving negative indices against array_shape in SlicedLowLevelWCS.__init__, before combine_slices runs, would fix both the flat and the nested case.
In the meantime ndcube resolves indices itself before slicing the WCS: sunpy/ndcube#979.
Activity
Fix slicing with negative and out-of-range indices
nabobalis commentedon Sep 27, 2026
Nested slicing hits the same problem through
combine_slices, which adds a negative integer to the parent slice's start instead of counting from the end of the parent. Seen on astropy 7.2.0;combine_slicesis the same on main.inner[-1]should be array index 4, butcombine_slicesreturns1 + (-1) = 0. I think resolving negative indices againstarray_shapeinSlicedLowLevelWCS.__init__, beforecombine_slicesruns, would fix both the flat and the nested case.In the meantime ndcube resolves indices itself before slicing the WCS: sunpy/ndcube#979.
Fix slicing with negative and out-of-range indices
astrofrog commentedon Oct 22, 2026
I believe this is fixed - the code above gives:
and if I comment out
wcs._naxis = [5], I get:Fix slicing with negative and out-of-range indices
nabobalis commentedon Oct 22, 2026
Thank you Tom