Skip to content

Commit 4afc0c8

Browse files
authored
Horne extraction fix (#256)
1 parent 12f9e6c commit 4afc0c8

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

CHANGES.rst

+12-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,18 @@ Bug Fixes
1313
Other changes
1414
^^^^^^^^^^^^^
1515

16-
1.5.0 (2024-01-16)
16+
17+
1.5.1 (2024-03-08)
18+
------------------
19+
20+
Bug Fixes
21+
^^^^^^^^^
22+
23+
- Changed Horne extraction to behave as before when using an interpolated spatial profile
24+
and not explicitly setting `bkgrd_prof` to `None`. The changed default behavior in 1.5.0
25+
caused problems in codes using specreduce.
26+
27+
1.5.0 (2024-03-06)
1728
------------------
1829

1930
New Features

specreduce/extract.py

+12-15
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,10 @@ class HorneExtract(SpecreduceOperation):
299299
The index of the image's cross-dispersion axis. [default: 0]
300300
301301
bkgrd_prof : `~astropy.modeling.Model` or None, optional
302-
A model for the image's background flux. If ``spatial_profile`` is set
303-
to ``interpolated_profile``, then ``bkgrd_prof`` must be set to None.
304-
[default: models.Polynomial1D(2)].
302+
A model for the image's background flux when using the ``gaussian``
303+
spatial profile. If ``spatial_profile`` is set to ``gaussian``, it defaults
304+
to ``models.Polynomial1D(2)``. Note that the ``interpolated_profile`` option
305+
does not support a background model, so ``bkgrd_prof`` must be left as ``None``.
305306
306307
spatial_profile : str or dict, optional
307308
The shape of the object profile. The first option is 'gaussian' to fit
@@ -341,7 +342,7 @@ class HorneExtract(SpecreduceOperation):
341342

342343
image: NDData
343344
trace_object: Trace
344-
bkgrd_prof: Model = field(default=models.Polynomial1D(2))
345+
bkgrd_prof: None | Model = None
345346
spatial_profile: str | dict = "gaussian"
346347
variance: np.ndarray = field(default=None)
347348
mask: np.ndarray = field(default=None)
@@ -599,7 +600,10 @@ def __call__(
599600
The index of the image's cross-dispersion axis.
600601
601602
bkgrd_prof : `~astropy.modeling.Model`, optional
602-
A model for the image's background flux.
603+
A model for the image's background flux when using the ``gaussian``
604+
spatial profile. If ``spatial_profile`` is set to ``gaussian``, it defaults
605+
to ``models.Polynomial1D(2)``. Note that the ``interpolated_profile`` option
606+
does not support a background model, so ``bkgrd_prof`` must be left as ``None``.
603607
604608
spatial_profile : str or dict, optional
605609
The shape of the object profile. The first option is 'gaussian' to fit
@@ -665,6 +669,9 @@ def __call__(
665669
n_bins_interpolated_profile = profile.get("n_bins_interpolated_profile", 10)
666670
interp_degree_interpolated_profile = profile.get("interp_degree_interpolated_profile", 1)
667671

672+
if bkgrd_prof is None and profile_type == 'gaussian':
673+
bkgrd_prof = models.Polynomial1D(2)
674+
668675
self.image = self._parse_image(image, variance, mask, unit, disp_axis)
669676
variance = self.image.uncertainty.represent_as(VarianceUncertainty).array
670677
mask = self.image.mask.astype(bool) | (~np.isfinite(self.image.data))
@@ -690,16 +697,6 @@ def __call__(
690697
else:
691698
mean_cross_pix = np.broadcast_to(ncross // 2, ndisp)
692699
else: # interpolated_profile
693-
# for now, bkgrd_prof must be None because a compound model can't
694-
# be created with a interpolator + model. i think there is a way
695-
# around this, but will follow up later
696-
if bkgrd_prof is not None:
697-
raise ValueError(
698-
"When `spatial_profile`is `interpolated_profile`,"
699-
"`bkgrd_prof` must be None. Background should"
700-
" be fit and subtracted from `img` beforehand."
701-
)
702-
703700
# determine interpolation degree from input and make tuple if int
704701
# this can also be moved to another method to parse the input
705702
# 'spatial_profile' arg, eventually

specreduce/tests/test_extract.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,12 @@ def test_horne_interpolated_profile(mk_test_img):
314314

315315
assert_quantity_allclose(horne_extract_gauss.spectrum.flux, horne_extract_self.spectrum.flux)
316316

317-
with pytest.raises(ValueError, match="When"):
318-
HorneExtract(
319-
image.data,
320-
trace,
321-
spatial_profile={"name": "interpolated_profile", "n_bins_interpolated_profile": 3},
322-
bkgrd_prof=models.Polynomial1D(2),
323-
variance=np.ones(image.data.shape),
324-
).spectrum
317+
horne_extract_self = HorneExtract(
318+
image.data,
319+
trace,
320+
spatial_profile={"name": "interpolated_profile", "n_bins_interpolated_profile": 3},
321+
variance=np.ones(image.data.shape),
322+
)
325323

326324

327325
def test_horne_interpolated_profile_norm(mk_test_img):

0 commit comments

Comments
 (0)