Skip to content

Commit

Permalink
implement average and median support for bkg_spectrum (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
gibsongreen authored Feb 28, 2025
1 parent 0ea84c9 commit c31a9af
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
23 changes: 20 additions & 3 deletions specreduce/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def bkg_image(self, image=None):
(image.shape[0], 1)) * image.unit,
spectral_axis=image.spectral_axis)

def bkg_spectrum(self, image=None):
def bkg_spectrum(self, image=None, bkg_statistic="sum"):
"""
Expose the 1D spectrum of the background.
Expand All @@ -341,6 +341,13 @@ def bkg_spectrum(self, image=None):
(spatial) direction is axis 0 and dispersion (wavelength)
direction is axis 1. If None, will extract the background
from ``image`` used to initialize the class. [default: None]
bkg_statistic : {'average', 'median', 'sum'}, optional
Statistical method used to collapse the background image. [default: ``'sum'``]
Supported values are:
- ``'average'`` : Uses the mean (`numpy.nanmean`).
- ``'median'`` : Uses the median (`numpy.nanmedian`).
- ``'sum'`` : Uses the sum (`numpy.nansum`).
Returns
-------
Expand All @@ -351,12 +358,22 @@ def bkg_spectrum(self, image=None):
"""
bkg_image = self.bkg_image(image)

if bkg_statistic == 'sum':
statistic_function = np.nansum
elif bkg_statistic == 'median':
statistic_function = np.nanmedian
elif bkg_statistic == 'average':
statistic_function = np.nanmean
else:
raise ValueError(f"Background statistic {bkg_statistic} is not supported. "
"Please choose from: average, median, or sum.")

try:
return bkg_image.collapse(np.nansum, axis=self.crossdisp_axis)
return bkg_image.collapse(statistic_function, axis=self.crossdisp_axis)
except u.UnitTypeError:
# can't collapse with a spectral axis in pixels because
# SpectralCoord only allows frequency/wavelength equivalent units...
ext1d = np.nansum(bkg_image.flux, axis=self.crossdisp_axis)
ext1d = statistic_function(bkg_image.flux, axis=self.crossdisp_axis)
return Spectrum1D(ext1d, bkg_image.spectral_axis)

def sub_image(self, image=None):
Expand Down
11 changes: 11 additions & 0 deletions specreduce/tests/test_background.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from astropy.nddata import NDData
import astropy.units as u
import numpy as np
from numpy.testing import assert_allclose
import pytest
from specutils import Spectrum1D

Expand Down Expand Up @@ -80,6 +81,16 @@ def test_background(mk_test_img_raw, mk_test_spec_no_spectral_axis,
assert np.isnan(bg.bkg_spectrum().flux).sum() == 0
assert np.isnan(bg.sub_spectrum().flux).sum() == 0

bkg_spec_avg = bg1.bkg_spectrum(bkg_statistic='average')
assert_allclose(bkg_spec_avg.mean().value, 14.5, rtol=0.5)

bkg_spec_median = bg1.bkg_spectrum(bkg_statistic='median')
assert_allclose(bkg_spec_median.mean().value, 14.5, rtol=0.5)

with pytest.raises(ValueError, match="Background statistic max is not supported. "
"Please choose from: average, median, or sum."):
bg1.bkg_spectrum(bkg_statistic='max')


def test_warnings_errors(mk_test_spec_no_spectral_axis):
image = mk_test_spec_no_spectral_axis
Expand Down

0 comments on commit c31a9af

Please sign in to comment.