Skip to content

Commit c31a9af

Browse files
authored
implement average and median support for bkg_spectrum (#253)
1 parent 0ea84c9 commit c31a9af

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

specreduce/background.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ def bkg_image(self, image=None):
330330
(image.shape[0], 1)) * image.unit,
331331
spectral_axis=image.spectral_axis)
332332

333-
def bkg_spectrum(self, image=None):
333+
def bkg_spectrum(self, image=None, bkg_statistic="sum"):
334334
"""
335335
Expose the 1D spectrum of the background.
336336
@@ -341,6 +341,13 @@ def bkg_spectrum(self, image=None):
341341
(spatial) direction is axis 0 and dispersion (wavelength)
342342
direction is axis 1. If None, will extract the background
343343
from ``image`` used to initialize the class. [default: None]
344+
bkg_statistic : {'average', 'median', 'sum'}, optional
345+
Statistical method used to collapse the background image. [default: ``'sum'``]
346+
Supported values are:
347+
348+
- ``'average'`` : Uses the mean (`numpy.nanmean`).
349+
- ``'median'`` : Uses the median (`numpy.nanmedian`).
350+
- ``'sum'`` : Uses the sum (`numpy.nansum`).
344351
345352
Returns
346353
-------
@@ -351,12 +358,22 @@ def bkg_spectrum(self, image=None):
351358
"""
352359
bkg_image = self.bkg_image(image)
353360

361+
if bkg_statistic == 'sum':
362+
statistic_function = np.nansum
363+
elif bkg_statistic == 'median':
364+
statistic_function = np.nanmedian
365+
elif bkg_statistic == 'average':
366+
statistic_function = np.nanmean
367+
else:
368+
raise ValueError(f"Background statistic {bkg_statistic} is not supported. "
369+
"Please choose from: average, median, or sum.")
370+
354371
try:
355-
return bkg_image.collapse(np.nansum, axis=self.crossdisp_axis)
372+
return bkg_image.collapse(statistic_function, axis=self.crossdisp_axis)
356373
except u.UnitTypeError:
357374
# can't collapse with a spectral axis in pixels because
358375
# SpectralCoord only allows frequency/wavelength equivalent units...
359-
ext1d = np.nansum(bkg_image.flux, axis=self.crossdisp_axis)
376+
ext1d = statistic_function(bkg_image.flux, axis=self.crossdisp_axis)
360377
return Spectrum1D(ext1d, bkg_image.spectral_axis)
361378

362379
def sub_image(self, image=None):

specreduce/tests/test_background.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from astropy.nddata import NDData
22
import astropy.units as u
33
import numpy as np
4+
from numpy.testing import assert_allclose
45
import pytest
56
from specutils import Spectrum1D
67

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

84+
bkg_spec_avg = bg1.bkg_spectrum(bkg_statistic='average')
85+
assert_allclose(bkg_spec_avg.mean().value, 14.5, rtol=0.5)
86+
87+
bkg_spec_median = bg1.bkg_spectrum(bkg_statistic='median')
88+
assert_allclose(bkg_spec_median.mean().value, 14.5, rtol=0.5)
89+
90+
with pytest.raises(ValueError, match="Background statistic max is not supported. "
91+
"Please choose from: average, median, or sum."):
92+
bg1.bkg_spectrum(bkg_statistic='max')
93+
8394

8495
def test_warnings_errors(mk_test_spec_no_spectral_axis):
8596
image = mk_test_spec_no_spectral_axis

0 commit comments

Comments
 (0)