Models
About implemented models
In principle, every model can be implemented in spectrafit
by extending the module spectrafit.models
by a new functions. It is important to know that the raise
check have to be extend by the new function name in the solver_model
and calculated_model
.
__implemented_models__ = [
"gaussian",
"lorentzian",
"voigt",
"pseudovoigt",
"exponential",
"power",
"linear",
"constant",
"erf",
"atan",
"log",
"heaviside",
"my_new_model",
]
...
for model in params:
model = model.lower()
if model.split("_")[0] not in __implemented_models__:
raise KeyError(f"{model} is not supported")
peak_kwargs[(model.split("_")[-1], model.split("_")[0])][model.split("_")[1]] = (
params[model]
)
for key, _kwarg in peak_kwargs.items():
if key[1] == "my_new_model":
val += my_new_model(x, **_kwarg)
...
def my_new_model(
x: np.array, amplitude: float = 1.0, center: float = 0.0, fwhmg: float = 1.0
) -> np.array:
r"""Return a 1-dimensional `m`y_new_model` distribution."""
...
lmfit
can be found in this example. So far, the built-in models of lmfit are not supported, yet. Change in notation for the Full Maximum Half Widht (FWHM)
The notation for the Full Maximum Half Widht (FWHM) is adapted due to changes in the **kwargs
-handling in the models; see also API and CHANGELOG. The notation becomes:
Method | Old Notation | New Notation |
---|---|---|
Gaussian-FWHM | fwhm | fwhmg |
Lorentzian-FWHM | fwhm | fwhml |
Pseudo-Voigt | fwhm_g | fwhmg |
Pseudo-Voigt | fwhm_l | fwhml |
Voigt | fwhm | fwhmv |
Implemented models¶
Here is a list of implemented models of spectrafit
:
Return a 1-dimensional Gaussian distribution.
\[ {\displaystyle g(x)={\frac {1}{\sigma {\sqrt {2\pi }}}}\exp ( -{\frac {1}{2}}{\frac {(x-\mu )^{2}}{\sigma ^{2}}} ) } \]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
amplitude | float | Amplitude of the Gaussian distribution. Defaults to 1.0. | 1.0 |
center | float | Center of the Gaussian distribution. Defaults to 0.0. | 0.0 |
fwhmg | float | Full width at half maximum (FWHM) of the Gaussian distribution. Defaults to 1.0. | 1.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Gaussian distribution of |
Source code in spectrafit/models.py
@staticmethod
def gaussian(
x: NDArray[np.float64],
amplitude: float = 1.0,
center: float = 0.0,
fwhmg: float = 1.0,
) -> NDArray[np.float64]:
r"""Return a 1-dimensional Gaussian distribution.
$$
{\displaystyle g(x)={\frac {1}{\sigma {\sqrt {2\pi }}}}\exp
( -{\frac {1}{2}}{\frac {(x-\mu )^{2}}{\sigma ^{2}}} ) }
$$
Args:
x (NDArray[np.float64]): `x`-values of the data.
amplitude (float, optional): Amplitude of the Gaussian distribution.
Defaults to 1.0.
center (float, optional): Center of the Gaussian distribution.
Defaults to 0.0.
fwhmg (float, optional): Full width at half maximum (FWHM) of the Gaussian
distribution. Defaults to 1.0.
Returns:
NDArray[np.float64]: Gaussian distribution of `x` given.
"""
sigma = fwhmg * Constants.fwhmg2sig
return np.array(amplitude / (Constants.sq2pi * sigma)) * np.exp(
-((1.0 * x - center) ** 2) / (2 * sigma**2)
)
Return a 1-dimensional Lorentzian distribution.
\[ f(x;x_{0},\gamma )={\frac {1}{\pi \gamma [ 1+ ( {\frac {x-x_{0}}{\gamma }})^{2} ] }} ={1 \over \pi \gamma } [ {\gamma ^{2} \over (x-x_{0})^{2}+\gamma ^{2}} ] \]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
amplitude | float | Amplitude of the Lorentzian distribution. Defaults to 1.0. | 1.0 |
center | float | Center of the Lorentzian distribution. Defaults to 0.0. | 0.0 |
fwhml | float | Full width at half maximum (FWHM) of the Lorentzian distribution. Defaults to 1.0. | 1.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | Union[NDArray[np.float64], float]: Lorentzian distribution of |
Source code in spectrafit/models.py
@staticmethod
def lorentzian(
x: NDArray[np.float64],
amplitude: float = 1.0,
center: float = 0.0,
fwhml: float = 1.0,
) -> NDArray[np.float64]:
r"""Return a 1-dimensional Lorentzian distribution.
$$
f(x;x_{0},\gamma )={\frac {1}{\pi \gamma
[ 1+ ( {\frac {x-x_{0}}{\gamma }})^{2} ]
}} ={1 \over \pi \gamma } [ {\gamma ^{2} \over (x-x_{0})^{2}+\gamma ^{2}} ]
$$
Args:
x (NDArray[np.float64]): `x`-values of the data.
amplitude (float, optional): Amplitude of the Lorentzian distribution.
Defaults to 1.0.
center (float, optional): Center of the Lorentzian distribution. Defaults to
0.0.
fwhml (float, optional): Full width at half maximum (FWHM) of the Lorentzian
distribution. Defaults to 1.0.
Returns:
Union[NDArray[np.float64], float]: Lorentzian distribution of `x` given.
"""
sigma = fwhml * Constants.fwhml2sig
return np.array(amplitude / (1 + ((1.0 * x - center) / sigma) ** 2)) / (
pi * sigma
)
Return a 1-dimensional Pseudo-Voigt distribution.
See also:
J. Appl. Cryst. (2000). 33, 1311-1316 doi.org/10.1107/S0021889800010219
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
amplitude | float | Amplitude of the Pseudo-Voigt distribution. Defaults to 1.0. | 1.0 |
center | float | Center of the Pseudo-Voigt distribution. Defaults to 0.0. | 0.0 |
fwhmg | float | Full width half maximum of the Gaussian distribution in the Pseudo-Voigt distribution. Defaults to 1.0. | 1.0 |
fwhml | float | Full width half maximum of the Lorentzian distribution in the Pseudo-Voigt distribution. Defaults to 1.0. | 1.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Pseudo-Voigt distribution of |
Source code in spectrafit/models.py
@staticmethod
def pseudovoigt(
x: NDArray[np.float64],
amplitude: float = 1.0,
center: float = 0.0,
fwhmg: float = 1.0,
fwhml: float = 1.0,
) -> NDArray[np.float64]:
"""Return a 1-dimensional Pseudo-Voigt distribution.
!!! note "See also:"
J. Appl. Cryst. (2000). 33, 1311-1316
https://doi.org/10.1107/S0021889800010219
Args:
x (NDArray[np.float64]): `x`-values of the data.
amplitude (float, optional): Amplitude of the Pseudo-Voigt distribution.
Defaults to 1.0.
center (float, optional): Center of the Pseudo-Voigt distribution.
Defaults to 0.0.
fwhmg (float, optional): Full width half maximum of the Gaussian
distribution in the Pseudo-Voigt distribution. Defaults to 1.0.
fwhml (float, optional): Full width half maximum of the Lorentzian
distribution in the Pseudo-Voigt distribution. Defaults to 1.0.
Returns:
NDArray[np.float64]: Pseudo-Voigt distribution of `x` given.
"""
f = np.power(
fwhmg**5
+ 2.69269 * fwhmg**4 * fwhml
+ 2.42843 * fwhmg**3 * fwhml**2
+ 4.47163 * fwhmg**2 * fwhml**3
+ 0.07842 * fwhmg * fwhml**4
+ fwhml**5,
0.2,
)
n = (
1.36603 * (fwhml / f)
- 0.47719 * (fwhml / f) ** 2
+ 0.11116 * (fwhml / f) ** 3
)
return np.array(
n
* DistributionModels.lorentzian(
x=x, amplitude=amplitude, center=center, fwhml=fwhml
)
+ (1 - n)
* DistributionModels.gaussian(
x=x, amplitude=amplitude, center=center, fwhmg=fwhmg
)
)
Return a 1-dimensional Voigt distribution.
\[ {\displaystyle V(x;\sigma ,\gamma )\equiv \int_{-\infty }^{\infty }G(x';\sigma ) L(x-x';\gamma )\,dx'} \]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
center | float | Center of the Voigt distribution. Defaults to 0.0. | 0.0 |
fwhmv | float | Full width at half maximum (FWHM) of the Lorentzian distribution. Defaults to 1.0. | 1.0 |
gamma | float | Scaling factor of the complex part of the Faddeeva Function. Defaults to None. | None |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Voigt distribution of |
Source code in spectrafit/models.py
@staticmethod
def voigt(
x: NDArray[np.float64],
center: float = 0.0,
fwhmv: float = 1.0,
gamma: Optional[float] = None,
) -> NDArray[np.float64]:
r"""Return a 1-dimensional Voigt distribution.
$$
{\displaystyle V(x;\sigma ,\gamma )\equiv
\int_{-\infty }^{\infty }G(x';\sigma )
L(x-x';\gamma )\,dx'}
$$
Args:
x (NDArray[np.float64]): `x`-values of the data.
center (float, optional): Center of the Voigt distribution. Defaults to 0.0.
fwhmv (float, optional): Full width at half maximum (FWHM) of the Lorentzian
distribution. Defaults to 1.0.
gamma (float, optional): Scaling factor of the complex part of the
[Faddeeva Function](https://en.wikipedia.org/wiki/Faddeeva_function).
Defaults to None.
Returns:
NDArray[np.float64]: Voigt distribution of `x` given.
"""
sigma = fwhmv * Constants.fwhmv2sig
if gamma is None:
gamma = sigma
z = (x - center + 1j * gamma) / (sigma * Constants.sq2)
return np.array(wofz(z).real / (sigma * Constants.sq2pi))
Return a 1-dimensional exponential decay.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
amplitude | float | Amplitude of the exponential function. Defaults to 1.0. | 1.0 |
decay | float | Decay of the exponential function. Defaults to 1.0. | 1.0 |
intercept | float | Intercept of the exponential function. Defaults to 0.0. | 0.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Exponential decay of |
Source code in spectrafit/models.py
@staticmethod
def exponential(
x: NDArray[np.float64],
amplitude: float = 1.0,
decay: float = 1.0,
intercept: float = 0.0,
) -> NDArray[np.float64]:
"""Return a 1-dimensional exponential decay.
Args:
x (NDArray[np.float64]): `x`-values of the data.
amplitude (float, optional): Amplitude of the exponential function.
Defaults to 1.0.
decay (float, optional): Decay of the exponential function. Defaults to 1.0.
intercept (float, optional): Intercept of the exponential function.
Defaults to 0.0.
Returns:
NDArray[np.float64]: Exponential decay of `x` given.
"""
return np.array(amplitude * np.exp(-x / decay) + intercept)
Return a 1-dimensional power function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
amplitude | float | Amplitude of the power function. Defaults to 1.0. | 1.0 |
exponent | float | Exponent of the power function. Defaults to 1.0. | 1.0 |
intercept | float | Intercept of the power function. Defaults to 0.0. | 0.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: power function of |
Source code in spectrafit/models.py
@staticmethod
def power(
x: NDArray[np.float64],
amplitude: float = 1.0,
exponent: float = 1.0,
intercept: float = 0.0,
) -> NDArray[np.float64]:
"""Return a 1-dimensional power function.
Args:
x (NDArray[np.float64]): `x`-values of the data.
amplitude (float, optional): Amplitude of the power function. Defaults to
1.0.
exponent (float, optional): Exponent of the power function. Defaults to 1.0.
intercept (float, optional): Intercept of the power function. Defaults to
0.0.
Returns:
NDArray[np.float64]: power function of `x` given.
"""
return np.array(amplitude * np.power(x, exponent) + intercept)
Return a 1-dimensional linear function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
slope | float | Slope of the linear function. Defaults to 1.0. | 1.0 |
intercept | float | Intercept of the linear function. Defaults to 0.0. | 0.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Linear function of |
Source code in spectrafit/models.py
@staticmethod
def linear(
x: NDArray[np.float64],
slope: float = 1.0,
intercept: float = 0.0,
) -> NDArray[np.float64]:
"""Return a 1-dimensional linear function.
Args:
x (NDArray[np.float64]): `x`-values of the data.
slope (float, optional): Slope of the linear function. Defaults to 1.0.
intercept (float, optional): Intercept of the linear function.
Defaults to 0.0.
Returns:
NDArray[np.float64]: Linear function of `x` given.
"""
return np.array(slope * x + intercept)
Return a 1-dimensional constant value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
amplitude | float | Amplitude of the constant. Defaults to 1.0. | 1.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Constant value of |
Source code in spectrafit/models.py
@staticmethod
def constant(
x: NDArray[np.float64],
amplitude: float = 1.0,
) -> NDArray[np.float64]:
"""Return a 1-dimensional constant value.
Args:
x (NDArray[np.float64]): `x`-values of the data.
amplitude (float, optional): Amplitude of the constant. Defaults to 1.0.
Returns:
NDArray[np.float64]: Constant value of `x` given.
"""
return np.array(np.linspace(amplitude, amplitude, len(x)))
Return a 1-dimensional error function.
\[ f(x) = \frac{2}{\sqrt{\pi}} \int_{0}^{x} e^{-t^2} dt \]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
amplitude | float | Amplitude of the error function. Defaults to 1.0. | 1.0 |
center | float | Center of the error function. Defaults to 0.0. | 0.0 |
sigma | float | Sigma of the error function. Defaults to 1.0. | 1.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Error function of |
Source code in spectrafit/models.py
@staticmethod
def erf(
x: NDArray[np.float64],
amplitude: float = 1.0,
center: float = 0.0,
sigma: float = 1.0,
) -> NDArray[np.float64]:
r"""Return a 1-dimensional error function.
$$
f(x) = \frac{2}{\sqrt{\pi}} \int_{0}^{x} e^{-t^2} dt
$$
Args:
x (NDArray[np.float64]): `x`-values of the data.
amplitude (float, optional): Amplitude of the error function.
Defaults to 1.0.
center (float, optional): Center of the error function. Defaults to 0.0.
sigma (float, optional): Sigma of the error function. Defaults to 1.0.
Returns:
NDArray[np.float64]: Error function of `x` given.
"""
return np.array(
amplitude * 0.5 * (1 + erf(DistributionModels._norm(x, center, sigma)))
)
Return a 1-dimensional Heaviside step function.
$$ f(x) = begin{cases} 0 & x < 0 \ 0.5 & x = 0 \ 1 & x > 0 end{cases} $$ Args: x (NDArray[np.float64]): x
-values of the data. amplitude (float, optional): Amplitude of the Heaviside step function. Defaults to 1.0. center (float, optional): Center of the Heaviside step function. Defaults to 0.0. sigma (float, optional): Sigma of the Heaviside step function. Defaults to 1.0.
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Heaviside step function of |
Source code in spectrafit/models.py
@staticmethod
def heaviside(
x: NDArray[np.float64],
amplitude: float = 1.0,
center: float = 0.0,
sigma: float = 1.0,
) -> NDArray[np.float64]:
r"""Return a 1-dimensional Heaviside step function.
$$
f(x) = \begin{cases}
0 & x < 0 \\
0.5 & x = 0 \\
1 & x > 0
\end{cases}
$$
Args:
x (NDArray[np.float64]): `x`-values of the data.
amplitude (float, optional): Amplitude of the Heaviside step function.
Defaults to 1.0.
center (float, optional): Center of the Heaviside step function.
Defaults to 0.0.
sigma (float, optional): Sigma of the Heaviside step function.
Defaults to 1.0.
Returns:
NDArray[np.float64]: Heaviside step function of `x` given.
"""
return np.array(
amplitude * 0.5 * (1 + np.sign(DistributionModels._norm(x, center, sigma)))
)
Return a 1-dimensional arctan step function.
\[ f(x) = \frac{1}{\pi} \arctan(\frac{x - c}{s}) \]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
amplitude | float | Amplitude of the arctan step function. Defaults to 1.0. | 1.0 |
center | float | Center of the arctan step function. Defaults to 0.0. | 0.0 |
sigma | float | Sigma of the arctan step function. Defaults to 1.0. | 1.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Arctan step function of |
Source code in spectrafit/models.py
@staticmethod
def atan(
x: NDArray[np.float64],
amplitude: float = 1.0,
center: float = 0.0,
sigma: float = 1.0,
) -> NDArray[np.float64]:
r"""Return a 1-dimensional arctan step function.
$$
f(x) = \frac{1}{\pi} \arctan(\frac{x - c}{s})
$$
Args:
x (NDArray[np.float64]): `x`-values of the data.
amplitude (float, optional): Amplitude of the arctan step function.
Defaults to 1.0.
center (float, optional): Center of the arctan step function.
Defaults to 0.0.
sigma (float, optional): Sigma of the arctan step function.
Defaults to 1.0.
Returns:
NDArray[np.float64]: Arctan step function of `x` given.
"""
return np.array(
amplitude
* 0.5
* (1 + np.arctan(DistributionModels._norm(x, center, sigma)) / pi)
)
Return a 1-dimensional logarithmic step function.
\[ f(x) = \frac{1}{1 + e^{-\frac{x - c}{s}}} \]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
amplitude | float | Amplitude of the logarithmic step function. Defaults to 1.0. | 1.0 |
center | float | Center of the logarithmic step function. Defaults to 0.0. | 0.0 |
sigma | float | Sigma of the logarithmic step function. Defaults to 1.0. | 1.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Logarithmic step function of |
Source code in spectrafit/models.py
@staticmethod
def log(
x: NDArray[np.float64],
amplitude: float = 1.0,
center: float = 0.0,
sigma: float = 1.0,
) -> NDArray[np.float64]:
r"""Return a 1-dimensional logarithmic step function.
$$
f(x) = \frac{1}{1 + e^{-\frac{x - c}{s}}}
$$
Args:
x (NDArray[np.float64]): `x`-values of the data.
amplitude (float, optional): Amplitude of the logarithmic step function.
Defaults to 1.0.
center (float, optional): Center of the logarithmic step function.
Defaults to 0.0.
sigma (float, optional): Sigma of the logarithmic step function.
Defaults to 1.0.
Returns:
NDArray[np.float64]: Logarithmic step function of `x` given.
"""
return np.array(
amplitude
* 0.5
* (1 + np.log(DistributionModels._norm(x, center, sigma)) / pi)
)
Return a 1-dimensional cumulative Gaussian function.
\[ f(x) = \frac{1}{2} \left[1 + erf\left(\frac{x - c}{s \sqrt{2}}\right)\right] \]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
amplitude | float | Amplitude of the Gaussian function. Defaults to 1.0. | 1.0 |
center | float | Center of the Gaussian function. Defaults to 0.0. | 0.0 |
fwhmg | float | Full width at half maximum of the Gaussian function. Defaults to 1.0. | 1.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Cumulative Gaussian function of |
Source code in spectrafit/models.py
@staticmethod
def cgaussian(
x: NDArray[np.float64],
amplitude: float = 1.0,
center: float = 0.0,
fwhmg: float = 1.0,
) -> NDArray[np.float64]:
r"""Return a 1-dimensional cumulative Gaussian function.
$$
f(x) = \frac{1}{2} \left[1 + erf\left(\frac{x - c}{s \sqrt{2}}\right)\right]
$$
Args:
x (NDArray[np.float64]): `x`-values of the data.
amplitude (float, optional): Amplitude of the Gaussian function. Defaults to
1.0.
center (float, optional): Center of the Gaussian function. Defaults to 0.0.
fwhmg (float, optional): Full width at half maximum of the Gaussian
function. Defaults to 1.0.
Returns:
NDArray[np.float64]: Cumulative Gaussian function of `x` given.
"""
sigma = fwhmg * Constants.fwhmg2sig
return np.array(
amplitude * 0.5 * (1 + erf((x - center) / (sigma * np.sqrt(2.0))))
)
Return a 1-dimensional cumulative Lorentzian function.
\[ f(x) = \frac{1}{\pi} \arctan\left(\frac{x - c}{s}\right) + \frac{1}{2} \]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
amplitude | float | Amplitude of the Lorentzian function. Defaults to 1.0. | 1.0 |
center | float | Center of the Lorentzian function. Defaults to 0.0. | 0.0 |
fwhml | float | Full width at half maximum of the Lorentzian function. Defaults to 1.0. | 1.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Cumulative Lorentzian function of |
Source code in spectrafit/models.py
@staticmethod
def clorentzian(
x: NDArray[np.float64],
amplitude: float = 1.0,
center: float = 0.0,
fwhml: float = 1.0,
) -> NDArray[np.float64]:
r"""Return a 1-dimensional cumulative Lorentzian function.
$$
f(x) = \frac{1}{\pi} \arctan\left(\frac{x - c}{s}\right) + \frac{1}{2}
$$
Args:
x (NDArray[np.float64]): `x`-values of the data.
amplitude (float, optional): Amplitude of the Lorentzian function.
Defaults to 1.0.
center (float, optional): Center of the Lorentzian function.
Defaults to 0.0.
fwhml (float, optional): Full width at half maximum of the Lorentzian
function. Defaults to 1.0.
Returns:
NDArray[np.float64]: Cumulative Lorentzian function of `x` given.
"""
sigma = fwhml * Constants.fwhml2sig
return np.array(amplitude * (np.arctan((x - center) / sigma) / pi) + 0.5)
Return a 1-dimensional cumulative Voigt function.
\[ f(x) = \frac{1}{2} \left[1 + erf\left(\frac{x - c}{s \sqrt{2}}\right)\right] \]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
amplitude | float | Amplitude of the Voigt function. Defaults to 1.0. | 1.0 |
center | float | Center of the Voigt function. Defaults to 0.0. | 0.0 |
fwhmv | float | Full width at half maximum of the Voigt function. Defaults to 1.0. | 1.0 |
gamma | float | Gamma of the Voigt function. Defaults to 1.0. | 1.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Cumulative Voigt function of |
Source code in spectrafit/models.py
@staticmethod
def cvoigt(
x: NDArray[np.float64],
amplitude: float = 1.0,
center: float = 0.0,
fwhmv: float = 1.0,
gamma: float = 1.0,
) -> NDArray[np.float64]:
r"""Return a 1-dimensional cumulative Voigt function.
$$
f(x) = \frac{1}{2} \left[1 + erf\left(\frac{x - c}{s \sqrt{2}}\right)\right]
$$
Args:
x (NDArray[np.float64]): `x`-values of the data.
amplitude (float, optional): Amplitude of the Voigt function. Defaults to
1.0.
center (float, optional): Center of the Voigt function. Defaults to 0.0.
fwhmv (float, optional): Full width at half maximum of the Voigt function.
Defaults to 1.0.
gamma (float, optional): Gamma of the Voigt function. Defaults to 1.0.
Returns:
NDArray[np.float64]: Cumulative Voigt function of `x` given.
"""
sigma = fwhmv * Constants.fwhmv2sig
return np.array(
amplitude
* 0.5
* (1 + erf((x - center) / (sigma * np.sqrt(2.0))))
* np.exp(-(((x - center) / gamma) ** 2))
)
Return a 1-dimensional second order polynomial function.
\[ f(x) = c_2 x^2 + c_1 x + c_0 \]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
coefficient0 | float | Zeroth coefficient of the polynomial function. Defaults to 1.0. | 1.0 |
coefficient1 | float | First coefficient of the polynomial function. Defaults to 1.0. | 1.0 |
coefficient2 | float | Second coefficient of the polynomial function. Defaults to 1.0. | 1.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Third order polynomial function of |
Source code in spectrafit/models.py
@staticmethod
def polynom2(
x: NDArray[np.float64],
coefficient0: float = 1.0,
coefficient1: float = 1.0,
coefficient2: float = 1.0,
) -> NDArray[np.float64]:
"""Return a 1-dimensional second order polynomial function.
$$
f(x) = c_2 x^2 + c_1 x + c_0
$$
Args:
x (NDArray[np.float64]): `x`-values of the data
coefficient0 (float, optional): Zeroth coefficient of the
polynomial function. Defaults to 1.0.
coefficient1 (float, optional): First coefficient of the
polynomial function. Defaults to 1.0.
coefficient2 (float, optional): Second coefficient of the
polynomial function. Defaults to 1.0.
Returns:
NDArray[np.float64]: Third order polynomial function of `x`
"""
return np.array(coefficient0 + coefficient1 * x + coefficient2 * x**2)
Return a 1-dimensional third order polynomial function.
\[ f(x) = c_3 x^3 + c_2 x^2 + c_1 x + c_0 \]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
coefficient0 | float | Zeroth coefficient of the polynomial function. Defaults to 1.0. | 1.0 |
coefficient1 | float | First coefficient of the polynomial function. Defaults to 1.0. | 1.0 |
coefficient2 | float | Second coefficient of the polynomial function. Defaults to 1.0. | 1.0 |
coefficient3 | float | Third coefficient of the polynomial function. Defaults to 1.0. | 1.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Third order polynomial function of |
Source code in spectrafit/models.py
@staticmethod
def polynom3(
x: NDArray[np.float64],
coefficient0: float = 1.0,
coefficient1: float = 1.0,
coefficient2: float = 1.0,
coefficient3: float = 1.0,
) -> NDArray[np.float64]:
"""Return a 1-dimensional third order polynomial function.
$$
f(x) = c_3 x^3 + c_2 x^2 + c_1 x + c_0
$$
Args:
x (NDArray[np.float64]): `x`-values of the data
coefficient0 (float, optional): Zeroth coefficient of the
polynomial function. Defaults to 1.0.
coefficient1 (float, optional): First coefficient of the
polynomial function. Defaults to 1.0.
coefficient2 (float, optional): Second coefficient of the
polynomial function. Defaults to 1.0.
coefficient3 (float, optional): Third coefficient of the
polynomial function. Defaults to 1.0.
Returns:
NDArray[np.float64]: Third order polynomial function of `x`
"""
return np.array(
coefficient0 + coefficient1 * x + coefficient2 * x**2 + coefficient3 * x**3
)
Return a 1-dimensional Pearson type I distribution.
\[ f(x) = \frac{A}{\sigma \sqrt{2 \pi}} \left[1 + \frac{(x - c)^2}{\sigma^2} \right]^{-\frac{1}{\nu}} \]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
amplitude | float | Amplitude of the Pearson type I function. Defaults to 1.0. | 1.0 |
center | float | Center of the Pearson type I function. Defaults to 0.0. | 0.0 |
sigma | float | Sigma of the Pearson type I function. Defaults to 1.0. | 1.0 |
exponent | float | Exponent of the Pearson type I function. Defaults to 1.0. | 1.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Pearson type I function of |
Source code in spectrafit/models.py
@staticmethod
def pearson1(
x: NDArray[np.float64],
amplitude: float = 1.0,
center: float = 0.0,
sigma: float = 1.0,
exponent: float = 1.0,
) -> NDArray[np.float64]:
r"""Return a 1-dimensional Pearson type I distribution.
$$
f(x) = \frac{A}{\sigma \sqrt{2 \pi}} \left[1 + \frac{(x - c)^2}{\sigma^2}
\right]^{-\frac{1}{\nu}}
$$
Args:
x (NDArray[np.float64]): `x`-values of the data.
amplitude (float, optional): Amplitude of the Pearson type I function.
Defaults to 1.0.
center (float, optional): Center of the Pearson type I function.
Defaults to 0.0.
sigma (float, optional): Sigma of the Pearson type I function.
Defaults to 1.0.
exponent (float, optional): Exponent of the Pearson type I function.
Defaults to 1.0.
Returns:
NDArray[np.float64]: Pearson type I function of `x` given.
"""
return np.array(
amplitude
/ (sigma * np.sqrt(2 * np.pi))
* np.power(1 + ((x - center) / sigma) ** 2, -1 / exponent)
)
Return a 1-dimensional Pearson type II distribution.
\[ f(x) = \frac{A}{\sigma \sqrt{2 \pi}} \left[1 + \frac{(x - c)^2}{2 \sigma^2} \right]^{-\nu} \]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
amplitude | float | Amplitude of the Pearson type II function. Defaults to 1.0. | 1.0 |
center | float | Center of the Pearson type II function. Defaults to 0.0. | 0.0 |
sigma | float | Sigma of the Pearson type II function. Defaults to 1.0. | 1.0 |
exponent | float | Exponent of the Pearson type II function. Defaults to 1.0. | 1.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Pearson type II function of |
Source code in spectrafit/models.py
@staticmethod
def pearson2(
x: NDArray[np.float64],
amplitude: float = 1.0,
center: float = 0.0,
sigma: float = 1.0,
exponent: float = 1.0,
) -> NDArray[np.float64]:
r"""Return a 1-dimensional Pearson type II distribution.
$$
f(x) = \frac{A}{\sigma \sqrt{2 \pi}} \left[1 + \frac{(x - c)^2}{2 \sigma^2}
\right]^{-\nu}
$$
Args:
x (NDArray[np.float64]): `x`-values of the data.
amplitude (float, optional): Amplitude of the Pearson type II function.
Defaults to 1.0.
center (float, optional): Center of the Pearson type II function.
Defaults to 0.0.
sigma (float, optional): Sigma of the Pearson type II function.
Defaults to 1.0.
exponent (float, optional): Exponent of the Pearson type II function.
Defaults to 1.0.
Returns:
NDArray[np.float64]: Pearson type II function of `x` given.
"""
return np.array(
amplitude
/ (sigma * np.sqrt(2 * pi))
* np.power(1 + ((x - center) / (2 * sigma)) ** 2, -exponent)
)
Return a 1-dimensional Pearson type III distribution.
\[ f(x) = \frac{A}{\sigma \sqrt{2 \pi}} \left[1 + \frac{(x - c)^2}{2 \sigma^2} \right]^{-\nu} \left[1 + \frac{\gamma}{\nu} \frac{x - c}{\sigma} \right]^{-\nu - 1} \]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
amplitude | float | Amplitude of the Pearson type III function. Defaults to 1.0. | 1.0 |
center | float | Center of the Pearson type III function. Defaults to 0.0. | 0.0 |
sigma | float | Sigma of the Pearson type III function. Defaults to 1.0. | 1.0 |
exponent | float | Exponent of the Pearson type III function. Defaults to 1.0. | 1.0 |
skewness | float | Skewness of the Pearson type III function. Defaults to 0.0. | 0.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Pearson type III function of |
Source code in spectrafit/models.py
@staticmethod
def pearson3(
x: NDArray[np.float64],
amplitude: float = 1.0,
center: float = 0.0,
sigma: float = 1.0,
exponent: float = 1.0,
skewness: float = 0.0,
) -> NDArray[np.float64]:
r"""Return a 1-dimensional Pearson type III distribution.
$$
f(x) = \frac{A}{\sigma \sqrt{2 \pi}} \left[1 + \frac{(x - c)^2}{2 \sigma^2}
\right]^{-\nu} \left[1 + \frac{\gamma}{\nu}
\frac{x - c}{\sigma} \right]^{-\nu - 1}
$$
Args:
x (NDArray[np.float64]): `x`-values of the data.
amplitude (float, optional): Amplitude of the Pearson type III function.
Defaults to 1.0.
center (float, optional): Center of the Pearson type III function.
Defaults to 0.0.
sigma (float, optional): Sigma of the Pearson type III function.
Defaults to 1.0.
exponent (float, optional): Exponent of the Pearson type III function.
Defaults to 1.0.
skewness (float, optional): Skewness of the Pearson type III function.
Defaults to 0.0.
Returns:
NDArray[np.float64]: Pearson type III function of `x` given.
"""
return np.array(
amplitude
/ (sigma * np.sqrt(2 * pi))
* np.power(1 + ((x - center) / (2 * sigma)) ** 2, -exponent)
* np.power(
1 + (skewness / exponent) * ((x - center) / sigma), -exponent - 1
)
)
Return a 1-dimensional Pearson type IV distribution.
\[ f(x) = \frac{A}{\sigma \sqrt{2 \pi}} \left[1 + \frac{(x - c)^2}{2 \sigma^2} \right]^{-\nu} \left[1 + \frac{\gamma}{\nu} \frac{x - c}{\sigma} \right]^{-\nu - 1} \left[1 + \frac{\delta}{\nu} \left(\frac{x - c}{\sigma}\right)^2 \right]^{-\nu - 1/2} \]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | NDArray[float64] |
| required |
amplitude | float | Amplitude of the Pearson type IV function. Defaults to 1.0. | 1.0 |
center | float | Center of the Pearson type IV function. Defaults to 0.0. | 0.0 |
sigma | float | Sigma of the Pearson type IV function. Defaults to 1.0. | 1.0 |
exponent | float | Exponent of the Pearson type IV function. Defaults to 1.0. | 1.0 |
skewness | float | Skewness of the Pearson type IV function. Defaults to 0.0. | 0.0 |
kurtosis | float | Kurtosis of the Pearson type IV function. Defaults to 0.0. | 0.0 |
Returns:
Type | Description |
---|---|
NDArray[float64] | NDArray[np.float64]: Pearson type IV function of |
Source code in spectrafit/models.py
@staticmethod
def pearson4(
x: NDArray[np.float64],
amplitude: float = 1.0,
center: float = 0.0,
sigma: float = 1.0,
exponent: float = 1.0,
skewness: float = 0.0,
kurtosis: float = 0.0,
) -> NDArray[np.float64]:
r"""Return a 1-dimensional Pearson type IV distribution.
$$
f(x) = \frac{A}{\sigma \sqrt{2 \pi}} \left[1 + \frac{(x - c)^2}{2 \sigma^2}
\right]^{-\nu} \left[1 + \frac{\gamma}{\nu}
\frac{x - c}{\sigma} \right]^{-\nu - 1}
\left[1 + \frac{\delta}{\nu}
\left(\frac{x - c}{\sigma}\right)^2 \right]^{-\nu - 1/2}
$$
Args:
x (NDArray[np.float64]): `x`-values of the data.
amplitude (float, optional): Amplitude of the Pearson type IV function.
Defaults to 1.0.
center (float, optional): Center of the Pearson type IV function.
Defaults to 0.0.
sigma (float, optional): Sigma of the Pearson type IV function.
Defaults to 1.0.
exponent (float, optional): Exponent of the Pearson type IV function.
Defaults to 1.0.
skewness (float, optional): Skewness of the Pearson type IV function.
Defaults to 0.0.
kurtosis (float, optional): Kurtosis of the Pearson type IV function.
Defaults to 0.0.
Returns:
NDArray[np.float64]: Pearson type IV function of `x` given.
"""
return np.array(
amplitude
/ (sigma * np.sqrt(2 * pi))
* np.power(1 + ((x - center) / (2 * sigma)) ** 2, -exponent)
* np.power(
1 + (skewness / exponent) * ((x - center) / sigma), -exponent - 1
)
* np.power(
1 + (kurtosis / exponent) * ((x - center) / sigma) ** 2,
-exponent - 1 / 2,
)
)
Important constants for the models¶
For calculating the models a few math constants are needed, which are implemented in the constants
module.
Mathematical constants for the curve models.
Constants
-
Natural logarithm of 2
\[ ln2 = \log{2} \]
-
Square root of 2 times pi
\[ sq2pi = \sqrt{2 \pi} \]
-
Square root of pi
\[ sqpi = \sqrt{ \pi} \]
-
Square root of 2
\[ sq2 = \sqrt{2} \]
-
Full width at half maximum to sigma for Gaussian
\[ fwhmg2sig = \frac{1}{ 2 \sqrt{2\log{2}}} \]
-
Full width at half maximum to sigma for Lorentzian
\[ fwhml2sig = \frac{1}{2} \]
-
Full width at half maximum to sigma for Voigt according to the article by Olivero and Longbothum1, check also XPSLibary website.
$$ fwhm_{text{Voigt}} approx 0.5346 cdot fwhm_{text{Gaussian}} + sqrt{ 0.2166 fwhm_{text{Lorentzian}}^2 + fwhm_{text{Gaussian}}^2 }
$$
In case of equal FWHM for Gaussian and Lorentzian, the Voigt FWHM can be defined as:
\[ fwhm_{\text{Voigt}} \approx 1.0692 + 2 \sqrt{0.2166 + 2 \ln{2}} \cdot \sigma \]
\[ fwhmv2sig = \frac{1}{fwhm_{\text{Voigt}}} \]
-
J.J. Olivero, R.L. Longbothum, Empirical fits to the Voigt line width: A brief review, Journal of Quantitative Spectroscopy and Radiative Transfer, Volume 17, Issue 2, 1977, Pages 233-236, ISSN 0022-4073, doi.org/10.1016/0022-4073(77)90161-3. ↩
Source code in spectrafit/models.py
@dataclass(frozen=True)
class Constants:
r"""Mathematical constants for the curve models.
!!! info "Constants"
1. Natural logarithm of 2
$$
ln2 = \log{2}
$$
2. Square root of 2 times pi
$$
sq2pi = \sqrt{2 \pi}
$$
3. Square root of pi
$$
sqpi = \sqrt{ \pi}
$$
4. Square root of 2
$$
sq2 = \sqrt{2}
$$
5. Full width at half maximum to sigma for Gaussian
$$
fwhmg2sig = \frac{1}{ 2 \sqrt{2\log{2}}}
$$
6. Full width at half maximum to sigma for Lorentzian
$$
fwhml2sig = \frac{1}{2}
$$
7. Full width at half maximum to sigma for Voigt according to the article by
Olivero and Longbothum[^1], check also
[XPSLibary website](https://xpslibrary.com/voigt-peak-shape/).
$$
fwhm_{\text{Voigt}} \approx 0.5346 \cdot fwhm_{\text{Gaussian}} +
\sqrt{ 0.2166 fwhm_{\text{Lorentzian}}^2 + fwhm_{\text{Gaussian}}^2 }
$$
In case of equal FWHM for Gaussian and Lorentzian, the Voigt FWHM can be
defined as:
$$
fwhm_{\text{Voigt}} \approx 1.0692 + 2 \sqrt{0.2166 + 2 \ln{2}} \cdot \sigma
$$
$$
fwhmv2sig = \frac{1}{fwhm_{\text{Voigt}}}
$$
[^1]:
J.J. Olivero, R.L. Longbothum,
_Empirical fits to the Voigt line width: A brief review_,
**Journal of Quantitative Spectroscopy and Radiative Transfer**,
Volume 17, Issue 2, 1977, Pages 233-236, ISSN 0022-4073,
https://doi.org/10.1016/0022-4073(77)90161-3.
"""
ln2 = log(2.0)
sq2pi = sqrt(2.0 * pi)
sqpi = sqrt(pi)
sq2 = sqrt(2.0)
fwhmg2sig = 1 / (2.0 * sqrt(2.0 * log(2.0)))
fwhml2sig = 1 / 2.0
fwhmv2sig = 1 / (2 * 0.5346 + 2 * sqrt(0.2166 + log(2) * 2))
Visualization of the models as a function of the parameters¶
About Peaks' Components
Comparing components of the peaks in a table is important because it allows for a quick and easy comparison of the different parameters that describe each peak. This can be useful for identifying trends or patterns in the data, as well as for identifying outliers or anomalies.
Additionally, having this information in a table format can make it easier to visualize and interpret the data, as well as to communicate the results to others. This can be also seen in example9_3.ipny
from spectrafit.plugins import notebook as nb
...
spn.solver_model(initial_model=initial_model, show_peaks=True)
This will provide an interactive table as well as allows to export the iterative results as a csv
-file.
About Peak's Components as *.csv
pseudovoigt_amplitude_1,pseudovoigt_amplitude_1,pseudovoigt_amplitude_1,pseudovoigt_center_1,pseudovoigt_center_1,pseudovoigt_center_1,pseudovoigt_fwhmg_1,pseudovoigt_fwhmg_1,pseudovoigt_fwhmg_1,pseudovoigt_fwhml_1,pseudovoigt_fwhml_1,pseudovoigt_fwhml_1,gaussian_amplitude_2,gaussian_amplitude_2,gaussian_amplitude_2,gaussian_center_2,gaussian_center_2,gaussian_center_2,gaussian_fwhmg_2,gaussian_fwhmg_2,gaussian_fwhmg_2,gaussian_amplitude_3,gaussian_amplitude_3,gaussian_amplitude_3,gaussian_center_3,gaussian_center_3,gaussian_center_3,gaussian_fwhmg_3,gaussian_fwhmg_3,gaussian_fwhmg_3,gaussian_amplitude_4,gaussian_amplitude_4,gaussian_amplitude_4,gaussian_center_4,gaussian_center_4,gaussian_center_4,gaussian_fwhmg_4,gaussian_fwhmg_4,gaussian_fwhmg_4,gaussian_amplitude_5,gaussian_amplitude_5,gaussian_amplitude_5,gaussian_center_5,gaussian_center_5,gaussian_center_5,gaussian_fwhmg_5,gaussian_fwhmg_5,gaussian_fwhmg_5,gaussian_amplitude_6,gaussian_amplitude_6,gaussian_amplitude_6,gaussian_center_6,gaussian_center_6,gaussian_center_6,gaussian_fwhmg_6,gaussian_fwhmg_6,gaussian_fwhmg_6
init_value,model_value,best_value,init_value,model_value,best_value,init_value,model_value,best_value,init_value,model_value,best_value,init_value,model_value,best_value,init_value,model_value,best_value,init_value,model_value,best_value,init_value,model_value,best_value,init_value,model_value,best_value,init_value,model_value,best_value,init_value,model_value,best_value,init_value,model_value,best_value,init_value,model_value,best_value,init_value,model_value,best_value,init_value,model_value,best_value,init_value,model_value,best_value,init_value,model_value,best_value,init_value,model_value,best_value,init_value,model_value,best_value
1.0,0.33834723012149714,0.33834723012149714,0.0,0.017248125260695968,0.017248125260695968,0.1,0.020000000004318036,0.020000000004318036,0.1,0.1999999999970698,0.1999999999970698,0.3,0.04935454783731008,0.04935454783731008,2.0,1.6275712126681712,1.6275712126681712,0.1,0.2999853736750539,0.2999853736750539,0.3,0.08603886973285346,0.08603886973285346,2.5,2.447935058411735,2.447935058411735,0.2,0.3999999771273954,0.3999999771273954,0.3,0.07288548037982234,0.07288548037982234,2.5,2.031809677600558,2.031809677600558,0.3,0.399999999994134,0.399999999994134,0.3,0.0806454229648127,0.0806454229648127,3.0,3.0955581713143245,3.0955581713143245,0.3,0.39999999989892293,0.39999999989892293,0.3,0.09759693340603837,0.09759693340603837,3.8,3.7000000000154216,3.7000000000154216,0.3,0.39999931341337847,0.39999931341337847
1.0,0.33834723012149714,0.33834723012149714,0.0,0.017248125260695968,0.017248125260695968,0.1,0.020000000004318036,0.020000000004318036,0.1,0.1999999999970698,0.1999999999970698,0.3,0.04935454783731008,0.04935454783731008,2.0,1.6275712126681712,1.6275712126681712,0.1,0.2999853736750539,0.2999853736750539,0.3,0.08603886973285346,0.08603886973285346,2.5,2.447935058411735,2.447935058411735,0.2,0.3999999771273954,0.3999999771273954,0.3,0.07288548037982234,0.07288548037982234,2.5,2.031809677600558,2.031809677600558,0.3,0.399999999994134,0.399999999994134,0.3,0.0806454229648127,0.0806454229648127,3.0,3.0955581713143245,3.0955581713143245,0.3,0.39999999989892293,0.39999999989892293,0.3,0.09759693340603837,0.09759693340603837,3.8,3.7000000000154216,3.7000000000154216,0.3,0.39999931341337847,0.39999931341337847
1.0,0.3384902152130924,0.3384902152130924,0.0,0.017262089305238426,0.017262089305238426,0.1,0.02000000000431808,0.02000000000431808,0.1,0.19999999999707,0.19999999999707,0.3,0.06390170759703961,0.06390170759703961,2.0,1.7470034358646442,1.7470034358646442,0.1,0.29999999999595617,0.29999999999595617,0.3,0.10843271104545171,0.10843271104545171,2.5,2.318373021191109,2.318373021191109,0.2,0.39999999999414,0.39999999999414,0.3,0.0828004001712187,0.0828004001712187,2.5,3.0506627778035975,3.0506627778035975,0.3,0.3999825114407111,0.3999825114407111,0.3,0.039900814326592204,0.039900814326592204,3.0,4.379956884593627,4.379956884593627,0.3,0.3999999999941403,0.3999999999941403,0.3,0.09812726904670366,0.09812726904670366,3.8,3.7000000000154216,3.7000000000154216,0.2,0.39999999999413927,0.39999999999413927
1.0,0.3383768308405881,0.3383768308405881,0.0,0.017249155005218952,0.017249155005218952,0.1,0.020000223602285063,0.020000223602285063,0.1,0.19999665144901568,0.19999665144901568,0.3,0.05316047862491946,0.05316047862491946,2.0,1.6591127700194552,1.6591127700194552,0.1,0.29999262582163666,0.29999262582163666,0.3,0.08210748667320089,0.08210748667320089,2.5,2.424765205447216,2.424765205447216,0.2,0.39949761254017074,0.39949761254017074,0.3,0.06636980111213131,0.06636980111213131,2.5,2.0669028672740284,2.0669028672740284,0.3,0.39999998671625336,0.39999998671625336,0.3,0.08125492663466871,0.08125492663466871,3.0,3.0662131700347164,3.0662131700347164,0.3,0.39999999999414,0.39999999999414,0.3,0.09830451265206608,0.09830451265206608,3.7,3.700483502444169,3.700483502444169,0.2,0.39999999997262314,0.39999999997262314