Skip to content

Commit 19369df

Browse files
committed
Fix slicing with negative and out-of-range indices
NDCubeSlicingMixin.__getitem__ passed negative and out-of-range indices straight to the WCS and NDMeta slicers. Before astropy 7.2.3/8.0.2 the WCS slicer takes negative values as offsets from 0, so cube[-1] and cube[-2:] sliced the WCS at negative pixels; from those versions on it raises for them when the WCS has no array_shape. NDMeta's slicer computed shapes with the wrong sign and raised. Resolve each item against self.shape after sanitize_slices, and size NDMeta axes with len(range(n)[item]). The slice step is dropped: sanitize_slices only lets a step of 1 through, and astropy before 7.2.3/8.0.2 rejects an explicit step when slicing an already-sliced WCS, so cube[1:][:2:1] failed. With NDMeta fixed, NDCollection's own negative-index workaround is no longer needed, and it raised TypeError for slices with a None bound such as collection[-2:], so remove it. NDCubeSequence.index_as_cube now resolves the common-axis index against the total common-axis length; it mishandled e.g. index_as_cube[:, -11:-5] and index_as_cube[:, -1]. Refs sunpy#860 and astropy/astropy#15557.
1 parent 2e925b1 commit 19369df

9 files changed

Lines changed: 52 additions & 36 deletions

File tree

‎changelog/979.bugfix.1.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix slicing `~ndcube.NDCube`, `~ndcube.NDCubeSequence` (including `~ndcube.NDCubeSequence.index_as_cube`), `~ndcube.NDCollection` and `~ndcube.NDMeta` with negative or out-of-range indices, which gave wrong world coordinates or raised errors with axis-aware metadata.

‎ndcube/meta.py‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -392,17 +392,8 @@ def __getitem__(self, item):
392392
if isinstance(axis_item, numbers.Integral):
393393
dropped_axes[i] = True
394394
elif isinstance(axis_item, slice):
395-
start = axis_item.start
396-
if start is None:
397-
start = 0
398-
if start < 0:
399-
start = data_shape[i] - start
400-
stop = axis_item.stop
401-
if stop is None:
402-
stop = data_shape[i]
403-
if stop < 0:
404-
stop = data_shape[i] - stop
405-
new_shape[i] = stop - start
395+
# Slicing a range of the axis length gives the new length, resolving negative and out-of-range bounds.
396+
new_shape[i] = len(range(data_shape[i])[axis_item])
406397
else:
407398
raise TypeError("Unrecognized slice type. "
408399
"Must be an int, slice and tuple of the same.")

‎ndcube/mixins/ndslicing.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ def __getitem__(self, item):
4141

4242
# Slice cube.
4343
item = tuple(sanitize_slices(item, len(self.shape)))
44+
# Resolve negative and out-of-range bounds against the cube's shape, as numpy does for the data.
45+
# Before astropy 7.2.3/8.0.2 the WCS slicer takes negative values as offsets from 0 (astropy#15557),
46+
# and since then it raises for them if the WCS has no array_shape.
47+
# Drop the step (at most 1 here): nested WCS slicing rejects an explicit step before those versions.
48+
item = tuple(slice(*i.indices(n)[:2]) if isinstance(i, slice) else range(n)[i]
49+
for i, n in zip(item, self.shape))
4450
sliced_cube = super().__getitem__(item)
4551
if meta_is_sliceable:
4652
self.meta = meta # Add unsliced meta back onto unsliced cube.

