Skip to content

Commit

Permalink
fix pixel_shape roundtrip bug, test against astropy test data
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Nov 13, 2024
1 parent 93b946e commit 8c6231b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
44 changes: 43 additions & 1 deletion asdf_astropy/converters/wcs/tests/test_wcs.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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)
4 changes: 1 addition & 3 deletions asdf_astropy/converters/wcs/wcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -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

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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/*"]

Expand Down

0 comments on commit 8c6231b

Please sign in to comment.