Skip to content

Commit

Permalink
Merge pull request #143 from kecnry/bg-spectrum
Browse files Browse the repository at this point in the history
Background.bkg_spectrum and sub_spectrum
  • Loading branch information
kecnry authored Oct 3, 2022
2 parents f6a834f + cade209 commit 44d3533
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 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
55 changes: 50 additions & 5 deletions 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 @@ -193,8 +194,8 @@ def bkg_image(self, image=None):
Parameters
----------
image : nddata-compatible image or None
image with 2-D spectral image data. If None, will use ``image`` passed
to extract the background.
image with 2-D spectral image data. If None, will extract
the background from ``image`` used to initialize the class.
Returns
-------
Expand All @@ -205,15 +206,37 @@ 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 extract
the background from ``image`` used to initialize the class.
Returns
-------
spec : `~specutils.Spectrum1D`
The background 1-D spectrum, with flux expressed in the same
units as the input image (or u.DN if none were provided) and
the spectral axis expressed in 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``.
Parameters
----------
image : nddata-compatible image or None
image with 2-D spectral image data. If None, will use ``image`` passed
to extract the background.
image with 2-D spectral image data. If None, will extract
the background from ``image`` used to initialize the class.
Returns
-------
Expand All @@ -228,6 +251,28 @@ 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 extract
the background from ``image`` used to initialize the class.
Returns
-------
spec : `~specutils.Spectrum1D`
The background 1-D spectrum, with flux expressed in the same
units as the input image (or u.DN if none were provided) and
the spectral axis expressed in 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
6 changes: 6 additions & 0 deletions specreduce/tests/test_background.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import astropy.units as u
from astropy.nddata import CCDData
from specutils import Spectrum1D

from specreduce.background import Background
from specreduce.tracing import FlatTrace
Expand Down Expand Up @@ -44,6 +45,11 @@ def test_background():
assert np.allclose(sub1, sub2)
assert np.allclose(sub1, sub3)

bkg_spec = bg1.bkg_spectrum()
assert isinstance(bkg_spec, Spectrum1D)
sub_spec = bg1.sub_spectrum()
assert isinstance(sub_spec, Spectrum1D)


def test_oob():
# image.shape (30, 10)
Expand Down

0 comments on commit 44d3533

Please sign in to comment.