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
26 changes: 26 additions & 0 deletions news/fitbase-standards.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
**Added:**

* <news item>

**Changed:**

* Rewrite the ``diffpy.srfit.fitbase`` docstrings in NumPy style, and correct
those that described the wrong types, defaults or raised exceptions.

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* Fix the ``equationFromString`` deprecation warning, which pointed to a
nonexistent ``RecipeOrganizer.get_equation_from_string`` instead of
``diffpy.srfit.fitbase.recipeorganizer.get_equation_from_string``.

**Security:**

* <news item>
99 changes: 54 additions & 45 deletions src/diffpy/srfit/fitbase/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,54 +32,59 @@
class Calculator(Operator, ParameterSet):
"""Base class for calculators.

A Calculator organizes Parameters and has a __call__ method that can
calculate a generic signal.
A Calculator organizes Parameters and has a ``__call__`` method that
can calculate a generic signal. Calculator is also an Operator
(diffpy.srfit.equation.literals.operators), so it can be used directly
in an evaluation network.

Attributes
Parameters
----------
name
A name for this organizer.
meta
A dictionary of metadata needed by the calculator.
_calculators
A managed dictionary of Calculators, indexed by name.
_constraints
A set of constrained Parameters. Constraints can be
added using the 'constrain' methods.
_parameters
A managed OrderedDict of contained Parameters.
_parsets
A managed dictionary of ParameterSets.
_restraints
A set of Restraints. Restraints can be added using the
'restrain' or 'confine' methods.
_eqfactory
A diffpy.srfit.equation.builder.EquationFactory
instance that is used create Equations from string.

Operator Attributes
-------------------
args
List of Literal arguments
nin
Number of inputs (<1 means this is variable)
nout
Number of outputs (1)
operation
Function that performs the operation, self.__call__
symbol
Same as name
_value
The value of the Operator.
value
Property for 'get_value'.

Properties
name : str
The name of the Calculator. It must be a valid attribute name.

Attributes
----------
names
Variable names (read only). See get_names.
values
Variable values (read only). See get_values.
name : str
The name of the Calculator.
meta : dict
The metadata needed by the calculator.
names : list of str
The names of the top-level Parameters (read only). See
``get_names``.
values : list
The values of the top-level Parameters (read only). See
``get_values``.
args : list
The Literal arguments of the operator, added with
``add_literal``.
nin : int
The number of inputs. The class value -1 means that any number of
inputs is allowed.
nout : int
The number of outputs, which is 1.
symbol : str
The symbolic representation of the operator, which is the same as
``name`` (read only).
value : object
The value of the operator (read only). See ``get_value``.
_value : object
The cached value of the operator, or None if it must be
recomputed.
_calculators : dict
The managed dictionary of Calculators, indexed by name.
_constraints : dict
The dictionary of Constraints, indexed by the constrained
Parameter. Constraints can be added using the ``add_constraint``
method.
_parameters : OrderedDict
The managed OrderedDict of contained Parameters.
_parsets : OrderedDict
The managed OrderedDict of contained ParameterSets.
_restraints : set
The set of Restraints. Restraints can be added using the
``add_soft_bounds`` method.
_eqfactory : diffpy.srfit.equation.builder.EquationFactory
The factory that is used to create Equations from strings.
"""

# define abstract attributes from the Operator base.
Expand All @@ -97,6 +102,7 @@ def __init__(self, name):

@property
def symbol(self):
"""The symbolic representation, which is the Calculator name."""
return self.name

