Skip to content

Commit 7623865

Browse files
authored
Merge pull request #127 from kecnry/bg-oob-checks
logic to detect background regions extending beyond image limits
2 parents 874f519 + fa2cb54 commit 7623865

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

CHANGES.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
1.1.0 (unreleased)
1+
1.2.0 (unreleased)
22
------------------
33

4+
Bug Fixes
5+
^^^^^^^^^
6+
7+
- Improved errors/warnings when background region extends beyond bounds of image. [#127]
8+
9+
10+
1.1.0
11+
-----
12+
413
New Features
514
^^^^^^^^^^^^
615

specreduce/background.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22

3+
import warnings
34
from dataclasses import dataclass, field
45

56
import numpy as np
@@ -87,6 +88,13 @@ def _to_trace(trace):
8788
bkg_wimage = np.zeros_like(self.image, dtype=np.float64)
8889
for trace in self.traces:
8990
trace = _to_trace(trace)
91+
if (np.any(trace.trace.data >= self.image.shape[self.crossdisp_axis]) or
92+
np.any(trace.trace.data < 0)):
93+
raise ValueError("center of background window goes beyond image boundaries")
94+
elif (np.any(trace.trace.data + self.width/2. >= self.image.shape[self.crossdisp_axis])
95+
or np.any(trace.trace.data - self.width/2. < 0)):
96+
warnings.warn("background window extends beyond image boundaries")
97+
# pass trace.trace.data to ignore any mask on the trace
9098
bkg_wimage += _ap_weight_image(trace,
9199
self.width,
92100
self.disp_axis,

specreduce/extract.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ def _ap_weight_image(trace, width, disp_axis, crossdisp_axis, image_shape):
7171
# loop in dispersion direction and compute weights.
7272
for i in range(image_shape[disp_axis]):
7373
# TODO trace must handle transposed data (disp_axis == 0)
74-
wimage[:, i] = _get_boxcar_weights(trace[i], hwidth, image_sizes)
74+
# pass trace.trace.data[i] to avoid any mask if part of the regions is out-of-bounds
75+
wimage[:, i] = _get_boxcar_weights(trace.trace.data[i], hwidth, image_sizes)
7576

7677
return wimage
7778

specreduce/tests/test_background.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
import numpy as np
23

34
import astropy.units as u
@@ -42,3 +43,13 @@ def test_background():
4243
sub3 = bg1.sub_image()
4344
assert np.allclose(sub1, sub2)
4445
assert np.allclose(sub1, sub3)
46+
47+
48+
def test_oob():
49+
# image.shape (30, 10)
50+
with pytest.warns(match="background window extends beyond image boundaries"):
51+
Background.two_sided(image, 25, 4, width=3)
52+
53+
with pytest.raises(ValueError,
54+
match="center of background window goes beyond image boundaries"):
55+
Background.two_sided(image, 25, 6, width=3)

specreduce/tracing.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ def shift(self, delta):
5050
delta : float
5151
Shift to be applied to the trace
5252
"""
53-
self.trace += delta
53+
# act on self.trace.data to ignore the mask and then re-mask when calling _bound_trace
54+
self.trace = np.asarray(self.trace.data) + delta
5455
self._bound_trace()
5556

5657
def _bound_trace(self):

0 commit comments

Comments
 (0)