‎ndcube/ndcollection.py‎

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -182,30 +182,7 @@ def __getitem__(self, item):
182182
new_keys = list(self.keys())
183183
# Slice meta if sliceable
184184
if hasattr(self.meta, "__ndcube_can_slice__") and self.meta.__ndcube_can_slice__:
185-
# Convert negative indices to positive indices as they are not supported by NDMeta.slice
186-
sanitized_item = copy.deepcopy(item)
187-
aligned_shape = self.aligned_dimensions
188-
if isinstance(item, numbers.Integral):
189-
if item < 0:
190-
sanitized_item = int(self.aligned_dimensions[0] + item)
191-
elif isinstance(item, slice):
192-
if (item.start is not None and item.start < 0) or (item.stop is not None and item.stop < 0):
193-
new_start = aligned_shape[0] + item.start if item.start < 0 else item.start
194-
new_stop = aligned_shape[0] + item.stop if item.stop < 0 else item.stop
195-
sanitized_item = slice(new_start, new_stop)
196-
else:
197-
sanitized_item = list(sanitized_item)
198-
for i, ax_it in enumerate(item):
199-
if isinstance(ax_it, numbers.Integral) and ax_it < 0:
200-
sanitized_item[i] = aligned_shape[i] + ax_it
201-
elif isinstance(ax_it, slice):
202-
if (ax_it.start is not None and ax_it.start < 0) or (ax_it.stop is not None and ax_it.stop < 0):
203-
new_start = aligned_shape[i] + ax_it.start if ax_it.start < 0 else ax_it.start
204-
new_stop = aligned_shape[i] + ax_it.stop if ax_it.stop < 0 else ax_it.stop
205-
sanitized_item[i] = slice(new_start, new_stop)
206-
sanitized_item = tuple(sanitized_item)
207-
# Use sanitized item to slice meta.
208-
new_meta = self.meta.slice[sanitized_item]
185+
new_meta = self.meta.slice[item]
209186
else:
210187
new_meta = copy.deepcopy(self.meta)
211188

‎ndcube/ndcube_sequence.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,14 +500,17 @@ def __getitem__(self, item):
500500
return self.seq[(slice(None), *item)]
501501
if isinstance(item[common_axis], numbers.Integral):
502502
# If common_axis item is an int or return an NDCube with dimensionality of N-1
503+
# Indexing a range resolves a negative index against the total common-axis length.
503504
sequence_index, common_axis_index = \
504505
utils.sequence.cube_like_index_to_sequence_and_common_axis_indices(
505-
item[common_axis], common_axis, common_axis_lengths)
506+
range(sum(common_axis_lengths))[item[common_axis]], common_axis, common_axis_lengths)
506507
# Insert index for common axis in item for slicing the NDCube.
507508
cube_item = copy.deepcopy(item)
508509
cube_item[common_axis] = common_axis_index
509510
return self.seq.data[sequence_index][tuple(cube_item)]
510511
# item can now only be a tuple whose common axis item is a non-None slice object.
512+
# Resolve negative and out-of-range bounds against the total common-axis length.
513+
item[common_axis] = slice(*item[common_axis].indices(sum(common_axis_lengths))[:2])
511514
# Convert item into iterable of SequenceItems and slice each cube appropriately.
512515
# item for common_axis must always be a slice for every cube,
513516
# even if it is only a length-1 slice.

‎ndcube/tests/test_meta.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ def test_slice_dependent_axes(basic_meta):
129129
assert_metas_equal(output, expected)
130130

131131

132+
@pytest.mark.parametrize(("item", "expected"), [(np.s_[:, -2:], [4, 6]), (np.s_[:, :-1], [2, 4]), (np.s_[:, 1:10], [4, 6])])
133+
def test_slice_negative_and_out_of_range_bounds(basic_meta, item, expected):
134+
output = basic_meta.slice[item]
135+
assert output["e"] == expected
136+
assert output.data_shape[1] == len(expected)
137+
138+
132139
def test_slice_by_str(basic_meta):
133140
meta = basic_meta
134141
assert meta["a"] == "hello"

