Skip to content

Commit

Permalink
Expand test and fix implementation of pixel2world_single_axis
Browse files Browse the repository at this point in the history
  • Loading branch information
astrofrog committed Jan 12, 2023
1 parent 393d1bd commit f31e88d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
14 changes: 12 additions & 2 deletions glue/core/coordinate_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,19 @@ def pixel2world_single_axis(wcs, *pixel, world_axis=None):
pixel_new.append(p.flat[0])
pixel = np.broadcast_arrays(*pixel_new)

result = wcs.pixel_to_world_values(*pixel)
# In the case of 1D WCS, there is an astropy issue which prevents us from
# passing arbitrary shapes - see https://github.com/astropy/astropy/issues/12154
# Therefore, we ravel the values and reshape afterwards

if len(pixel) == 1 and pixel[0].ndim > 1:
pixel_shape = pixel[0].shape
pixel = [pixel[0].ravel()]
result = wcs.pixel_to_world_values(*pixel)
result = result.reshape(pixel_shape)
else:
result = wcs.pixel_to_world_values(*pixel)

if len(pixel) > 1 or np.ndim(result) > 1:
if len(pixel) > 1:
result = result[world_axis]

return np.broadcast_to(result, original_shape)
Expand Down
18 changes: 18 additions & 0 deletions glue/core/tests/test_coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,24 @@ def test_pixel2world_single_axis():
assert_allclose(pixel2world_single_axis(coord, x, y, z, world_axis=2), [1.5, 1.5, 1.5])


def test_pixel2world_single_axis_1d():

# Regression test for issues that occurred for 1D WCSes

coord = WCSCoordinates(naxis=1)
coord.wcs.ctype = ['FREQ']
coord.wcs.crpix = [1]
coord.wcs.crval = [1]
coord.wcs.cdelt = [1]

x = np.array([0.2, 0.4, 0.6])
expected = np.array([1.2, 1.4, 1.6])

assert_allclose(pixel2world_single_axis(coord, x, world_axis=0), expected)
assert_allclose(pixel2world_single_axis(coord, x.reshape((1, 3)), world_axis=0), expected.reshape((1, 3)))
assert_allclose(pixel2world_single_axis(coord, x.reshape((3, 1)), world_axis=0), expected.reshape((3, 1)))


def test_affine():

matrix = np.array([[2, 3, -1], [1, 2, 2], [0, 0, 1]])
Expand Down

0 comments on commit f31e88d

Please sign in to comment.