Example #1
0
def makeRecipe(x, y, dy, A, sig, x0):
    """Make a FitRecipe for fitting a Gaussian curve to data.    
    """
    profile = Profile()
    profile.setObservedProfile(x, y, dy)

    contribution = FitContribution("g1")
    contribution.setProfile(profile, xname="x")
    contribution.setEquation("A * exp(-0.5*(x-x0)**2/sigma**2)")

    recipe = FitRecipe()
    recipe.addContribution(contribution)
    recipe.addVar(contribution.A, A)
    recipe.addVar(contribution.x0, x0)
    recipe.addVar(contribution.sigma, sig)
    return recipe
Example #2
0
 def _makeRecipe(self, x, y, dy):
     '''Make a FitRecipe for fitting a Gaussian curve to data.
     '''
     profile = Profile()
     profile.setObservedProfile(x, y, dy)
     contribution = FitContribution("g1")
     contribution.setProfile(profile, xname="x")
     contribution.registerStringFunction('1/sqrt(2 * pi * sig**2)',
                                         name='gaussnorm')
     contribution.setEquation(
         "A * gaussnorm * exp(-0.5 * (x - x0)**2/sig**2)")
     recipe = FitRecipe()
     recipe.addContribution(contribution)
     recipe.addVar(contribution.A)
     recipe.addVar(contribution.x0)
     recipe.addVar(contribution.sig)
     recipe.clearFitHooks()
     self.recipe = recipe
     return
Example #3
0
 def _makeRecipe(self, x, y, dy):
     '''Make a FitRecipe for fitting a Gaussian curve to data.
     '''
     profile = Profile()
     profile.setObservedProfile(x, y, dy)
     contribution = FitContribution("g1")
     contribution.setProfile(profile, xname="x")
     contribution.registerStringFunction(
             '1/sqrt(2 * pi * sig**2)', name='gaussnorm')
     contribution.setEquation(
             "A * gaussnorm * exp(-0.5 * (x - x0)**2/sig**2)")
     recipe = FitRecipe()
     recipe.addContribution(contribution)
     recipe.addVar(contribution.A)
     recipe.addVar(contribution.x0)
     recipe.addVar(contribution.sig)
     recipe.clearFitHooks()
     self.recipe = recipe
     return
Example #4
0
def makeRecipe():
    """Make the recipe for the fit.

    The instructions for what we want to refine, and how to refine it will be
    defined within a FitRecipe instance. The job of a FitRecipe is to collect
    and associate all the data, the fitting equations, fitting variables,
    constraints and restrations. We will demonstrate each of these within the
    code. 

    Data is held within a Profile object. The Profile is simply a container
    that holds the data, and the theoretical profile once it has been
    calculated.

    Data is associated with a fitting equation within a FitContribution. The
    FitContribution defines the equation and parameters that will be adjusted
    to fit the data. The fitting equation can be defined within a function or
    optionally within the ProfileGenerator class. We won't need the
    ProfileGenerator class in this example since the signature of the fitting
    equation (the 'debye' function defined below) is so simple. The
    FitContribution also defines the residual function to optimize for the
    data/equation pair. This can be modified, but we won't do that here.
    
    """
        
    ## The Profile
    # Create a Profile to hold the experimental and calculated signal.
    profile = Profile()

    # Load data and add it to the profile. It is our responsibility to get our
    # data into the profile.
    xydy = numpy.array(map(float, data.split()), dtype=float).reshape(-1,3)
    x, y, dy = numpy.hsplit(xydy, 3)
    profile.setObservedProfile(x, y, dy)

    ## The FitContribution
    # The FitContribution associates the profile with the Debye function. 
    contribution = FitContribution("pb")
    # Tell the contribution about the Profile. We will need to use the
    # independent variable (the temperature) from the data to calculate the
    # theoretical signal, so give it an informative name ('T') that we can use
    # later.
    contribution.setProfile(profile, xname="T")

    # We now need to create the fitting equation.  We tell the FitContribution
    # to use the 'debye' function defined below. The 'registerFunction' method
    # will let us do this. Since we haven't told it otherwise,
    # 'registerFunction' will extract the name of the function ('debye') and
    # the names of the arguments ('T', 'm', 'thetaD'). These arguments will
    # become Parameters of the FitContribution. Since we named the x-variable
    # 'T' above, the 'T' in the 'debye' equation will refer to this x-variable
    # whenever it is used.
    contribution.registerFunction(debye)

    # Now we can create the fitting equation. We want to extend the 'debye'
    # equation by adding a vertical offset. We could wrap 'debye' in a new
    # function with an offset, and register that instead of 'debye', but what
    # we do here is easier. 
    #
    # When we set the fitting equation, we do not need to specify the
    # Parameters to the 'debye' function since the FitContribution already
    # knows what they are. If we choose to specify the arguments, we can make
    # adjustments to their input values.  We wish to have the thetaD value in
    # the debye equation to be positive, so we specify the input as abs(thetaD)
    # in the equation below.  Furthermore, we know 'm', the mass of lead, so we
    # can specify that as well.
    contribution.setEquation("debye(T, 207.2, abs(thetaD)) + offset")

    ## The FitRecipe
    # The FitRecipe lets us define what we want to fit. It is where we can
    # create variables, constraints and restraints. If we had multiple profiles
    # to fit simultaneously, the contribution from each could be added to the
    # recipe.
    recipe = FitRecipe()
    recipe.addContribution(contribution)

    # Specify which Parameters we want to refine.

    # Vary the offset
    recipe.addVar(contribution.offset, 0)
    # We also vary the Debye temperature.
    recipe.addVar(contribution.thetaD, 100)

    # We would like to 'suggest' that the offset should remain positive. This
    # is somethine that we know about the system that might help the refinement
    # converge to a physically reasonable result.  We will do this with a soft
    # contraint, or restraint. Here we restrain the offset variable to between
    # 0 and infinity. We tell the recipe that we want to scale the penalty for
    # breaking the restraint by the point-average chi^2 value so that the
    # restraint is roughly as significant as any other data point throughout
    # the fit.
    recipe.restrain(recipe.offset, lb = 0, scaled = True)

    # We're done setting up the recipe. We can now do other things with it.
    return recipe
Example #5
0
def makeRecipe():
    """Make the recipe for the fit.

    The instructions for what we want to refine, and how to refine it will be
    defined within a FitRecipe instance. The job of a FitRecipe is to collect
    and associate all the data, the fitting equations, fitting variables,
    constraints and restrations. We will demonstrate each of these within the
    code.

    Data is held within a Profile object. The Profile is simply a container
    that holds the data, and the theoretical profile once it has been
    calculated.

    Data is associated with a fitting equation within a FitContribution. The
    FitContribution defines the equation and parameters that will be adjusted
    to fit the data. The fitting equation can be defined within a function or
    optionally within the ProfileGenerator class. We won't need the
    ProfileGenerator class in this example since the signature of the fitting
    equation (the 'debye' function defined below) is so simple. The
    FitContribution also defines the residual function to optimize for the
    data/equation pair. This can be modified, but we won't do that here.

    """

    ## The Profile
    # Create a Profile to hold the experimental and calculated signal.
    profile = Profile()

    # Load data and add it to the profile. It is our responsibility to get our
    # data into the profile.
    xydy = numpy.array(map(float, data.split()), dtype=float).reshape(-1,3)
    x, y, dy = numpy.hsplit(xydy, 3)
    profile.setObservedProfile(x, y, dy)

    ## The FitContribution
    # The FitContribution associates the profile with the Debye function.
    contribution = FitContribution("pb")
    # Tell the contribution about the Profile. We will need to use the
    # independent variable (the temperature) from the data to calculate the
    # theoretical signal, so give it an informative name ('T') that we can use
    # later.
    contribution.setProfile(profile, xname="T")

    # We now need to create the fitting equation.  We tell the FitContribution
    # to use the 'debye' function defined below. The 'registerFunction' method
    # will let us do this. Since we haven't told it otherwise,
    # 'registerFunction' will extract the name of the function ('debye') and
    # the names of the arguments ('T', 'm', 'thetaD'). These arguments will
    # become Parameters of the FitContribution. Since we named the x-variable
    # 'T' above, the 'T' in the 'debye' equation will refer to this x-variable
    # whenever it is used.
    contribution.registerFunction(debye)

    # Now we can create the fitting equation. We want to extend the 'debye'
    # equation by adding a vertical offset. We could wrap 'debye' in a new
    # function with an offset, and register that instead of 'debye', but what
    # we do here is easier.
    #
    # When we set the fitting equation, we do not need to specify the
    # Parameters to the 'debye' function since the FitContribution already
    # knows what they are. If we choose to specify the arguments, we can make
    # adjustments to their input values.  We wish to have the thetaD value in
    # the debye equation to be positive, so we specify the input as abs(thetaD)
    # in the equation below.  Furthermore, we know 'm', the mass of lead, so we
    # can specify that as well.
    contribution.setEquation("debye(T, 207.2, abs(thetaD)) + offset")

    ## The FitRecipe
    # The FitRecipe lets us define what we want to fit. It is where we can
    # create variables, constraints and restraints. If we had multiple profiles
    # to fit simultaneously, the contribution from each could be added to the
    # recipe.
    recipe = FitRecipe()
    recipe.addContribution(contribution)

    # Specify which Parameters we want to refine.

    # Vary the offset
    recipe.addVar(contribution.offset, 0)
    # We also vary the Debye temperature.
    recipe.addVar(contribution.thetaD, 100)

    # We would like to 'suggest' that the offset should remain positive. This
    # is somethine that we know about the system that might help the refinement
    # converge to a physically reasonable result.  We will do this with a soft
    # contraint, or restraint. Here we restrain the offset variable to between
    # 0 and infinity. We tell the recipe that we want to scale the penalty for
    # breaking the restraint by the point-average chi^2 value so that the
    # restraint is roughly as significant as any other data point throughout
    # the fit.
    recipe.restrain(recipe.offset, lb = 0, scaled = True)

    # We're done setting up the recipe. We can now do other things with it.
    return recipe
