Skip to content

Commit

Permalink
Background.bkg_spectrum and sub_spectrum
Browse files Browse the repository at this point in the history
  • Loading branch information
kecnry committed Sep 28, 2022
1 parent 78e68b7 commit fa35868
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ New Features
^^^^^^^^^^^^

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

API Changes
^^^^^^^^^^^
Expand Down
45 changes: 44 additions & 1 deletion specreduce/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

import numpy as np
from astropy.nddata import NDData
from astropy import units as u

from specreduce.extract import _ap_weight_image
from specreduce.extract import _ap_weight_image, _to_spectrum1d_pixels
from specreduce.tracing import Trace, FlatTrace

__all__ = ['Background']
Expand Down Expand Up @@ -205,6 +206,27 @@ def bkg_image(self, image=None):

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

def bkg_spectrum(self, image=None):
"""
Expose the 1D spectrum of the background.
Parameters
----------
image : nddata-compatible image or None
image with 2-D spectral image data. If None, will use ``image`` passed
to extract the background.
Returns
-------
spec : `~specutils.Spectrum1D`
The background 1d spectrum with flux expressed in the same
units as the input image, or u.DN, and pixel units
"""
bkg_image = self.bkg_image(image=image)

ext1d = np.sum(bkg_image, axis=self.crossdisp_axis)
return _to_spectrum1d_pixels(ext1d * getattr(image, 'unit', u.DN))

def sub_image(self, image=None):
"""
Subtract the computed background from ``image``.
Expand All @@ -228,6 +250,27 @@ def sub_image(self, image=None):
else:
return image - self.bkg_image(image)

def sub_spectrum(self, image=None):
"""
Expose the 1D spectrum of the background-subtracted image.
Parameters
----------
image : nddata-compatible image or None
image with 2-D spectral image data. If None, will use ``image`` passed
to extract the background.
Returns
-------
spec : `~specutils.Spectrum1D`
The background-subtracted 1d spectrum with flux expressed in the same
units as the input image, or u.DN, and pixel units
"""
sub_image = self.sub_image(image=image)

ext1d = np.sum(sub_image, axis=self.crossdisp_axis)
return _to_spectrum1d_pixels(ext1d * getattr(image, 'unit', u.DN))

def __rsub__(self, image):
"""
Subtract the background from an image.
Expand Down
18 changes: 8 additions & 10 deletions specreduce/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ def _ap_weight_image(trace, width, disp_axis, crossdisp_axis, image_shape):
return wimage


def _to_spectrum1d_pixels(fluxes):
# TODO: add wavelength units, uncertainty and mask to spectrum1D object
return Spectrum1D(spectral_axis=np.arange(len(fluxes)) * u.pixel,
flux=fluxes)


@dataclass
class BoxcarExtract(SpecreduceOperation):
"""
Expand Down Expand Up @@ -189,12 +195,7 @@ def __call__(self, image=None, trace_object=None, width=None,

# extract
ext1d = np.sum(image * wimage, axis=crossdisp_axis)

# TODO: add wavelenght units, uncertainty and mask to spectrum1D object
spec = Spectrum1D(spectral_axis=np.arange(len(ext1d)) * u.pixel,
flux=ext1d * getattr(image, 'unit', u.DN))

return spec
return _to_spectrum1d_pixels(ext1d * getattr(image, 'unit', u.DN))


@dataclass
Expand Down Expand Up @@ -432,10 +433,7 @@ def __call__(self, image=None, trace_object=None,
extraction = result * norms

# convert the extraction to a Spectrum1D object
pixels = np.arange(img.shape[disp_axis]) * u.pix
spec_1d = Spectrum1D(spectral_axis=pixels, flux=extraction * unit)

return spec_1d
return _to_spectrum1d_pixels(extraction * unit)


@dataclass
Expand Down

0 comments on commit fa35868

Please sign in to comment.