Skip to content

Commit

Permalink
Add border_width checks
Browse files Browse the repository at this point in the history
  • Loading branch information
larrybradley committed Oct 24, 2024
1 parent dcfe917 commit b54519c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 9 additions & 5 deletions photutils/detection/peakfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


def find_peaks(data, threshold, *, box_size=3, footprint=None, mask=None,
border_width=None, npeaks=np.inf, centroid_func=None,
border_width=0, npeaks=np.inf, centroid_func=None,
error=None, wcs=None):
"""
Find local peaks in an image that are above a specified threshold
Expand Down Expand Up @@ -74,9 +74,9 @@ def find_peaks(data, threshold, *, box_size=3, footprint=None, mask=None,
A boolean mask with the same shape as ``data``, where a `True`
value indicates the corresponding element of ``data`` is masked.
border_width : bool, optional
border_width : int, optional
The width in pixels to exclude around the border of the
``data``.
``data``. Must be an non-negative integer.
npeaks : int, optional
The maximum number of peaks to return. When the number of
Expand Down Expand Up @@ -126,7 +126,6 @@ def find_peaks(data, threshold, *, box_size=3, footprint=None, mask=None,
arrays, unit = process_quantities((data, threshold, error),
('data', 'threshold', 'error'))
data, threshold, error = arrays

data = np.asanyarray(data)

if np.all(data == data.flat[0]):
Expand All @@ -140,6 +139,11 @@ def find_peaks(data, threshold, *, box_size=3, footprint=None, mask=None,
raise ValueError('A threshold array must have the same shape as '
'the input data.')

if border_width < 0:
raise ValueError('border_width must be a non-negative integer.')
if int(border_width) != border_width:
raise ValueError('border_width must be an integer.')

# remove NaN values to avoid runtime warnings
nan_mask = np.isnan(data)
if np.any(nan_mask):
Expand All @@ -161,7 +165,7 @@ def find_peaks(data, threshold, *, box_size=3, footprint=None, mask=None,
raise ValueError('data and mask must have the same shape')
peak_goodmask = np.logical_and(peak_goodmask, ~mask)

if border_width is not None:
if border_width > 0:
for i in range(peak_goodmask.ndim):
peak_goodmask = peak_goodmask.swapaxes(0, i)
peak_goodmask[:border_width] = False
Expand Down
7 changes: 7 additions & 0 deletions photutils/detection/tests/test_peakfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ def test_border_width(self, data):
tbl1 = find_peaks(data, 0.1, box_size=3, border_width=25)
assert len(tbl1) < len(tbl0)

match = 'border_width must be a non-negative integer'
with pytest.raises(ValueError, match=match):
find_peaks(data, 0.1, box_size=3, border_width=-1)
match = 'border_width must be an integer'
with pytest.raises(ValueError, match=match):
find_peaks(data, 0.1, box_size=3, border_width=3.1)

def test_box_size_int(self, data):
"""
Test non-integer box_size.
Expand Down

0 comments on commit b54519c

Please sign in to comment.