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])

Polynomial Fermi edge with a linear intensity background.

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

Polynomial Fermi edge with a linear intensity background.

The edge position is

\[E_F(\alpha) = \sum_{i=0}^{n} c_i \alpha^i,\]

where degree is \(n\). A Gaussian convolution is applied along eV.

Parameters:

degree (int, default: 1) – Degree of the polynomial that describes the edge position as a function of alpha.

Notes

eV, \(E_F\), and resolution are in eV. alpha is in degrees, temp is in K, and resolution is the Gaussian FWHM. Coefficient c{i} has units of eV per degree raised to i. const_bkg and offset have the units of the returned intensity. lin_bkg has intensity per eV units.

Calling the function with separate NumPy coordinates returns a flattened array in (eV, alpha) order. If both coordinates are xarray.DataArray objects, the result has their broadcast dimensions and coordinates. If only one coordinate is a DataArray, the result is a NumPy array. The Gaussian convolution delegates to do_convolve_2d().

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. Supported shapes are "lorentzian", "gaussian", and "voigt", together with their documented aliases. If omitted, all peaks are Lorentzian.

  • fd (bool, default: True) – Whether to multiply the peaks and background by a Fermi-Dirac distribution. This adds efermi in the units of x, temp in K, and offset in the units of the dependent data. When this option is enabled, x and efermi must be in eV.

  • 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) – Whether to convolve the complete model with a Gaussian kernel. If True, the model includes resolution, the Gaussian FWHM in the units of x.

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

  • segmented (bool, default: False) – Whether to convolve the model in contiguous uniformly spaced segments. Use True when x contains large gaps or discontinuities.

Notes

Peak parameters use the prefix p{i}_, where i starts at zero. All peak positions and widths use the units of x.

  • Gaussian and Lorentzian peaks use center, width, and height. width is the FWHM and height is the intensity at center. The corresponding sigma or gamma and amplitude parameters are derived.

  • Voigt peaks use center, sigma, gamma, and amplitude. sigma is the Gaussian standard deviation, gamma is the Lorentzian HWHM, and amplitude is the integrated peak area. width and height are derived.

Background parameter units follow from the dependent data and x. For example, const_bkg has intensity units and lin_bkg has intensity per unit of x.

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})