Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make peak_local_max return indices sorted, always #2435

Merged
merged 3 commits into from
Jan 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 44 additions & 44 deletions skimage/feature/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,30 +147,30 @@ def blob_dog(image, min_sigma=1, max_sigma=50, sigma_ratio=1.6, threshold=2.0,
--------
>>> from skimage import data, feature
>>> feature.blob_dog(data.coins(), threshold=.5, max_sigma=40)
array([[ 45. , 336. , 16.777216],
[ 52. , 155. , 16.777216],
[ 52. , 216. , 16.777216],
[ 54. , 42. , 16.777216],
[ 54. , 276. , 10.48576 ],
[ 58. , 100. , 10.48576 ],
[ 120. , 272. , 16.777216],
array([[ 120. , 272. , 16.777216],
[ 193. , 213. , 16.777216],
[ 263. , 245. , 16.777216],
[ 185. , 347. , 16.777216],
[ 128. , 154. , 10.48576 ],
[ 198. , 155. , 10.48576 ],
[ 124. , 337. , 10.48576 ],
[ 45. , 336. , 16.777216],
[ 195. , 102. , 16.777216],
[ 125. , 45. , 16.777216],
[ 125. , 208. , 10.48576 ],
[ 127. , 102. , 10.48576 ],
[ 128. , 154. , 10.48576 ],
[ 185. , 347. , 16.777216],
[ 193. , 213. , 16.777216],
[ 261. , 173. , 16.777216],
[ 194. , 277. , 16.777216],
[ 195. , 102. , 16.777216],
[ 127. , 102. , 10.48576 ],
[ 125. , 208. , 10.48576 ],
[ 267. , 115. , 10.48576 ],
[ 263. , 302. , 16.777216],
[ 196. , 43. , 10.48576 ],
[ 198. , 155. , 10.48576 ],
[ 260. , 46. , 16.777216],
[ 261. , 173. , 16.777216],
[ 263. , 245. , 16.777216],
[ 263. , 302. , 16.777216],
[ 267. , 115. , 10.48576 ],
[ 267. , 359. , 16.777216]])
[ 267. , 359. , 16.777216],
[ 54. , 276. , 10.48576 ],
[ 58. , 100. , 10.48576 ],
[ 52. , 155. , 16.777216],
[ 52. , 216. , 16.777216],
[ 54. , 42. , 16.777216]])

Notes
-----
Expand Down Expand Up @@ -262,23 +262,23 @@ def blob_log(image, min_sigma=1, max_sigma=50, num_sigma=10, threshold=.2,
>>> img = data.coins()
>>> img = exposure.equalize_hist(img) # improves detection
>>> feature.blob_log(img, threshold = .3)
array([[ 113. , 323. , 1. ],
[ 121. , 272. , 17.33333333],
[ 124. , 336. , 11.88888889],
[ 126. , 46. , 11.88888889],
[ 126. , 208. , 11.88888889],
[ 127. , 102. , 11.88888889],
[ 128. , 154. , 11.88888889],
[ 185. , 344. , 17.33333333],
array([[ 124. , 336. , 11.88888889],
[ 198. , 155. , 11.88888889],
[ 194. , 213. , 17.33333333],
[ 121. , 272. , 17.33333333],
[ 263. , 244. , 17.33333333],
[ 194. , 276. , 17.33333333],
[ 197. , 44. , 11.88888889],
[ 198. , 103. , 11.88888889],
[ 198. , 155. , 11.88888889],
[ 266. , 115. , 11.88888889],
[ 128. , 154. , 11.88888889],
[ 260. , 174. , 17.33333333],
[ 263. , 244. , 17.33333333],
[ 198. , 103. , 11.88888889],
[ 126. , 208. , 11.88888889],
[ 127. , 102. , 11.88888889],
[ 263. , 302. , 17.33333333],
[ 266. , 115. , 11.88888889]])
[ 197. , 44. , 11.88888889],
[ 185. , 344. , 17.33333333],
[ 126. , 46. , 11.88888889],
[ 113. , 323. , 1. ]])

Notes
-----
Expand Down Expand Up @@ -370,23 +370,23 @@ def blob_doh(image, min_sigma=1, max_sigma=30, num_sigma=10, threshold=0.01,
>>> from skimage import data, feature
>>> img = data.coins()
>>> feature.blob_doh(img)
array([[ 121. , 271. , 30. ],
[ 123. , 44. , 23.55555556],
[ 123. , 205. , 20.33333333],
array([[ 197. , 153. , 20.33333333],
[ 124. , 336. , 20.33333333],
[ 126. , 101. , 20.33333333],
[ 126. , 153. , 20.33333333],
[ 156. , 302. , 30. ],
[ 185. , 348. , 30. ],
[ 195. , 100. , 23.55555556],
[ 192. , 212. , 23.55555556],
[ 121. , 271. , 30. ],
[ 126. , 101. , 20.33333333],
[ 193. , 275. , 23.55555556],
[ 195. , 100. , 23.55555556],
[ 197. , 44. , 20.33333333],
[ 197. , 153. , 20.33333333],
[ 260. , 173. , 30. ],
[ 262. , 243. , 23.55555556],
[ 123. , 205. , 20.33333333],
[ 270. , 363. , 30. ],
[ 265. , 113. , 23.55555556],
[ 270. , 363. , 30. ]])
[ 262. , 243. , 23.55555556],
[ 185. , 348. , 30. ],
[ 156. , 302. , 30. ],
[ 123. , 44. , 23.55555556],
[ 260. , 173. , 30. ],
[ 197. , 44. , 20.33333333]])

Notes
-----
Expand Down
6 changes: 3 additions & 3 deletions skimage/feature/corner.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,10 +903,10 @@ def corner_peaks(image, min_distance=1, threshold_abs=None, threshold_rel=0.1,
[ 0., 0., 1., 1., 0.],
[ 0., 0., 0., 0., 0.]])
>>> peak_local_max(response)
array([[2, 2],
[2, 3],
array([[3, 3],
[3, 2],
[3, 3]])
[2, 3],
[2, 2]])
>>> corner_peaks(response)
array([[2, 2]])

Expand Down
19 changes: 10 additions & 9 deletions skimage/feature/peak.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ def _get_high_intensity_peaks(image, mask, num_peaks):
"""
Return the highest intensity peak coordinates.
"""
# get coordinates of peaks
coord = np.nonzero(mask)
# select num_peaks peaks

intensities = image[coord]
sorted_indices = np.argsort(intensities)[::-1]

# num_peaks might be np.inf, so this check is required
if len(coord[0]) > num_peaks:
intensities = image[coord]
idx_maxsort = np.argsort(intensities)
coord = np.transpose(coord)[idx_maxsort][-num_peaks:]
else:
coord = np.column_stack(coord)
return coord
sorted_indices = sorted_indices[:num_peaks]

return np.transpose(coord)[sorted_indices]


def peak_local_max(image, min_distance=1, threshold_abs=None,
Expand Down Expand Up @@ -72,7 +72,8 @@ def peak_local_max(image, min_distance=1, threshold_abs=None,
-------
output : ndarray or ndarray of bools

* If `indices = True` : (row, column, ...) coordinates of peaks.
* If `indices = True` : (row, column, ...) coordinates of peaks sorted
decreasingly w.r.t. the corresponding intensities.
* If `indices = False` : Boolean array shaped like `image`, with peaks
represented by True values.

Expand Down
40 changes: 28 additions & 12 deletions skimage/feature/tests/test_peak.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ def test_flat_peak():
assert len(peaks) == 4


def test_sorted_peaks():
image = np.zeros((5, 5), dtype=np.uint8)
image[1, 1] = 20
image[3, 3] = 10
peaks = peak.peak_local_max(image, min_distance=1)
assert peaks.tolist() == [[1, 1], [3, 3]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe test something more interesting here? For example,

image = np.zeros((3, 10))
image[1, (1, 3, 5, 7)] = (1, 3, 2, 4)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@soupault Done.


image = np.zeros((3, 10))
image[1, (1, 3, 5, 7)] = (1, 3, 2, 4)
peaks = peak.peak_local_max(image, min_distance=1)
assert peaks.tolist() == [[1, 7], [1, 3], [1, 5], [1, 1]]


def test_num_peaks():
image = np.zeros((7, 7), dtype=np.uint8)
image[1, 1] = 10
Expand Down Expand Up @@ -165,11 +178,12 @@ def test_indices_with_labels():
for jmin, jmax in ((0, 30), (30, 60)):
expected[imin:imax, jmin:jmax] = ndi.maximum_filter(
image[imin:imax, jmin:jmax], footprint=footprint)
expected = (expected == image)
expected = np.transpose(np.nonzero(expected == image))
expected = expected[np.argsort(image[tuple(expected.T)])[::-1]]
result = peak.peak_local_max(image, labels=labels, min_distance=1,
threshold_rel=0, footprint=footprint,
indices=True, exclude_border=False)
assert (result == np.transpose(expected.nonzero())).all()
assert (result == expected).all()


def test_ndarray_indices_false():
Expand Down Expand Up @@ -357,11 +371,12 @@ def test_3D():
[[15, 15, 15]])
assert_equal(peak.peak_local_max(image, min_distance=6, threshold_rel=0),
[[15, 15, 15]])
assert_equal(peak.peak_local_max(image, min_distance=10, threshold_rel=0,
exclude_border=False),
[[5, 5, 5], [15, 15, 15]])
assert_equal(peak.peak_local_max(image, min_distance=5, threshold_rel=0),
[[5, 5, 5], [15, 15, 15]])
assert sorted(peak.peak_local_max(image, min_distance=10, threshold_rel=0,
exclude_border=False).tolist()) == \
[[5, 5, 5], [15, 15, 15]]
assert sorted(peak.peak_local_max(image, min_distance=5,
threshold_rel=0).tolist()) == \
[[5, 5, 5], [15, 15, 15]]


def test_4D():
Expand All @@ -372,11 +387,12 @@ def test_4D():
[[15, 15, 15, 15]])
assert_equal(peak.peak_local_max(image, min_distance=6, threshold_rel=0),
[[15, 15, 15, 15]])
assert_equal(peak.peak_local_max(image, min_distance=10, threshold_rel=0,
exclude_border=False),
[[5, 5, 5, 5], [15, 15, 15, 15]])
assert_equal(peak.peak_local_max(image, min_distance=5, threshold_rel=0),
[[5, 5, 5, 5], [15, 15, 15, 15]])
assert sorted(peak.peak_local_max(image, min_distance=10, threshold_rel=0,
exclude_border=False).tolist()) == \
[[5, 5, 5, 5], [15, 15, 15, 15]]
assert sorted(peak.peak_local_max(image, min_distance=5,
threshold_rel=0).tolist()) == \
[[5, 5, 5, 5], [15, 15, 15, 15]]


def test_threshold_rel_default():
Expand Down