From f77def35d897f49b963e87f286ce340ff9ebba2c Mon Sep 17 00:00:00 2001 From: yangzetian Date: Mon, 1 Apr 2013 20:34:47 +0800 Subject: [PATCH 1/5] Fix peak_local_max's output for ndarray when indices is set to False --- skimage/feature/peak.py | 3 ++- skimage/feature/tests/test_peak.py | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/skimage/feature/peak.py b/skimage/feature/peak.py index 9c7a934f..fb6e3465 100644 --- a/skimage/feature/peak.py +++ b/skimage/feature/peak.py @@ -150,5 +150,6 @@ def peak_local_max(image, min_distance=10, threshold_abs=0, threshold_rel=0.1, if indices is True: return coordinates else: - out[coordinates[:, 0], coordinates[:, 1]] = True + nd_indices = tuple(coordinates.T.tolist()) + out[nd_indices] = True return out diff --git a/skimage/feature/tests/test_peak.py b/skimage/feature/tests/test_peak.py index 3ef1f12d..46854c41 100644 --- a/skimage/feature/tests/test_peak.py +++ b/skimage/feature/tests/test_peak.py @@ -116,6 +116,12 @@ def test_indices_with_labels(): indices=True, exclude_border=False) assert (result == np.transpose(expected.nonzero())).all() +def test_ndarray_indices_false(): + nd_image = np.zeros((5,5,5)) + nd_image[2,2,2] = 1 + peaks = peak.peak_local_max(nd_image, min_distance=1, indices=False) + assert (peaks == nd_image.astype(np.bool)).all() + if __name__ == '__main__': from numpy import testing From afde2b8ab8d937aa19394497a88792ac21b59149 Mon Sep 17 00:00:00 2001 From: yangzetian Date: Tue, 2 Apr 2013 12:35:11 +0800 Subject: [PATCH 2/5] change doc for indices and remove the unnecessarily tolist() operation --- skimage/feature/peak.py | 6 +++--- skimage/feature/tests/test_peak.py | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/skimage/feature/peak.py b/skimage/feature/peak.py index fb6e3465..1eadfc68 100644 --- a/skimage/feature/peak.py +++ b/skimage/feature/peak.py @@ -33,8 +33,8 @@ def peak_local_max(image, min_distance=10, threshold_abs=0, threshold_rel=0.1, If True, `min_distance` excludes peaks from the border of the image as well as from each other. indices : bool - If True, the output will be a matrix representing peak coordinates. - If False, the output will be a boolean matrix shaped as `image.shape` + If True, the output will be an array representing peak coordinates. + If False, the output will be a boolean array shaped as `image.shape` with peaks present at True elements. num_peaks : int Maximum number of peaks. When the number of peaks exceeds `num_peaks`, @@ -150,6 +150,6 @@ def peak_local_max(image, min_distance=10, threshold_abs=0, threshold_rel=0.1, if indices is True: return coordinates else: - nd_indices = tuple(coordinates.T.tolist()) + nd_indices = tuple(coordinates.T) out[nd_indices] = True return out diff --git a/skimage/feature/tests/test_peak.py b/skimage/feature/tests/test_peak.py index 46854c41..ae04d432 100644 --- a/skimage/feature/tests/test_peak.py +++ b/skimage/feature/tests/test_peak.py @@ -116,12 +116,13 @@ def test_indices_with_labels(): indices=True, exclude_border=False) assert (result == np.transpose(expected.nonzero())).all() + def test_ndarray_indices_false(): nd_image = np.zeros((5,5,5)) nd_image[2,2,2] = 1 peaks = peak.peak_local_max(nd_image, min_distance=1, indices=False) assert (peaks == nd_image.astype(np.bool)).all() - + if __name__ == '__main__': from numpy import testing From d9e6b509c5c79ad2d85caf64049978fbf8d1762b Mon Sep 17 00:00:00 2001 From: Juan Nunez-Iglesias Date: Wed, 3 Apr 2013 17:56:04 +1100 Subject: [PATCH 3/5] Make `exclude_borders` option in peak_local_max nD Prior modifications to peak_local_max make it nd-aware, but this does not extend to the exclude_borders option. This commit changes that. --- skimage/feature/peak.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/skimage/feature/peak.py b/skimage/feature/peak.py index 1eadfc68..574d3e24 100644 --- a/skimage/feature/peak.py +++ b/skimage/feature/peak.py @@ -130,11 +130,12 @@ def peak_local_max(image, min_distance=10, threshold_abs=0, threshold_rel=0.1, image *= mask if exclude_border: - # Remove the image borders - image[:min_distance] = 0 - image[-min_distance:] = 0 - image[:, :min_distance] = 0 - image[:, -min_distance:] = 0 + # zero out the image borders + for i in xrange(image.ndim): + image = image.swapaxes(0, i) + image[:min_distance] = 0 + image[-min_distance:] = 0 + image = image.swapaxes(0, i) # find top peak candidates above a threshold peak_threshold = max(np.max(image.ravel()) * threshold_rel, threshold_abs) From 6e4ec78de2c44c9323aec39d58041166b1799d6c Mon Sep 17 00:00:00 2001 From: Juan Nunez-Iglesias Date: Wed, 3 Apr 2013 18:09:21 +1100 Subject: [PATCH 4/5] Use range instead of xrange --- 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 574d3e24..3268831a 100644 --- a/skimage/feature/peak.py +++ b/skimage/feature/peak.py @@ -131,7 +131,7 @@ def peak_local_max(image, min_distance=10, threshold_abs=0, threshold_rel=0.1, if exclude_border: # zero out the image borders - for i in xrange(image.ndim): + for i in range(image.ndim): image = image.swapaxes(0, i) image[:min_distance] = 0 image[-min_distance:] = 0 From da055e7ca38d6c877cac02cce130100ee1b13b97 Mon Sep 17 00:00:00 2001 From: yangzetian Date: Wed, 3 Apr 2013 19:21:04 +0800 Subject: [PATCH 5/5] Add test for nD `exclude_border` option --- skimage/feature/tests/test_peak.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/skimage/feature/tests/test_peak.py b/skimage/feature/tests/test_peak.py index ae04d432..39dc8d8f 100644 --- a/skimage/feature/tests/test_peak.py +++ b/skimage/feature/tests/test_peak.py @@ -124,6 +124,17 @@ def test_ndarray_indices_false(): assert (peaks == nd_image.astype(np.bool)).all() +def test_ndarray_exclude_border(): + nd_image = np.zeros((5,5,5)) + nd_image[[1,0,0],[0,1,0],[0,0,1]] = 1 + nd_image[3,0,0] = 1 + nd_image[2,2,2] = 1 + expected = np.zeros_like(nd_image, dtype=np.bool) + expected[2,2,2] = True + result = peak.peak_local_max(nd_image, min_distance=2, indices=False) + assert (result == expected).all() + + if __name__ == '__main__': from numpy import testing testing.run_module_suite()