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

Refactor reference file loading #233

Merged
merged 16 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
- Cleaned up the code dealing with downloading data files in calibrat…
…ion_data.

- Marked several of the function arguments used by the previous approach as deprecated (pending) using deprecated_renamed_argument.
- Improved tests to bring calibration_data coverage to 87%.
  • Loading branch information
hpparvi committed Nov 28, 2024
commit db5d5ad8b41c0fb6b2d4c9e51d9316228ce9e6df
25 changes: 11 additions & 14 deletions specreduce/calibration_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from astropy import units as u
from astropy.table import Table, vstack, QTable
from astropy.utils import deprecated_renamed_argument
from astropy.utils.data import get_pkg_data_filename, conf, get_pkg_data_fileobj
from astropy.utils.exceptions import AstropyUserWarning
from astropy.coordinates import SpectralCoord
Expand Down Expand Up @@ -105,6 +106,7 @@
}


@deprecated_renamed_argument('show_progress', None, '1.4.2', pending=True)
def load_pypeit_calibration_lines(
lamps: Sequence | None = None,
wave_air: bool = False,
Expand Down Expand Up @@ -158,19 +160,16 @@
linelists = []
for lamp in lamps:
if lamp in PYPEIT_CALIBRATION_LINELISTS:
data_path = f"arc_lines/lists/{lamp}_lines.dat"
try:
data_path = f"arc_lines/lists/{lamp}_lines.dat"
with get_pkg_data_fileobj(data_path, cache=cache) as data_file:
lines_tab = Table.read(data_file.read(),
format='ascii.fixed_width',
comment='#')
linelists.append(lines_tab)
linelists.append(Table.read(data_file.read(), format='ascii.fixed_width', comment='#'))
except URLError as e:
warnings.warn(f"Downloading of {data_path} failed: {e}", AstropyUserWarning)

Check warning on line 168 in specreduce/calibration_data.py

View check run for this annotation

Codecov / codecov/patch

specreduce/calibration_data.py#L167-L168

Added lines #L167 - L168 were not covered by tests
else:
warnings.warn(
f"{lamp} not in the list of supported calibration "
"line lists: {PYPEIT_CALIBRATION_LINELISTS}."
f"line lists: {PYPEIT_CALIBRATION_LINELISTS}."
)
if len(linelists) == 0:
warnings.warn(f"No calibration lines loaded from {lamps}.")
Expand All @@ -185,6 +184,7 @@
return linelist


@deprecated_renamed_argument('cache', None, '1.4.2', pending=True)
def load_MAST_calspec(
filename: str | Path,
cache: bool = True,
Expand Down Expand Up @@ -220,14 +220,13 @@
if filename.exists() and filename.is_file():
file_path = filename
else:
url = "https://archive.stsci.edu/hlsps/reference-atlases/cdbs/calspec/"
with conf.set_temp("dataurl", url):
with conf.set_temp("dataurl", "https://archive.stsci.edu/hlsps/reference-atlases/cdbs/calspec/"):
try:
file_path = get_pkg_data_filename(str(filename), show_progress=show_progress)
except URLError as e:
msg = f"Downloading of {filename} failed: {e}"
warnings.warn(msg, AstropyUserWarning)
file_path = None

Check warning on line 229 in specreduce/calibration_data.py

View check run for this annotation

Codecov / codecov/patch

specreduce/calibration_data.py#L226-L229

Added lines #L226 - L229 were not covered by tests

if file_path is None:
return None
Expand All @@ -244,6 +243,7 @@
return spectrum


@deprecated_renamed_argument('show_progress', None, '1.4.2', pending=True)
def load_onedstds(
dataset: str = "snfactory",
specfile: str = "EG131.dat",
Expand Down Expand Up @@ -281,13 +281,11 @@
try:
data_path = str(Path("onedstds") / Path(dataset) / Path(specfile))
with get_pkg_data_fileobj(data_path, cache=cache) as data_file:
t = Table.read(data_file.read(),
format="ascii",
names=['wavelength', 'ABmag', 'binsize'])
t = Table.read(data_file.read(), format="ascii", names=['wavelength', 'ABmag', 'binsize'])
except URLError as e:
msg = f"Can't load {specfile} from {dataset}: {e}."
warnings.warn(msg, AstropyUserWarning)
return None

Check warning on line 288 in specreduce/calibration_data.py

View check run for this annotation

Codecov / codecov/patch

specreduce/calibration_data.py#L285-L288

Added lines #L285 - L288 were not covered by tests

# the specreduce_data standard star spectra all provide wavelengths in angstroms
spectral_axis = t['wavelength'].data * u.angstrom
Expand Down Expand Up @@ -335,6 +333,7 @@
transmission : Extinction expressed as fractional transmission

"""
@deprecated_renamed_argument('show_progress', None, '1.4.2', pending=True)
def __init__(
self,
model: str = "kpno",
Expand Down Expand Up @@ -376,9 +375,7 @@
with conf.set_temp("dataurl", SPECREDUCE_DATA_URL):
data_path = str(Path("extinction") / Path(f"{model}extinct.dat"))
with get_pkg_data_fileobj(data_path, cache=cache) as data_file:
t = Table.read(data_file.read(),
format="ascii",
names=['wavelength', 'extinction'])
t = Table.read(data_file.read(), format="ascii", names=['wavelength', 'extinction'])

# the specreduce_data models all provide wavelengths in angstroms
spectral_axis = t['wavelength'].data * u.angstrom
Expand Down
9 changes: 9 additions & 0 deletions specreduce/tests/test_extinction.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ def test_custom_linear_model():
assert len(ext.transmission) > 0


@pytest.mark.remote_data
def test_unsupported_model():
"""
Test loading of a nonexistent model
"""
with pytest.raises(ValueError, match='Requested extinction model,'):
ext = AtmosphericExtinction(model='bad_model')


@pytest.mark.remote_data
def test_missing_extinction_unit():
"""
Expand Down
9 changes: 9 additions & 0 deletions specreduce/tests/test_linelists.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ def test_pypeit_empty():
assert line_tab is None


@pytest.mark.remote_data
def test_pypeit_none():
"""
Test to make sure None is returned if calibration lamp list is None.
"""
line_tab = load_pypeit_calibration_lines(None, cache=True, show_progress=False)
assert line_tab is None


@pytest.mark.remote_data
def test_pypeit_input_validation():
"""
Expand Down
8 changes: 8 additions & 0 deletions specreduce/tests/test_specphot_stds.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from astropy.utils.exceptions import AstropyUserWarning

from specreduce.calibration_data import load_MAST_calspec, load_onedstds

Expand All @@ -15,3 +16,10 @@ def test_load_onedstds():
sp = load_onedstds()
assert sp is not None
assert len(sp.spectral_axis) > 0


@pytest.mark.remote_data
def test_load_onedstds_bad_dataset():
with pytest.warns(AstropyUserWarning, match="Specfied dataset,"):
sp = load_onedstds("snffactory")
assert sp is None
Loading