Skip to content

Commit

Permalink
Fix some of the coordinate classes for the case of single coordinates…
Browse files Browse the repository at this point in the history
… to be APE 14-compliant
  • Loading branch information
astrofrog committed Jan 13, 2023
1 parent 9752d8c commit 0f62f17
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
19 changes: 15 additions & 4 deletions glue/core/coordinate_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ def pixel2world_single_axis(wcs, *pixel, world_axis=None):

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 = wcs.pixel_to_world_values(pixel[0].ravel())
result = result.reshape(pixel_shape)
else:
result = wcs.pixel_to_world_values(*pixel)
Expand Down Expand Up @@ -112,9 +111,21 @@ def world2pixel_single_axis(wcs, *world, pixel_axis=None):
world_new.append(w.flat[0])
world = np.broadcast_arrays(*world_new)

result = wcs.world_to_pixel_values(*world)
# 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(world) == 1 and world[0].ndim > 1:
world_shape = world[0].shape
result = wcs.world_to_pixel_values(world[0].ravel())
result = result.reshape(world_shape)
else:
result = wcs.world_to_pixel_values(*world)

if len(world) > 1:
result = result[pixel_axis]

return np.broadcast_to(result[pixel_axis], original_shape)
return np.broadcast_to(result, original_shape)


def world_axis(wcs, data, *, pixel_axis=None, world_axis=None):
Expand Down
20 changes: 16 additions & 4 deletions glue/core/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,16 @@ def __init__(self):
super().__init__(pixel_n_dim=10, world_n_dim=10)

def pixel_to_world_values(self, *pixel):
return pixel
if self.pixel_n_dim == 1:
return pixel[0]
else:
return pixel

def world_to_pixel_values(self, *world):
return world
if self.world_n_dim == 1:
return world[0]
else:
return world


class IdentityCoordinates(Coordinates):
Expand All @@ -102,10 +108,16 @@ def __init__(self, n_dim=None):
super().__init__(pixel_n_dim=n_dim, world_n_dim=n_dim)

def pixel_to_world_values(self, *pixel):
return pixel
if self.pixel_n_dim == 1:
return pixel[0]
else:
return pixel

def world_to_pixel_values(self, *world):
return world
if self.world_n_dim == 1:
return world[0]
else:
return world

@property
def axis_correlation_matrix(self):
Expand Down
10 changes: 8 additions & 2 deletions glue/core/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,10 +818,16 @@ def world_axis_names(self):
return ['Custom {0}'.format(axis) for axis in range(3)]

def world_to_pixel_values(self, *world):
return tuple([0.4 * w for w in world])
if self.pixel_n_dim == 1:
return 0.4 * world[0]
else:
return tuple([0.4 * w for w in world])

def pixel_to_world_values(self, *pixel):
return tuple([2.5 * p for p in pixel])
if self.world_n_dim == 1:
return 2.5 * pixel[0]
else:
return tuple([2.5 * p for p in pixel])

data1.coords = CustomCoordinates()

Expand Down
1 change: 1 addition & 0 deletions glue/core/tests/test_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def test_1d_world_link():
dc.add_link(LinkSame(d2.world_component_ids[0], d1.id['x']))

assert d2.world_component_ids[0] in d1.externally_derivable_components

np.testing.assert_array_equal(d1[d2.world_component_ids[0]], x)
np.testing.assert_array_equal(d1[d2.pixel_component_ids[0]], x)

Expand Down

0 comments on commit 0f62f17

Please sign in to comment.