# Overload me!
Expand All @@ -122,6 +128,9 @@ def __call__(self, *args):
def operation(self, *args):
"""Calculate and cache the signal produced by this Calculator.

This calls ``__call__`` and stores the result as the operator
value.

Parameters
----------
*args
Expand Down
111 changes: 62 additions & 49 deletions src/diffpy/srfit/fitbase/fitcontribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,46 +88,45 @@ class FitContribution(ParameterSet):

Attributes
----------
name
A name for this FitContribution.
profile
A Profile that holds the measured (and calculated)
signal.
_calculators
A managed dictionary of Calculators, indexed by name.
_constraints
A set of constrained Parameters. Constraints can be
added using the ``constrain`` methods.
_generators
A managed dictionary of ProfileGenerators.
_parameters
A managed OrderedDict of parameters.
_restraints
A set of Restraints. Restraints can be added using the
``restrain`` method.
_parsets
A managed dictionary of ParameterSets.
_eqfactory
A diffpy.srfit.equation.builder.EquationFactory
instance that is used to create constraints and
restraints from string
_eq
The FitContribution equation that will be optimized.
_reseq
The residual equation.
_xname
Name of the x-variable
_yname
Name of the y-variable
_dyname
Name of the dy-variable

Properties
----------
names
Variable names (read only). See get_names.
values
Variable values (read only). See get_values.
name : str
The name of this FitContribution.
profile : Profile or None
The Profile that holds the measured (and calculated)
signal. It is None until ``set_profile`` is called.
names : list of str
The names of the top-level Parameters (read only). See
``get_names``.
values : list
The values of the top-level Parameters (read only). See
``get_values``.
_calculators : dict
The managed dictionary of Calculators, indexed by name.
_constraints : dict
The dictionary of Constraints, indexed by the constrained
Parameter. Constraints can be added using the
``add_constraint`` method.
_generators : dict
The managed dictionary of ProfileGenerators, indexed by name.
_parameters : OrderedDict
The managed OrderedDict of parameters.
_restraints : set
The set of Restraints. Restraints can be added using the
``add_soft_bounds`` method.
_parsets : OrderedDict
The managed OrderedDict of ParameterSets.
_eqfactory : diffpy.srfit.equation.builder.EquationFactory
The factory that is used to create constraints and
restraints from strings.
_eq : Equation or None
The profile equation that calculates the signal.
_reseq : Equation or None
The residual equation that will be optimized.
_xname : str or None
The name of the x-variable.
_yname : str or None
The name of the y-variable.
_dyname : str or None
The name of the dy-variable.
"""

def __init__(self, name):
Expand All @@ -153,6 +152,13 @@ def __init__(self, name):
def set_profile(self, profile, xname=None, yname=None, dyname=None):
"""Assign the Profile for this FitContribution.

The x, y and dy Parameters of the Profile are added to this
FitContribution under the names ``xname``, ``yname`` and
``dyname``. The Profile is also assigned to every
ProfileGenerator of this FitContribution, and the residual
equation is set to ``"chiv"`` if a profile equation exists but no
residual equation has been set.

Parameters
----------
profile : Profile
Expand All @@ -173,6 +179,11 @@ def set_profile(self, profile, xname=None, yname=None, dyname=None):
this is None (default), then the name specified by the
Profile for this parameter will be used. This variable is
usable within string equations with the specified name.

Raises
------
TypeError
If ``profile`` is not a Profile instance.
"""
# Enforce type of the profile argument
if not isinstance(profile, Profile):
Expand Down Expand Up @@ -238,8 +249,9 @@ def add_profile_generator(self, gen, name=None):
gen : ProfileGenerator
The ProfileGenerator instance to add.
name : str, optional
A name for the calculator. If name is None (default), then
the ProfileGenerator's name attribute will be used.
The name used for the ProfileGenerator in the profile
equation. If name is None (default), then the
ProfileGenerator's name attribute will be used.

Raises
------
Expand Down Expand Up @@ -289,14 +301,14 @@ def set_equation(self, eqstr, ns={}):
Parameters
----------
eqstr : str
A string representation of the equation. Any Parameter
The string representation of the equation. Any Parameter
registered by ``add_parameter`` or ``set_profile``, or function
registered by ``register_calculator``, ``register_function`` or
``register_string_function`` can be used in the equation
by name. Other names will be turned into Parameters of this
FitContribution.
ns : dict, optional
A dictionary of Parameters, indexed by name, that are used
The dictionary of Parameters, indexed by name, that are used
in the eqstr, but not registered (default {}).

Raises
Expand Down Expand Up @@ -377,15 +389,13 @@ def set_residual_equation(self, eqstr):
Parameters
----------
eqstr : str
A string representation of the residual. If eqstr is None
(default), then the previous residual equation will be
used, or the chi2 residual will be used if that does not
exist.
The string representation of the residual, or one of the
preset names ``"chiv"`` and ``"resv"``.

Raises
------
SrFitError
If the Profile is not yet defined.
If the Profile or the profile equation is not yet defined.
ValueError
If eqstr depends on a Parameter that is not part of the
FitContribution.
Expand Down Expand Up @@ -478,6 +488,9 @@ def residual(self):
def evaluate(self):
"""Evaluate the contribution equation and update profile.ycalc.

The calculated signal is stored in ``profile.ycalc`` only if a
Profile has been assigned.

Returns
-------
numpy.ndarray
Expand Down
Loading
Loading