From 85f121c1c85266ccb089b9919d800ff050799793 Mon Sep 17 00:00:00 2001 From: Derek Homeier Date: Tue, 22 Feb 2022 02:44:31 +0100 Subject: [PATCH] TST: fix spectrum1d_2d reshape, catch WCS warnings --- .../translators/tests/test_spectrum1d.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/glue_astronomy/translators/tests/test_spectrum1d.py b/glue_astronomy/translators/tests/test_spectrum1d.py index 88685b4..e90e3a9 100644 --- a/glue_astronomy/translators/tests/test_spectrum1d.py +++ b/glue_astronomy/translators/tests/test_spectrum1d.py @@ -1,4 +1,5 @@ import pytest +import warnings import numpy as np from numpy.testing import assert_allclose, assert_equal @@ -125,7 +126,7 @@ def test_to_spectrum1d_default_attribute(): @pytest.mark.parametrize('mode', ('wcs1d', 'wcs3d', 'lookup')) -def test_from_spectrum1d(mode): +def test_from_spectrum1d(mode, recwarn): if mode == 'wcs3d': # This test is intended to be run with the version of Spectrum1D based @@ -195,10 +196,16 @@ def test_from_spectrum1d(mode): print(uncertainty) assert_quantity_allclose(spec_new.uncertainty.quantity, np.ones((5, 4, 4))*0.01*u.Jy**2) + + assert len(recwarn) == 3 + for i in range(3): + w = recwarn.pop(UserWarning) + assert "Input WCS indicates that the spectral axis is not last." in str(w.message) else: assert_quantity_allclose(spec_new.flux, [2, 3, 4, 5] * u.Jy) assert spec_new.uncertainty is not None assert_quantity_allclose(spec_new.uncertainty.quantity, [0.1, 0.1, 0.1, 0.1] * u.Jy**2) + assert len(recwarn) == 0 def test_spectrum1d_2d_data(): @@ -207,6 +214,7 @@ def test_spectrum1d_2d_data(): # Note that Spectrum1D will typically have a 1D spectral WCS even if the # data is N-dimensional, so we need to pad the WCS before passing it to # glue and un-pad it when translating back. + # Also Spectrum1D.flux has the spectral axis along last dimension, not first. # We test both the case where the WCS is 2D and the case where it is 1D @@ -215,7 +223,7 @@ def test_spectrum1d_2d_data(): wcs.wcs.cdelt = [10] wcs.wcs.set() - flux = np.ones((3, 2)) * u.Unit('Jy') + flux = np.arange(1, 7).reshape((3, 2)) * u.Unit('Jy') spec = Spectrum1D(flux, wcs=wcs, meta={'instrument': 'spamcam'}) @@ -231,7 +239,7 @@ def test_spectrum1d_2d_data(): assert isinstance(data, Data) assert len(data.main_components) == 1 assert data.main_components[0].label == 'flux' - assert_allclose(data['flux'], flux.value) + assert_allclose(data['flux'], flux.value.swapaxes(-1, 0)) assert data.coords.pixel_n_dim == 2 assert data.coords.world_n_dim == 2 @@ -240,7 +248,7 @@ def test_spectrum1d_2d_data(): assert data.coordinate_components[0].label == 'Pixel Axis 0 [y]' assert data.coordinate_components[1].label == 'Pixel Axis 1 [x]' - assert data.coordinate_components[2].label == 'Offset' + assert data.coordinate_components[2].label == 'World 0' assert data.coordinate_components[3].label == 'Frequency' assert_equal(data['Offset'], [[0, 0], [1, 1], [2, 2]])