Skip to content

Commit

Permalink
Add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ojustino committed Dec 21, 2022
1 parent 1edc7b6 commit bd72fae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Bug Fixes
- Output 1D spectra from Background no longer include NaNs. Output 1D
spectra from BoxcarExtract no longer include NaNs when none are present
in the extraction window. NaNs in the window will still propagate to
Boxcar1D's extracted 1D spectrum. [#159]
BoxcarExtract's extracted 1D spectrum. [#159]

- Backgrounds using median statistic properly ignore zero-weighted pixels
[#159]
Expand Down
27 changes: 21 additions & 6 deletions specreduce/tests/test_background.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
# Test image is comprised of 30 rows with 10 columns each. Row content
# is row index itself. This makes it easy to predict what should be the
# value extracted from a region centered at any arbitrary Y position.
image = np.ones(shape=(30, 10))
for j in range(image.shape[0]):
image[j, ::] *= j
image = Spectrum1D(image * u.DN,
uncertainty=VarianceUncertainty(np.ones_like(image)))
img = np.ones(shape=(30, 10))
for j in range(img.shape[0]):
img[j, ::] *= j
image = Spectrum1D(img * u.DN,
uncertainty=VarianceUncertainty(np.ones_like(img)))
image_um = Spectrum1D(image.flux,
spectral_axis=np.arange(image.data.shape[1]) * u.um,
uncertainty=VarianceUncertainty(np.ones_like(image.data)))
Expand All @@ -28,7 +28,7 @@ def test_background():
# Try combinations of extraction center, and even/odd
# extraction aperture sizes.
#
trace_pos = 15.0
trace_pos = 15
trace = FlatTrace(image, trace_pos)
bkg_sep = 5
bkg_width = 2
Expand Down Expand Up @@ -72,10 +72,25 @@ def test_background():
assert isinstance(bkg_spec, Spectrum1D)
sub_spec = bg1.sub_spectrum()
assert isinstance(sub_spec, Spectrum1D)

# test that width==0 results in no background
bg = Background.two_sided(image, trace, bkg_sep, width=0)
assert np.all(bg.bkg_image().flux == 0)

# test that any NaNs in input image (whether in or outside the window) don't
# propagate to _bkg_array (which affects bkg_image and sub_image methods) or
# the final 1D spectra.
img[0, 0] = np.nan # out of window
img[trace_pos, 0] = np.nan # in window
stats = ['average', 'median']

for st in stats:
bg = Background(img, trace-bkg_sep, width=bkg_width, statistic=st)
assert np.isnan(bg.image.flux).sum() == 2
assert np.isnan(bg._bkg_array).sum() == 0
assert np.isnan(bg.bkg_spectrum().flux).sum() == 0
assert np.isnan(bg.sub_spectrum().flux).sum() == 0


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

0 comments on commit bd72fae

Please sign in to comment.