Example #6
0
import matplotlib.pyplot as plt
plt.ion()
plt.clf()
plt.hold(False)
plt.plot(xobs, yobs, 'x')
plt.title('y = 0.5*x + 3 generated with a normal noise at sigma=0.3')
plt.show()

# <demo> --- stop ---

# We are going to define a line fitting regression using SrFit.
# At first we create a SrFit Profile object that holds the observed data.

from diffpy.srfit.fitbase import Profile
linedata = Profile()
linedata.setObservedProfile(xobs, yobs, dyobs)

# The second step is to create a FitContribution object, which associates
# observed profile with a mathematical model for the dependent variable.

from diffpy.srfit.fitbase import FitContribution
linefit = FitContribution('linefit')
linefit.setProfile(linedata)
linefit.setEquation("A * x + B")

# SrFit objects can be examined by calling their show() function.  SrFit
# parses the model equation and finds two parameters A, B at independent
# variable x.  The values of parameters A, B are at this stage undefined.

linefit.show()
Example #7
0
# Plot the generated "observed" data (xobs, yobs).

import matplotlib.pyplot as plt
plt.ion(); plt.clf(); plt.hold(False)
plt.plot(xobs, yobs, 'x')
plt.title('y = 0.5*x + 3 generated with a normal noise at sigma=0.3')
plt.show()

# <demo> --- stop ---

# We are going to define a line fitting regression using SrFit.
# At first we create a SrFit Profile object that holds the observed data.

from diffpy.srfit.fitbase import Profile
linedata = Profile()
linedata.setObservedProfile(xobs, yobs, dyobs)

# The second step is to create a FitContribution object, which associates
# observed profile with a mathematical model for the dependent variable.

from diffpy.srfit.fitbase import FitContribution
linefit = FitContribution('linefit')
linefit.setProfile(linedata)
linefit.setEquation("A * x + B")

# SrFit objects can be examined by calling their show() function.  SrFit
# parses the model equation and finds two parameters A, B at independent
# variable x.  The values of parameters A, B are at this stage undefined.

linefit.show()
Example #8
0
import matplotlib.pyplot as plt
plt.ion()
plt.clf()
plt.hold(False)
plt.plot(xobs, yobs, 'x')
plt.title('Two peaks simulated')
plt.show()

# <demo> --- stop ---

# We are going to define a line fitting regression using SrFit.
# At first we create a SrFit Profile object that holds the observed data.

from diffpy.srfit.fitbase import Profile
profile = Profile()
profile.setObservedProfile(xobs, yobs)

# The second step is to create a FitContribution object, which associates
# observed profile with a mathematical model for the dependent variable.

from diffpy.srfit.fitbase import FitContribution
large_gaussian = FitContribution("g1")
large_gaussian.setProfile(profile, xname="x")
large_gaussian.registerStringFunction('1/sqrt(2 * pi * lgsig**2)',
                                      name='gaussnorm')
large_gaussian.setEquation(
    "lgA * gaussnorm * exp(-0.5 * (x - lgx0)**2/lgsig**2)")

small_gaussian = FitContribution("g2")
small_gaussian.setProfile(profile, xname="x")
small_gaussian.registerStringFunction('1/sqrt(2 * pi * sgsig**2)',