I'm working on an image analysis program, and I narrowed my bottleneck by trying to place 2D Gauss many times in a small window (20x20) pixels. 90% of the execution time is spent on this code.
I am using the code provided in the scipy cookbook for this problem:
def gaussian(height, center_x, center_y, width_x, width_y): """Returns a gaussian function with the given parameters""" width_x = float(width_x) width_y = float(width_y) return lambda x,y: height*exp( -(((center_x-x)/width_x)**2+((center_y-y)/width_y)**2)/2) def moments(data): """Returns (height, x, y, width_x, width_y) the gaussian parameters of a 2D distribution by calculating its moments """ total = data.sum() X, Y = indices(data.shape) x = (X*data).sum()/total y = (Y*data).sum()/total col = data[:, int(y)] width_x = sqrt(abs((arange(col.size)-y)**2*col).sum()/col.sum()) row = data[int(x), :] width_y = sqrt(abs((arange(row.size)-x)**2*row).sum()/row.sum()) height = data.max() return height, x, y, width_x, width_y def fitgaussian(data): """Returns (height, x, y, width_x, width_y) the gaussian parameters of a 2D distribution found by a fit""" params = moments(data) errorfunction = lambda p: ravel(gaussian(*p)(*indices(data.shape)) - data) p, success = optimize.leastsq(errorfunction, params, maxfev=50, ftol=1.49012e-05) return p
I managed to cut the execution time in half by combining the errorfunction () and gaussian () functions, so every time the lessfqq () function calls the errorfunction () function, instead of two, one function call is called.
This makes me think that most of the remaining runtime is spent on function overhead, since the least squares () algorithm calls the errorfunction () function.
Is there a way to reduce this overhead? I'm at a loss as lesssq () takes a function as input.
I apologize in advance if my description is confusing, I'm a mechanical engineer, studying, and I learn Python when I go. Please let me know if there is any other information that would be helpful.