question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cutout2D behaviour for non-rectangular pixel sizes

See original GitHub issue

Description

When specifying a non-rectangular pixel size for the WCS provided to the Cutout2D class, the cutout does not return the expected cutout size and data shape.

Steps to Reproduce

from astropy.nddata.utils import Cutout2D
from astropy.wcs.utils import celestial_frame_to_wcs, proj_plane_pixel_scales
from astropy.coordinates import SkyCoord
import numpy as np

center = SkyCoord("0d", "0d", frame="icrs")
wcs = celestial_frame_to_wcs(center.frame, projection="CAR")

wcs.wcs.crval = (0, 0)
wcs.wcs.cdelt = (-5, 5)
wcs.wcs.crpix = (36.5, 18.5)

data = np.ones((36, 72))
print(data.shape)

size = [90, 180] * u.deg
c2d = Cutout2D(data=data, position=center, size=size, wcs=wcs)
print(c2d.data.shape)

Prints:

(36, 72)
(18, 36)

So it behaves as expected and returns a data array with half the input shape in all axes.

In case of specifying an un-even pixel size:

from astropy.nddata.utils import Cutout2D
from astropy.wcs.utils import celestial_frame_to_wcs, proj_plane_pixel_scales
from astropy.coordinates import SkyCoord
import numpy as np

center = SkyCoord("0d", "0d", frame="icrs")
wcs = celestial_frame_to_wcs(center.frame, projection="CAR")

wcs.wcs.crval = (0, 0)
wcs.wcs.cdelt = (-10, 5)
wcs.wcs.crpix = (18.5, 18.5)

data = np.ones((36, 36))
print(data.shape)

size = [90, 180] * u.deg
c2d = Cutout2D(data=data, position=center, size=size, wcs=wcs)
print(c2d.data.shape)

It prints:

(36, 36)
(9, 36)

While I would have expected (18, 18). Maybe it seems the lon and lat axes are switched in this case? cc @LauraOlivera

System Details

macOS-10.14.6-x86_64-i386-64bit
Python 3.8.6 | packaged by conda-forge | (default, Jan 25 2021, 23:22:12) 
[Clang 11.0.1 ]
Numpy 1.20.0
astropy 4.1
Scipy 1.6.0
Matplotlib 3.3.4

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
adonathcommented, Mar 3, 2021

From a quick look at the code, I think I already found the bug:

  • The size argument of the Cutout2D class is given in (lat, lon) order, as documented in the docstring
  • When converting the size to pixel in the loop here proj_plane_pixel_scales(wcs) returns the pixel size in (lon, lat) order
  • So it seems like the incorrect pixel size is used to convert the size to pixels…lon for lat and the other way around.

If someone confirms the bug, I could possibly implement a fix myself.

0reactions
larrybradleycommented, Apr 30, 2021

“axis of the image” refers to the axis of data, e.g. (x, y) not WCS axis. In general (lat, lon) (in whichever order) aren’t going to be aligned along the (x, y) axes, so I don’t know another way to do this other than to use proj_plane_pixel_scales (the data cutouts are always rectangles in (x, y) space). We should update the documentation if that is confusing. Perhaps even explicitly by stating the cutout pixel size will be:

  • nx_cutout = x_size (ang) / x_pixscale
  • ny_cutout = y_size (ang) / y_pixscale

where the x and y pixel scales are computed using proj_plane_pixel_scales

Would that help?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cutout2D — Astropy v5.2
If size is in angular units, the cutout size is converted to pixels using the pixel scales along each axis of the image...
Read more >
python-astropy-4.0.2-bp153.1.10 - SUSE Package Hub
Changed the behavior when slicing a table (either in rows or with a list of column names) so now the sliced output gets...
Read more >
astropy/CHANGES.rst at main - GitHub
The pixel argument to astropy.visualization.wcsaxes.ticklabels. ... Fixed Cutout2D output WCS NAXIS values to reflect the cutout image size. [#7552] ...
Read more >
Cutout2D not centering on galaxies - Stack Overflow
Cutout2D not centering on galaxies ... y_pix) size = (200, 200) #size I want the image to be in pixels cut_test = Cutout2D(data,...
Read more >
Cutout2D not updated wcs properly (?) - Google Groups
So the pixels in both images seem to have the same coordinates, but the images don't align when ... cutout = Cutout2D(scidata, position,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found