"""Example of nDimArrhenius Fit.""" from Stoner import Data import Stoner.Fit as SF from numpy import linspace from numpy.random import normal #Make some data T = linspace(50, 500, 101) R = SF.nDimArrhenius(T + normal(size=len(T), scale=5.0, loc=1.0), 1E6, 0.5, 2) d = Data(T, R, setas="xy", column_headers=["T", "Rate"]) #Curve_fit on its own d.curve_fit(SF.nDimArrhenius, p0=[1E6, 0.5, 2], result=True, header="curve_fit") d.setas = "xyy" d.plot() d.annotate_fit(SF.nDimArrhenius, x=150, y=6E5) # lmfit using lmfit guesses fit = SF.NDimArrhenius() p0 = fit.guess(R, x=T) d.lmfit(fit, p0=p0, result=True, header="lmfit") d.setas = "x..y" d.plot() d.annotate_fit(SF.NDimArrhenius, x=150, y=3.5E5, prefix="NDimArrhenius") d.title = "n-D Arrhenius Test Fit" d.ylabel = "Rate" d.xlabel = "Temperature (K)"
d.plot(fmt="m-", label="Polyfit") d.text( -9, 450, "Polynominal co-efficients\n{}".format(d["2nd-order polyfit coefficients"]), fontdict={"size": "x-small", "color": "magenta"}, ) d.setas = "xy" d.curve_fit(SF.quadratic, result=True, header="Curve-fit") d.setas = "x...y" d.plot(fmt="b-", label="curve-fit") d.annotate_fit( SF.quadratic, prefix="quadratic", x=0.2, y=0.65, fontdict={"size": "x-small", "color": "blue"}, ) d.setas = "xy" fit = SF.Quadratic() p0 = fit.guess(y, x=x) d.lmfit(SF.Quadratic, p0=p0, result=True, header="lmfit") d.setas = "x...y" d.plot(fmt="g-", label="lmfit") d.annotate_fit( SF.Quadratic, prefix="Quadratic", x=0.65,
# Curve_fit on its own d.curve_fit(vftEquation, p0=params, result=True, header="curve_fit") # lmfit uses some guesses p0 = params d.lmfit(VFTEquation, result=True, header="lmfit") # Plot these results too d.setas = "x..yy" d.plot(fmt=["b-", "g-"]) # Annotate the graph d.annotate_fit( vftEquation, x=0.25, y=0.35, fontdict={ "size": "x-small", "color": "blue" }, mode="eng", ) d.annotate_fit( VFTEquation, x=0.5, y=0.35, prefix="VFTEquation", fontdict={ "size": "x-small", "color": "green" }, mode="eng", )
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Fit Ic(B) to Airy function. """ from Stoner import Data, __home__ from Stoner.Fit import Ic_B_Airy import os os.chdir(os.path.join(__home__, "..", "doc", "samples", "Fitting")) data = Data("data/Ic_B.txt", setas={"x": "Magnet Output", "y": "Ic"}) data.lmfit(Ic_B_Airy, result=True, header="Fit") data.setas = {"x": "Magnet Output", "y": ["Ic", "Fit"]} data.plot(fmt=["r+", "b-"]) data.annotate_fit(Ic_B_Airy, mode="eng", x=0.6, y=0.5, fontsize="small") data.title = "Critical current vs Field for $4\mu m$ junction" data.xlabel = r"Magnetic Field $\mu_0H (\mathrm{T})$" data.ylabel = r"Critical Current $I_c (A)$"
data.del_rows(isnan(data.y)) #Normalise data on y axis between +/- 1 data.normalise(base=(-1.,1.), replace=True) #Swap x and y axes around so that R is x and T is y data=~data #Curve fit a straight line, using only the central 90% of the resistance transition data.curve_fit(linear,bounds=lambda x,r:-threshold<x<threshold,result=True,p0=[7.0,0.0]) #result=True to record fit into metadata #Plot the results data.setas[-1]="y" data.subplot(1,len(r_cols),i+1) data.plot(fmt=["k.","r-"]) data.annotate_fit(linear,x=-1.,y=7.3c,fontsize="small") data.title="Ramp {}".format(data[iterator][0]) row.extend([data["linear:intercept"],data["linear:intercept err"]]) data.tight_layout() result+=np.array(row) result.column_headers=["Ramp","Sample 4 R","dR","Sample 7 R","dR"] result.setas="xyeye" result.plot(fmt=["k.","r."])
"""Example of PowerLaw Fit.""" from Stoner import Data import Stoner.Fit as SF from numpy import linspace from numpy.random import normal # Make some data T = linspace(50, 500, 101) R = SF.powerLaw(T, 1e-2, 0.6666666) * normal(size=len(T), scale=0.1, loc=1.0) d = Data(T, R, setas="xy", column_headers=["T", "Rate"]) # Curve_fit on its own d.curve_fit(SF.powerLaw, p0=[1, 0.5], result=True, header="curve_fit") d.setas = "xyy" d.plot(fmt=["r.", "b-"]) d.annotate_fit(SF.powerLaw, x=0.5, y=0.25) # lmfit using lmfit guesses fit = SF.PowerLaw() p0 = fit.guess(R, x=T) d.lmfit(fit, p0=p0, result=True, header="lmfit") d.setas = "x..y" d.plot(fmt="g-") d.annotate_fit(SF.PowerLaw, x=0.5, y=0.05, prefix="PowerLaw") d.title = "Powerlaw Test Fit" d.ylabel = "Rate" d.xlabel = "Temperature (K)"
from Stoner import Data import Stoner.Fit as SF from numpy import linspace,ones_like from numpy.random import normal #Make some data V=linspace(-4,4,1000) I=SF.fowlerNordheim(V,2500,3.2,15.0)+normal(size=len(V),scale=10E-6) dI=ones_like(V)*10E-6 d=Data(V,I,dI,setas="xye",column_headers=["Bias","Current","Noise"]) d.curve_fit(SF.fowlerNordheim,p0=[2500,3.2,15.0],result=True,header="curve_fit") d.setas="xyey" d.plot(fmt=["r.","b-"]) d.annotate_fit(SF.fowlerNordheim,x=0,y=10,prefix="fowlerNordheim",fontdict={"size":"x-small"}) d.setas="xye" fit=SF.FowlerNordheim() p0=[2500,5.2,15.0] p0=fit.guess(I,x=V) for p,v,mi,mx in zip(["A","phi","d"],[2500,3.2,15.0],[100,1,5],[1E4,20.0,30.0]): p0[p].value=v p0[p].bounds=[mi,mx] d.lmfit(SF.FowlerNordheim,p0=p0,result=True,header="lmfit") d.setas="x...y" d.plot() d.annotate_fit(fit,x=-3,y=-60,prefix="FowlerNordheim",fontdict={"size":"x-small"}) d.ylabel="Current" d.title="Fowler-Nordheim Model test"
G = SF.fluchsSondheimer(B, *params) + normal(size=len(B), scale=5e-5) dG = ones_like(B) * 5e-5 d = Data( B, G, dG, setas="xye", column_headers=["Thickness (nm)", "Conductance", "dConductance"], ) d.curve_fit(SF.fluchsSondheimer, p0=params, result=True, header="curve_fit") d.setas = "xye" d.lmfit(SF.FluchsSondheimer, p0=params, result=True, header="lmfit") d.setas = "xyeyy" d.plot(fmt=["r.", "b-", "g-"]) d.annotate_fit( SF.fluchsSondheimer, x=0.2, y=0.6, fontdict={"size": "x-small", "color": "blue"} ) d.annotate_fit( SF.FluchsSondheimer, x=0.2, y=0.4, fontdict={"size": "x-small", "color": "green"}, prefix="FluchsSondheimer", ) d.title = "Fluchs-Sondheimer Fit" d.tight_layout()
@simple_model.guesser def guess_vals(y, x=None): """Should guess parameter values really!""" m = (y.max() - y.min()) / (x[y.argmax()] - x[y.argmin()]) c = x.mean() * m - y.mean() # return one value per parameter return [m, c] # Add a function to sry vonstraints on parameters (optional) @simple_model.hinter def hint_parameters(): """Five some hints about the parameter.""" return {"m": {"max": 10.0, "min": 0.0}, "c": {"max": 5.0, "min": -5.0}} # Create some x,y data x = linspace(0, 10, 101) y = 4.5 * x - 2.3 + normal(scale=0.4, size=len(x)) # Make The Data object d = Data(x, y, setas="xy", column_headers=["X", "Y"]) # Do the fit d.lmfit(simple_model, result=True) # Plot the result d.setas = "xyy" d.plot(fmt=["r+", "b-"]) d.title = "Simple Model Fit" d.annotate_fit(simple_model, x=0.05, y=0.5)
# Curve_fit on its own d.curve_fit(vftEquation, p0=params, result=True, header="curve_fit") # lmfit uses some guesses p0 = params d.lmfit(VFTEquation, p0=p0, result=True, header="lmfit") # Plot these results too d.setas = "x..yy" d.plot(fmt=["b-", "g-"]) # Annotate the graph d.annotate_fit( vftEquation, x=0.25, y=0.35, fontdict={"size": "x-small", "color": "blue"}, mode="eng", ) d.annotate_fit( VFTEquation, x=0.5, y=0.35, prefix="VFTEquation", fontdict={"size": "x-small", "color": "green"}, mode="eng", ) # reset the columns for the fit d.setas = "xye.." # Now do the odr fit (will overwrite lmfit's metadata)
) d.curve_fit(SF.kittelEquation, p0=copy(params), result=True, header="curve_fit") fit = SF.KittelEquation() p0 = fit.guess(G, x=B) d.lmfit(fit, p0=p0, result=True, header="lmfit") d.setas = "xyeyy" d.plot(fmt=["r.", "b-", "g-"]) d.annotate_fit( SF.kittelEquation, x=0.5, y=0.25, fontdict={"size": "x-small", "color": "blue"}, mode="eng", ) d.annotate_fit( SF.KittelEquation, x=0.5, y=0.05, fontdict={"size": "x-small", "color": "green"}, prefix="KittelEquation", mode="eng", ) d.title = "Kittel Fit" d.fig.gca().xaxis.set_major_formatter(TexEngFormatter()) d.fig.gca().yaxis.set_major_formatter(TexEngFormatter()) d.tight_layout()
from numpy.random import normal # Make some data T = linspace(200, 350, 101) R = SF.arrhenius(T + normal(size=len(T), scale=3.0, loc=0.0), 1e6, 0.5) E = 10 ** ceil(log10(abs(R - SF.arrhenius(T, 1e6, 0.5)))) d = Data(T, R, E, setas="xye", column_headers=["T", "Rate"]) # Curve_fit on its own d.curve_fit(SF.arrhenius, p0=(1e6, 0.5), result=True, header="curve_fit") d.setas = "xyey" d.plot(fmt=["r.", "b-"]) d.annotate_fit( SF.arrhenius, x=0.5, y=0.5, mode="eng", fontdict={"size": "x-small", "color": "blue"}, ) # lmfit using lmfit guesses fit = SF.Arrhenius() d.setas = "xye" d.lmfit(fit, result=True, header="lmfit") d.setas = "x...y" d.plot(fmt="g-") d.annotate_fit( SF.Arrhenius, x=0.5, y=0.35, prefix="Arrhenius",
result.data[:, c] = (resfldr[1][:, c] + resfldr[0][:, c]) / 2.0 for c in [1, 3, 5, 7]: result.data[:, c] = gmean((resfldr[0][:, c], resfldr[1][:, c]), axis=0) # Doing the Kittel fit with an orthogonal distance regression as we have x errors not y errors p0 = [2, 200e3, 10e3] # Some sensible guesses result.lmfit( Inverse_Kittel, p0=p0, result=True, header="Kittel Fit", output="report" ) result.setas[-1] = "y" result.template.yformatter = TexEngFormatter result.template.xformatter = TexEngFormatter result.labels = None result.figure(figsize=(6, 8)) result.subplot(211) result.plot(fmt=["r.", "b-"]) result.annotate_fit(Inverse_Kittel, x=7e9, y=1e5, fontdict={"size": 8}) result.ylabel = "$H_{res} \\mathrm{(Am^{-1})}$" result.title = "Inverse Kittel Fit" # Get alpha result.subplot(212) result.setas(y="Delta_H", e="Delta_H.stderr", x="Freq") result.y /= mu_0 result.e /= mu_0 result.lmfit(Linear, result=True, header="Width", output="report") result.setas[-1] = "y" result.plot(fmt=["r.", "b-"]) result.annotate_fit(Linear, x=5.5e9, y=2.8e3, fontdict={"size": 8})
setas="xye", column_headers=["Field $Oe$", r"$\nu (Hz)$", r"\delta $\nu (Hz)$"]) d.curve_fit(SF.kittelEquation, p0=copy(params), result=True, header="curve_fit") fit = SF.KittelEquation() p0 = fit.guess(G, x=B) d.lmfit(fit, p0=p0, result=True, header="lmfit") d.setas = "xyeyy" d.plot(fmt=["r.", "b-", "g-"]) d.annotate_fit(SF.kittelEquation, x=4000, y=2E8, fontdict={"size": "x-small"}, mode="eng") d.annotate_fit(SF.KittelEquation, x=20000, y=2E8, fontdict={"size": "x-small"}, prefix="KittelEquation", mode="eng") d.title = "Kittel Fit" d.fig.gca().xaxis.set_major_formatter(TexEngFormatter()) d.fig.gca().yaxis.set_major_formatter(TexEngFormatter()) d.tight_layout()
"""USe curve_fit to fit a straight line.""" from Stoner import Data def linear(x, m, c): """Straight line function.""" return m * x + c d = Data("curve_fit_data.dat", setas="xye") d.plot(fmt="ro") fit = d.curve_fit(linear, result=True, replace=False, header="Fit", output="report") d.setas = "x..y" d.plot(fmt="b-") d.annotate_fit(linear) d.draw() print(fit.fit_report())
from numpy import linspace, ones_like from numpy.random import normal # Make some data V = linspace(-10, 10, 1000) I = SF.bdr(V, 2.5, 3.2, 0.3, 15.0, 1.0) + normal(size=len(V), scale=1.0e-3) dI = ones_like(V) * 1.0e-3 # Curve fit d = Data(V, I, dI, setas="xye", column_headers=["Bias", "Current", "Noise"]) d.curve_fit(SF.bdr, p0=[2.5, 3.2, 0.3, 15.0, 1.0], result=True, header="curve_fit") d.setas = "xyey" d.plot(fmt=["r.", "b-"]) d.annotate_fit( SF.bdr, x=0.6, y=0.05, prefix="bdr", fontdict={"size": "x-small", "color": "blue"} ) # lmfit d.setas = "xy" fit = SF.BDR(missing="drop") p0 = fit.guess(I, x=V) for p, v, mi, mx in zip( ["A", "phi", "dphi", "d", "mass"], [2.500, 3.2, 0.3, 15.0, 1.0], [0.100, 1.0, 0.05, 5.0, 0.5], [10, 10.0, 2.0, 30.0, 5.0], ): p0[p].value = v p0[p].bounds = [mi, mx] d.lmfit(fit, p0=p0, result=True, header="lmfit")
B = linspace(-8, 8, 201) params = [1e-3, 2.0, 0.25, 1.4] G = SF.wlfit(B, *params) + normal(size=len(B), scale=5e-7) dG = ones_like(B) * 5e-7 d = Data( B, G, dG, setas="xye", column_headers=["Field $\\mu_0H (T)$", "Conductance", "dConductance"], ) d.curve_fit(SF.wlfit, p0=copy(params), result=True, header="curve_fit") d.setas = "xye" d.lmfit(SF.WLfit, p0=copy(params), result=True, header="lmfit") d.setas = "xyeyy" d.plot(fmt=["r.", "b-", "g-"]) d.annotate_fit(SF.wlfit, x=0.05, y=0.75, fontdict={"size": "x-small", "color": "blue"}) d.annotate_fit( SF.WLfit, x=0.05, y=0.5, fontdict={"size": "x-small", "color": "green"}, prefix="WLfit", ) d.title = "Weak Localisation Fit" d.tight_layout()
from numpy.random import normal # Make some data T = linspace(200, 350, 101) R = SF.arrhenius(T + normal(size=len(T), scale=3.0, loc=0.0), 1e6, 0.5) E = 10 ** ceil(log10(np_abs(R - SF.arrhenius(T, 1e6, 0.5)))) d = Data(T, R, E, setas="xye", column_headers=["T", "Rate"]) # Curve_fit on its own d.curve_fit(SF.arrhenius, p0=(1e6, 0.5), result=True, header="curve_fit") d.setas = "xyey" d.plot(fmt=["r.", "b-"]) d.annotate_fit( SF.arrhenius, x=0.5, y=0.5, mode="eng", fontdict={"size": "x-small", "color": "blue"}, ) # lmfit using lmfit guesses fit = SF.Arrhenius() d.setas = "xye" d.lmfit(fit, result=True, header="lmfit") d.setas = "x...y" d.plot(fmt="g-") d.annotate_fit( SF.Arrhenius, x=0.5, y=0.35, prefix="Arrhenius",
from numpy.random import normal # Make some data V = linspace(-4, 4, 1000) I = SF.fowlerNordheim(V, 2500, 3.2, 15.0) + normal(size=len(V), scale=1e-6) dI = ones_like(V) * 10e-6 d = Data(V, I, dI, setas="xye", column_headers=["Bias", "Current", "Noise"]) d.curve_fit(SF.fowlerNordheim, p0=[2500, 3.2, 15.0], result=True, header="curve_fit") d.setas = "xyey" d.plot(fmt=["r.", "b-"]) d.annotate_fit( SF.fowlerNordheim, x=0.2, y=0.6, prefix="fowlerNordheim", fontdict={"size": "x-small", "color": "blue"}, ) d.setas = "xye" fit = SF.FowlerNordheim() p0 = [2500, 5.2, 15.0] p0 = fit.guess(I, x=V) for p, v, mi, mx in zip( ["A", "phi", "d"], [2500, 3.2, 15.0], [100, 1, 5], [1e4, 20.0, 30.0] ): p0[p].value = v p0[p].bounds = [mi, mx] d.lmfit(SF.FowlerNordheim, p0=p0, result=True, header="lmfit") d.setas = "x...y"
"""Example of using lmfit to do a bounded fit.""" from Stoner import Data from Stoner.Fit import StretchedExp #Load dat and plot d = Data("lmfit_data.txt", setas="xy") # Do the fit d.lmfit(StretchedExp, result=True, header="Fit", prefix="") # plot d.setas = "xyy" d.plot(fmt=["+", "-"]) # Make apretty label using Stoner.Util methods text = "$y=A\\exp\\left[\\left(-\\frac{x}{x_0}\\right)^\\beta\\right]$\n" text += d.annotate_fit(StretchedExp, text_only=True) d.text(6, 4E4, text) #Adjust layout NB pass-through method to pyplot used here d.tight_layout()
"""USe curve_fit to fit a straight line.""" from Stoner import Data def linear(x, m, c): """Straight line function.""" return m * x + c d = Data("curve_fit_data.dat", setas="xye") d.plot(fmt="ro") popt, pcov = d.curve_fit(linear, result=True, replace=False, header="Fit") d.setas = "x..y" d.plot(fmt="b-") d.annotate_fit(linear) d.draw()
from numpy.random import normal # Make some data V = linspace(-4, 4, 1001) I = SF.simmons(V, 2500, 5.2, 15.0) + normal(size=len(V), scale=100e-9) dI = ones_like(V) * 100e-9 d = Data(V, I, dI, setas="xye", column_headers=["Bias", "Current", "Noise"]) d.curve_fit(SF.simmons, p0=[2500, 5.2, 15.0], result=True, header="curve_fit") d.setas = "xyey" d.plot(fmt=["r.", "b-"]) d.annotate_fit( SF.simmons, x=0.25, y=0.25, prefix="simmons", fontdict={"size": "x-small", "color": "blue"}, ) d.setas = "xye" fit = SF.Simmons() p0 = [2500, 5.2, 15.0] d.lmfit(SF.Simmons, p0=p0, result=True, header="lmfit") d.setas = "x...y" d.plot(fmt="g-") d.annotate_fit( fit, x=0.65, y=0.25, prefix="Simmons",
# Make some data V = linspace(-4, 4, 1001) I = SF.simmons(V, 2500, 5.2, 15.0) + normal(size=len(V), scale=100e-9) dI = ones_like(V) * 100e-9 d = Data(V, I, dI, setas="xye", column_headers=["Bias", "Current", "Noise"]) d.curve_fit(SF.simmons, p0=[2500, 5.2, 15.0], result=True, header="curve_fit") d.setas = "xyey" d.plot(fmt=["r.", "b-"]) d.annotate_fit( SF.simmons, x=0.25, y=0.25, prefix="simmons", fontdict={ "size": "x-small", "color": "blue" }, ) d.setas = "xye" fit = SF.Simmons() p0 = [2500, 5.2, 15.0] d.lmfit(SF.Simmons, p0=p0, result=True, header="lmfit") d.setas = "x...y" d.plot(fmt="g-") d.annotate_fit( fit, x=0.65, y=0.25,
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Fit IV data to various RSJ models """ from Stoner import Data, __home__ from Stoner.Fit import RSJ_Noiseless, RSJ_Simple import os os.chdir(os.path.join(__home__, "..", "doc", "samples", "Fitting")) data = Data("data/IV.txt", setas={"x": "Current", "y": "Voltage"}) # Fit data with both versions of the RSJ model data.lmfit(RSJ_Simple, result=True, header="Simple", prefix="simple") data.lmfit(RSJ_Noiseless, result=True, header="Noiseless", prefix="noiseless") # Set column assignments and plot the data and fits data.setas = {"x": "Current", "y": ["Voltage", "Simple", "Noiseless"]} data.plot(fmt=["r+", "b-", "g-"]) # Annotate fits data.annotate_fit( RSJ_Simple, prefix="simple", mode="eng", x=0.15, y=0.1, fontsize="small" ) data.annotate_fit( RSJ_Noiseless, prefix="noiseless", mode="eng", x=0.55, y=0.1, fontsize="small" )
import Stoner.Fit as SF from numpy import linspace from numpy.random import normal # Make some data T = linspace(200, 350, 101) R = SF.modArrhenius(T, 1e6, 0.5, 1.5) * normal( scale=0.00005, loc=1.0, size=len(T)) d = Data(T, R, setas="xy", column_headers=["T", "Rate"]) # Curve_fit on its own d.curve_fit(SF.modArrhenius, p0=[1e6, 0.5, 1.5], result=True, header="curve_fit") d.setas = "xyy" d.plot(fmt=["r.", "b-"]) d.annotate_fit(SF.modArrhenius, x=0.2, y=0.5) # lmfit using lmfit guesses fit = SF.ModArrhenius() p0 = [1e6, 0.5, 1.5] d.lmfit(fit, p0=p0, result=True, header="lmfit") d.setas = "x..y" d.plot() d.annotate_fit(SF.ModArrhenius, x=0.2, y=0.25, prefix="ModArrhenius") d.title = "Modified Arrhenius Test Fit" d.ylabel = "Rate" d.xlabel = "Temperature (K)"
"""Example of nDimArrhenius Fit.""" from Stoner import Data import Stoner.Fit as SF from numpy import linspace from numpy.random import normal # Make some data T = linspace(50, 500, 101) R = SF.nDimArrhenius(T + normal(size=len(T), scale=5.0, loc=1.0), 1e6, 0.5, 2) d = Data(T, R, setas="xy", column_headers=["T", "Rate"]) # Curve_fit on its own d.curve_fit(SF.nDimArrhenius, p0=[1e6, 0.5, 2], result=True, header="curve_fit") d.setas = "xyy" d.plot(fmt=["r.", "b-"]) d.annotate_fit(SF.nDimArrhenius, x=0.25, y=0.3) # lmfit using lmfit guesses fit = SF.NDimArrhenius() p0 = fit.guess(R, x=T) d.lmfit(fit, p0=p0, result=True, header="lmfit") d.setas = "x..y" d.plot(fmt="g-") d.annotate_fit(SF.NDimArrhenius, x=0.25, y=0.05, prefix="NDimArrhenius") d.title = "n-D Arrhenius Test Fit" d.ylabel = "Rate" d.xlabel = "Temperature (K)"
d.curve_fit(func, p0=copy(params)[0:2], result=True, header="curve_fit") d.setas = "xye" fit = SF.Langevin() p0 = fit.guess(G, x=B) for p, v in zip(p0, params): p0[p].set(v) p0[p].max = v * 5 p0[p].min = 0 p0[p].vary = p != "T" d.lmfit(fit, p0=p0, result=True, header="lmfit") d.setas = "xyeyy" d.plot(fmt=["r.", "b-", "g-"]) d.annotate_fit( func, x=0.1, y=0.5, fontdict={"size": "x-small", "color": "blue"}, mode="eng" ) d.annotate_fit( SF.Langevin, x=0.1, y=0.25, fontdict={"size": "x-small", "color": "green"}, prefix="Langevin", mode="eng", ) d.title = "langevin Fit"
setas="xye", column_headers=["Thickness (nm)", "Conductance", "dConductance"], ) d.curve_fit(SF.fluchsSondheimer, p0=params, result=True, header="curve_fit") d.setas = "xye" d.lmfit(SF.FluchsSondheimer, p0=params, result=True, header="lmfit") d.setas = "xyeyy" d.plot(fmt=["r.", "b-", "g-"]) d.annotate_fit(SF.fluchsSondheimer, x=0.2, y=0.6, fontdict={ "size": "x-small", "color": "blue" }) d.annotate_fit( SF.FluchsSondheimer, x=0.2, y=0.4, fontdict={ "size": "x-small", "color": "green" }, prefix="FluchsSondheimer", ) d.title = "Fluchs-Sondheimer Fit" d.tight_layout()
import Stoner.Fit as SF from numpy import linspace,ones_like from numpy.random import normal #Make some data V=linspace(-10,10,1000) I=SF.bdr(V,2.5,3.2,0.3,15.0,1.0)+normal(size=len(V),scale=1.0E-3) dI=ones_like(V)*1.0E-3 #Curve fit d=Data(V,I,dI,setas="xye",column_headers=["Bias","Current","Noise"]) d.curve_fit(SF.bdr,p0=[2.5,3.2,0.3,15.0,1.0],result=True,header="curve_fit") d.setas="xyey" d.plot(fmt=["r.","b-"]) d.annotate_fit(SF.bdr,x=-4,y=1.0,prefix="bdr",fontdict={"size":"x-small"}) #lmfit d.setas="xy" fit=SF.BDR(missing="drop") p0=fit.guess(I,x=V) for p,v,mi,mx in zip(["A","phi","dphi","d","mass"],[2.500,3.2,0.3,15.0,1.0],[0.100,1.0,0.05,5.0,0.5],[10,10.0,2.0,30.0,5.0]): p0[p].value=v p0[p].bounds=[mi,mx] d.lmfit(fit,p0=p0,result=True,header="lmfit") d.setas="x...y" d.plot() d.annotate_fit(fit,x=-4,y=-20.0,prefix="BDR",fontdict={"size":"x-small"}) d.ylabel="Current" d.title="BDR Model test"
"""Example of using lmfit to do a bounded fit.""" from Stoner import Data from Stoner.Fit import StretchedExp # Load dat and plot d = Data("lmfit_data.txt", setas="xy") # Do the fit d.lmfit(StretchedExp, result=True, header="Fit", prefix="") # plot d.setas = "xyy" d.plot(fmt=["+", "-"]) # Make apretty label using Stoner.Util methods text = r"$y=A e^{-\left(\frac{x}{x_0}\right)^\beta}$" + "\n" text += d.annotate_fit(StretchedExp, text_only=True, prefix="") d.text(6, 4e4, text)
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 = "" ax = d.subplot2grid((3, 1), (0, 0), rowspan=2) d.setas = "xyy" d.plot(fmt=["r.", "b-"]) 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 = u"Differential Evolution 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 = "" ax = d.subplot2grid((3, 1), (0, 0), rowspan=2) d.setas = "xyy" d.plot(fmt=["ro", "b-"]) d.xticklabels = [[]] d.xlabel = "" # Annotate plot with fitting parameters d.annotate_fit(func, prefix="Model", x=7.2, y=3.45, 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 = u"Orthogonal Distance Regression Fit"
from numpy import linspace, ones_like from numpy.random import normal from copy import deepcopy T = linspace(4.2, 300, 101) params = [265, 65, 1.0, 5] params2 = deepcopy(params) G = SF.blochGrueneisen(T, *params) + normal(size=len(T), scale=5E-5) dG = ones_like(T) * 5E-5 d = Data(T, G, dG, setas="xye", column_headers=["Temperature (K)", "Resistivity", "dR"]) d.curve_fit(SF.blochGrueneisen, p0=params, result=True, header="curve_fit") d.setas = "xy" d.lmfit(SF.BlochGrueneisen, p0=params2, result=True, header="lmfit") d.setas = "xyeyy" d.plot(fmt=["r.", "b-", "g-"]) d.annotate_fit(SF.blochGrueneisen, x=20, y=65.05, fontdict={"size": "x-small"}) d.annotate_fit(SF.BlochGrueneisen, x=100, y=65, fontdict={"size": "x-small"}, prefix="BlochGrueneisen") d.title = "Bloch-Grueneisen Fit" d.tight_layout()
d = Data(T, R, dR, setas="xye", column_headers=["T", "Rate"]) #Curve_fit on its own d.curve_fit(SF.vftEquation, p0=params, result=True, header="curve_fit") # lmfit using lmfit guesses fit = SF.VFTEquation() p0 = params d.lmfit(fit, p0=p0, result=True, header="lmfit") d.setas = "xyeyyy" d.plot(fmt=["k+", "r-", "b-"]) d.yscale = "log" d.ylim = (1E-43, 1) d.annotate_fit(SF.vftEquation, x=270, y=1E-27, fontdict={"size": "x-small"}, mode="eng") d.annotate_fit(SF.VFTEquation, x=240, y=1E-40, prefix="VFTEquation", fontdict={"size": "x-small"}, mode="eng") d.title = "VFT Equation Test Fit" d.ylabel = "Rate" d.xlabel = "Temperature (K)"
d.plot(fmt="ro") # plot our data func = lambda x, A, B, C: A + B * exp(-x / C) # Do the fitting and plot the result fit = d.differential_evolution( 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 = "" ax = d.subplot2grid((3, 1), (0, 0), rowspan=2) d.setas = "xyy" d.plot(fmt=["r.", "b-"]) 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 = u"Differential Evolution Fit"
d["2nd-order polyfit coefficients"]), fontdict={ "size": "x-small", "color": "magenta" }, ) d.setas = "xy" d.curve_fit(SF.quadratic, result=True, header="Curve-fit") d.setas = "x...y" d.plot(fmt="b-", label="curve-fit") d.annotate_fit( SF.quadratic, prefix="quadratic", x=0.2, y=0.65, fontdict={ "size": "x-small", "color": "blue" }, ) d.setas = "xy" fit = SF.Quadratic() p0 = fit.guess(y, x=x) d.lmfit(SF.Quadratic, p0=p0, result=True, header="lmfit") d.setas = "x...y" d.plot(fmt="g-", label="lmfit") d.annotate_fit( SF.Quadratic, prefix="Quadratic",
"""Example of Arrhenius Fit.""" from Stoner import Data import Stoner.Fit as SF from numpy import linspace from numpy.random import normal #Make some data T=linspace(200,350,101) R=SF.arrhenius(T+normal(size=len(T),scale=1.0,loc=1.0),1E6,0.5) d=Data(T,R,setas="xy",column_headers=["T","Rate"]) #Curve_fit on its own d.curve_fit(SF.arrhenius,p0=[1E6,0.5],result=True,header="curve_fit") d.setas="xyy" d.plot() d.annotate_fit(SF.arrhenius,x=200,y=0.04) # lmfit using lmfit guesses fit=SF.Arrhenius() p0=fit.guess(R,x=T) d.lmfit(fit,p0=p0,result=True,header="lmfit") d.setas="x..y" d.plot() d.annotate_fit(SF.Arrhenius,x=200,y=0.02,prefix="Arrhenius") d.title="Arrhenius Test Fit" d.ylabel="Rate" d.xlabel="Temperature (K)"
ODRModel, result=True, header="ODR-Fit", residuals=True, output="report", prefix="ODRModel", ) # 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+", label="Fit residuals") d.setas = "x....y" d.plot(fmt="b+", label="ODRModel Residuals") d.title = "" ax = d.subplot2grid((3, 1), (0, 0), rowspan=2) d.setas = "xyy.y" d.plot(fmt=["ro", "g-", "b-"]) d.xticklabels = [[]] d.ax_xlabel = "" # Annotate plot with fitting parameters d.annotate_fit(PowerLaw, x=0.1, y=0.25, fontdict={"size": "x-small"}) d.annotate_fit( ODRModel, x=0.65, y=0.15, fontdict={"size": "x-small"}, prefix="ODRModel" ) d.title = u"curve_fit with models"
y = lorentzian_diff(x, *params) + normal(size=len(x), scale=0.5) dy = ones_like(x) * 5e-3 d = Data(x, y, dy, setas="xye", column_headers=["Time", "Signal", "dM"]) d.curve_fit(lorentzian_diff, p0=copy(params), result=True, header="curve_fit") d.setas = "xye" d.lmfit(Lorentzian_diff, result=True, header="lmfit", prefix="lmfit") d.setas = "xyeyy" d.plot(fmt=["r+", "b-", "g-"]) d.annotate_fit( lorentzian_diff, x=0.6, y=0.2, fontdict={"size": "x-small", "color": "blue"}, mode="eng", ) d.annotate_fit( Lorentzian_diff, x=0.05, y=0.2, fontdict={"size": "x-small", "color": "green"}, prefix="lmfit", mode="eng", ) d.title = "Differential Lorentzian Fit"
params = [265, 65, 1.0, 5] params2 = deepcopy(params) G = blochGrueneisen(T, *params) + normal(size=len(T), scale=5e-5) dG = ones_like(T) * 5e-5 d = Data( T, G, dG, setas="xye", column_headers=["Temperature (K)", "Resistivity", "dR"], ) d.curve_fit(blochGrueneisen, p0=params, result=True, header="curve_fit") d.setas = "xy" d.lmfit(BlochGrueneisen, result=True, header="lmfit") d.setas = "xyeyy" d.plot(fmt=["r.", "b-", "g-"]) d.annotate_fit(blochGrueneisen, x=0.65, y=0.35, fontdict={"size": "x-small"}) d.annotate_fit( BlochGrueneisen, x=0.65, y=0.05, fontdict={"size": "x-small"}, prefix="BlochGrueneisen", ) d.title = "Bloch-Grueneisen Fit" d.tight_layout()
p0 = fit.guess(G, x=B) for p, v in zip(p0, params): p0[p].set(v) p0[p].max = v * 5 p0[p].min = 0 p0[p].vary = p != "T" d.lmfit(fit, p0=p0, result=True, header="lmfit") d.setas = "xyeyy" d.plot(fmt=["r.", "b-", "g-"]) d.annotate_fit(func, x=0.1, y=0.5, fontdict={ "size": "x-small", "color": "blue" }, mode="eng") d.annotate_fit( SF.Langevin, x=0.1, y=0.25, fontdict={ "size": "x-small", "color": "green" }, prefix="Langevin", mode="eng", ) d.title = "langevin Fit"
header="ODR-Fit", residuals=True, output="report", prefix="ODRModel", ) # 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+", label="Fit residuals") d.setas = "x....y" d.plot(fmt="b+", label="ODRModel Residuals") d.title = "" ax = d.subplot2grid((3, 1), (0, 0), rowspan=2) d.setas = "xyy.y" d.plot(fmt=["ro", "g-", "b-"]) d.xticklabels = [[]] d.ax_xlabel = "" # Annotate plot with fitting parameters d.annotate_fit(PowerLaw, x=0.1, y=0.25, fontdict={"size": "x-small"}) d.annotate_fit(ODRModel, x=0.65, y=0.15, fontdict={"size": "x-small"}, prefix="ODRModel") d.title = u"curve_fit with models"
"""Example of nDimArrhenius Fit.""" from Stoner import Data import Stoner.Fit as SF from numpy import linspace from numpy.random import normal # Make some data T = linspace(200, 350, 101) R = SF.modArrhenius(T, 1e6, 0.5, 1.5) * normal(scale=0.00005, loc=1.0, size=len(T)) d = Data(T, R, setas="xy", column_headers=["T", "Rate"]) # Curve_fit on its own d.curve_fit(SF.modArrhenius, p0=[1e6, 0.5, 1.5], result=True, header="curve_fit") d.setas = "xyy" d.plot(fmt=["r.", "b-"]) d.annotate_fit(SF.modArrhenius, x=0.2, y=0.5) # lmfit using lmfit guesses fit = SF.ModArrhenius() p0 = [1e6, 0.5, 1.5] d.lmfit(fit, p0=p0, result=True, header="lmfit") d.setas = "x..y" d.plot() d.annotate_fit(SF.ModArrhenius, x=0.2, y=0.25, prefix="ModArrhenius") d.title = "Modified Arrhenius Test Fit" d.ylabel = "Rate" d.xlabel = "Temperature (K)"
import Stoner.Fit as SF from numpy import linspace, ones_like from numpy.random import normal from copy import deepcopy T = linspace(4.2, 300, 101) params = [265, 65, 1.0, 5] params2 = deepcopy(params) G = SF.blochGrueneisen(T, *params) + normal(size=len(T), scale=5e-5) dG = ones_like(T) * 5e-5 d = Data(T, G, dG, setas="xye", column_headers=["Temperature (K)", "Resistivity", "dR"]) d.curve_fit(SF.blochGrueneisen, p0=params, result=True, header="curve_fit") d.setas = "xy" d.lmfit(SF.BlochGrueneisen, p0=params2, result=True, header="lmfit") d.setas = "xyeyy" d.plot(fmt=["r.", "b-", "g-"]) d.annotate_fit(SF.blochGrueneisen, x=0.65, y=0.35, fontdict={"size": "x-small"}) d.annotate_fit( SF.BlochGrueneisen, x=0.65, y=0.05, fontdict={"size": "x-small"}, prefix="BlochGrueneisen", ) d.title = "Bloch-Grueneisen Fit" d.tight_layout()