Module Parameters

Provides an easy and transparent handling of fit parameters.

Fitparameters can be handled as instances of Fitparameter which are collected and managed by an instance of ParameterPool. To allow for transparent modelling, Fitparameter is derived from Parameter which can hold a constant value. I.e. the code using a parameter does not have to know if it is a constant Parameter or a variable Fitparameter. The values of both are obtained in the same way with getValue.

Derived parameters which are arbitrary functions of other parameters can be defined with instance of DerivedParameter.

All parameter classes support also the creation of simple derived paramters by arithmetic operations.

Functions of one variable which are parametrized can be defined with instances of :class:` ParametrizedFunction`. It is not derived from the class Parameter.

>>> from PyXMRTool import Parameters
>>> import math
>>> const=Parameters.Parameter(30.7)
>>> const.getValue()
30.7
>>> pp=Parameters.ParameterPool()
>>> par_real=pp.newParameter('par_real',start_val=0,lower_lim=-10,upper_lim=10)
>>> par_complex=pp.newParameter('par_complex',start_val=0+0j,lower_lim=-10-1j,upper_lim=100.3+20j)
>>> fitpararray = [1,2,3]
>>> par_real.getValue(fitpararray)
1
>>> par_complex.getValue(fitpararray)
(2+3j)
>>> simple_derived_par = ((par_complex + 5*par_real)*0.68)**const
>>> simple_derived_par.getValue(fitpararray)
(8.367641368810413e+21-1.1467109965804664e+21j)
>>>
>>> def sinus(A,w): return A*math.sin(w)
>>> derived_par = Parameters.DerivedParameter(sinus, const, par_real)
>>> derived_par.getValue(fitpararray)
25.833159233602423
>>> print((derived_par**2).getValue(fitpararray))
667.352115989
>>> def growth(t, A, rate): return A*math.exp(t/float(rate))
>>> par_func_growth = Parameters.ParametrizedFunction(growth, const, par_real)
>>> print(par_func_growth.getValue(5.3,fitpararray))
6150.34006623
>>> f_growth = par_func_growth.getFunction(fitpararray)
>>> print(f_growth(5.3))
6150.34006623
class Parameters.Parameter(value=None)[source]

Bases: object

Base class for parameters. It contains a (complex) value which is set as instantiation and cannot be changed.

This class (and all derived classes) supports creation of dependent parameters by arithmetic operations.

__init__(value=None)[source]

Initialize with a number as (complex) value.

getValue(fitpararray=None)[source]

Returns the value.

fitpararray is not used and only there to make this function forward compatible with the Fitparameter class and with dependent parameters.

class Parameters.Fitparameter(name, fixed=False, start_val=None, lower_lim=None, upper_lim=None)[source]

Bases: Parameters.Parameter

Contains name, starting value and limits of a fitting parameter and knows how to get the parameter value out of an array of values of fitparameters if it is attached to an instance of ParameterPool.

__init__(name, fixed=False, start_val=None, lower_lim=None, upper_lim=None)[source]

Initialize a fitparameter at least with a name.

Usually, you (the user) should not instantiate a fitparameter directly. Instead use ParameterPool.newParameter().

Parameters:
  • name (str) – Name of the fit parameter. Use names without whitespaces. Otherwise there can be problems when using a parameter file.
  • fixed (bool) – If True,the parameter will not be varied during a fit routine and the limits are not necessary.
  • start_val (number) – Start value of the fit parameter.
  • upper_lim (number) – Lower limit of the fit parameter.
  • upper_lim – Upper limit of the fit parameter.
fix()[source]

Fix parameter during fitting.

unfix()[source]

Set parameter as variable during fitting.

getValue(fitpararray=None)[source]

Return the value of the parameter corresponding to the given array of values.

This method only works if the fitparameter is connected to an instance of ParameterPool. Therefore, create instances of Fitparameter always using ParameterPool.newParameter().

name

(str) Name of the parameter. Read-only.

fixed

(bool) Determines if fitparameter is fixed (True) or unfixed (False) during fitting procedures.

start_val

(number) Start value of the fitparameter for fitting procedures.

lower_lim

(number) Lower limit of the fitparameter for fitting procedures.

upper_lim

(number) Upper limit of the fitparameter for fitting procedures.

index

(int) Determines which entry in the fitparameter array is interpreted as the value of this fitparameter. Usually this property should not be changed by you (the user) but by the instance of ParameterPool where the fitparameter is connected to.

complex

(bool) Signals if fitparameter is a complex number (True) or not (False). Read-only.

class Parameters.DerivedParameter(f, *args)[source]

