Hi, I would like to compute local extrema of an array (2D/3D), ie get a list of points (coords + value). How could I do this? Any hint? TIA. Cheers, -- Fred
I would like to compute local extrema of an array (2D/3D), ie get a list of points (coords + value).
How could I do this?
Any hint?
A sort of old-school image-processing way of doing this is to calculate the maximum of a moving neighborhood (maximum filter: scipy.ndimage.maximum_filter), and then look to see where the original array is equal to the maximum in the neighborhood. maxima = (a == scipy.ndimage.maximum_filter(a, 4)) will give a boolean array the same shape as a, with True in the positions where the points are the local maxima in a 4x4 (or 4x4x4, depending on if a is 2d or 3d, etc.) square window. You can use numpy.nonzero to get the coordinates out of the 'maxima' array, and can use fancy indexing (a[maxima] or a[numpy.nonzero(maxima])) to pull out the values. Obviously this approach is a bit flaky -- boundary conditions can be a bit tricky, depending on what you want; also this it fails on plateaux, determining everything to be a maximum. Plus you have to choose a window size... this can be helpful for suppressing extrema produced by noise, though. A 3x3 window will give the most fine- grained account of the extremal (and plateau / saddle) points. Zach
Hi, You could create multiple thresholds along one axis of the array, and get all elements above each threshold value (=region). You build a region-stack from this; a 2D/3D boolean array that says which elements are above the threshold value for each threshold you used. If a region contains a region in a higher threshold value level (higher up in the stack) that means you haven't found the local maximum yet; if it doesn't, that is your top. This method would allow flats to be detected as well. The last thing you have to do is: for every remaining region that is a local maximum, you compute the coordinates and value as the mean of all coordinates or values contained in the region. Hope this helps Martin 2009/8/26 fred <fredmfp@gmail.com>:
Hi,
I would like to compute local extrema of an array (2D/3D), ie get a list of points (coords + value).
How could I do this?
Any hint?
TIA.
Cheers,
-- Fred _______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
Here's what I wrote for finding extrema of a 1D array def extrema(signal, hilow="Hi"): ''' hilow = "Hi", "Low", or "Both" depending on whether you want upper or lower extrema, or all of them. ''' a = np.sign(np.diff(signal)) zerolocs = np.transpose(np.where( (a[1:]+a[0:-1]==0.) + (a==0.)[0:-1] )).flatten() + 1 zerolocs = zerolocs[zerolocs>=a.argmax()] # remove leading zeros if zerolocs[0] < 1: zerolocs = zerolocs[1:] if zerolocs[-1]>len(a)-1: zerolocs = zerolocs[0:-1] if hilow == "Low" : return zerolocs[np.where(a[zerolocs] >0)] elif hilow == "Hi" : return zerolocs[np.where(a[zerolocs] <=0)] else : return zerolocs
Hi,
You could create multiple thresholds along one axis of the array, and get all elements above each threshold value (=region). You build a region-stack from this; a 2D/3D boolean array that says which elements are above the threshold value for each threshold you used. If a region contains a region in a higher threshold value level (higher up in the stack) that means you haven't found the local maximum yet; if it doesn't, that is your top.
This method would allow flats to be detected as well.
The last thing you have to do is: for every remaining region that is a local maximum, you compute the coordinates and value as the mean of all coordinates or values contained in the region.
Hope this helps Martin
2009/8/26 fred <fredmfp@gmail.com>:
Hi,
I would like to compute local extrema of an array (2D/3D), ie get a list of points (coords + value).
How could I do this?
Any hint?
TIA.
Cheers,
-- Fred _______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
_______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
-- ----------------------------------------------------------------------- | Alan K. Jackson | To see a World in a Grain of Sand | | alan@ajackson.org | And a Heaven in a Wild Flower, | | www.ajackson.org | Hold Infinity in the palm of your hand | | Houston, Texas | And Eternity in an hour. - Blake | -----------------------------------------------------------------------
Hi Fred, you could try something along these lines (assuming your data is called 'data' and numpy is imported as np): d1 = np.diff(np.sign(np.diff(data)) maxima_indices = np.where(d1 < -.5) + 1 minima_indices = np.where(d1 > .5) + 1 you can then get the values using the indices on your original data. best wishes, David ----- Original Message ---- From: fred <fredmfp@gmail.com> To: SciPy Users List <scipy-user@scipy.org> Sent: Wednesday, 26 August, 2009 11:32:48 PM Subject: [SciPy-User] computing local extrema Hi, I would like to compute local extrema of an array (2D/3D), ie get a list of points (coords + value). How could I do this? Any hint? TIA. Cheers, -- Fred _______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
Oops, overlooked the 2D/3D part, but the idea should be able to be generalised if you replace the 'diff' with a convolution with a differential kernel. eg: scipy.ndimage.convolve(data, array([[0, -.5, 0], [-.5, 1, 0], [0,0,0]])) David ----- Original Message ---- From: David Baddeley <david_baddeley@yahoo.com.au> To: SciPy Users List <scipy-user@scipy.org> Sent: Thursday, 27 August, 2009 9:41:06 AM Subject: Re: [SciPy-User] computing local extrema Hi Fred, you could try something along these lines (assuming your data is called 'data' and numpy is imported as np): d1 = np.diff(np.sign(np.diff(data)) maxima_indices = np.where(d1 < -.5) + 1 minima_indices = np.where(d1 > .5) + 1 you can then get the values using the indices on your original data. best wishes, David ----- Original Message ---- From: fred <fredmfp@gmail.com> To: SciPy Users List <scipy-user@scipy.org> Sent: Wednesday, 26 August, 2009 11:32:48 PM Subject: [SciPy-User] computing local extrema Hi, I would like to compute local extrema of an array (2D/3D), ie get a list of points (coords + value). How could I do this? Any hint? TIA. Cheers, -- Fred _______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user _______________________________________________ SciPy-User mailing list SciPy-User@scipy.org http://mail.scipy.org/mailman/listinfo/scipy-user
I'm back to this thread. Just to say I thank you all. I took the Zachary's approach, which fits best my needs. Cheers, -- Fred
participants (5)
-
alan@ajackson.org
-
David Baddeley
-
fred
-
Martin van Leeuwen
-
Zachary Pincus