python - Fitting a 2D Gaussian function using scipy.optimize.curve_fit - ValueError and minpack.error -


i intend fit 2d gaussian function images showing laser beam parameters fwhm , position. far tried understand how define 2d gaussian function in python , how pass x , y variables it.

i've written little script defines function, plots it, adds noise , tries fit using curve_fit. seems work except last step in try fit model function noisy data. here code:

import scipy.optimize opt import numpy np import pylab plt   #define model function , pass independant variables x , y list def twod_gaussian((x,y), amplitude, xo, yo, sigma_x, sigma_y, theta, offset):     xo = float(xo)     yo = float(yo)         = (np.cos(theta)**2)/(2*sigma_x**2) + (np.sin(theta)**2)/(2*sigma_y**2)     b = -(np.sin(2*theta))/(4*sigma_x**2) + (np.sin(2*theta))/(4*sigma_y**2)     c = (np.sin(theta)**2)/(2*sigma_x**2) + (np.cos(theta)**2)/(2*sigma_y**2)     return offset + amplitude*np.exp( - (a*((x-xo)**2) + 2*b*(x-xo)*(y-yo) + c*((y-yo)**2)))  # create x , y indices x = np.linspace(0, 200, 201) y = np.linspace(0, 200, 201) x,y = np.meshgrid(x, y)  #create data data = twod_gaussian((x, y), 3, 100, 100, 20, 40, 0, 10)  # plot twod_gaussian data generated above plt.figure() plt.imshow(data) plt.colorbar()  # add noise data , try fit data generated beforehand initial_guess = (3,100,100,20,40,0,10)  data_noisy = data + 0.2*np.random.normal(size=len(x))  popt, pcov = opt.curve_fit(twod_gaussian, (x,y), data_noisy, p0 = initial_guess) 

here error message when running script using winpython 64-bit python 2.7:

valueerror: object deep desired array traceback (most recent call last):   file "<stdin>", line 1, in <module>   file "c:\python\winpython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile     execfile(filename, namespace)   file "e:/work computer/software/python/fitting scripts/2d gaussian function fit/2d_gaussian_levmarq_v2.py", line 39, in <module>     popt, pcov = opt.curve_fit(twod_gaussian, (x,y), data_noisy, p0 = initial_guess)   file "c:\python\winpython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\scipy\optimize\minpack.py", line 533, in curve_fit     res = leastsq(func, p0, args=args, full_output=1, **kw)   file "c:\python\winpython-64bit-2.7.6.2\python-2.7.6.amd64\lib\site-packages\scipy\optimize\minpack.py", line 378, in leastsq     gtol, maxfev, epsfcn, factor, diag) minpack.error: result function call not proper array of floats. 

what doing wrong? how pass independent variables model function/curve_fit?

the output of twod_gaussian needs 1d. can add .ravel() onto end of last line, this:

def twod_gaussian((x, y), amplitude, xo, yo, sigma_x, sigma_y, theta, offset):     xo = float(xo)     yo = float(yo)         = (np.cos(theta)**2)/(2*sigma_x**2) + (np.sin(theta)**2)/(2*sigma_y**2)     b = -(np.sin(2*theta))/(4*sigma_x**2) + (np.sin(2*theta))/(4*sigma_y**2)     c = (np.sin(theta)**2)/(2*sigma_x**2) + (np.cos(theta)**2)/(2*sigma_y**2)     g = offset + amplitude*np.exp( - (a*((x-xo)**2) + 2*b*(x-xo)*(y-yo)                              + c*((y-yo)**2)))     return g.ravel() 

you'll need reshape output plotting, e.g:

# create x , y indices x = np.linspace(0, 200, 201) y = np.linspace(0, 200, 201) x, y = np.meshgrid(x, y)  #create data data = twod_gaussian((x, y), 3, 100, 100, 20, 40, 0, 10)  # plot twod_gaussian data generated above plt.figure() plt.imshow(data.reshape(201, 201)) plt.colorbar() 

do fitting before:

# add noise data , try fit data generated beforehand initial_guess = (3,100,100,20,40,0,10)  data_noisy = data + 0.2*np.random.normal(size=data.shape)  popt, pcov = opt.curve_fit(twod_gaussian, (x, y), data_noisy, p0=initial_guess) 

and plot results:

data_fitted = twod_gaussian((x, y), *popt)  fig, ax = plt.subplots(1, 1) ax.hold(true) ax.imshow(data_noisy.reshape(201, 201), cmap=plt.cm.jet, origin='bottom',     extent=(x.min(), x.max(), y.min(), y.max())) ax.contour(x, y, data_fitted.reshape(201, 201), 8, colors='w') plt.show() 

enter image description here


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -