Skip to content

Commit 4b0bdd5

Browse files
committed
Fix Background.sub_image for physical wavelengths
1 parent 0ec792d commit 4b0bdd5

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

specreduce/background.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import numpy as np
77
from astropy.nddata import NDData
8-
from astropy.units import UnitTypeError
8+
from astropy import units as u
99
from specutils import Spectrum1D
1010

1111
from specreduce.core import _ImageParser
@@ -258,7 +258,7 @@ def bkg_spectrum(self, image=None):
258258

259259
try:
260260
return bkg_image.collapse(np.sum, axis=self.crossdisp_axis)
261-
except UnitTypeError:
261+
except u.UnitTypeError:
262262
# can't collapse with a spectral axis in pixels because
263263
# SpectralCoord only allows frequency/wavelength equivalent units...
264264
ext1d = np.sum(bkg_image.flux, axis=self.crossdisp_axis)
@@ -280,10 +280,14 @@ def sub_image(self, image=None):
280280
"""
281281
image = self._parse_image(image)
282282

283+
# a compare_wcs argument is needed for Spectrum1D.subtract() in order to
284+
# avoid a TypeError from SpectralCoord when image's spectral axis is in
285+
# pixels. it is not needed when image's spectral axis has physical units
286+
kwargs = ({'compare_wcs': None} if image.spectral_axis.unit == u.pix
287+
else {})
288+
283289
# https://docs.astropy.org/en/stable/nddata/mixins/ndarithmetic.html
284-
# (compare_wcs argument needed to avoid TypeError from SpectralCoord
285-
# when image's spectral axis is in pixels)
286-
return image.subtract(self.bkg_image(image), compare_wcs=None)
290+
return image.subtract(self.bkg_image(image), **kwargs)
287291

288292
def sub_spectrum(self, image=None):
289293
"""
@@ -306,7 +310,7 @@ def sub_spectrum(self, image=None):
306310

307311
try:
308312
return sub_image.collapse(np.sum, axis=self.crossdisp_axis)
309-
except UnitTypeError:
313+
except u.UnitTypeError:
310314
# can't collapse with a spectral axis in pixels because
311315
# SpectralCoord only allows frequency/wavelength equivalent units...
312316
ext1d = np.sum(sub_image.flux, axis=self.crossdisp_axis)
@@ -316,4 +320,6 @@ def __rsub__(self, image):
316320
"""
317321
Subtract the background from an image.
318322
"""
323+
# NOTE: will not be called until specutils PR #988 is merged, released,
324+
# and pinned here
319325
return self.sub_image(image)

specreduce/tests/test_background.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
image[j, ::] *= j
1919
image = Spectrum1D(image * u.DN,
2020
uncertainty=VarianceUncertainty(np.ones_like(image)))
21+
image_um = Spectrum1D(image.flux,
22+
spectral_axis=np.arange(image.data.shape[1]) * u.um,
23+
uncertainty=VarianceUncertainty(np.ones_like(image.data)))
2124

2225

2326
def test_background():
@@ -29,13 +32,22 @@ def test_background():
2932
trace = FlatTrace(image, trace_pos)
3033
bkg_sep = 5
3134
bkg_width = 2
32-
# all the following should be equivalent:
35+
36+
# all the following should be equivalent, whether image's spectral axis
37+
# is in pixels or physical units:
3338
bg1 = Background(image, [trace-bkg_sep, trace+bkg_sep], width=bkg_width)
3439
bg2 = Background.two_sided(image, trace, bkg_sep, width=bkg_width)
3540
bg3 = Background.two_sided(image, trace_pos, bkg_sep, width=bkg_width)
3641
assert np.allclose(bg1.bkg_array, bg2.bkg_array)
3742
assert np.allclose(bg1.bkg_array, bg3.bkg_array)
3843

44+
bg4 = Background(image_um, [trace-bkg_sep, trace+bkg_sep], width=bkg_width)
45+
bg5 = Background.two_sided(image_um, trace, bkg_sep, width=bkg_width)
46+
bg6 = Background.two_sided(image_um, trace_pos, bkg_sep, width=bkg_width)
47+
assert np.allclose(bg1.bkg_array, bg4.bkg_array)
48+
assert np.allclose(bg1.bkg_array, bg5.bkg_array)
49+
assert np.allclose(bg1.bkg_array, bg6.bkg_array)
50+
3951
# test that creating a one_sided background works
4052
Background.one_sided(image, trace, bkg_sep, width=bkg_width)
4153

@@ -51,6 +63,14 @@ def test_background():
5163
# assert np.allclose(sub1.flux, sub2.flux)
5264
assert np.allclose(sub2.flux, sub3.flux)
5365

66+
# NOTE: uncomment sub4 test once Spectrum1D and Background subtraction works
67+
# sub4 = image_um - bg4
68+
sub5 = bg4.sub_image(image_um)
69+
sub6 = bg4.sub_image()
70+
assert np.allclose(sub2.flux, sub5.flux)
71+
# assert np.allclose(sub4.flux, sub5.flux)
72+
assert np.allclose(sub5.flux, sub6.flux)
73+
5474
bkg_spec = bg1.bkg_spectrum()
5575
assert isinstance(bkg_spec, Spectrum1D)
5676
sub_spec = bg1.sub_spectrum()

0 commit comments

Comments
 (0)