These routines / classes provide a method for fitting of data using mostly least squares methods. There are two main methods here. The pyspec.fit.fit class provides a class for fitting of data. The pyspec.fit.fitdata subroutine serves as a wrapper around the pyspec.fit.fit class. Here the data is taken from the current selected figure. Basically this means that you can fit any set of data which is on the current figure.
General class to perform non-linear least squares fitting of data.
This class serves as a wrapper around various fit methods, requiring a standard function to allow for easy fitting of data.
- x : ndarray
- x data
- y : ndarray
- y data
- e : ndarray
- y error
- funcs : list
- A list of functions to be fitted. These functions should be formed like the standard fitting functions in the pyspec.fitfuncs.
- guess : array or string
- Array of the initial guess, or string for guess type:
- ‘auto’ : Auto guess of parameters
- quiet : bool
- If True then don’t print to the console any results.
- ifix : ndarray
- An array containing a ‘1’ for fixed parameters
- xlimits : ndarray
- An (n x 2) array of the limits in x to fit.
- xlimitstype : string
- Either ‘world’ or ‘index’
- optimiser : string :
- Optimizer to use for fit: ‘mpfit’, ‘leastsq’, ‘ODR’
- interactive : bool
- True/False launch interactive fitting mode
- r2min : float
- If r^2 is less than this value, drop into interactive mode
Return the chi-squared value for the fit


where
, where P is the number of parameters.
If dist is ‘poisson’ then the data is divided by the model answer. i.e.
chi-squared value
Evaluate the fit functions with the fesult of a fit
returns f(x) : ndarray
Start the fit
Calculate the sandard deviation of the residuals
Start the fit
Return a string containing the text results of the fit
Force fitting of a function to graphed data
All the additional *args and **kwargs are passed onto the fit class (see class pyspec.fit.fit for details).
pyspec.fit.fit instance with result.
Single constant value

Gaussian defined by amplitide

Linear (strait line)

Lorentzian defined by amplitude

Lorentzian squared defined by amplitude

by the relation:
Lorentzian squared function defined by area
Lorentzian defined by area

Guess the “vital statistics” of a peak.
This function guesses the peak’s center width, integral, height and linear background (m, c) from the x and y data passed to it.
Power function

Pseudo Voight function
User defined functions can easily be written. An example of a fit function to provide a linear (straight line) is shown below:
def linear(x, p, mode='eval'):
if mode == 'eval':
out = (p[0] * x) + p[1]
elif mode == 'params':
out = ['grad','offset']
elif mode == 'name':
out = "Linear"
elif mode == 'guess':
g = peakguess(x, p)
out = g[4:6]
else:
out = []
return out
This function is called by the fitting routine each time the function needs to be evaluated. It is also called other times, to gain information about the function. The mode parameter is used to let the function know what is required. The default value of mode should be set to 'eval'. The possible values of mode are discussed below
from x and p, where p is the parameters
through
the second parameter p. The function should return it’s best
guess based on the experimental data, and return those
parameters. The peakguess function is provided for convenience
which can guess the vital statistics of a peak from the
data. This option is not essential but if it is not implemented
then the user must provide a numerical guess to the fit class.Finally, the default response should be to return a null list [].