I need to optimize this part of an image processing application.
It is basically the sum of the pixels binned by their distance from the central spot.
d
Taken from a numpy Enhancement Proposal I am working on:
pp.plot(*group_by(np.round(R, 5).flatten()).mean(data.flatten()))
The call to mean returns the unique values in R, and the mean of corresponding values in data over identical values in R.
So not quite the same as a histogram based solution; you don't have to remap to a new grid, which is nice if you want to fit a radial profile, without loss of information. Performance should be slightly better than your original solution. Also, standard deviations can be computed with the same efficiency.
Here is my latest draft numpy group_by EP; not a very concise answer as such, but a very general one. I hope we can all agree numpy needs something like np.group_by(keys).reduce(values); if you have any feedback, it would be welcome.