Skip to content

Commit

Permalink
test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bmorris3 committed Jul 29, 2023
1 parent 69125fb commit 604bcce
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 141 deletions.
6 changes: 3 additions & 3 deletions aesop/spectra.py
Expand Up @@ -316,7 +316,7 @@ def __init__(self, spectrum_list, header=None, name=None, fits_path=None,
self.time = time

@classmethod
def from_fits(cls, path):
def from_fits(cls, path, format=None):
"""
Load an echelle spectrum from a FITS file.
Expand All @@ -326,7 +326,7 @@ def from_fits(cls, path):
Path to the FITS file
"""
spectrum_list = [Spectrum1D.from_specutils(s)
for s in SpectrumCollection.read(path)]
for s in SpectrumCollection.read(path, format=format)]
header = fits.getheader(path)

name = header.get('OBJNAME', None)
Expand Down Expand Up @@ -928,7 +928,7 @@ def _poly_model(p, x):
Polynomial model for lstsq continuum normalization
"""
x_mean = x.mean()
return np.polyval(p, (x - x_mean).value)
return np.polyval(p, x - x_mean)


def _residuals(p, x, y):
Expand Down
31 changes: 0 additions & 31 deletions aesop/tests/coveragerc

This file was deleted.

11 changes: 5 additions & 6 deletions aesop/tests/test_spectrum1d.py
Expand Up @@ -4,7 +4,6 @@
import astropy.units as u
import pytest
from astropy.units import UnitsError
from astropy.tests.helper import remote_data
from astropy.utils.data import download_file

from ..spectra import Spectrum1D, EchelleSpectrum
Expand All @@ -25,20 +24,20 @@ def test_constructor():
flux=f)


@remote_data
@pytest.mark.remote_data
def test_read_fits():
url = ('https://drive.google.com/uc?export=download&id='
'1vf8rNhAiUJ1NO9VkwZOhcK6avJICfETH')
url = ('https://stsci.box.com/shared/static/'
'mu4fa1fmq1lw8boem12e2umyi99skbdl.fits')

path = download_file(url, show_progress=False)

echelle_spectrum = EchelleSpectrum.from_fits(path)
echelle_spectrum = EchelleSpectrum.from_fits(path, format='iraf')

assert hasattr(echelle_spectrum, 'header')
assert hasattr(echelle_spectrum, 'time')
assert hasattr(echelle_spectrum, 'name')
assert str(echelle_spectrum) == ('<EchelleSpectrum: 107 orders, '
'3506.8-10612.5 Angstrom>')
'3506.8-10612.4 Angstrom>')

# There should be more flux in the 40th order (redder) than 0th (bluer)
assert echelle_spectrum[40].flux.mean() > echelle_spectrum[0].flux.mean()
Expand Down
110 changes: 9 additions & 101 deletions docs/getting_started.rst
Expand Up @@ -31,8 +31,8 @@ spectroscopic standard O star BD+28 4211. We also create an
>>> from astropy.utils.data import download_file
>>> target_url = 'https://drive.google.com/uc?export=download&id=1lkZi4p6ZGeeyDf_Z0eexMy0GHnr92_dv'
>>> spectroscopic_standard_url = 'https://drive.google.com/uc?export=download&id=1YiY1DP5Ec4mumvzjWj4u5DmFiEyn9MgH'
>>> target_url = 'https://stsci.box.com/shared/static/mu4fa1fmq1lw8boem12e2umyi99skbdl.fits'
>>> spectroscopic_standard_url = 'https://stsci.box.com/shared/static/18fa008byy2500yrfhuooyxs5d6pwc6e.fits'
>>> target_path = download_file(target_url, show_progress=False)
>>> standard_path = download_file(spectroscopic_standard_url, show_progress=False)
Expand Down Expand Up @@ -65,8 +65,8 @@ make a quick plot of the 73rd order of the target's echelle spectrum:

from astropy.utils.data import download_file

target_url = 'https://drive.google.com/uc?export=download&id=1lkZi4p6ZGeeyDf_Z0eexMy0GHnr92_dv'
spectroscopic_standard_url = 'https://drive.google.com/uc?export=download&id=1YiY1DP5Ec4mumvzjWj4u5DmFiEyn9MgH'
target_url = 'https://stsci.box.com/shared/static/mu4fa1fmq1lw8boem12e2umyi99skbdl.fits'
spectroscopic_standard_url = 'https://stsci.box.com/shared/static/18fa008byy2500yrfhuooyxs5d6pwc6e.fits'

target_path = download_file(target_url)
standard_path = download_file(spectroscopic_standard_url)
Expand Down Expand Up @@ -148,8 +148,8 @@ continuum normalization method,

from astropy.utils.data import download_file

target_url = 'https://drive.google.com/uc?export=download&id=1lkZi4p6ZGeeyDf_Z0eexMy0GHnr92_dv'
spectroscopic_standard_url = 'https://drive.google.com/uc?export=download&id=1YiY1DP5Ec4mumvzjWj4u5DmFiEyn9MgH'
target_url = 'https://stsci.box.com/shared/static/mu4fa1fmq1lw8boem12e2umyi99skbdl.fits'
spectroscopic_standard_url = 'https://stsci.box.com/shared/static/18fa008byy2500yrfhuooyxs5d6pwc6e.fits'

target_path = download_file(target_url)
standard_path = download_file(spectroscopic_standard_url)
Expand Down Expand Up @@ -181,7 +181,7 @@ object:
>>> spec1d = target_spectrum.to_Spectrum1D()
>>> print(spec1d)
<Spectrum1D: 3561.9-10391.0 Angstrom>
<Spectrum1D: 3562.4-10380.9 Angstrom>
>>> spec1d.plot() # doctest: +SKIP
Expand All @@ -194,8 +194,8 @@ telluric absorption. Here's what it looks like:
import matplotlib.pyplot as plt
from astropy.utils.data import download_file

target_url = 'https://drive.google.com/uc?export=download&id=1lkZi4p6ZGeeyDf_Z0eexMy0GHnr92_dv'
spectroscopic_standard_url = 'https://drive.google.com/uc?export=download&id=1YiY1DP5Ec4mumvzjWj4u5DmFiEyn9MgH'
target_url = 'https://stsci.box.com/shared/static/mu4fa1fmq1lw8boem12e2umyi99skbdl.fits'
spectroscopic_standard_url = 'https://stsci.box.com/shared/static/18fa008byy2500yrfhuooyxs5d6pwc6e.fits'

target_path = download_file(target_url)
standard_path = download_file(spectroscopic_standard_url)
Expand All @@ -217,95 +217,3 @@ telluric absorption. Here's what it looks like:
plt.xlabel('Wavelength [Angstrom]')
plt.ylabel('Flux')
plt.show()

.. _getting_started-alltogether:

Putting it all together
-----------------------

Now let's do all of this analysis on a very different star -- this time a
Wolf-Rayet star:

.. code-block:: python
>>> # Download example spectra
>>> from astropy.utils.data import download_file
>>> target_url = 'http://staff.washington.edu/tzdw/wr_echelle/wr124.0001.wfrmcpc.fits'
>>> spectroscopic_standard_url = 'http://staff.washington.edu/tzdw/wr_echelle/HIP107864.0003.wfrmcpc.fits'
>>> target_path = download_file(target_url, show_progress=False) # doctest: +REMOTE_DATA
>>> standard_path = download_file(spectroscopic_standard_url, show_progress=False) # doctest: +REMOTE_DATA
>>> # Open those example spectra with aesop
>>> from aesop import EchelleSpectrum
>>> target_spectrum = EchelleSpectrum.from_fits(target_path)
>>> standard_spectrum = EchelleSpectrum.from_fits(standard_path)
>>> # Continuum normalize the spectra against a standard, and with lstsq
>>> target_spectrum.continuum_normalize_from_standard(standard_spectrum,
... polynomial_order=8)
>>> target_spectrum.continuum_normalize_lstsq(polynomial_order=2)
>>> # Plot the concatenated 1D spectrum
>>> spec1d = target_spectrum.to_Spectrum1D()
>>> spec1d.plot() # doctest: +SKIP
.. plot::

from astropy.utils.data import download_file

target_url = 'http://staff.washington.edu/tzdw/wr_echelle/wr124.0001.wfrmcpc.fits'
spectroscopic_standard_url = 'http://staff.washington.edu/tzdw/wr_echelle/HIP107864.0003.wfrmcpc.fits'

target_path = download_file(target_url, show_progress=False) # doctest: +REMOTE_DATA
standard_path = download_file(spectroscopic_standard_url, show_progress=False) # doctest: +REMOTE_DATA

from aesop import EchelleSpectrum

target_spectrum = EchelleSpectrum.from_fits(target_path)
standard_spectrum = EchelleSpectrum.from_fits(standard_path)

target_spectrum.continuum_normalize_from_standard(standard_spectrum,
polynomial_order=8)
target_spectrum.continuum_normalize_lstsq(polynomial_order=2)

spec1d = target_spectrum.to_Spectrum1D()
spec1d.plot()
plt.ylim([0, 3])
plt.xlim([3500, 10000])
plt.xlabel('Wavelength [Angstrom]')
plt.ylabel('Flux')
plt.show()

But note that these spectra have lots of emission, which our continuum
normalization preserves:

.. plot::

from astropy.utils.data import download_file

target_url = 'http://staff.washington.edu/tzdw/wr_echelle/wr124.0001.wfrmcpc.fits'
spectroscopic_standard_url = 'http://staff.washington.edu/tzdw/wr_echelle/HIP107864.0003.wfrmcpc.fits'

target_path = download_file(target_url, show_progress=False) # doctest: +REMOTE_DATA
standard_path = download_file(spectroscopic_standard_url, show_progress=False) # doctest: +REMOTE_DATA

from aesop import EchelleSpectrum

target_spectrum = EchelleSpectrum.from_fits(target_path)
standard_spectrum = EchelleSpectrum.from_fits(standard_path)

target_spectrum.continuum_normalize_from_standard(standard_spectrum,
polynomial_order=8)
target_spectrum.continuum_normalize_lstsq(polynomial_order=2)

spec1d = target_spectrum.to_Spectrum1D()
spec1d.plot()
import matplotlib.pyplot as plt
plt.ylim([0, 6])
plt.xlim([6520, 6620])
plt.xlabel('Wavelength [Angstrom]')
plt.ylabel('Flux')
plt.show()
77 changes: 77 additions & 0 deletions tox.ini
@@ -0,0 +1,77 @@
[tox]
env_list =
check-style
test{-oldestdeps,-pyargs}
test-xdist
build-{dist,docs}
isolated_build = true
# This is included for testing of the template. You can remove it safely.
skip_missing_interpreters = True

[testenv:check-style]
description = check code style with ruff
change_dir = .
skip_install = true
deps =
ruff
commands =
ruff .

[testenv]
# tox environments are constructed with so-called 'factors' (or terms)
# separated by hyphens, e.g. test-devdeps-cov. Lines below starting with factor:
# will only take effect if that factor is included in the environment name. To
# see a list of example environments that can be run, along with a description,
# run:
#
# tox -l -v
#
description =
run tests
oldestdeps: with the oldest supported version of key dependencies
xdist: in parallel

use_develop = true
pass_env =
HOME
CI
TOXENV

# Suppress display of matplotlib plots generated during docs build
set_env =
MPLBACKEND=agg

deps =
oldestdeps: minimum_dependencies
xdist: pytest-xdist

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

commands_pre =
oldestdeps: minimum_dependencies aesop --filename requirements-min.txt
oldestdeps: pip install -r requirements-min.txt
pip freeze

commands =
pip freeze
test: pytest --pyargs aesop {toxinidir}/docs {posargs}

[testenv:check-build]
description = check PEP517 package build
change_dir = .
skip_install = true
deps =
build
twine
commands =
python -m build --sdist .
twine check dist/*

[testenv:build-docs]
description = invoke sphinx-build to build the HTML docs
change_dir = docs
extras = docs
commands =
sphinx-build -W -b html . _build/html

0 comments on commit 604bcce

Please sign in to comment.