Data.odr¶
- Data.odr(model, xcol=None, ycol=None, **kwargs)¶
Wrap the scipy.odr orthogonal distance regression fitting.
- Parameters:
datafile (Data) – Data object to work with if not being used as a bound method.
model (scipy.odr.Model, lmfit_mod.Models.Model or callable) – The model that describes the data. See below for more details.
xcol (index or None) – Columns to be used for the x data for the fitting. If not givem defaults to the
Stoner.Core.DataFile.setasx columnycol (index or None) – Columns to be used for the y data for the fitting. If not givem defaults to the
Stoner.Core.DataFile.setasy column
- Keyword Arguments:
p0 (list, tuple, array or callable) – A vector of initial parameter values to try. See the notes to
Stoner.Data.curve_fit()for more details.sigma_x (index) – The index of the column with the x-error bars
sigma_y (index) – The index of the column with the x-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)
output (str, default "fit") – Specify what to return.
**kwargs – Other arguments to pass to _odr_one or initial p0 values for curve_fit.
- Returns:
( various ) –
- The return value is determined by the output parameter. Options are
”fit” just the
scipy.odr.Outputinstance (default)- ”row” just a one dimensional numpy array of the fit parameters interleaved with their
uncertainties
”full” a tuple of the fit instance and the row.
- ”data” a copy of the
Stoner.Core.DataFileobject with the fit recorded in the emtadata and optionally
as a column of data.
- ”data” a copy of the
Notes
The function tries to make use of whatever model you give it. Specifically, it accepts:
A subclass or an instance of
scipy.odr.Model: this is the native model type for the underlying scipy odr package.A subclass or instance of an lmfit_mod.Models.Model: the
Stoner.analysis.fitting.modelspackage has a number of useful prebuilt lmfit models that can be used directly by this function.A callable function which should have a signature f(x,parameter1,parameter2…) and not the scip.odr standard f(beta,x)
- This function is designed to be as compatible as possible with
Stoner.Data.curve_fit()and Stoner.Data.lmfit()to facilitate easy of switching between them.
See also
Example
"""Simple use of lmfit to fit data.""" from numpy import exp, linspace, random from Stoner import Data from Stoner.plot.utils import errorfill random.seed(12345) # Ensure consistent random numbers! # Make some data def func(x, A, B, C): """Simple fitting function.""" return A + B * exp(-x / C) x = linspace(0, 10.0, 101) y = func(x, 2, 4, 1.5) + random.normal(scale=0.2, size=101) x += +random.normal(scale=0.1, size=101) d = Data(x, y, column_headers=["Time", "Signal"], setas="xy") # Do the fitting and plot the result fit = d.odr( func, result=True, header="Fit", A=1, B=3, C=1.2, prefix="Model", residuals=True, ) # Reset labels d.labels = [] # Make nice two panel plot layout ax = d.subplot2grid((3, 1), (2, 0)) d.setas = "x..y" d.plot(fmt="g+") d.title = "" # Plot up the data ax = d.subplot2grid((3, 1), (0, 0), rowspan=2) d.setas = "xy" d.plot(fmt="ro") d.setas = "x.y" d.plot(plotter=errorfill, yerr=0.2, color="orange") d.plot(plotter=errorfill, xerr=0.1, color="orange", label=None) d.xticklabels = [[]] d.xlabel = "" # Annotate plot with fitting parameters d.annotate_fit(func, prefix="Model", x=0.7, y=0.3, fontdict={"size": "x-small"}) text = r"$y=A+Be^{-x/C}$" + "\n\n" d.text(7.2, 3.9, text, fontdict={"size": "x-small"}) d.title = "Orthogonal Distance Regression Fit"