amep.functions.Gaussian2d#

class amep.functions.Gaussian2d#

Bases: BaseFunction

Two-dimensional Gaussian.

__init__()#

Initializes a function object of Two-dimensional Gaussian function.

Equivalent to the probability density function of a normal distribution in two dimensions. By the central limit theorem this is a good guess for most peak shapes that arise from many random processes. This parametrization can include correlations via the angle variable \(\theta\). The function is given by

\[g(x)= A\exp\left(-\left(\vec{x}-\vec{\mu}\right)^T R(\Theta)^{-1}\sigma^{-2}R(\Theta) \left(\vec{x}-\vec{\mu}\right) \right)+b\]

where \(\vec{x}\) is the vector composed the x and y coordinates, \(\vec{\mu}\) is the mean vector composed of \(\mu_x\) and \(\mu_y\) and \(\sigma^{-2}\) is the diagonal matrix with the inverse variances \(\sigma_x^{-2}\) and \(\sigma_y^{-2}\) as entries.

Return type:

None.

Examples

>>> import amep
>>> import numpy as np
>>> x = np.linspace(-10, 10, 500)
>>> y = np.linspace(-10, 10, 500)
>>> X,Y = np.meshgrid(x,y)
>>> g2d = amep.functions.Gaussian2d()
>>> Z = g2d.generate(
...     amep.utils.mesh_to_coords(X,Y),
...     p=[1,0,0,2,5,np.pi/3]
... ).reshape(X.shape)
>>> fig, axs = amep.plot.new(figsize=(3,3))
>>> amep.plot.field(axs, Z, X, Y)
>>> axs.set_xlabel(r'$x$')
>>> axs.set_ylabel(r'$y$')
>>> fig.savefig('./figures/functions/functions-Gaussian2d.png')
../_images/functions-Gaussian2d.png

Methods

__init__()

Initializes a function object of Two-dimensional Gaussian function.

f(p, x)

2D Gaussian.

fit(xdata, ydata[, p0, sigma, maxit, verbose])

Fits the function self.f to the given data by using ODR (orthogonal distance regression).

generate(x[, p])

Returns the y values for given x values.

Attributes

errors

Returns the fit errors for each parameter as an array.

keys

mean

name

nparams

output

params

Returns an array of the optimal fit parameters.

results

Returns the dictionary of fit results including parameter names, parameter values, and fit errors.

std

property errors: ndarray#

Returns the fit errors for each parameter as an array.

Returns:

Fit errors.

Return type:

np.ndarray

f(p, x)#

2D Gaussian. The function is given by

\[g(x)= A\exp\left(-\left(\vec{x}-\vec{\mu}\right)^T R(\Theta)^{-1}\sigma^{-2}R(\Theta) \left(\vec{x}-\vec{\mu}\right) \right)+b\]

where \(\vec{x}\) is the vector composed the x and y coordinates, \(\vec{\mu}\) is the mean vector composed of \(\mu_x\) and \(\mu_y\) and \(\sigma^{-2}\) is the diagonal matrix with the inverse variances \(\sigma_x^{-2}\) and \(\sigma_y^{-2}\) as entries.

Parameters:
  • p (list) – Parameters \((A,\mu_x,\mu_y,\sigma_x,\sigma_y,\Theta)\).

  • x (np.ndarray) – x-values as 2d array of shape (N,2).

Returns:

1D array of shape (N,).

Return type:

np.ndarray

Examples

>>>
fit(xdata: ndarray, ydata: ndarray, p0: list | None = None, sigma: ndarray | None = None, maxit: int | None = None, verbose: bool = False) None#

Fits the function self.f to the given data by using ODR (orthogonal distance regression).

Parameters:
  • xdata (np.ndarray) – x values.

  • ydata (np.ndarray) – y values.

  • p0 (list or None, optional) – List of initial values. The default is None.

  • sigma (np.ndarray or None, optional) – Absolute error for each data point. The default is None.

  • maxit (int, optional) – Maximum number of iterations. The default is 200.

  • verbose (bool, optional) – If True, the main results are printed. The default is False.

Return type:

None.

generate(x: ndarray, p: list | None = None) ndarray#

Returns the y values for given x values.

Parameters:
  • x (np.ndarray) – x values.

  • p (list, optional) – List of parameters. The default is None.

Returns:

y – f(x)

Return type:

np.ndarray

property params: ndarray#

Returns an array of the optimal fit parameters.

Returns:

Fit parameter values.

Return type:

np.ndarray

property results: dict#

Returns the dictionary of fit results including parameter names, parameter values, and fit errors.

Returns:

Fit results.

Return type:

dict