Deprecate threshold and replace absolute and relative thresholds.

This commit is contained in:
Tony S Yu
2012-04-15 23:30:06 -04:00
parent b9b9cd5c65
commit 187bc6d5b8
2 changed files with 35 additions and 5 deletions
+17 -5
View File
@@ -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
+18
View File
@@ -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()