Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/examples/coreshellnp.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def makeRecipe(stru1, stru2, datname):
# very little to the PDF.
from diffpy.srfit.pdf.characteristicfunctions import shellCF, sphericalCF

contribution.registerFunction(sphericalCF, name="f_CdS")
contribution.registerFunction(shellCF, name="f_ZnS")
contribution.register_function(sphericalCF, name="f_CdS")
contribution.register_function(shellCF, name="f_ZnS")

# Write the fitting equation. We want to sum the PDFs from each phase and
# multiply it by a scaling factor.
Expand Down
6 changes: 3 additions & 3 deletions docs/examples/debyemodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ def makeRecipe():
contribution.set_profile(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
# to use the 'debye' function defined below. The 'register_function' method
# will let us do this. Since we haven't told it otherwise,
# 'registerFunction' will extract the name of the function ('debye') and
# 'register_function' 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)
contribution.register_function(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
Expand Down
6 changes: 3 additions & 3 deletions docs/examples/debyemodelII.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ def plotResults(recipe):
recipe.highT.profile.set_calculation_range(xmin="obs", xmax="obs")
T = recipe.lowT.profile.x
U = recipe.lowT.profile.y
# We can use a FitContribution's 'evaluateEquation' method to evaluate
# We can use a FitContribution's 'evaluate_equation' method to evaluate
# expressions involving the Parameters and other aspects of the
# FitContribution. Here we evaluate the fitting equation, which is always
# accessed using the name "eq". We access it this way (rather than through
# the Profile's ycalc attribute) because we changed the calculation range
# above, and we therefore need to recalculate the profile.
lowU = recipe.lowT.evaluateEquation("eq")
highU = recipe.highT.evaluateEquation("eq")
lowU = recipe.lowT.evaluate_equation("eq")
highU = recipe.highT.evaluate_equation("eq")

# Now we can plot this.
import pylab
Expand Down
6 changes: 3 additions & 3 deletions docs/examples/npintensity.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def makeRecipe(strufile, datname):

# This creates a callable equation named "bkgd" within the FitContribution,
# and turns the polynomial coefficients into Parameters.
contribution.registerStringFunction(bkgdstr, "bkgd")
contribution.register_string_function(bkgdstr, "bkgd")

# We will create the broadening function that we need by creating a python
# function and registering it with the FitContribution.
Expand All @@ -247,7 +247,7 @@ def gaussian(q, q0, width):

# This registers the python function and extracts the name and creates
# Parameters from the arguments.
contribution.registerFunction(gaussian)
contribution.register_function(gaussian)

# Center the Gaussian so it is not truncated.
contribution.q0.value = x[len(x) // 2]
Expand Down Expand Up @@ -342,7 +342,7 @@ def plotResults(recipe):

Imeas = recipe.bucky.profile.y
Icalc = recipe.bucky.profile.ycalc
bkgd = recipe.bucky.evaluateEquation("bkgd")
bkgd = recipe.bucky.evaluate_equation("bkgd")
diff = Imeas - Icalc

import pylab
Expand Down
12 changes: 6 additions & 6 deletions docs/examples/npintensityII.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ def makeRecipe(strufile, datname1, datname2):
bkgdstr = "b0 + b1*q + b2*q**2 + b3*q**3 + b4*q**4 + b5*q**5 + b6*q**6 +\
b7*q**7 +b8*q**8 + b9*q**9"

contribution1.registerStringFunction(bkgdstr, "bkgd")
contribution2.registerStringFunction(bkgdstr, "bkgd")
contribution1.register_string_function(bkgdstr, "bkgd")
contribution2.register_string_function(bkgdstr, "bkgd")

# We will create the broadening function by registering a python function.
pi = numpy.pi
Expand All @@ -119,8 +119,8 @@ def gaussian(q, q0, width):
* exp(-0.5 * ((q - q0) / width) ** 2)
)

contribution1.registerFunction(gaussian)
contribution2.registerFunction(gaussian)
contribution1.register_function(gaussian)
contribution2.register_function(gaussian)
# Center the gaussian
contribution1.q0.value = x[len(x) // 2]
contribution2.q0.value = x[len(x) // 2]
Expand Down Expand Up @@ -196,11 +196,11 @@ def plotResults(recipe):
# Plot this for fun.
I1 = recipe.bucky1.profile.y
Icalc1 = recipe.bucky1.profile.ycalc
bkgd1 = recipe.bucky1.evaluateEquation("bkgd")
bkgd1 = recipe.bucky1.evaluate_equation("bkgd")
diff1 = I1 - Icalc1
I2 = recipe.bucky2.profile.y
Icalc2 = recipe.bucky2.profile.ycalc
bkgd2 = recipe.bucky2.evaluateEquation("bkgd")
bkgd2 = recipe.bucky2.evaluate_equation("bkgd")
diff2 = I2 - Icalc2
offset = 1.2 * max(I2) * numpy.ones_like(I2)
I1 += offset
Expand Down
6 changes: 3 additions & 3 deletions docs/examples/nppdfcrystal.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def makeRecipe(ciffile, grdata):
# Register the nanoparticle shape factor.
from diffpy.srfit.pdf.characteristicfunctions import sphericalCF

pdfcontribution.registerFunction(sphericalCF, name="f")
pdfcontribution.register_function(sphericalCF, name="f")

# Now we set up the fitting equation.
pdfcontribution.set_equation("f * G")
Expand Down Expand Up @@ -91,10 +91,10 @@ def plotResults(recipe):
diffzero = -0.8 * max(g) * numpy.ones_like(g)
diff = g - gcalc + diffzero

gcryst = recipe.pdf.evaluateEquation("G")
gcryst = recipe.pdf.evaluate_equation("G")
gcryst /= recipe.scale.value

fr = recipe.pdf.evaluateEquation("f")
fr = recipe.pdf.evaluate_equation("f")
fr *= max(g) / fr[0]

import pylab
Expand Down
6 changes: 3 additions & 3 deletions docs/examples/nppdfsas.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def makeRecipe(ciffile, grdata, iqdata):

# Register the calculator with the pdf contribution and define the fitting
# equation.
pdfcontribution.registerCalculator(cfcalculator)
pdfcontribution.register_calculator(cfcalculator)
# The PDF for a nanoscale crystalline is approximated by
# Gnano = f * Gcryst
pdfcontribution.set_equation("f * G")
Expand Down Expand Up @@ -156,10 +156,10 @@ def plotResults(recipe):
diffzero = -0.8 * max(g) * numpy.ones_like(g)
diff = g - gcalc + diffzero

gcryst = recipe.pdf.evaluateEquation("G")
gcryst = recipe.pdf.evaluate_equation("G")
gcryst /= recipe.scale.value

fr = recipe.pdf.evaluateEquation("f")
fr = recipe.pdf.evaluate_equation("f")
fr *= max(g) / fr[0]

import pylab
Expand Down
10 changes: 5 additions & 5 deletions docs/examples/threedoublepeaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def makeRecipe():
def gaussian(t, mu, sig):
return 1 / (2 * pi * sig**2) ** 0.5 * exp(-0.5 * ((t - mu) / sig) ** 2)

contribution.registerFunction(gaussian, name="peakshape")
contribution.register_function(gaussian, name="peakshape")

def delta(t, mu):
"""Calculate a delta-function.
Expand All @@ -70,12 +70,12 @@ def delta(t, mu):
sig = t[1] - t[0]
return gaussian(t, mu, sig)

contribution.registerFunction(delta)
contribution.register_function(delta)

# Here is another one
bkgdstr = "b0 + b1*t + b2*t**2 + b3*t**3 + b4*t**4 + b5*t**5 + b6*t**6"

contribution.registerStringFunction(bkgdstr, "bkgd")
contribution.register_string_function(bkgdstr, "bkgd")

# Now define our fitting equation. We will hardcode the peak ratios.
contribution.set_equation(
Expand Down Expand Up @@ -120,7 +120,7 @@ def peakloc(mu):
l2 = 1.0
return 180 / pi * arcsin(pi / 180 * l2 * sin(mu) / l1)

recipe.registerFunction(peakloc)
recipe.register_function(peakloc)
recipe.constrain(contribution.mu12, "peakloc(mu11)")
recipe.constrain(contribution.mu22, "peakloc(mu21)")
recipe.constrain(contribution.mu32, "peakloc(mu31)")
Expand All @@ -134,7 +134,7 @@ def sig(sig0, dsig, mu):
"""Calculate the peak broadening with respect to position."""
return sig0 * (1 - dsig * mu**2)

recipe.registerFunction(sig)
recipe.register_function(sig)
recipe.fix("mu")
# Now constrain the peak widths to this
recipe.sig0.value = 0.001
Expand Down
37 changes: 37 additions & 0 deletions news/recipeorg-dep1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
**Added:**

* Added ``iterate_over_parameters`` method.
* Added ``register_calculator`` method.
* Added ``register_function`` method.
* Added ``register_string_function`` method.
* Added ``evaluate_equation`` method.
* Added ``is_constrained`` method.
* Added ``get_constrained_parameters`` method.
* Added ``clear_all_constraints`` method.

**Changed:**

* <news item>

**Deprecated:**

* Deprecated ``iterPars`` method. Use ``iterate_over_parameters`` instead.
* Deprecated ``registerCalculator`` method. Use ``register_calculator`` instead.
* Deprecated ``registerFunction`` method. Use ``register_function`` instead.
* Deprecated ``registerStringFunction`` method. Use ``register_string_function`` instead.
* Deprecated ``evaluateEquation`` method. Use ``evaluate_equation`` instead.
* Deprecated ``isConstrained`` method. Use ``is_constrained`` instead.
* Deprecated ``getConstrainedPars`` method. Use ``get_constrained_parameters`` instead.
* Deprecated ``clearConstraints`` method. Use ``clear_all_constraints`` instead.

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
40 changes: 32 additions & 8 deletions src/diffpy/srfit/equation/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@

_builders = {}

EquationFactory_base = "diffpy.srfit.equation.builder.EquationFactory"
removal_version = "4.0.0"

registerFunction_dep_msg = build_deprecation_message(
EquationFactory_base,
"registerFunction",
"register_function",
removal_version,
)


class EquationFactory(object):
"""A Factory for equations.
Expand Down Expand Up @@ -208,23 +218,26 @@ def registerOperator(self, name, op):
opbuilder = wrapOperator(name, op)
return self.registerBuilder(name, opbuilder)

def registerFunction(self, name, func, argnames):
def register_function(self, name, func, argnames):
"""Register a named function with the factory.

This will register a builder for the function.

Attributes
Parameters
----------
name
name : str
The name of the function
func
A callable python object
argnames
func : callable
The callable python object
argnames : list of str
The argument names for func. If these names do not
correspond to builders, then new constants with value 0
will be created for each name.

Returns the registered builder.
Returns
-------
registered_builder : OperatorBuilder
The registered builder.
"""
for n in argnames:
if n not in self.builders:
Expand All @@ -234,8 +247,19 @@ def registerFunction(self, name, func, argnames):
builder = self.builders[argname]
argliteral = builder.literal
opbuilder.literal.addLiteral(argliteral)
registered_builder = self.registerBuilder(name, opbuilder)
return registered_builder

return self.registerBuilder(name, opbuilder)
@deprecated(registerFunction_dep_msg)
def registerFunction(self, name, func, argnames):
"""This function has been deprecated and will be removed in
version 4.0.0.

Please use
diffpy.srfit.equation.builder.EquationFactory.register_function
instead.
"""
return self.register_function(name, func, argnames)

def registerBuilder(self, name, builder):
"""Register builder in this module so it can be used in
Expand Down
2 changes: 1 addition & 1 deletion src/diffpy/srfit/fitbase/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
overloaded to accept external arguments. Calculators are used to wrap
registered functions so that the function's Parameters are contained in
an object specific to the function. A custom Calculator can be added to
another RecipeOrganizer with the 'registerCalculator' method.
another RecipeOrganizer with the 'register_calculator' method.
"""

__all__ = ["Calculator"]
Expand Down
4 changes: 2 additions & 2 deletions src/diffpy/srfit/fitbase/fitcontribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ def set_equation(self, eqstr, ns={}):
eqstr
A string representation of the equation. Any Parameter
registered by addParameter or setProfile, or function
registered by setCalculator, registerFunction or
registerStringFunction can be can be used in the equation
registered by setCalculator, register_function or
register_string_function can be can be used in the equation
by name. Other names will be turned into Parameters of this
FitContribution.
ns
Expand Down
6 changes: 3 additions & 3 deletions src/diffpy/srfit/fitbase/fitrecipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class FitRecipe(_fitrecipe_interface, RecipeOrganizer):
lambda self: [
v.name
for v in self._parameters.values()
if not (self.is_free(v) or self.isConstrained(v))
if not (self.is_free(v) or self.is_constrained(v))
],
doc="names of the fixed refinable variables",
)
Expand All @@ -204,7 +204,7 @@ class FitRecipe(_fitrecipe_interface, RecipeOrganizer):
[
v.value
for v in self._parameters.values()
if not (self.is_free(v) or self.isConstrained(v))
if not (self.is_free(v) or self.is_constrained(v))
]
),
doc="values of the fixed refinable variables",
Expand Down Expand Up @@ -622,7 +622,7 @@ def __verify_parameters(self):

# Get all parameters with a value of None
badpars = []
for par in self.iterPars():
for par in self.iterate_over_parameters():
try:
par.getValue()
except ValueError:
Expand Down
2 changes: 1 addition & 1 deletion src/diffpy/srfit/fitbase/parameterset.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def setConst(self, const=True):
Flag indicating if the parameter is constant (default
True).
"""
for par in self.iterPars():
for par in self.iterate_over_parameters():
par.setConst(const)

return
Expand Down
Loading
Loading