Skip to content

Commit 44d3533

Browse files
authored
Merge pull request #143 from kecnry/bg-spectrum
Background.bkg_spectrum and sub_spectrum
2 parents f6a834f + cade209 commit 44d3533

File tree

4 files changed

+66
-15
lines changed

4 files changed

+66
-15
lines changed

CHANGES.rst

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ New Features
1515
^^^^^^^^^^^^
1616

1717
- ``peak_method`` as an optional argument to ``KosmosTrace`` [#115]
18+
- ``Background`` has new methods for exposing the 1D spectrum of the background or
19+
background-subtracted regions [#143]
1820

1921
API Changes
2022
^^^^^^^^^^^

specreduce/background.py

+50-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
import numpy as np
77
from astropy.nddata import NDData
8+
from astropy import units as u
89

9-
from specreduce.extract import _ap_weight_image
10+
from specreduce.extract import _ap_weight_image, _to_spectrum1d_pixels
1011
from specreduce.tracing import Trace, FlatTrace
1112

1213
__all__ = ['Background']
@@ -193,8 +194,8 @@ def bkg_image(self, image=None):
193194
Parameters
194195
----------
195196
image : nddata-compatible image or None
196-
image with 2-D spectral image data. If None, will use ``image`` passed
197-
to extract the background.
197+
image with 2-D spectral image data. If None, will extract
198+
the background from ``image`` used to initialize the class.
198199
199200
Returns
200201
-------
@@ -205,15 +206,37 @@ def bkg_image(self, image=None):
205206

206207
return np.tile(self.bkg_array, (image.shape[0], 1))
207208

209+
def bkg_spectrum(self, image=None):
210+
"""
211+
Expose the 1D spectrum of the background.
212+
213+
Parameters
214+
----------
215+
image : nddata-compatible image or None
216+
image with 2-D spectral image data. If None, will extract
217+
the background from ``image`` used to initialize the class.
218+
219+
Returns
220+
-------
221+
spec : `~specutils.Spectrum1D`
222+
The background 1-D spectrum, with flux expressed in the same
223+
units as the input image (or u.DN if none were provided) and
224+
the spectral axis expressed in pixel units.
225+
"""
226+
bkg_image = self.bkg_image(image=image)
227+
228+
ext1d = np.sum(bkg_image, axis=self.crossdisp_axis)
229+
return _to_spectrum1d_pixels(ext1d * getattr(image, 'unit', u.DN))
230+
208231
def sub_image(self, image=None):
209232
"""
210233
Subtract the computed background from ``image``.
211234
212235
Parameters
213236
----------
214237
image : nddata-compatible image or None
215-
image with 2-D spectral image data. If None, will use ``image`` passed
216-
to extract the background.
238+
image with 2-D spectral image data. If None, will extract
239+
the background from ``image`` used to initialize the class.
217240
218241
Returns
219242
-------
@@ -228,6 +251,28 @@ def sub_image(self, image=None):
228251
else:
229252
return image - self.bkg_image(image)
230253

254+
def sub_spectrum(self, image=None):
255+
"""
256+
Expose the 1D spectrum of the background-subtracted image.
257+
258+
Parameters
259+
----------
260+
image : nddata-compatible image or None
261+
image with 2-D spectral image data. If None, will extract
262+
the background from ``image`` used to initialize the class.
263+
264+
Returns
265+
-------
266+
spec : `~specutils.Spectrum1D`
267+
The background 1-D spectrum, with flux expressed in the same
268+
units as the input image (or u.DN if none were provided) and
269+
the spectral axis expressed in pixel units.
270+
"""
271+
sub_image = self.sub_image(image=image)
272+
273+
ext1d = np.sum(sub_image, axis=self.crossdisp_axis)
274+
return _to_spectrum1d_pixels(ext1d * getattr(image, 'unit', u.DN))
275+
231276
def __rsub__(self, image):
232277
"""
233278
Subtract the background from an image.

specreduce/extract.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ def _ap_weight_image(trace, width, disp_axis, crossdisp_axis, image_shape):
102102
return wimage
103103

104104

105+
def _to_spectrum1d_pixels(fluxes):
106+
# TODO: add wavelength units, uncertainty and mask to spectrum1D object
107+
return Spectrum1D(spectral_axis=np.arange(len(fluxes)) * u.pixel,
108+
flux=fluxes)
109+
110+
105111
@dataclass
106112
class BoxcarExtract(SpecreduceOperation):
107113
"""
@@ -189,12 +195,7 @@ def __call__(self, image=None, trace_object=None, width=None,
189195

190196
# extract
191197
ext1d = np.sum(image * wimage, axis=crossdisp_axis)
192-
193-
# TODO: add wavelenght units, uncertainty and mask to spectrum1D object
194-
spec = Spectrum1D(spectral_axis=np.arange(len(ext1d)) * u.pixel,
195-
flux=ext1d * getattr(image, 'unit', u.DN))
196-
197-
return spec
198+
return _to_spectrum1d_pixels(ext1d * getattr(image, 'unit', u.DN))
198199

199200

200201
@dataclass
@@ -432,10 +433,7 @@ def __call__(self, image=None, trace_object=None,
432433
extraction = result * norms
433434

434435
# convert the extraction to a Spectrum1D object
435-
pixels = np.arange(img.shape[disp_axis]) * u.pix
436-
spec_1d = Spectrum1D(spectral_axis=pixels, flux=extraction * unit)
437-
438-
return spec_1d
436+
return _to_spectrum1d_pixels(extraction * unit)
439437

440438

441439
@dataclass

specreduce/tests/test_background.py

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import astropy.units as u
55
from astropy.nddata import CCDData
6+
from specutils import Spectrum1D
67

78
from specreduce.background import Background
89
from specreduce.tracing import FlatTrace
@@ -44,6 +45,11 @@ def test_background():
4445
assert np.allclose(sub1, sub2)
4546
assert np.allclose(sub1, sub3)
4647

48+
bkg_spec = bg1.bkg_spectrum()
49+
assert isinstance(bkg_spec, Spectrum1D)
50+
sub_spec = bg1.sub_spectrum()
51+
assert isinstance(sub_spec, Spectrum1D)
52+
4753

4854
def test_oob():
4955
# image.shape (30, 10)

0 commit comments

Comments
 (0)