From 187bc6d5b85766d041f6f18a0691e54709de315c Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Sun, 15 Apr 2012 23:30:06 -0400 Subject: [PATCH] Deprecate threshold and replace absolute and relative thresholds. --- skimage/feature/peak.py | 22 +++++++++++++++++----- skimage/feature/tests/test_peak.py | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/skimage/feature/peak.py b/skimage/feature/peak.py index 225f42cd..3471381b 100644 --- a/skimage/feature/peak.py +++ b/skimage/feature/peak.py @@ -1,8 +1,10 @@ +import warnings import numpy as np from scipy import ndimage -def peak_local_max(image, min_distance=10, threshold=0.1): +def peak_local_max(image, min_distance=10, threshold='deprecated', + threshold_abs=0, threshold_rel=0.1): """Return coordinates of peaks in an image. Peaks are the local maxima in a region of `2 * min_distance + 1` @@ -13,11 +15,17 @@ def peak_local_max(image, min_distance=10, threshold=0.1): image: ndarray of floats Input image. - min_distance: int, optional + min_distance: int Minimum number of pixels separating peaks and image boundary. - threshold: float, optional - Candidate peaks are calculated as `max(image) * threshold`. + threshold : float + Deprecated. See `threshold_rel`. + + threshold_abs: float + Minimum intensity of peaks. + + threshold_rel: float + Minimum intensity of peaks calculated as `max(image) * threshold_rel`. Returns ------- @@ -37,8 +45,12 @@ def peak_local_max(image, min_distance=10, threshold=0.1): image[:, :min_distance] = 0 image[:, -min_distance:] = 0 + if not threshold == 'deprecated': + msg = "`threshold` parameter deprecated; use `threshold_rel instead." + warnings.warn(msg, DeprecationWarning) + threshold_rel = threshold # find top corner candidates above a threshold - corner_threshold = np.max(image.ravel()) * threshold + corner_threshold = max(np.max(image.ravel()) * threshold_rel, threshold_abs) image_t = (image >= corner_threshold) * 1 # get coordinates of peaks diff --git a/skimage/feature/tests/test_peak.py b/skimage/feature/tests/test_peak.py index 48e8f67c..7f54b7cd 100644 --- a/skimage/feature/tests/test_peak.py +++ b/skimage/feature/tests/test_peak.py @@ -1,4 +1,5 @@ import numpy as np +from numpy.testing import assert_array_almost_equal as assert_close from skimage.feature import peak @@ -18,6 +19,23 @@ def test_noisy_peaks(): assert tuple(loc) in peak_locations +def test_relative_threshold(): + image = np.zeros((5, 5), dtype=np.uint8) + image[1, 1] = 10 + image[3, 3] = 21 + peaks = peak.peak_local_max(image, min_distance=1, threshold_rel=0.5) + assert len(peaks) == 1 + assert_close(peaks, [(3, 3)]) + + +def test_absolute_threshold(): + image = np.zeros((5, 5), dtype=np.uint8) + image[1, 1] = 10 + image[3, 3] = 21 + peaks = peak.peak_local_max(image, min_distance=1, threshold_abs=11) + assert len(peaks) == 1 + assert_close(peaks, [(3, 3)]) + if __name__ == '__main__': from numpy import testing testing.run_module_suite()