From beb24d33acf957d31b1c7f7c953cf5cf684f5f6d Mon Sep 17 00:00:00 2001 From: Stuart Mumford Date: Thu, 3 Oct 2024 14:21:06 +0100 Subject: [PATCH] Make it so CompositeFrame follows the same ordering This means that _prop is in native order and is sorted into axes order --- gwcs/coordinate_frames.py | 52 ++++++--------------- gwcs/tests/test_api_slicing.py | 5 +- gwcs/tests/test_coordinate_systems.py | 67 ++++++++++++++++++--------- 3 files changed, 61 insertions(+), 63 deletions(-) diff --git a/gwcs/coordinate_frames.py b/gwcs/coordinate_frames.py index f63eb8b1..1bfc6992 100644 --- a/gwcs/coordinate_frames.py +++ b/gwcs/coordinate_frames.py @@ -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, @@ -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) @@ -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: diff --git a/gwcs/tests/test_api_slicing.py b/gwcs/tests/test_api_slicing.py index 89197903..3d38a9e9 100644 --- a/gwcs/tests/test_api_slicing.py +++ b/gwcs/tests/test_api_slicing.py @@ -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) @@ -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] == () diff --git a/gwcs/tests/test_coordinate_systems.py b/gwcs/tests/test_coordinate_systems.py index a54091e7..75f2895f 100644 --- a/gwcs/tests/test_coordinate_systems.py +++ b/gwcs/tests/test_coordinate_systems.py @@ -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)