Model

This module provides a template class to build models that can be used to fit the data.

class model.Component(name, func, skip_convolve=False, **funcArgs)

Component class to be used with the Model class.

Parameters:
  • name (str) – Name for the component.
  • func (callable) – The function to be used for this component.
  • skip_convolve (bool) – If True, no convolution is performed for this model. It can be useful for background or normalization terms.
  • funcArgs (dict of str, int, float or arrays) –

    Values to be passed to the function arguments. This is a dicitonary of argument names mapped to values. The values can be of different types:

    • int, float or array, the values are directly passed to the function.
    • str, the values are evaluated first. If any word in the string is present in the Model.params dictionary keys, the corresponding parameter value is substituted.

Examples

For a Model class that has the following key in its params attribute: (‘amplitude’, ‘sigma’), the component for a Lorentzian, the width of which depends on a defined vector q, can be created using:

>>> def lorentzian(x, scale, width):
...     return scale / np.pi * width / (x**2 + width**2)
>>> myComp = Component(
...     'lor', lorentzian, scale='scale', width='width * q**2')

If the Lorentzian width is constant, use:

>>> myComp = Component('lor', lorentzian, scale='scale', width=5)

Some math functions can be used as well (below the exponential):

>>> myComp = Component('lor', lorentzian, scale='np.exp(-q**2 * msd)')
eval(x, params, **kwargs)

Evaluate the components using the given parameters.

Parameters:
  • params (Parameters instance) – Parameters to be passed to the component
  • kwargs (dict) – Additional parameters to be passed to the function. Can override params.
processFuncArgs(params, **kwargs)

Return the evaluated argument for the function using given parameters and keyword arguments.

class model.FindParamNames(key, params)

Helper class to parse strings to evaluation for function arguments in Component.

Parameters:params (Parameters) – An instance of Parameters from which the parameter names are to be found and substituted by the corresponding values.
visit_Name(node)

Name visitor.

class model.Model(params, name='Model', convolutions=None, on_undef_conv='numeric')

Model class to be used within nPDyn.

The model is structured in components that can be added together, each component consisting of a name, a callable function and a dictionary of parameters. The parameters of two different components can have the same name such that they can be shared by several components just like for the switching diffusive state model.

Also, the components are separated in two classes, namely eisfComponents and qisfComponents, in order to provide the possibility to separately extract the elastic and quasi-elastic parts for analysis and plotting.

Parameters:
  • params (Parameters instance) – Parameters to be used with the model
  • name (str, optional) – A name for the model.
  • convolutions (dict of dict) – Dictionary that defines the mapping ‘(function1, function2)’ to ‘convolutionFunction(function1, function2)’. Analytic convolutions or user defined operators can be defined this way.
  • on_undef_conv ({'raise', 'numeric'}) – Defines the behavior of the class on missing convolution function in the ‘convolutions’ attribute. The option ‘raise’ leads to a KeyError and the option ‘numeric’ to a numerical convolution.
addComponent(comp, op='+')

Add a component to the model.

Parameters:
  • comp (Component) – An instance of Component to be added to the model.
  • op ({"+", "-", "*", "/"}, optional) – Operator to be used to combine the new component with the others. If this is the first component, the operator is ignored. (default “+”)
bic

Return the bayesian information criterion (BIC).

components

Return the model components.

copy()

Return a copy of the model.

eval(x, params=None, convolve=None, **kwargs)
Perform the assembly of the components and call
the provided functions with their parameters to compute the model.
Parameters:
  • x (np.ndarray) – Values for the x-axis variable
  • params (list, np.array, optional) – Parameters to be passed to the components. Will override existing parameters in self.params.
  • convolve (Model) – Another model to be convolved with this one.
  • kwargs – Additional keyword arguments to be passed to the components. Can override params too.
Returns:

  • If returnComponents is False – The computed model in an array, the dimensions of which depend on x and params attributes and the function called.
  • else – A dictionary with key being the component names and the values are the evaluated components.

eval_components(x, params=None, convolve=None, **kwargs)

Alias for eval with ‘returnComponents’ set to True.

Perform the computation of the components with the given x-axis values, parameters and convolutions.

Returns:
  • A dictionary with key being the component names and the values
  • are the evaluated components.
fit(x, data=None, weights=None, fit_method='curve_fit', fit_kws=None, params=None, **kwargs)

Fit the experimental data using the provided arguments.

Parameters:
  • x (np.ndarray) – Values for the indenpendent variable.
  • data (np.ndarray) – Experimental data to be fitted.
  • weights (np.ndarray, optional) – Weights associated with the experimental data (the experimental errors).
  • fit_method (str, optional) – The method to be used for fitting. Currently available methods are (from Scipy): - “curve_fit” - “basinhopping” - “differential_evolution” - “shgo” - “minimize”
  • fit_kws (dict, optional) – Additional keywords to be passed to the fit method.
  • params (Parameters class instance, optional) – Parameters to be used (default None, will use the parameters associated with the model).
  • kwargs (dict, optional) – Additional keywords arguments to give for the evaluation of the model. Can override parameters too.
Returns:

Return type:

A copy of the fitted model instance.

fitResult

Return the full result of the fit.

on_undef_conv

Return the class behavior on undefined convolution.

optParams

Return the result of the fit.

userkws

Return the keywords used for the fit.