Peak models

As for the background models, the peak models in Fitspy are issued either from predefined models, or by user-defined models.

Predefined models

The predefined peak models available in Fitspy relies on the following parameters:

  • \(x0\) : the peak position

  • \(ampli\) : the peak amplitude

  • \(fwhm\) : the Full Width at Half Maximum

  • \(fwhm_l\) : the fwhm at the left side of x0 (for the asymmetric models)

  • \(fwhm_r\) : the fwhm at the right side of x0 (for the asymmetric models)

  • \(alpha\) : weighting coefficient (used in the Pseudovoigt model)

Gaussian

\[ampli * exp({-(x-x0)^2/(2*\sigma^2)}) \quad with \quad \sigma = fwhm / (2*\sqrt{2*log(2)})\]

Lorentzian

\[ampli * fwhm^2 / [4 * ((x - x0)^2 + fwhm^2 / 4)]\]

Asymetric Gaussian

\[(x < x0) * Gaussian(fwhm_l) + (x ≥ x0) * Gaussian(fwhm_r)\]

Asymetric Lorentzian

\[(x < x0) * Lorentzian(fwhm_l) + (x ≥ x0) * Lorentzian(fwhm_r)\]

Pseudovoigt

\[alpha * Gaussian + (1 - alpha) * Lorentzian\]

User-defined models

As for the background, users models can be defined through literal expressions in a ‘.txt’ file or from python scripts in a ‘.py’ file.

Example of a model defined by a literal expression:

Gaussian_1 = ampli * exp(-(x - x0) ** 2 / (fwhm**2 / (4 * log(2))))

Example of a model defined in python:

import numpy as np
from lmfit.models import GaussianModel
from fitspy import PEAK_MODELS

LMFIT_GAUSSIAN_MODEL = GaussianModel()

def gaussian_2(x, ampli, fwhm, x0):
    sigma = fwhm / (2. * np.sqrt(2. * np.log(2.)))
    coef = 1. / (2 * sigma ** 2)
    return ampli * np.exp(-coef * (x - x0) ** 2)

def gaussian_3(x, x0, ampli, fwhm):
    sigma = fwhm / (2. * np.sqrt(2. * np.log(2.)))
    return sigma * np.sqrt(2. * np.pi) * LMFIT_GAUSSIAN_MODEL.eval(x=x, center=x0, amplitude=ampli, sigma=sigma)

PEAK_MODELS.update({"Gaussian_2": gaussian_2})
PEAK_MODELS.update({"Gaussian_3": gaussian_3})

where in the examples given above, the resulting Gaussian_1, Gaussian_2 and Gaussian_3 yield identical results to those obtained from the predefined Gaussian model.

The users models must be defined with the same parameters that those used in the predefined models concerning the peak position and amplitude (x and ampli). Concerning the width of the peak models, it is recommended (but not required) to use the fwhm (or derived parameter). Extra parameters can also be used but in such cases, it is important to know that these parameters will be initialized to 1 before the fitting process and will be not subject to any range limitations.

Similarly to the background models:

Through the GUI, the corresponding ‘.txt’ or ‘.py’ files can be loaded via the button Load located to the right of the Peak model combobox.

In python, the users models can be loaded by the functions load_models_from_txt() and load_models_from_py(). (See example in ex_gui_users_defined_models.py )

For recurrent use, the user-defined models can be defined in files named peak_models.txt or peak_models.py to put in %HOMEUSER%/Fitspy.