示例#1
0
"""Simple use of lmfit to fit data."""
from Stoner import Data
from numpy import linspace,exp,random

#Make some data
x=linspace(0,10.0,101)
y=2+4*exp(-x/1.7)+random.normal(scale=0.2,size=101)

d=Data(x,y,column_headers=["Time","Signal"],setas="xy")

d.plot(fmt="ro") # plot our data

#Do the fitting and plot the result
fit = d.lmfit(lambda x,A,B,C:A+B*exp(-x/C),result=True,header="Fit",A=1,B=1,C=1)
d.setas="x.y"
d.labels=[]
d.plot(fmt="b-")

# Make nice label of the parameters
text=r"$y=A+Be^{-x/C}$"+"\n\n"
text+="\n".join([d.format(k,latex=True) for k in ["Model:A","Model:B","Model:C"]])
d.text(5,4,text,fontdict={"size":"x-small"})
示例#2
0
    return res, res.column_headers


###############################################################################################
#################### Important - if using multiprocessing on Windows, this  block must be ######
#################### Inside a if __name__=="__main__": test. ###################################
###############################################################################################

if __name__ == "__main__":
    # Load data
    d = Data(join(__home__, "..", "sample-data", "FMR-data.txt"))
    # Rename columns and reset plot labels
    d.rename("multi[1]:y",
             "Field").rename("multi[0]:y",
                             "Frequency").rename("Absorption::X", "FMR")
    d.labels = None

    # Deine x and y columns and normalise to a big number
    d.setas(x="Field", y="FMR")  # pylint: disable=not-callable
    d.normalise(base=(-1e6, 1e6))
    fldr = d.split(field_sign, "Frequency")

    # Split the data file into separate files by frequencies and sign of field
    fldr = PlotFolder(fldr)  # Convert to a PlotFolder
    fldr.template = template  # Set my custom plot template
    for f in fldr[-1]:  # Invert the negative field side
        f.x = -f.x[::-1]
        f.y = -f.y[::-1]

    resfldr = PlotFolder(
    )  # Somewhere to keep the results from +ve and -ve fields
y = 2 + 4 * exp(-x / 1.7) + random.normal(scale=0.2, size=101)

d = Data(x, y, column_headers=["Time", "Signal"], setas="xy")

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"})
    return res, res.column_headers


###############################################################################################
#################### Important - if using multiprocessing on Windows, this  block must be ######
#################### Inside a if __name__=="__main__": test. ###################################
###############################################################################################

if __name__ == "__main__":
    # Load data
    d = Data(join(__home__, "..", "sample-data", "FMR-data.txt"))
    # Rename columns and reset plot labels
    d.rename("multi[1]:y", "Field").rename("multi[0]:y", "Frequency").rename(
        "Absorption::X", "FMR"
    )
    d.labels = None

    # Deine x and y columns and normalise to a big number
    d.setas(x="Field", y="FMR")
    d.normalise(base=(-1e6, 1e6))
    fldr = d.split(field_sign, "Frequency")

    # Split the data file into separate files by frequencies and sign of field
    fldr = PlotFolder(fldr)  # Convert to a PlotFolder
    fldr.template = template  # Set my custom plot template
    for f in fldr[-1]:  # Invert the negative field side
        f.x = -f.x[::-1]
        f.y = -f.y[::-1]

    resfldr = PlotFolder()  # Somewhere to keep the results from +ve and -ve fields