erlab.analysis.fit.functions.dynamic

Class-based dynamic functions for fitting.

These functions are not limited to a single function form, and can be used to create complex models.

Functions

get_args_kwargs(func)

Get all argument names and default values from a function signature.

Classes

DynamicFunction()

Base class for dynamic functions.

FermiEdge2dFunction([degree])

MultiPeakFunction(npeaks[, peak_shapes, fd, ...])

Multiple peaks with optional Fermi-Dirac distribution and background.

PolynomialFunction([degree])

A callable class for an arbitrary degree polynomial.

class erlab.analysis.fit.functions.dynamic.DynamicFunction[source]

Bases: object

Base class for dynamic functions.

Dynamic functions exploits the way lmfit handles asteval functions in lmfit.Model._parse_params.

property argnames: list[str]
property kwargs: list[tuple[str, float]]
class erlab.analysis.fit.functions.dynamic.FermiEdge2dFunction(degree=1)[source]

Bases: DynamicFunction

property argnames: list[str]
property kwargs: list[tuple[str, float]]
pre_call(eV, alpha, **params)[source]
class erlab.analysis.fit.functions.dynamic.MultiPeakFunction(npeaks, peak_shapes=None, *, fd=True, background='linear', degree=2, convolve=True, oversample=3, segmented=False)[source]

Bases: DynamicFunction

Multiple peaks with optional Fermi-Dirac distribution and background.

Parameters:
  • npeaks (int) – The number of peaks to fit.

  • peak_shapes (list[str] | str | None, default: None) – The shape(s) of the peaks in the model. If a list of strings is provided, each string represents the shape of a peak. If a single string is provided, it will be split by spaces to create a list of peak shapes. If not provided, the default peak shape will be used for all peaks.

  • fd (bool, default: True) – Flag indicating whether the model should be multiplied by the Fermi-Dirac distribution. This adds three parameters to the model: efermi, temp, and offset, each corresponding to the Fermi level, temperature in K, and constant background.

  • background (Literal['constant', 'linear', 'polynomial', 'none', 'shirley'], default: "linear") –

    The type of background to include in the model. Possible values are:

    Value

    Additional parameters

    ’none’

    None

    ’constant’

    const_bkg

    ’linear’

    lin_bkg, const_bkg

    ’polynomial’

    c0, c1, … depending on degree

    ’shirley’

    const_bkg, lin_bkg, k_slope, and k_step_i with i from 0 to npeaks - 1

    Note

    The ‘shirley’ background is calculated by erlab.analysis.fit.functions.general.active_shirley() See its documentation for details about the parameters.

  • degree (int, default: 2) – The degree of the polynomial background. Only used if background is 'polynomial'. Default is 2.

  • convolve (bool, default: True) – Flag indicating whether the model should be convolved with a gaussian kernel. If True, adds a resolution parameter to the model, corresponding to the FWHM of the gaussian kernel.

  • oversample (int, default: 3) – Factor by which to oversample x during convolution to reduce numerical artifacts.

  • segmented (bool, default: False) – Flag indicating whether to convolve the model in segments. If True, the model will be convolved in segments of uniform spacing. This must be set to True when fitting data with large gaps or discontinuities in the x-axis.

PEAK_SHAPES: ClassVar[dict[Callable, list[str]]] = {<function gaussian_wh>: ['gaussian', 'gauss', 'g'], <function lorentzian_wh>: ['lorentzian', 'lor', 'l'], <function voigt>: ['voigt', 'v']}

Mapping of peak functions to their string aliases.

DEFAULT_PEAK: str = 'lorentzian'
property peak_all_args: dict[Callable, PeakArgs]
property peak_argnames: dict[Callable, list[str]]
property peak_funcs: Sequence[Callable]
property argnames: list[str]
property kwargs: list[tuple[str, float]]
peak_param_hints(index, prefix)[source]
eval_peak(index, x, **params)[source]
eval_peaks(x, **params)[source]

Get a list of peak contributions.

eval_bkg_components(x, **params)[source]
eval_bkg(x, **params)[source]
eval_fd(x, **params)[source]
pre_call(x, **params)[source]
class erlab.analysis.fit.functions.dynamic.PolynomialFunction(degree=1)[source]

Bases: DynamicFunction

A callable class for an arbitrary degree polynomial.

Parameters:

degree (int, default: 1) – The degree of the polynomial.

property argnames: list[str]
property kwargs: list[tuple[str, float]]
erlab.analysis.fit.functions.dynamic.get_args_kwargs(func)[source]

Get all argument names and default values from a function signature.

Parameters:

func (Callable) – The function to inspect.

Returns:

  • args (list of str) – A list of argument names with no default value.

  • args_default (dict) – A dictionary of keyword arguments with their default values.

Return type:

tuple[list[str], dict[str, Any]]

Note

This function does not support function signatures containing varargs.

Example

>>> def my_func(a, b=10):
...     pass
>>> get_args_kwargs(my_func)
(['a'], {'b': 10})