Skip to content

Commit

Permalink
Begin deprecation process for bkg_array
Browse files Browse the repository at this point in the history
  • Loading branch information
ojustino committed Nov 22, 2022
1 parent c9e076e commit 53fd1a4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
16 changes: 10 additions & 6 deletions specreduce/background.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np
from astropy.nddata import NDData
from astropy.utils.decorators import deprecated_attribute
from astropy import units as u
from specutils import Spectrum1D

Expand Down Expand Up @@ -56,6 +57,9 @@ class Background(_ImageParser):
disp_axis: int = 1
crossdisp_axis: int = 0

# TO-DO: update bkg_array with Spectrum1D alternative (is bkg_image enough?)
bkg_array = deprecated_attribute('bkg_array', '1.3')

def __post_init__(self):
"""
Determine the background from an image for subtraction.
Expand Down Expand Up @@ -93,7 +97,7 @@ def _to_trace(trace):
if self.width < 0:
raise ValueError("width must be positive")
if self.width == 0:
self.bkg_array = np.zeros(self.image.shape[self.disp_axis])
self._bkg_array = np.zeros(self.image.shape[self.disp_axis])
return

if isinstance(self.traces, Trace):
Expand Down Expand Up @@ -130,13 +134,13 @@ def _to_trace(trace):
self.bkg_wimage = bkg_wimage

if self.statistic == 'average':
self.bkg_array = np.average(self.image.data,
weights=self.bkg_wimage,
axis=self.crossdisp_axis)
self._bkg_array = np.average(self.image.data,
weights=self.bkg_wimage,
axis=self.crossdisp_axis)
elif self.statistic == 'median':
med_image = self.image.data.copy()
med_image[np.where(self.bkg_wimage) == 0] = np.nan
self.bkg_array = np.nanmedian(med_image, axis=self.crossdisp_axis)
self._bkg_array = np.nanmedian(med_image, axis=self.crossdisp_axis)
else:
raise ValueError("statistic must be 'average' or 'median'")

Expand Down Expand Up @@ -231,7 +235,7 @@ def bkg_image(self, image=None):
Spectrum1D object with same shape as ``image``.
"""
image = self._parse_image(image)
return Spectrum1D(np.tile(self.bkg_array,
return Spectrum1D(np.tile(self._bkg_array,
(image.shape[0], 1)) * image.unit,
spectral_axis=image.spectral_axis)

Expand Down
10 changes: 5 additions & 5 deletions specreduce/tests/test_background.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ def test_background():
bg1 = Background(image, [trace-bkg_sep, trace+bkg_sep], width=bkg_width)
bg2 = Background.two_sided(image, trace, bkg_sep, width=bkg_width)
bg3 = Background.two_sided(image, trace_pos, bkg_sep, width=bkg_width)
assert np.allclose(bg1.bkg_array, bg2.bkg_array)
assert np.allclose(bg1.bkg_array, bg3.bkg_array)
assert np.allclose(bg1.bkg_image().flux, bg2.bkg_image().flux)
assert np.allclose(bg1.bkg_image().flux, bg3.bkg_image().flux)

bg4 = Background(image_um, [trace-bkg_sep, trace+bkg_sep], width=bkg_width)
bg5 = Background.two_sided(image_um, trace, bkg_sep, width=bkg_width)
bg6 = Background.two_sided(image_um, trace_pos, bkg_sep, width=bkg_width)
assert np.allclose(bg1.bkg_array, bg4.bkg_array)
assert np.allclose(bg1.bkg_array, bg5.bkg_array)
assert np.allclose(bg1.bkg_array, bg6.bkg_array)
assert np.allclose(bg1.bkg_image().flux, bg4.bkg_image().flux)
assert np.allclose(bg1.bkg_image().flux, bg5.bkg_image().flux)
assert np.allclose(bg1.bkg_image().flux, bg6.bkg_image().flux)

# test that creating a one_sided background works
Background.one_sided(image, trace, bkg_sep, width=bkg_width)
Expand Down

0 comments on commit 53fd1a4

Please sign in to comment.