Data.odr

Data.odr(model, xcol=None, ycol=None, **kargs)

Wrap the scipy.odr orthogonal distance regression fitting.

Parameters:
  • model (scipy.odr.Model, lmfit.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.setas x column

  • ycol (index or None) – Columns to be used for the y data for the fitting. If not givem defaults to the Stoner.Core.DataFile.setas y 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.

Returns:

( various )

The return value is determined by the output parameter. Options are
  • ”fit” just the scipy.odr.Output instance (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.DataFile object with the fit recorded in the

    emtadata and optionally

    as a column of data.

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.models.Model: the Stoner.analysis.fitting.models package 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 AnalysisMixin.curve_fit() and

AnalysisMixin.lmfit() to facilitate easy of switching between them.

See also

Example

"""Simple use of lmfit to fit data."""
from numpy import linspace, exp, random

from Stoner import Data
from Stoner.plot.utils import errorfill

random.seed(12345)  # Ensure consistent random numbers!

# Make some data
x = linspace(0, 10.0, 101)
y = 2 + 4 * exp(-x / 1.7) + 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")

func = lambda x, A, B, C: A + B * exp(-x / C)

# Do the fitting and plot the result
fit = d.odr(
    func,
    result=True,
    header="Fit",
    A=1,
    B=1,
    C=1,
    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"

(png, hires.png, pdf)

../_images/odrfit1.png