From 22ffc20fc44a23450fb312208cc0bb61f3ad1e3d Mon Sep 17 00:00:00 2001 From: kpk09 Date: Sun, 25 Jan 2015 00:15:20 +0000 Subject: [PATCH 1/4] Added broken test for feature.peak_local_max when the image is 3D and num_peaks is set. The current code only holds for 2D images. --- skimage/feature/tests/test_peak.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/skimage/feature/tests/test_peak.py b/skimage/feature/tests/test_peak.py index ec130c66..09e52495 100644 --- a/skimage/feature/tests/test_peak.py +++ b/skimage/feature/tests/test_peak.py @@ -82,6 +82,15 @@ def test_num_peaks(): assert (3, 5) in peaks_limited +def test_num_peaks3D(): + # Issue 1354: the old code only hold for 2D arrays + # and this code would die with IndexError + image = np.zeros((10, 10, 100)) + image[5,5,::5] = np.arange(20) + peaks_limited = peak.peak_local_max(image, min_distance=1, num_peaks=2) + assert len(peaks_limited) == 2 + + def test_reorder_labels(): image = np.random.uniform(size=(40, 60)) i, j = np.mgrid[0:40, 0:60] From c5ac705123f302026dfbc3cc3353a8b3f38154bc Mon Sep 17 00:00:00 2001 From: kpk09 Date: Sun, 25 Jan 2015 00:19:12 +0000 Subject: [PATCH 2/4] Fixed bug in feature.peak_local_max: num_peaks should now be usable for ND arrays. --- skimage/feature/peak.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/feature/peak.py b/skimage/feature/peak.py index e70f6df1..7580efc2 100644 --- a/skimage/feature/peak.py +++ b/skimage/feature/peak.py @@ -149,7 +149,7 @@ def peak_local_max(image, min_distance=10, threshold_abs=0, threshold_rel=0.1, coordinates = np.transpose((image > peak_threshold).nonzero()) if coordinates.shape[0] > num_peaks: - intensities = image[coordinates[:, 0], coordinates[:, 1]] + intensities = image.flat[np.ravel_multi_index(coordinates.transpose(),image.shape)] idx_maxsort = np.argsort(intensities)[::-1] coordinates = coordinates[idx_maxsort][:num_peaks] From 305f5fd2259a5f1196a44ba5e0c10ca713f17aca Mon Sep 17 00:00:00 2001 From: kpk09 Date: Sun, 25 Jan 2015 00:22:11 +0000 Subject: [PATCH 3/4] Using numpy.argwhere makes code easier to read --- skimage/feature/peak.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/feature/peak.py b/skimage/feature/peak.py index 7580efc2..599a6e58 100644 --- a/skimage/feature/peak.py +++ b/skimage/feature/peak.py @@ -146,7 +146,7 @@ def peak_local_max(image, min_distance=10, threshold_abs=0, threshold_rel=0.1, peak_threshold = max(np.max(image.ravel()) * threshold_rel, threshold_abs) # get coordinates of peaks - coordinates = np.transpose((image > peak_threshold).nonzero()) + coordinates = np.argwhere(image > peak_threshold) if coordinates.shape[0] > num_peaks: intensities = image.flat[np.ravel_multi_index(coordinates.transpose(),image.shape)] From b06420fe6ab0975f880b90e0f81f30e1e648fcbd Mon Sep 17 00:00:00 2001 From: kpk09 Date: Sun, 25 Jan 2015 01:09:36 +0000 Subject: [PATCH 4/4] Added Kevin Keraudren to the list of contributors: test and fix in feature.peak_local_max --- CONTRIBUTORS.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index d57b6015..92ac35b3 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -202,4 +202,7 @@ Extended the image labelling implementation so it also works on 3D images. - Salvatore Scaramuzzino - RectTool example \ No newline at end of file + RectTool example + +- Kevin Keraudren + Fix and test for feature.peak_local_max