Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of eperimental data points.
In this exercise we will calculate the sum of the "medipix.edf" image along the vertical and horizontal axis and fit a gaussian function, either using scipy or the module from PyMca.
In [1]:
%pylab inline
In [2]:
import fabio
img = fabio.open("medipix.edf").data
imshow(np.log(img))
Out[2]:
In [3]:
vert = img.sum(axis=1)
plot(vert)
title("Sum along the horizontal direction")
Out[3]:
The fitting function is a function of the data-point (xdata) and of a sert of parameters.
For a gaussian function defined as: gaussian
In [4]:
def gaussian(x, height, center, sigma, offset):
return height*exp(-(x-center)**2/(2*sigma**2)) + offset
An essential part is the initial set of parameters p0:
In [5]:
p0 = [70000, 140, 10,10000]
xdata = numpy.arange(len(img),dtype="float")
ydata = vert.astype("float")
plot(vert, label="Actual data")
plot(gaussian(xdata, *p0), label="Initial guess")
legend()
title("Before the fit:")
Out[5]:
In [6]:
from scipy.optimize import curve_fit
p,cov = curve_fit(gaussian, xdata, ydata, p0)
print(p)
In [7]:
%timeit p,cov = curve_fit(gaussian, xdata, ydata, p0)
In [8]:
#Display the result of the fit
plot(gaussian(xdata, *p0), label="Initial guess")
plot(gaussian(xdata, *p), label="Fitted curve")
plot(vert,label="Experimental data")
legend()
title("Result of the fit")
Out[8]:
In [9]:
try:
from PyMca5.PyMca import Gefit
except ImportError:
from PyMca import Gefit
PyMca's Levenberg-Marquardt fitting module needs the function to be layed out the other way around:
In [10]:
def gaussian_ge(param, xdata):
return gaussian(xdata, *param)
p, chi2, err = Gefit.LeastSquaresFit(gaussian_ge, p0, xdata=xdata, ydata=ydata)
print(p)
In [11]:
%timeit p, chi2, err = Gefit.LeastSquaresFit(gaussian_ge, p0, xdata=xdata, ydata=ydata)
In [12]:
#Display the result of the fit
plot(gaussian(xdata, *p0), label="Initial guess")
plot(gaussian(xdata, *p), label="Fitted curve")
plot(vert,label="Experimental data")
legend()
title("Result of the fit")
Out[12]:
Curve fitting goes alway via the following steps:
Note that some optimizer don't fit the function but instead minimize the error_function: error(param) = y_data - function(x_data) It is often necessary to look at the documentation of the fitting function