Skip to content

Error with 3D WCS slicing #18410

Closed
Closed

Description

@astrofrog

Description

Slicing a 3D WCS does not work

Expected behavior

In [4]: wcs = WCS(naxis=3)[:8, :10, :10]

How to Reproduce

In [5]: wcs = WCS(naxis=3)[:8, :10, :10]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[5], line 1
----> 1 wcs = WCS(naxis=3)[:8, :10, :10]

File ~/Code/astropy/astropy/wcs/wcs.py:3438, in WCS.__getitem__(self, item)
   3433 def __getitem__(self, item):
   3434     # "getitem" is a shortcut for self.slice; it is very limited
   3435     # there is no obvious and unambiguous interpretation of wcs[1,2,3]
   3436     # We COULD allow wcs[1] to link to wcs.sub([2])
   3437     # (wcs[i] -> wcs.sub([i+1])
-> 3438     return self.slice(item)

File ~/Code/astropy/astropy/wcs/wcs.py:3414, in WCS.slice(self, view, numpy_order)
   3409             table.crval[wcs_index] -= iview.start
   3411 try:
   3412     # range requires integers but the other attributes can also
   3413     # handle arbitrary values, so this needs to be in a try/except.
-> 3414     nitems = len(builtins.range(self._naxis[wcs_index])[iview])
   3415 except TypeError as exc:
   3416     if "indices must be integers" not in str(exc):

IndexError: list index out of range

Versions

platform
--------
platform.platform() = 'Linux-6.1.0-37-amd64-x86_64-with-glibc2.36'
platform.version() = '#1 SMP PREEMPT_DYNAMIC Debian 6.1.140-1 (2025-05-22)'
platform.python_version() = '3.11.2'

packages
--------
astropy              7.2.dev250+g0303742123.d20250709
numpy                2.3.1
scipy                1.16.0
matplotlib           3.10.3
pandas               2.3.1
pyerfa               2.0.1.5

Activity

  1. astrofrog commented on Jul 9, 2025

    @astrofrog
    MemberAuthor

    This works fine for 2D WCS, I think the issue might be that _naxis is always of length 2.

  2. pllim commented on Jul 9, 2025

    @pllim
    Member

    Why does this sound familiar? Why does @Cadair pop up in this suppressed memory?

  3. ayshih commented on Jul 9, 2025

    @ayshih
    Contributor

    This works fine for 2D WCS, I think the issue might be that _naxis is always of length 2.

    As clarification, _naxis is set to the sentinel value of [0, 0] when the pixel/array shape is undefined. (I just tangled with this sentinel value on #18405.) Apparently slice() has no awareness of this sentinel value. If you define the pixel/array shape for the 3D WCS, _naxis will properly be a 3-element list, and then slicing works fine:

    >>> from astropy.wcs import WCS
    >>> wcs = WCS(naxis=3)
    >>> wcs.pixel_shape = (1, 1, 1)
    >>> wcs[:8, :10, :10]
    WCS Keywords
    
    Number of WCS axes: 3
    CTYPE : '' '' ''
    CRVAL : 0.0 0.0 0.0
    CRPIX : 0.0 0.0 0.0
    PC1_1 PC1_2 PC1_3  : 1.0 0.0 0.0
    PC2_1 PC2_2 PC2_3  : 0.0 1.0 0.0
    PC3_1 PC3_2 PC3_3  : 0.0 0.0 1.0
    CDELT : 1.0 1.0 1.0
    NAXIS : 1  1  1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions