Created
January 16, 2011 12:16
-
-
Save grmwld/781740 to your computer and use it in GitHub Desktop.
detect local maxima.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
import scipy.ndimage as ndimage | |
import numpy | |
def local_maxima(array, min_distance = 1, periodic=False, edges_allowed=True): | |
"""Find all local maxima of the array, separated by at least min_distance.""" | |
array = numpy.asarray(array) | |
cval = 0 | |
if periodic: | |
mode = 'wrap' | |
elif edges_allowed: | |
mode = 'nearest' | |
else: | |
mode = 'constant' | |
cval = array.max()+1 | |
max_points = array == ndimage.maximum_filter(array, 1+2*min_distance, mode=mode, cval=cval) | |
return [indices[max_points] for indices in numpy.indices(array.shape)] | |
s = [1, 4, 6, 8, 3, 2, 0] | |
d = [2, 4, 6, 8, 23, 6, 4, 2, 5, 7, 8, 9, 4, 2, 0] | |
print local_maxima(s) | |
print local_maxima(d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can i do this for a matrix?