‎ndcube/tests/test_ndcollection.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ def test_construct_with_dict():
6363
[("cube0", cube0[:, -3:-1]), ("cube1", cube1[:, :, -3:-1]), ("cube2", cube2[:, -3:-1])],
6464
aligned_axes=aligned_axes, meta=cube_coll_meta.slice[1:3])),
6565
66+
(slice(-2, None), cube_collection, NDCollection(
67+
[("cube0", cube0[:, -2:]), ("cube1", cube1[:, :, -2:]), ("cube2", cube2[:, -2:])],
68+
aligned_axes=aligned_axes, meta=cube_coll_meta.slice[2:4])),
69+
6670
((slice(None), slice(1, 2)), cube_collection, NDCollection(
6771
[("cube0", cube0[:, :, 1:2]), ("cube1", cube1[1:2]), ("cube2", cube2[:, :, 1:2])],
6872
aligned_axes=aligned_axes, meta=cube_coll_meta.slice[:, 1:2])),

‎ndcube/tests/test_ndcube_slice_and_crop.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,3 +637,19 @@ def test_crop_by_values_quantity_table_coordinate():
637637
wcs=cube.extra_coords)
638638
assert cropped.shape == (10, 5)
639639
np.testing.assert_array_equal(cropped.data, data[3:13, 1:6])
640+
641+
642+
@pytest.mark.parametrize(("item", "expected"), [(np.s_[-2:], np.s_[3:]), (np.s_[1:-1], np.s_[1:4]), (np.s_[:10], np.s_[:])])
643+
def test_slice_negative_and_out_of_range_bounds(ndcube_4d_axis_aware_meta, item, expected):
644+
cube = ndcube_4d_axis_aware_meta
645+
helpers.assert_cubes_equal(cube[item], cube[expected])
646+
647+
648+
def test_slice_negative_integer_drops_correct_coordinate(ndcube_4d_ln_lt_l_t):
649+
cube = ndcube_4d_ln_lt_l_t
650+
assert cube[..., -1].global_coords["time"] == cube[..., 11].global_coords["time"]
651+
assert cube[..., 1:][..., -1].global_coords["time"] == cube[..., 11].global_coords["time"]
652+
653+
654+
def test_slice_nested_with_step_one(ndcube_4d_ln_lt_l_t):
655+
assert ndcube_4d_ln_lt_l_t[1:][:2:1].shape == (2, 8, 10, 12)

‎ndcube/tests/test_ndcubesequence.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def test_slice_common_axis(ndc, item, expected_common_axis):
8080
("ndcubesequence_4c_ln_lt_l_cax1", np.s_[:, 2:4], (2, 2, 1, 4)),
8181
("ndcubesequence_4c_ln_lt_l_cax1", np.s_[:, 0:6], (2, 2, 3, 4)),
8282
("ndcubesequence_4c_ln_lt_l_cax1", np.s_[0, 0:6], (2, 3, 4)),
83+
("ndcubesequence_4c_ln_lt_l_cax1", np.s_[:, -11:-5], (3, 2, (2, 3, 1), 4)),
8384
],
8485
indirect=("ndc",))
8586
def test_index_as_cube(ndc, item, expected_shape):
@@ -220,3 +221,13 @@ def test_slice_meta(ndcubesequence_4c_ln_lt_l_cax1):
220221
"pixel response": u.Quantity([100] * 4, unit=u.percent)},
221222
axes={"exposure time": 0, "pixel response": 0}, data_shape=(4, 2, 4))
222223
helpers.assert_metas_equal(sliced_seq.meta, expected_meta)
224+
225+
226+
def test_slice_negative_indices(ndcubesequence_4c_ln_lt_l_cax1):
227+
seq = ndcubesequence_4c_ln_lt_l_cax1
228+
helpers.assert_cubesequences_equal(seq[-2:, :, -1], seq[2:, :, 2])
229+
230+
231+
def test_index_as_cube_negative_int(ndcubesequence_3c_l_ln_lt_cax1):
232+
seq = ndcubesequence_3c_l_ln_lt_cax1
233+
helpers.assert_cubes_equal(seq.index_as_cube[:, -1], seq.index_as_cube[:, seq.cube_like_shape[1] - 1])

0 commit comments

Comments
 (0)