ncempy.algo.gaussND module¶
A module with Gaussian functions for creating Gaussian distributions in 1D, 2D and 3D. There are also functions with the same name and _FIT which can be used to fit an intensity distribution in a ndarray with the corresponding Gaussian distribution using scipy’s curve_fit.
A 1D Lorentz and a 1D gaussLorentz function is also included for fitting zero loss peaks in EELS spectra.
These functions were written assuming you use np.meshgrid() with indexing = ‘xy’. If you use ‘ij’ indexing then be careful about how you pass in the x and y coordinates.
- ncempy.algo.gaussND.MatrixQuaternionRot(vector, theta)[source]¶
Quaternion tp rotate a given theta angle around the given vector.
adapted from code by Yongsoo Yang, yongsoo.ysyang@gmail.com
- Parameters:
vector (ndarray, 3-element) – A non-zero 3-element numpy array representing rotation axis
theta (float, degrees) – Rotation angle in degrees
- Returns:
Author – yongsoo.ysyang@gmail.com
- Return type:
Yongsoo Yang, Dept. of Physics and Astronomy, UCLA
- ncempy.algo.gaussND.gauss1D(x, x0, sigma)[source]¶
Returns the value of a gaussian at a 2D set of points for the given standard deviation with maximum normalized to 1.
- Parameters:
- Returns:
g – A vector of size (N,) of the Gaussian distribution evaluated at the input x values.
- Return type:
ndarray
Note
Calculate the half width at half maximum (HWHM) as >> HWHM = sqrt(2*log(2))*stDev ~ 1.18*stDev or if x goes from -1 to 1 >> 0.5*size(x)*stDev
- ncempy.algo.gaussND.gauss2D(x, y, x0, y0, sigma_x, sigma_y)[source]¶
Calculates the value of a Gaussian at a 2D set of points for the given standard deviations with maximum normalized to 1. The Gaussian axes are assumed to be 90 degrees from each other.
- Parameters:
x (ndarray) – A 2D array of size (M,N) of points for the x values. Use x, y = np.meshgrid(range(M),range(N),indexing=’xy’).
y (ndarray) – A 2D array of size (M,N) of points for the x values. Use x, y = np.meshgrid(range(M),range(N),indexing=’xy’).
x0 (float) – The center of the Gaussian along the x direction.
y0 (float) – The center of the Gaussian along the y direction.
sigma_x (float) – Standard deviation of the Gaussian along x.
sigma_y (float) – Standard deviation of the Gaussian along y.
- Returns:
g – A ndarray of size (N, M) of the Gaussian distribution evaluated at the input x values.
- Return type:
ndarray
Note
The Gaussian is normalized such that the peak == 1. To normalize the integral divide by 2 * np.pi * sigma_x * sigma_y
- ncempy.algo.gaussND.gauss2D_FIT(xy, x0, y0, sigma_x, sigma_y)[source]¶
Version of gauss2D used for fitting (1 x_data input (xy) and flattened output). Returns the value of a gaussian at a 2D set of points for the given standard deviation with maximum normalized to 1. The Gaussian axes are assumed to be 90 degrees from each other.
- Parameters:
xy (tuple) – A (N,M) shaped tuple containing the vectors of points for the evaluation points.
x0 (float) – The x center of the Gaussian.
y0 (float) – The y center of the Gaussian.
sigma_x (float) – The standard deviation of the Gaussian along x.
sigma_y (float) – The standard deviation of the Gaussian along y.
- Returns:
g2_norm – The Gaussian distribution with maximum value normalized to 1. The 2D ndarray is reshaped into a (N*M,) array for use in fitting functions in numpy and scipy.
- Return type:
ndarray
- ncempy.algo.gaussND.gauss2D_poly_FIT(xy, x0, y0, A, B, C)[source]¶
Returns the flattened values of an elliptical gaussian at a 2D set of points for the given polynomial pre-factors with maximum normalized to 1. The Gaussian axes are assumed to be 90 degrees from each other.
The matrix looks like [[A,B],[B,C]]. See https://en.wikipedia.org/wiki/Gaussian_function
- Parameters:
xy (tuple) – Evaluation points along x and y of shape (N,M)
x0 (float) – Center of the maximum along x.
y0 (float) – Center of the maximum along y.
A (float) – The A pre-factor for the polynomial expansion in the exponent.
B (float) – The B pre-factor for the polynomial expansion in the exponent.
C (float) – The C pre-factor for the polynomial expansion in the exponent.
- Returns:
g2_norm – A (N, M) sized 2D ndarray with a Gaussaian distribution rotated.
- Return type:
ndarray
- ncempy.algo.gaussND.gauss2D_theta(x, y, x0, y0, sigma_x, sigma_y, theta)[source]¶
Returns the value of a generalized Gaussian at a 2D set of points for the given standard deviation with maximum normalized to 1. The Gaussian axes (assumed to be 90 degrees from each other) can be oriented at different angles to the output array axes using theta.
- Parameters:
x (ndarray or list) – Evaluation points along x of size (N,)
y (ndarray) – Evaluation points along y of size (M,)
x0 (float) – Center of the maximum along x.
y0 (float) – Center of the maximum along y.
sigma_x (float) – Standard deviation along x.
sigma_y (float) – Standard deviation along y.
theta (float) – The rotation of the Gaussian principle axes from the array horizontal and vertical. In radians.
- Returns:
g2_norm – A (N, M) sized 2D ndarray with a Gaussian distribution rotated.
- Return type:
ndarray
- ncempy.algo.gaussND.gauss2D_theta_FIT(xy, x0, y0, sigma_x, sigma_y, theta)[source]¶
Version of gauss2D_theta used for fitting (1 x_data input and flattened output). Returns the value of a gaussian at a 2D set of points for the given standard deviation with maximum normalized to 1. The Gaussian axes can be oriented at different angles (theta) in radians.
- Parameters:
xy (tuple) – Evaluation points along x and y of shape (N,M)
x0 (float) – Center of the maximum along x.
y0 (float) – Center of the maximum along y.
sigma_x (float) – Standard deviation along x.
sigma_y (float) – Standard deviation along y.
theta (float) – The rotation of the Gaussian principle axes from the array horizontal and vertical. In radians.
- Returns:
g2_norm – A (N, M) sized 2D ndarray with a Gaussian distribution rotated.
- Return type:
ndarray
- ncempy.algo.gaussND.gauss3D(x, y, z, x0, y0, z0, sigma_x, sigma_y, sigma_z)[source]¶
Returns the value of a gaussian at a 2D set of points for the given standard deviations with maximum normalized to 1. The Gaussian axes are assumed to be 90 degrees from each other.
Note
Be careful about the indexing used in meshgrid and the order in which you pass the x, y, z variables in.
- Parameters:
x (ndarray, from numpy.meshgrid) – 2D arrays of points (from meshgrid)
y (ndarray, from numpy.meshgrid) – 2D arrays of points (from meshgrid)
z (ndarray, from numpy.meshgrid) – 2D arrays of points (from meshgrid)
x0 (float) – The x, y, z centers of the Gaussian
y0 (float) – The x, y, z centers of the Gaussian
z0 (float) – The x, y, z centers of the Gaussian
sigma_x (float) – The standard deviations of the Gaussian.
sigma_y (float) – The standard deviations of the Gaussian.
sigma_z (float) – The standard deviations of the Gaussian.
- Returns:
g3_norm – A 3D ndarray
- Return type:
ndarray
- ncempy.algo.gaussND.gauss3DGEN_FIT((x, y, z), x0, y0, z0, sigma_x, sigma_y, sigma_z, Angle1, Angle2, Angle3, BG, Height)[source]¶
Returns the value of a gaussian at a 3D set of points for the given sub-pixel positions with standard deviations, 3D Eular rotation angles, constant Background value, and Gaussian peak height.
adapted from code by Yongsoo Yang, yongsoo.ysyang@gmail.com
Note
This is a work in progress. Needs more testing.
- Parameters:
xyz (tuple of 3 np.ndarray) – 3D arrays of points (from meshgrid) combined in a tuple
x0 (float) – The x, y, z centers of the Gaussian
y0 (float) – The x, y, z centers of the Gaussian
z0 (float) – The x, y, z centers of the Gaussian
sigma_x (float) – standard deviations along x,y,z direction before 3D angular rotation
sigma_y (float) – standard deviations along x,y,z direction before 3D angular rotation
sigma_z (float) – standard deviations along x,y,z direction before 3D angular rotation
Angle1 (float, degrees) – Tait-Bryan angles in ZYX convention for 3D rotation in degrees
Angle2 (float, degrees) – Tait-Bryan angles in ZYX convention for 3D rotation in degrees
Angle3 (float, degrees) – Tait-Bryan angles in ZYX convention for 3D rotation in degrees
BG (float) – Background
Height (float) – The peak height of the Gaussian function
- Returns:
The 3D Gaussian
- Return type:
ndarray, 3D
- ncempy.algo.gaussND.gauss3D_FIT((x, y, z), x0, y0, z0, sigma_x, sigma_y, sigma_z)[source]¶
Returns the value of a gaussian at a 2D set of points for the given standard deviations with maximum normalized to 1. The Gaussian axes are assumed to be 90 degrees from each other.
Note
Be careful about the indexing used in meshgrid and the order in which you pass the x, y, z variables in.
- Parameters:
xyz (tuple of ndarrays) – A tuple containing the 3D arrays of points (from meshgrid)
x0 (float) – The x, y, z centers of the Gaussian
y0 (float) – The x, y, z centers of the Gaussian
z0 (float) – The x, y, z centers of the Gaussian
sigma_x (float) – The standard deviations of the Gaussian.
sigma_y (float) – The standard deviations of the Gaussian.
sigma_z (float) – The standard deviations of the Gaussian.
- Returns:
g3_norm – A flattened array for fitting.
- Return type:
ndarray
- ncempy.algo.gaussND.gauss3D_poly(x, y, z, x0, y0, z0, A, B, C, D, E, F)[source]¶
gauss3Dpoly_FIT((x,y,z),x0,y0,z0,A,B,C,D,E,F) Returns the value of a gaussian at a 2D set of points for the given standard deviations with maximum normalized to 1. The Gaussian axes are not locked to be 90 degrees.
- Parameters:
x (ndarray, 3D) – 3D arrays of points (from meshgrid)
y (ndarray, 3D) – 3D arrays of points (from meshgrid)
z (ndarray, 3D) – 3D arrays of points (from meshgrid)
x0 (float) – The x, y, z centers of the Gaussian
y0 (float) – The x, y, z centers of the Gaussian
z0 (float) – The x, y, z centers of the Gaussian
A (float) – The polynomial values for the fit
B (float) – The polynomial values for the fit
C (float) – The polynomial values for the fit
D (float) – The polynomial values for the fit
E (float) – The polynomial values for the fit
F (float) – The polynomial values for the fit
- Returns:
The 3D Gaussian
- Return type:
ndarray, 3D
- ncempy.algo.gaussND.gauss3D_poly_FIT(xyz, x0, y0, z0, A, B, C, D, E, F)[source]¶
gauss3Dpoly_FIT((x,y,z),x0,y0,z0,A,B,C,D,E,F) Returns the value of a gaussian at a 2D set of points for the given standard deviations with maximum normalized to 1. The Guassian axes are not locked to be 90 degrees.
- Parameters:
xyz (tuple of ndarrays) – 3D arrays of points (from meshgrid) combined in a tuple
x0 (float) – The x, y, z centers of the Gaussian
y0 (float) – The x, y, z centers of the Gaussian
z0 (float) – The x, y, z centers of the Gaussian
A (float) – The polynomial values for the fit
B (float) – The polynomial values for the fit
C (float) – The polynomial values for the fit
D (float) – The polynomial values for the fit
E (float) – The polynomial values for the fit
F (float) – The polynomial values for the fit
- Returns:
The 3D Gaussian
- Return type:
ndarray, 3D
- ncempy.algo.gaussND.gaussLorentz1D(x, x0, w)[source]¶
A Gaussian-Lorentzian function in one dimension.
- Parameters:
- Returns:
lg – A vector of size (N,) of the Lorentzian Gaussian distribution evaluated at the input x values.
- Return type:
ndarray