Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace deprecated AsdfInFits #1032

Merged
merged 3 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ test =
graphviz
coverage
spectral-cube
stdatamodels>=1.1.0
docs =
# TODO: Remove numpy pinning when astropy v5.2.1 is released.
numpy<1.24
sphinx-astropy
matplotlib
graphviz
# TODO: Remove pinning when ndcube fixed
# See https://github.com/astropy/specutils/issues/1028
ndcube<2.1
Copy link
Contributor

@rosteen rosteen Mar 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NDCube 2.1.1 was released with the fix for this, we should be able to remove this pin now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, there is still sunpy/ndcube#604

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, blast. Alright, it stays for now 😆


[options.package_data]
specutils = data/*
Expand Down
59 changes: 30 additions & 29 deletions specutils/io/default_loaders/tests/test_jwst_reader.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
import warnings
import gwcs.coordinate_frames as cf
import numpy as np
import pytest
from astropy import coordinates as coord
from astropy import units as u
from astropy.io import fits
from astropy.table import Table
import astropy.units as u
from astropy.io.registry import IORegistryError
from astropy.utils.exceptions import AstropyUserWarning
from astropy.modeling import models
from astropy import coordinates as coord
import gwcs.coordinate_frames as cf
from astropy.table import Table
from astropy.utils.exceptions import AstropyUserWarning
from gwcs.wcs import WCS
import pytest

from asdf.exceptions import AsdfDeprecationWarning
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
category=AsdfDeprecationWarning,
message=r"AsdfInFits has been deprecated.*",
)
from asdf import fits_embed

from specutils import Spectrum1D, SpectrumList

# ASDF_LT_2_14_1
try:
from stdatamodels import asdf_in_fits
except ImportError:
HAS_STDATAMODELS = False
else:
HAS_STDATAMODELS = True


# The c1d/x1d reader tests --------------------------

Expand Down Expand Up @@ -398,33 +396,36 @@ def create_image_hdu(name='SCI', data=None, shape=None, hdrs=[], ndim=3):


@pytest.fixture(scope='function')
def cube(tmpdir, tmp_asdf):
def cube(tmp_path, tmp_asdf):
""" Mock a JWST s3d cube """
hdulist = fits.HDUList()
hdulist.append(fits.PrimaryHDU())
hdulist["PRIMARY"].header["TELESCOP"] = ("JWST", "comment")
hdulist["PRIMARY"].header["FLUXEXT"] = ("ERR", "comment")
hdulist["PRIMARY"].header["ERREXT"] = ("ERR", "comment")
hdulist["PRIMARY"].header["MASKEXT"] = ("DQ", "comment")
prihdu = fits.PrimaryHDU()
prihdu.header["TELESCOP"] = ("JWST", "comment")
prihdu.header["FLUXEXT"] = ("ERR", "comment")
prihdu.header["ERREXT"] = ("ERR", "comment")
prihdu.header["MASKEXT"] = ("DQ", "comment")
hdulist = fits.HDUList([prihdu])

# Add ImageHDU for cubes
shape = [30, 10, 10]
shape = (30, 10, 10)
hdulist.append(create_image_hdu(name='SCI', shape=shape, hdrs=[("BUNIT", 'MJy')]))
hdulist.append(create_image_hdu(name='ERR', shape=shape,
hdrs=[("BUNIT", 'MJy'), ('ERRTYPE', 'ERR')]))
hdulist.append(create_image_hdu(name='DQ', shape=shape))

# Mock the ASDF extension
hdulist.append(fits.BinTableHDU(name='ASDF'))
ff = fits_embed.AsdfInFits(hdulist, tmp_asdf)
tmpfile = str(tmpdir.join('jwst_embedded_asdf.fits'))
ff.write_to(tmpfile)

if HAS_STDATAMODELS:
tmpfile = str(tmp_path / 'jwst_embedded_asdf.fits')
asdf_in_fits.write(tmpfile, tmp_asdf, hdulist=hdulist, overwrite=True)

return hdulist


def test_jwst_s3d_single(tmpdir, cube):
@pytest.mark.skipif(not HAS_STDATAMODELS, reason="requires stdatamodels")
def test_jwst_s3d_single(tmp_path, cube):
"""Test Spectrum1D.read for JWST x1d data"""
tmpfile = str(tmpdir.join('jwst_s3d.fits'))
tmpfile = str(tmp_path / 'jwst_s3d.fits')
cube.writeto(tmpfile)

data = Spectrum1D.read(tmpfile, format='JWST s3d')
Expand Down
10 changes: 5 additions & 5 deletions specutils/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def test_generic_spectrum_from_table(recwarn):
assert spectrum.spectral_axis.unit == table['wave'].unit
assert spectrum.flux.unit == table['flux'].unit
assert spectrum.spectral_axis.unit == table['wave'].unit
assert np.alltrue(spectrum.spectral_axis == table['wave'])
assert np.alltrue(spectrum.flux == table['flux'])
assert np.all(spectrum.spectral_axis == table['wave'])
assert np.all(spectrum.flux == table['flux'])

# Add uncertainties and retest
err = 0.01*flux
Expand All @@ -43,9 +43,9 @@ def test_generic_spectrum_from_table(recwarn):
assert spectrum.flux.unit == table['flux'].unit
assert spectrum.uncertainty.unit == table['err'].unit
assert spectrum.spectral_axis.unit == table['wave'].unit
assert np.alltrue(spectrum.spectral_axis == table['wave'])
assert np.alltrue(spectrum.flux == table['flux'])
assert np.alltrue(spectrum.uncertainty.array == table['err'])
assert np.all(spectrum.spectral_axis == table['wave'])
assert np.all(spectrum.flux == table['flux'])
assert np.all(spectrum.uncertainty.array == table['err'])

# Test for warning if standard deviation is zero or negative
err[0] = 0.
Expand Down
6 changes: 3 additions & 3 deletions specutils/tests/test_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ def test_generic_ecsv_reader(tmpdir):
assert spectrum.flux.unit == table['flux'].unit
assert spectrum.uncertainty.unit == table['uncertainty'].unit
assert spectrum.spectral_axis.unit == table['wave'].unit
assert np.alltrue(spectrum.spectral_axis == table['wave'])
assert np.alltrue(spectrum.flux == table['flux'])
assert np.alltrue(spectrum.uncertainty.array == table['uncertainty'])
assert np.all(spectrum.spectral_axis == table['wave'])
assert np.all(spectrum.flux == table['flux'])
assert np.all(spectrum.uncertainty.array == table['uncertainty'])


@remote_access([{'id': '1481119', 'filename': 'COS_FUV.fits'},
Expand Down
5 changes: 0 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ deps =
devdeps: scipy>=0.0.dev0
devdeps: git+https://github.com/spacetelescope/gwcs.git#egg=gwcs

# While nice to test in principle, we cannot enable this in CI
# because this will generate AsdfWarning and AstropyDeprecationWarning
# so this setting is not used.
external: jwst

# The following indicates which extras_require from setup.cfg will be installed
extras =
test
Expand Down