Source code for pysrw.tools
"""Generic utils for data analysis."""
import numpy as np
from copy import deepcopy
"""A collection of general purpose functions or tools for the manipulation
of the computed data."""
[docs]
def tupledeepcopy(tupleIn):
return tuple([deepcopy(el) for el in tupleIn])
[docs]
def findIdx(array, value):
"""Find the closest value out of an array.
Args:
array: array to search for.
value: desired value.
Returns:
int: the index of the `array` item closest to `value`.
"""
return np.argmin(np.abs(array - value))
[docs]
def gaussian1p(x, sigma):
r"""A non-normalized Gaussian with unitary amplitude and zero mean.
.. math::
y = e^{-\frac{x^2}{2 \sigma^2}}
Args:
x: coordinate axis.
sigma: standard deviation.
Returns:
float: array of y values
"""
return np.exp(-x** 2 / (2 * sigma ** 2))
[docs]
def gaussian2p(x, a, sigma):
r"""A non-normalized Gaussian with zero mean.
.. math::
y = a \cdot e^{-\frac{x^2}{2 \sigma^2}}
Args:
x: coordinate axis.
a: amplitude.
sigma: standard deviation.
Returns:
float: array of y values
"""
return a * np.exp(-x**2 / (2 * sigma ** 2))
[docs]
def gaussian3p(x, a, mu, sigma):
r"""A non-normalized Gaussian.
.. math::
y = a \cdot e^{-\frac{(x-\mu)^2}{2 \sigma^2}}
Args:
x: coordinate axis.
a: amplitude.
mu: mean.
sigma: standard deviation.
Returns:
float: array of values
"""
return a * np.exp(-(x - mu) ** 2 / (2 * sigma ** 2))
[docs]
def gaussian4p(x, a0, a1, mu, sigma):
r"""A non-normalized Gaussian with a constant offset.
.. math::
y = a0 + a1 \cdot e^{-\frac{(x-\mu)^2}{2 \sigma^2}}
Args:
x: coordinate axis.
a0: constant offset.
a1: amplitude.
mu: mean.
sigma: standard deviation.
Returns:
float: array of values
"""
return a0 + a1 * np.exp(-(x - mu) ** 2 / (2 * sigma ** 2))
[docs]
def gaussian1pNorm(x, sigma):
r"""The normalized version of :py:func:`gaussian1p`.
.. math::
y = \frac{1}{\sqrt{2 \pi} \sigma} e^{-\frac{x^2}{2 \sigma^2}}
Args:
x: coordinate axis.
sigma: standard deviation.
Returns:
float: array of y values
"""
return 1 / (np.sqrt(2 * np.pi) * sigma) * np.exp(-x** 2 / (2 * sigma ** 2))
[docs]
def gaussian2pNorm(x, a, sigma):
r"""The normalized version of :py:func:`gaussian2p`.
.. math::
y = \frac{a}{\sqrt{2 \pi} \sigma} e^{-\frac{x^2}{2 \sigma^2}}
Args:
x: coordinate axis.
a: coefficient scaling the area of the distribution.
sigma: standard deviation.
Returns:
float: array of y values
"""
return a / (np.sqrt(2 * np.pi) * sigma) * np.exp(-x** 2 / (2 * sigma ** 2))