Skip to content

Commit

Permalink
Make it so CompositeFrame follows the same ordering
Browse files Browse the repository at this point in the history
This means that _prop is in native order and is sorted into axes order
  • Loading branch information
Cadair committed Oct 3, 2024
1 parent 8264b7e commit beb24d3
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 63 deletions.
52 changes: 14 additions & 38 deletions gwcs/coordinate_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,43 +719,35 @@ class CompositeFrame(CoordinateFrame):
Parameters
----------
frames : list
List of frames (TemporalFrame, CelestialFrame, SpectralFrame, CoordinateFrame).
List of constituient frames.
name : str
Name for this frame.
"""

def __init__(self, frames, name=None):
self._frames = frames[:]
naxes = sum([frame._naxes for frame in self._frames])

axes_type = list(range(naxes))
unit = list(range(naxes))
axes_names = list(range(naxes))
ph_type = list(range(naxes))
axes_order = []
axes_type = []
axes_names = []
unit = []
ph_type = []

for frame in frames:
axes_order.extend(frame.axes_order)

# Stack the raw (not-native) ordered properties
for frame in frames:
unsorted_prop = zip(
frame.axes_order,
frame._prop.axes_type,
frame._prop.unit,
frame._prop.axes_names,
frame._prop.axis_physical_types
)
for ind, axtype, un, n, pht in unsorted_prop:
axes_type[ind] = axtype
axes_names[ind] = n
unit[ind] = un
ph_type[ind] = pht
axes_type += list(frame._prop.axes_type)
axes_names += list(frame._prop.axes_names)
unit += list(frame._prop.unit)
ph_type += list(frame._prop.axis_physical_types)

if len(np.unique(axes_order)) != len(axes_order):
raise ValueError("Incorrect numbering of axes, "
"axes_order should contain unique numbers, "
"got {}.".format(axes_order))
f"got {axes_order}.")

super().__init__(naxes, axes_type=axes_type,
axes_order=axes_order,
Expand All @@ -766,24 +758,11 @@ def __init__(self, frames, name=None):

@property
def frames(self):
"""
The constituient frames that comprise this `CompositeFrame`.
"""
return self._frames

@property
def unit(self):
return self._prop.unit

@property
def axes_names(self):
return self._prop.axes_names

@property
def axes_type(self):
return self._prop.axes_type

@property
def axis_physical_types(self):
return self._prop.axis_physical_types

def __repr__(self):
return repr(self.frames)

Expand Down Expand Up @@ -826,9 +805,6 @@ def _wao_renamed_classes_iter(self):

@property
def world_axis_object_components(self):
"""
We need to generate the components respecting the axes_order.
"""
out = [None] * self.naxes

for frame, components in self._wao_renamed_components_iter:
Expand Down
5 changes: 3 additions & 2 deletions gwcs/tests/test_api_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,8 @@ def test_no_array_shape(gwcs_3d_galactic_spectral):

def test_ellipsis_none_types(gwcs_3d_galactic_spectral):
pht = list(gwcs_3d_galactic_spectral.output_frame._axis_physical_types)
pht[1] = None
# This index is in "axes_order" ordering
pht[2] = None
gwcs_3d_galactic_spectral.output_frame._prop.axis_physical_types = tuple(pht)

wcs = SlicedLowLevelWCS(gwcs_3d_galactic_spectral, Ellipsis)
Expand All @@ -467,7 +468,7 @@ def test_ellipsis_none_types(gwcs_3d_galactic_spectral):
('spectral', 0),
('celestial', 0)]

assert all([callable(l) for l in last_one])
assert all([callable(last) for last in last_one])

assert wcs.world_axis_object_classes['celestial'][0] is SkyCoord
assert wcs.world_axis_object_classes['celestial'][1] == ()
Expand Down
67 changes: 44 additions & 23 deletions gwcs/tests/test_coordinate_systems.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,26 +478,47 @@ def test_ucd1_to_ctype(caplog):


def test_celestial_ordering():
c1 = cf.CelestialFrame(
reference_frame=coord.ICRS(),
axes_order=(0, 1),
axes_names=("lon", "lat"),
unit=(u.deg, u.arcsec),
axis_physical_types=("custom:lon", "custom:lat"),
)
c2 = cf.CelestialFrame(
reference_frame=coord.ICRS(),
axes_order=(1, 0),
axes_names=("lon", "lat"),
unit=(u.deg, u.arcsec),
axis_physical_types=("custom:lon", "custom:lat"),
)

assert c1.axes_names == ("lon", "lat")
assert c2.axes_names == ("lat", "lon")

assert c1.unit == (u.deg, u.arcsec)
assert c2.unit == (u.arcsec, u.deg)

assert c1.axis_physical_types == ("custom:lon", "custom:lat")
assert c2.axis_physical_types == ("custom:lat", "custom:lon")
c1 = cf.CelestialFrame(
reference_frame=coord.ICRS(),
axes_order=(0, 1),
axes_names=("lon", "lat"),
unit=(u.deg, u.arcsec),
axis_physical_types=("custom:lon", "custom:lat"),
)
c2 = cf.CelestialFrame(
reference_frame=coord.ICRS(),
axes_order=(1, 0),
axes_names=("lon", "lat"),
unit=(u.deg, u.arcsec),
axis_physical_types=("custom:lon", "custom:lat"),
)

assert c1.axes_names == ("lon", "lat")
assert c2.axes_names == ("lat", "lon")

assert c1.unit == (u.deg, u.arcsec)
assert c2.unit == (u.arcsec, u.deg)

assert c1.axis_physical_types == ("custom:lon", "custom:lat")
assert c2.axis_physical_types == ("custom:lat", "custom:lon")


def test_composite_ordering():
print("boo")
c1 = cf.CelestialFrame(
reference_frame=coord.ICRS(),
axes_order=(1, 0),
axes_names=("lon", "lat"),
unit=(u.deg, u.arcsec),
axis_physical_types=("custom:lon", "custom:lat"),
)
spec = cf.SpectralFrame(
axes_order=(2,),
axes_names=("spectral",),
unit=u.AA,
)
comp = cf.CompositeFrame([c1, spec])
assert comp.axes_names == ("lat", "lon", "spectral")
assert comp.axis_physical_types == ("custom:lat", "custom:lon", "em.wl")
assert comp.unit == (u.arcsec, u.deg, u.AA)
assert comp.axes_order == (1, 0, 2)

0 comments on commit beb24d3

Please sign in to comment.