Skip to content

Commit

Permalink
tests etc
Browse files Browse the repository at this point in the history
  • Loading branch information
cshanahan1 committed May 6, 2024
1 parent 852bc7c commit e5c898d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Specviz
Specviz2d
^^^^^^^^^

- Add option to use self-derived spatial profile for Horne extract in spectral extraction plugin. [#2845]

API Changes
-----------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def __init__(self, *args, **kwargs):
self.horne_ext_profile = SelectPluginComponent(self,
items='horne_ext_profile_items',
selected='horne_ext_profile_selected',
manual_options=['Gaussian', 'Self (interpolated)'])
manual_options=['Gaussian', 'Self (interpolated)']) # noqa

self.ext_add_results = AddResults(self, 'ext_results_label',
'ext_results_label_default',
Expand Down Expand Up @@ -969,14 +969,15 @@ def export_extract(self, **kwargs):
# setup dict of interpolation options
n_bins_interpolated_profile = self.self_prof_n_bins
interp_degree = (self.self_prof_interp_degree_x, self.self_prof_interp_degree_y)
spatial_profile = {'n_bins_interpolated_profile': n_bins_interpolated_profile,
spatial_profile = {'name': 'interpolated_profile',
'n_bins_interpolated_profile': n_bins_interpolated_profile,
'interp_degree': interp_degree}
elif self.horne_ext_profile_selected == 'Gaussian':
spaital_profile = 'gaussian'
spatial_profile = 'gaussian'
else:
raise ValueError("Horne extraction profile must either be 'Gaussian' or 'Self (interpolated)'")
raise ValueError("Horne extraction profile must either be 'Gaussian' or 'Self (interpolated)'") # noqa

ext = extract.HorneExtract(inp_sp2d, trace) # , spatial_profile=spatial_profile)
ext = extract.HorneExtract(inp_sp2d, trace, spatial_profile=spatial_profile)
else:
raise NotImplementedError(f"extraction type '{self.ext_type_selected}' not supported") # noqa

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import gwcs
import pytest
from astropy.modeling import models
from astropy.nddata import VarianceUncertainty
import astropy.units as u
from astropy.utils.data import download_file
import numpy as np
from packaging.version import Version
from specreduce import tracing, background, extract
from specutils import Spectrum1D
Expand Down Expand Up @@ -185,3 +189,63 @@ def test_spectrum_on_top(specviz2d_helper):
pext = specviz2d_helper.app.get_tray_item_from_name('spectral-extraction')
assert pext.bg_type_selected == 'OneSided'
assert pext.bg_separation < 0


@pytest.mark.filterwarnings('ignore')
def test_horne_extract_self_profile(specviz2d_helper):

spec2d = np.zeros((40, 100))
spec2dvar = np.ones((40, 100))

for ii in range(spec2d.shape[1]):
mgaus = models.Gaussian1D(amplitude=10,
mean=(9.+(20/spec2d.shape[1])*ii),
stddev=2)
rg = np.arange(0, spec2d.shape[0], 1)
gaus = mgaus(rg)
spec2d[:, ii] = gaus

wave = np.arange(0, spec2d.shape[1], 1)
objectspec = Spectrum1D(spectral_axis=wave*u.m,
flux=spec2d*u.Jy,
uncertainty=VarianceUncertainty(spec2dvar*u.Jy*u.Jy))

specviz2d_helper.load_data(objectspec)
pext = specviz2d_helper.app.get_tray_item_from_name('spectral-extraction')

trace_fit = tracing.FitTrace(objectspec,
trace_model=models.Polynomial1D(degree=1),
window=13, peak_method='gaussian', guess=20)
pext.import_trace(trace_fit)

pext.ext_type_selected = "Horne"
pext.horne_ext_profile_selected = "Self (interpolated)"

# check that correct defaults are set
assert pext.self_prof_n_bins == 10
assert pext.self_prof_interp_degree_x == 1
assert pext.self_prof_interp_degree_y == 1

sp_ext = pext.export_extract_spectrum()

bg_sub = pext.export_bg_sub()

extract_horne_interp = extract.HorneExtract(bg_sub, trace_fit,
spatial_profile='interpolated_profile')

assert np.all(extract_horne_interp.spectrum.flux == sp_ext.flux)

# now try changing from defaults
pext.self_prof_n_bins = 5
pext.self_prof_interp_degree_x = 2
pext.self_prof_interp_degree_y = 2

sp_ext = pext.export_extract_spectrum()
bg_sub = pext.export_bg_sub()

extract_horne_interp = extract.HorneExtract(bg_sub, trace_fit,
spatial_profile={'name': 'interpolated_profile',
'n_bins_interpolated_profile': 5,
'interp_degree': (2, 2)})

assert np.all(extract_horne_interp.spectrum.flux == sp_ext.flux)

0 comments on commit e5c898d

Please # to comment.