Skip to content

WCS slicing gives TypeError (used to work) #5522

Closed

Description

@pllim

@mcara wrote some WCS manipulation codes for me that used to work but are now broken with the latest dev version. Relevant snippets below.

# Shrink the image
data = zoom(data, zoom_factor)

# Adjust WCS

slice_factor = 1 / zoom_factor
old_wcs = wcs.WCS(hdr)  # To account for distortion, add "pf" as 2nd arg

# Supress RuntimeWarning about ignoring cdelt because cd is present.
with warnings.catch_warnings():
    warnings.simplefilter('ignore', RuntimeWarning)
    # Update if cropping
    new_wcs = old_wcs.slice((np.s_[::slice_factor], np.s_[::slice_factor]))
Traceback (most recent call last):
...
  File ".../stginga/utils.py", line 349, in scale_image
    (np.s_[::slice_factor], np.s_[::slice_factor]))
  File ".../astropy-1.3.dev16480-py3.5-linux-x86_64.egg/astropy/wcs/wcs.py", line 2983, in slice
    nitems = len(builtins.range(self._naxis[wcs_index])[iview])
TypeError: slice indices must be integers or None or have an __index__ method

How do I make it work again?

Activity

  1. MSeifert04 commented on Nov 29, 2016

    @MSeifert04
    Contributor

    How do I make it work again?

    Convince @nden and @astrofrog that #5455 should be merged. 😄

  2. astrofrog commented on Nov 29, 2016

    @astrofrog
    Member

    Seriously though, what is wrong with simply requiring:

    slice_factor = int(1 / zoom_factor)
    

    ?

    @pllim - does that solve your issue?

    More often than not, floating point indices are bugs/unintended. Numpy are even phasing those out and being strict about them. Why should we go in the opposite direction?

  3. added
    Affects-devPRs and issues that do not impact an existing Astropy release
    on Nov 29, 2016
  4. MSeifert04 commented on Nov 29, 2016

    @MSeifert04
    Contributor

    For any zoom_factor >= 1 the int(1 / zoom_factor) will be 0 and thus leave an "empty" wcs.

  5. astrofrog commented on Nov 29, 2016

    @astrofrog
    Member

    @MSeifert04 I agree but in that case, what does slicing with slice e.g. slice(None, None, 0.3) mean?

  6. astrofrog commented on Nov 29, 2016

    @astrofrog
    Member

    (since @pllim wrote # Shrink the image I am assuming zoom_factor is < 1 in her use case)

  7. pllim commented on Nov 29, 2016

    @pllim
    MemberAuthor

    Yes, for my case, using int(1 / zoom_factor) seems to work. I cannot comment on zoom_factor > 1. I just use the code written by someone else as-is. I'm glad the fix for my case is simple. Thanks!

  8. pllim commented on Nov 29, 2016

    @pllim
    MemberAuthor

    Although... it might not be accurate but should not be a problem for my use case.

    >>> 1 / 0.22
    4.545
    >>> int(1 / 0.22)
    4
  9. MSeifert04 commented on Nov 29, 2016

    @MSeifert04
    Contributor

    @astrofrog I think that's another case of WCS without image and WCS with image. If you zoom with interpolation the new points might represent floating point indices of the original array (not sure if scipy.ndimage.zoom qualifies) and the value of that index is calculated by interpolation. If you now slice the WCS with integers you'll truncate the real index and loose accuracy.

    If you zoom without interpolation I totally agree and casting to integers (either round or truncate) is necessary!

  10. astrofrog commented on Nov 29, 2016

    @astrofrog
    Member

    @MSeifert04 - thanks for the clarification - let's discuss this further in #5455. I'm open to the idea of allowing floating point slicing but I think we need to have a separate class for WCS and WCS + NAXIS given the very different behaviors (see latest comment in #5455)

  11. MSeifert04 commented on Nov 29, 2016

    @MSeifert04
    Contributor

    I think #5455 should be merged (not necessarily as-is) just to remain backwards-compatible, so better to discuss this in #5461?

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

    Affects-devPRs and issues that do not impact an existing Astropy releasewcs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions