Data.curve_fit

Data.curve_fit(func, xcol=None, ycol=None, sigma=None, **kargs)

General curve fitting function passed through from scipy.

Parameters:
  • func (callable, lmfit.Model, odr.Model) – The fitting function with the form def f(x,*p) where p is a list of fitting parameters

  • xcol (index, Iterable) – The index of the x-column data to fit. If list or other iterable sends a tuple of x columns to func for N-d fitting.

  • ycol (index, list of indices or array) – The index of the y-column data to fit. If an array, then should be 1D and the same length as the data. If ycol is a list of indices then the columns are iterated over in turn, fitting occurring for each one. In this case the return value is a list of what would be returned for a single column fit.

Keyword Arguments:
  • p0 (list, tuple, array or callable) – A vector of initial parameter values to try. See notes below.

  • sigma (index) – The index of the column with the y-error bars

  • bounds (callable) – A callable object that evaluates true if a row is to be included. Should be of the form f(x,y)

  • result (bool) – Determines whether the fitted data should be added into the DataFile object. If result is True then the last column will be used. If result is a string or an integer then it is used as a column index. Default to None for not adding fitted data

  • replace (bool) – Inidcatesa whether the fitted data replaces existing data or is inserted as a new column (default False)

  • header (string or None) – If this is a string then it is used as the name of the fitted data. (default None)

  • absolute_sigma (bool) – If False, sigma denotes relative weights of the data points. The default True means that the sigma parameter is the reciprocal of the absolute standard deviation.

  • output (str, default "fit") – Specify what to return.

Returns:

(various)

The return value is determined by the output parameter. Options are:
  • ”fit” (tuple of popt,pcov) Optimal values of the fitting parameters p, and the

    variance-co-variance matrix for the fitting parameters.

  • ”row” just a one dimensional numpy array of the fit parameters interleaved with their

    uncertainties

  • ”full” a tuple of (popt,pcov,dictionary of optional outputs, message, return code, row).

  • ”data” a copy of the Stoner.Core.DataFile object with fit recorded in the

    metadata and optionally as a new column.

Note

If the columns are not specified (or set to None) then the X and Y data are taken using the Stoner.Core.DataFile.setas attribute.

The fitting function should have prototype y=f(x,p[0],p[1],p[2]…) The x-column and y-column can be anything that Stoner.Core.DataFile.find_col() can use as an index but typucally either strings to be matched against column headings or integers. The initial parameter values and weightings default to None which corresponds to all parameters starting at 1 and all points equally weighted. The bounds function has format b(x, y-vec) and rewturns true if the point is to be used in the fit and false if not.

The absolute_sigma keyword determines whether the returned covariance matrix pcov is based on estimated errors in the data, and is not affected by the overall magnitude of the values in sigma. Only the relative magnitudes of the sigma values matter. If True, sigma describes one standard deviation errors of the input data points. The estimated covariance in pcov is based on these values.

The starting vector p0 can be either a list, tuple or array, or a callable that will produce a list, tuple or array. IF callable, it should take the form:

def p0_func(ydata,x=xdata):

and return a list of parameter values that is in the same order as the model function. If p0 is not given and a lmfit.Model or scipy.odr.Model is supplied as the model function, then the model’s estimates of the starting values will be used instead.