Bases: Parameters.Parameter

Objects of this class represent derived parameters which can be arbitrary functions of other parameters.

__init__(f, *args)[source]

Initialize a derived parameter as function f of the parameters *args which are instances of the class Parameter.

BEWARE: The user is responsible for a matching number of arguments.

Parameters:
  • f (function) – Function of a arbitrary number of real or complex arguments which returns a real or complex number
  • *args (Parameters.Parameter) – The parameter objects from which the DerivedParameter object is derived. Should be the same number of arguments as expected by f. (The star means that this is a variable number of arguments).
getValue(fitpararray=None)[source]

Return the value of the derived parameter corresponding to the given array of fit values.

fitpararray is only allowed to be None if the DerivedParameter is not derived from instances of Fitparameter.

class Parameters.ParameterPool(parfilename=None)[source]

Bases: object

Collects a pool of Parameter objects and connects them with a parameter file.

__init__(parfilename=None)[source]

Initialize a new ParameterPool.

Read parameter initialisation from file parfilename if given. As soon as you connect a parameter file to the pool, its initialisation values have priority over local initialisations with newParameter().

newParameter(name, fixed=False, start_val=None, lower_lim=None, upper_lim=None)[source]

Create a new Fitparameter inside the parameter pool and return a reference to it or return the reference to an already existing one with the same name. This is the usual way to create instances of Fitparameter.

Parameters:
  • name (str) – Name of the fit parameter. Use names without whitespaces. Otherwise there can be problems when using a parameter file.
  • fixed (bool) – If True,the parameter will not be varied during a fit routine and the limits are not necessary.
  • start_val (number) – Start value of the fit parameter.
  • upper_lim (number) – Lower limit of the fit parameter.
  • upper_lim – Upper limit of the fit parameter.
getParameter(name)[source]

Return existing parameter with name name or return None if not existing.

getIndex(name)[source]

Return index of an existing parameter with name name within the expected fitpararray.

readFromFile(parfilename)[source]

Read parameters and there initialisation values from file parfilename*, append them to the pool or overwrite existing once.

Lines in the parameter file should be in the format:
<start_value>  <fixed>  <lower_limit>  <upper_limit>  <name>  <comments/other stuff>
Lines starting with # are ignored.
Values of <fixed> have to be either 0 or 1 (not False or True).
writeToFile(parfilename, fitpararray=None)[source]

Write parameters and there initialisation value to file parfilename.

Can be used to create a template for a parameter initialisation file or to store parameters after fitting. If fitpararray is given, these values are written as start values. Else the stored start values are used.

getStartLowerUpper()[source]

Return a tupel of fitpararrays of parameter start values, lower and upper limits for usage with Fitparameter.getValue().

Each of the arrays contains the values in the order of occurence of the corresponding parameter in the pool (order of parameter creation), but only of those parameters which are not fixed. Real parameters first, then the complex ones.

setStartValues(fitpararray)[source]

Set the start values of all parameters which are not fixed using fitpararray in the order of occurence in the pool (order of parameter creation). First real and then complex ones.

Can be used e.g. to write the result of a fit as start values a the parameter file.

getFitArrayLen()[source]

Return length of the fitpararray which is needed for setStartValues(), writeToFile() and Fitparameter.getValue() and which is given by getStartLowerUpper().

getNames()[source]

Return the names of all registered Fitparameters as a list in the same order they should be in the fitpararray.

class Parameters.ParametrizedFunction(f, *args)[source]

Bases: object

Object of this class can be used for an easy implementation of a function of one argument which should be parametrized by fitparameters.

__init__(f, *args)[source]

Initialize the parametrized function with a function f and an arbitrary number of parameters *args.

BEWARE: The user is responsible for a matching number of arguments.

Parameters:
  • f (function) – Function f(x,*f_args) of an argument x and and an arbitrary number of real or complex arguments, which parametrize the function.
  • *args (Parameters.Parameter) – The parameter objects which parametrize the function. Should be the same number of arguments as expected by f without the x.
getValue(x, fitpararray=None)[source]

Return the value of the parametrized function for a certain x corresponding to the given array of fit values.

fitpararray is only allowed to be None if the used Parameters are not derived from instances of Fitparameter.

getFunction(fitpararray=None)[source]

Return the function for a specific set of parameter values given by the fitpararray.

fitpararray is only allowed to be None if the used parameters are not derived from instances of Fitparameter.

getParameter(x)[source]

Return the parametrized value for a certain x. Result is an instance of DerivedParameter

x can also be an instance of Parameter!!!

fitpararray is only allowed to be None if the used parameters are not derived from instances of Fitparameter.