Skip to content

Commit

Permalink
Merge pull request #147 from LSSTDESC/v3.0.3
Browse files Browse the repository at this point in the history
v3.0.3: small corrections
  • Loading branch information
jeremyneveu authored Dec 20, 2023
2 parents 11430a0 + 9527b4c commit 3d0558e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 15 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ jobs:
pytest tests/test_simulator.py --log-cli-level=INFO --cov --cov-append --cov-report lcov
pytest tests/test_utils.py --log-cli-level=INFO --cov --cov-append --cov-report lcov
pytest tests/test_fitter.py --log-cli-level=INFO --cov --cov-append --cov-report lcov
pytest tests/test_multispectra.py --log-cli-level=INFO --cov --cov-append --cov-report lcov
- name: Run doctests and coverage
shell: bash -l {0}
Expand Down
2 changes: 1 addition & 1 deletion config/ctio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SPECTRACTOR_DECONVOLUTION_PSF2D = True
# deconvolve spectrogram with full forward model: False, True
SPECTRACTOR_DECONVOLUTION_FFM = True
# library to compute atmospheric transmission: none, libradtran, getobsatmo
SPECTRACTOR_ATMOSPHERE_SIM = getobsatmo
SPECTRACTOR_ATMOSPHERE_SIM = none

[instrument]
# instrument name
Expand Down
10 changes: 9 additions & 1 deletion spectractor/fit/fit_multispectra.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def _build_sim_sample(spectra, aerosols=0.05, ozone=300, pwv=5, angstrom_exponen
Examples
--------
>>> parameters.SPECTRACTOR_ATMOSPHERE_SIM = "getobsatmo"
>>> spectra = _build_sim_sample([Spectrum("./tests/data/reduc_20170530_134_spectrum.fits")])
>>> len(spectra)
1
Expand Down Expand Up @@ -76,6 +77,7 @@ def _build_test_sample(targets=["HD111980"]*3, zs=np.linspace(1, 2, 3), aerosols
Examples
--------
>>> parameters.SPECTRACTOR_ATMOSPHERE_SIM = "getobsatmo"
>>> _build_test_sample(targets=["HD111980"]*3, zs=[1, 1.5, 2]) #doctest: +ELLIPSIS
CTIO site name validated...
"""
Expand Down Expand Up @@ -135,6 +137,7 @@ def __init__(self, output_file_name, spectra, fixed_A1s=True, fixed_deltas=True,
Examples
--------
>>> parameters.SPECTRACTOR_ATMOSPHERE_SIM = "getobsatmo"
>>> spectra = _build_test_sample(targets=["HD111980"]*3, zs=np.linspace(1, 2, 3))
>>> w = MultiSpectraFitWorkspace("./outputs/test", spectra, bin_width=5, verbose=True)
>>> w.output_file_name
Expand Down Expand Up @@ -173,7 +176,7 @@ def __init__(self, output_file_name, spectra, fixed_A1s=True, fixed_deltas=True,
for ip in range(self.deltas_first_index, self.nspectra + self.deltas_first_index):
fixed[ip] = True
labels = ["VAOD", "angstrom_exp", "ozone [db]", "PWV [mm]", "reso"] + [f"delta_{k}" for k in range(self.nspectra)] + [f"A1_{k}" for k in range(self.nspectra)]
axis_names = ["VAOD", r'$\"a$', "ozone [db]", "PWV [mm]", "reso"] + [f"$\delta_{k}$" for k in range(self.nspectra)] + ["$A_1^{(" + str(k) + ")}$" for k in range(self.nspectra)]
axis_names = ["VAOD", r'$\"a$', "ozone [db]", "PWV [mm]", "reso"] + [rf"$\delta_{k}$" for k in range(self.nspectra)] + ["$A_1^{(" + str(k) + ")}$" for k in range(self.nspectra)]
bounds = [(0, 1), (0, 3), (100, 700), (0, 20), (0.1, 100)] + [(-20, 20)] * self.nspectra + [(1e-3, 2)] * self.nspectra
if fixed[4]: # reso
bounds[4] = (-1, 0)
Expand Down Expand Up @@ -488,6 +491,7 @@ def simulate(self, aerosols, angstrom_exponent, ozone, pwv, reso, *A1s):
Examples
--------
>>> parameters.SPECTRACTOR_ATMOSPHERE_SIM = "getobsatmo"
>>> spectra = spectra = _build_test_sample(targets=["HD111980"]*3, zs=np.linspace(1, 2, 3))
>>> w = MultiSpectraFitWorkspace("./outputs/test", spectra, bin_width=5, verbose=True)
>>> lambdas, model, model_err = w.simulate(*w.params.values)
Expand Down Expand Up @@ -589,6 +593,7 @@ def plot_fit(self): # pragma: no cover
Examples
--------
>>> parameters.SPECTRACTOR_ATMOSPHERE_SIM = "getobsatmo"
>>> spectra = _build_test_sample(targets=["HD111980"]*3, zs=np.linspace(1, 2, 3))
>>> w = MultiSpectraFitWorkspace("./outputs/test", spectra, bin_width=5, verbose=True)
>>> w.simulate(*w.params.values) #doctest: +ELLIPSIS
Expand Down Expand Up @@ -668,6 +673,7 @@ def plot_transmissions(self): # pragma: no cover
Examples
--------
>>> parameters.SPECTRACTOR_ATMOSPHERE_SIM = "getobsatmo"
>>> spectra = _build_test_sample(targets=["HD111980"]*3, zs=np.linspace(1, 2, 3))
>>> w = MultiSpectraFitWorkspace("./outputs/test", spectra, bin_width=5, verbose=True)
>>> _ = w.simulate(*w.params.values)
Expand Down Expand Up @@ -743,6 +749,7 @@ def plot_A1s(self): # pragma: no cover
"""
Examples
--------
>>> parameters.SPECTRACTOR_ATMOSPHERE_SIM = "getobsatmo"
>>> spectra = _build_test_sample(targets=["HD111980"]*3, zs=np.linspace(1, 2, 3))
>>> w = MultiSpectraFitWorkspace("./outputs/test", spectra, bin_width=5, verbose=True)
>>> w.cov = np.eye(3 + w.nspectra - 1)
Expand Down Expand Up @@ -864,6 +871,7 @@ def run_multispectra_minimisation(fit_workspace, method="newton", verbose=False,
Examples
--------
>>> parameters.SPECTRACTOR_ATMOSPHERE_SIM = "getobsatmo"
>>> spectra = _build_test_sample(targets=["HD111980"]*10, zs=np.linspace(1, 2, 10),
... aerosols=0.05, angstrom_exponent=1.5, ozone=300, pwv=3)
>>> parameters.VERBOSE = True
Expand Down
6 changes: 3 additions & 3 deletions spectractor/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2365,14 +2365,14 @@ def set_sources_file_name(file_name, output_directory=""):
Examples
--------
>>> set_sources_file_name("image.fits", output_directory="")
'image_wcs/image.xyls'
'image_wcs/image.axy'
>>> set_sources_file_name("image.png", output_directory="outputs")
'outputs/image_wcs/image.xyls'
'outputs/image_wcs/image.axy'
"""
output_directory = set_wcs_output_directory(file_name, output_directory=output_directory)
tag = set_wcs_tag(file_name)
return os.path.join(output_directory, f"{tag}.xyls")
return os.path.join(output_directory, f"{tag}.axy")


def set_gaia_catalog_file_name(file_name, output_directory=""):
Expand Down
3 changes: 3 additions & 0 deletions tests/test_fullchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def make_image():
def test_ctio_fullchain():
parameters.VERBOSE = True
parameters.DEBUG = False
parameters.SPECTRACTOR_ATMOSPHERE_SIM = "libradtran"
sim_image = "./tests/data/sim_20170530_134.fits"

# load test and make image simulation
Expand All @@ -120,6 +121,7 @@ def test_ctio_fullchain():
logbook = LogBook(logbook="./tests/data/ctiofulllogbook_jun2017_v5.csv")
disperser_label, target, xpos, ypos = logbook.search_for_image(tag)
load_config("./config/ctio.ini")
parameters.SPECTRACTOR_ATMOSPHERE_SIM = "libradtran"
parameters.PSF_POLY_ORDER = PSF_POLY_ORDER
parameters.CCD_REBIN = 1
# JN: > 1 not working well for now: I guess CTIO spectra are too narrow
Expand Down Expand Up @@ -191,6 +193,7 @@ def test_ctio_fullchain():
assert np.isclose(np.abs(w.params.values[9]), 0, atol=1e-3) # B

parameters.DEBUG = False
parameters.SPECTRACTOR_ATMOSPHERE_SIM = "libradtran"
w = SpectrogramFitWorkspace(spectrum, atmgrid_file_name=atmgrid_filename, fit_angstrom_exponent=False,
verbose=True, plot=True, live_fit=False)
run_spectrogram_minimisation(w, method="newton")
Expand Down
26 changes: 16 additions & 10 deletions tests/test_multispectra.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import matplotlib as mpl
mpl.use('Agg') # must be run first! But therefore requires noqa E402 on all other imports

from spectractor.fit.fit_multispectra import _build_test_sample, MultiSpectraFitWorkspace, run_multispectra_minimisation
from spectractor import parameters
import numpy as np
from spectractor.fit.fit_multispectra import _build_test_sample, MultiSpectraFitWorkspace, run_multispectra_minimisation # noqa: E402
from spectractor import parameters # noqa: E402
from spectractor.tools import uvspec_available # noqa: E402
import numpy as np # noqa: E402
import os # noqa: E402
import unittest # noqa: E402
import astropy.config # noqa: E402


NSPECTRA = 4
OZONE = 300
PWV = 5
AEROSOLS = 0.05
LOG10A = -2
VAOD = 0.05
ANGEXP = 1.2

@unittest.skipIf(uvspec_available() is False, 'Skipping to avoid libradtran dependency')
@astropy.config.set_temp_cache(os.path.join(os.path.abspath(os.path.dirname(__file__)), "data", "cache"))
def test_multispectra():
spectra = _build_test_sample(NSPECTRA, aerosols=AEROSOLS, ozone=OZONE, pwv=PWV, angstrom_exponent=10**LOG10A)
spectra = _build_test_sample(targets=["HD111980"]*3, zs=np.linspace(1, 2, 3),
aerosols=VAOD, ozone=OZONE, pwv=PWV, angstrom_exponent=ANGEXP)
parameters.VERBOSE = True

nsigma = 3
labels = ["VAOD", "LOG10A", "OZONE", "PWV"]
truth = [AEROSOLS, LOG10A, OZONE, PWV]
nsigma = 5
labels = ["VAOD", "ANGEXP", "OZONE", "PWV"]
truth = [VAOD, ANGEXP, OZONE, PWV]
indices = [0, 1, 2, 3]
for method in ["noprior", "spectrum"]:
w = MultiSpectraFitWorkspace("./tests/data/multispectra_test", spectra, bin_width=10, verbose=True, fixed_deltas=True,
Expand Down
1 change: 1 addition & 0 deletions tests/test_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def test_simulator():
parameters.VERBOSE = True
parameters.DEBUG = False
load_config('config/ctio.ini')
parameters.SPECTRACTOR_ATMOSPHERE_SIM = "libradtran"

for file_name in file_names:
spectrum = Spectrum(file_name)
Expand Down

0 comments on commit 3d0558e

Please sign in to comment.