Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

DanRyanIrish
Copy link
Contributor

@DanRyanIrish DanRyanIrish commented Aug 14, 2025

Description

This PR provides an optional way to overcome a bug in SlicedLowLevelWCS, whereby slices that would reduce a data array associated with the WCS to length-0 axes is not caught. WCS cannot describe scalars or length-0 array axes. However, the current sanitization of inputs to SlicedLowLevelWCS currently allows such scenarios. This PR introduces a new optional kwarg to SlicedLowLevelWCS (presliced_shape) and another to sanitize_slices (shape), which give the shape of the data array described by the WCS. If either are set, an additional set of sanitization checks is performed to catch the cases below.

(This bug was identified by @hayesla when using ndcube.)

Case 1: Slices with stop >= start

Slice items like slice(3, 2), slice(-2, -4), and slice(1, 1) can be applied to a data array and will return array([]). However, this is an array that cannot be validly described by a WCS. This PR solves this problem by erroring if stop - start <= 0.

Case 2: Slices with mixed positive and negative indices

If the slice item mixes and matches positive and negative indices, e.g. slice(3, -2) or slice(-4, 5), the above check cannot be done without knowing the length of the relevant axis. In this case, the shape info is combined with the info in the slice object to perform the same check as in Case 1.

Case 3: Indices or slices whose range are entirely outside the data array

If we have an array of length 3, and we apply an index or slice whose range is beyond this, e.g. 6, -6, slice(5, 8) or slice(-20, -15), this will again lead to an array of array([]) being returned which cannot be described by a WCS. A separate check is performed to catch these cases. But, like Case 2, this requires the data shape information provided by the new kwargs.

Discussion on Making These Checks Optional

The reason for making the checks of Cases 2 and 3 optional is that a WCS can be a standalone object without associated with a data array. If such a WCS would like to be sliced for some reason, shape information may not be available.

The check in Case 1 can actually be performed without the shape information and so could be performed every time. I think this is a valid choice, but the reasons I didn't go that way here are:

  1. Doing them every time would technically be a breaking change. Only performing it when shape is set, is not.
  2. The slice items in Case 1 are valid slice items for arrays, even if they return non-WCS-describable arrays. Some users may not want this level of restriction.
  3. Always checking Case 1 but not Case 2 would probably feel like a bug to any users who encounter it, since if you want this check performed, you probably want it regardless of the combination of positive and negative slice indices.
  4. Doing all the checks only if shape is set, simplifies the code somewhat. (A less crucial consideration)

Fixes #

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

Copy link
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

Catches cases when negative slice start or index values are beyond extent of array associated
with SlicedLowLevelWCS. Also added more test coverage.
@pllim pllim requested a review from Cadair August 14, 2025 16:16
@pllim pllim added this to the v7.2.0 milestone Aug 14, 2025
@pllim pllim added Bug API change PRs and issues that change an existing API, possibly requiring a deprecation period labels Aug 14, 2025
@astrofrog
Copy link
Member

astrofrog commented Aug 22, 2025

I'm in favor of adding in these checks, but I wonder if presliced_shape is really necessary since APE 14 provides a way for the WCS to contain shape info:

https://docs.astropy.org/en/latest/api/astropy.wcs.WCS.html#astropy.wcs.WCS.array_shape

If this is correctly set, it could be used in the validation instead of having to have this be passed explicitly? Or is there a reason that array_shape can't be used?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API change PRs and issues that change an existing API, possibly requiring a deprecation period Bug wcs.wcsapi
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants