From 8c6231be349e0a88de46b063fb5aff91ae7d1a77 Mon Sep 17 00:00:00 2001 From: Brett Date: Thu, 7 Nov 2024 12:22:05 -0500 Subject: [PATCH] fix pixel_shape roundtrip bug, test against astropy test data --- asdf_astropy/converters/wcs/tests/test_wcs.py | 44 ++++++++++++++++++- asdf_astropy/converters/wcs/wcs.py | 4 +- pyproject.toml | 1 + 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/asdf_astropy/converters/wcs/tests/test_wcs.py b/asdf_astropy/converters/wcs/tests/test_wcs.py index 728c10b..70206fd 100644 --- a/asdf_astropy/converters/wcs/tests/test_wcs.py +++ b/asdf_astropy/converters/wcs/tests/test_wcs.py @@ -1,9 +1,27 @@ +import warnings + import numpy as np import pytest -from astropy.wcs import WCS, DistortionLookupTable, Sip +from astropy.io import fits +from astropy.utils.data import get_pkg_data_filename, get_pkg_data_filenames +from astropy.wcs import WCS, DistortionLookupTable, FITSFixedWarning, Sip from asdf_astropy.testing.helpers import assert_wcs_roundtrip +_astropy_test_header_filenames = list(get_pkg_data_filenames("tests/data/maps", "astropy.wcs", "*.hdr")) + list( + get_pkg_data_filenames("tests/data/spectra", "astropy.wcs", "*.hdr"), +) + +_astropy_test_fits_filenames = [ + get_pkg_data_filename(f"tests/data/{fn}", "astropy.wcs") + for fn in [ + "ie6d07ujq_wcs.fits", + "j94f05bgq_flt.fits", + "sip.fits", + "sip2.fits", + ] +] + def create_empty_wcs(): return WCS() @@ -76,3 +94,27 @@ def create_tabular_wcs(): def test_roundtrip(wcs_gen, tmp_path, version): wcs = wcs_gen() assert_wcs_roundtrip(wcs, tmp_path, version) + + +@pytest.mark.parametrize("fn", _astropy_test_header_filenames) +@pytest.mark.parametrize("version", ["1.5.0", "1.6.0"]) +def test_astropy_data_header_roundtrip(fn, tmp_path, version): + with open(fn) as f: + header = f.read() + + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=FITSFixedWarning) + wcs = WCS(header) + + assert_wcs_roundtrip(wcs, tmp_path, version) + + +@pytest.mark.parametrize("fn", _astropy_test_fits_filenames) +@pytest.mark.parametrize("version", ["1.5.0", "1.6.0"]) +def test_astropy_data_fits_roundtrip(fn, tmp_path, version): + with fits.open(fn) as ff: + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=FITSFixedWarning) + wcs = WCS(ff[0].header, ff) + + assert_wcs_roundtrip(wcs, tmp_path, version) diff --git a/asdf_astropy/converters/wcs/wcs.py b/asdf_astropy/converters/wcs/wcs.py index 24cf623..3ed9f7a 100644 --- a/asdf_astropy/converters/wcs/wcs.py +++ b/asdf_astropy/converters/wcs/wcs.py @@ -2,7 +2,7 @@ # These attributes don't end up in the hdulist and # instead will be stored in "attrs" -_WCS_ATTRS = ("naxis", "colsel", "keysel", "key", "pixel_shape", "pixel_bounds") +_WCS_ATTRS = ("naxis", "colsel", "keysel", "key", "pixel_bounds") class WCSConverter(Converter): @@ -18,7 +18,6 @@ def from_yaml_tree(self, node, tag, ctx): if naxis := attrs.pop("naxis"): hdulist[0].header["naxis"] = naxis - pixel_shape = attrs.pop("pixel_shape") pixel_bounds = attrs.pop("pixel_bounds") wcs = WCS(hdulist[0].header, fobj=hdulist, **attrs) @@ -29,7 +28,6 @@ def from_yaml_tree(self, node, tag, ctx): wcs.sip = wcs._read_sip_kw(hdulist[0].header, attrs.get("key", " ")) wcs.wcs.set() - wcs.pixel_shape = pixel_shape wcs.pixel_bounds = pixel_bounds return wcs diff --git a/pyproject.toml b/pyproject.toml index 5cfadc1..d817beb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -97,6 +97,7 @@ extend-ignore = [ "SLF001", # private-member-access "FBT001", # boolean positional arguments in function definition "RUF012", # mutable class attributes should be annotated with typing.ClassVar + "PTH123", # replace open with Path.open ] extend-exclude = ["docs/*"]