xrayutilities.math package

Submodules

xrayutilities.math.algebra module

module providing analytic algebraic functions not implemented in scipy or any other dependency of xrayutilities. In particular the analytic solution of a quartic equation which is needed for the solution of the dynamic scattering equations.

xrayutilities.math.algebra.solve_quartic(a4, a3, a2, a1, a0)[source]

analytic solution [1] of the general quartic equation. The solved equation takes the form:

a4*z**4 + a3*z**3 + a2*z**2 + a1*z + a0

Returns:

tuple of the four (complex) solutions of aboves equation.

[1] http://mathworld.wolfram.com/QuarticEquation.html

xrayutilities.math.fit module

module with a function wrapper to scipy.optimize.leastsq for fitting of a 2D function to a peak or a 1D Gauss fit with the odr package

xrayutilities.math.fit.fit_peak2d(x, y, data, start, drange, fit_function, maxfev=2000)[source]

fit a two dimensional function to a two dimensional data set e.g. a reciprocal space map

Parameters:

**x,y: data coordinates (do NOT need to be regularly spaced)**

data:data set used for fitting (e.g. intensity at the data coords)
start:set of starting parameters for the fit used as first parameter of function fit_function
drange:limits for the data ranges used in the fitting algorithm, e.g. it is clever to use only a small region around the peak which should be fitted: [xmin,xmax,ymin,ymax]
fit_function:function which should be fitted, must accept the parameters (x,y,*params)
Returns:
(fitparam,cov):the set of fitted parameters and covariance matrix
xrayutilities.math.fit.gauss_fit(xdata, ydata, iparams=[], maxit=300)[source]

Gauss fit function using odr-pack wrapper in scipy similar to :https: //github.com/tiagopereira/python_tips/wiki/Scipy%3A-curve-fitting

Parameters:

**xdata: xcoordinates of the data to be fitted**

ydata:ycoordinates of the data which should be fit

**keyword parameters:**

iparams:initial paramters for the fit, determined automatically if not given
maxit:maximal iteration number of the fit
Returns:

params,sd_params,itlim

the Gauss parameters as defined in function Gauss1d(x, *param) and their

errors of the fit, as well as a boolean flag which is false in the case of

a successful fit

xrayutilities.math.fit.linregress(x, y)[source]

fast linregress to avoid usage of scipy.stats which is slow! NaN values in y are ignored by this function.

Parameters:**x,y: data coordinates and values**
Returns:p, rsq: parameters of the linear fit (slope, offest) and the R^2 value

Examples

>>> (k, d), R2 = xu.math.linregress(x, y)
xrayutilities.math.fit.multGaussFit(*args, **kwargs)[source]

convenience function to keep API stable see multPeakFit for documentation

xrayutilities.math.fit.multGaussPlot(*args, **kwargs)[source]

convenience function to keep API stable see multPeakPlot for documentation

xrayutilities.math.fit.multPeakFit(x, data, peakpos, peakwidth, dranges=None, peaktype='Gaussian')[source]

function to fit multiple Gaussian/Lorentzian peaks with linear background to a set of data

Parameters:

**x: x-coordinate of the data**

data:data array with same length as x
peakpos:initial parameters for the peak positions
peakwidth:initial values for the peak width
dranges:list of tuples with (min,max) value of the data ranges to use. does not need to have the same number of entries as peakpos
peaktype:type of peaks to be used: can be either ‘Gaussian’ or ‘Lorentzian’
Returns:

pos,sigma,amp,background

pos:list of peak positions derived by the fit
sigma:list of peak width derived by the fit
amp:list of amplitudes of the peaks derived by the fit
background:array of background values at positions x
xrayutilities.math.fit.multPeakPlot(x, fpos, fwidth, famp, background, dranges=None, peaktype='Gaussian', fig='xu_plot', fact=1.0)[source]

function to plot multiple Gaussian/Lorentz peaks with background values given by an array

Parameters:

**x: x-coordinate of the data**

fpos:list of positions of the peaks
fwidth:list of width of the peaks
famp:list of amplitudes of the peaks
background:array with background values
dranges:list of tuples with (min,max) value of the data ranges to use. does not need to have the same number of entries as fpos
peaktype:type of peaks to be used: can be either ‘Gaussian’ or ‘Lorentzian’
fig:matplotlib figure number or name
fact:factor to use as multiplicator in the plot
xrayutilities.math.fit.peak_fit(xdata, ydata, iparams=[], peaktype='Gauss', maxit=300, background='constant', plot=False, func_out=False, debug=False)[source]

fit function using odr-pack wrapper in scipy similar to :https: //github.com/tiagopereira/python_tips/wiki/Scipy%3A-curve-fitting for Gauss, Lorentz or Pseudovoigt-functions

Parameters:

**xdata: xcoordinates of the data to be fitted**

ydata:ycoordinates of the data which should be fit

**keyword parameters:**

iparams:initial paramters for the fit, determined automatically if not specified
peaktype:type of peak to fit: ‘Gauss’, ‘Lorentz’, ‘PseudoVoigt’, ‘PseudoVoigtAsym’, ‘PseudoVoigtAsym2’
maxit:maximal iteration number of the fit
background:type of background, either ‘constant’ or ‘linear’
plot:flag to ask for a plot to visually judge the fit. If plot is a string it will be used as figure name, which makes reusing the figures easier.
func_out:returns the fitted function, which takes the independent variables as only argument (f(x))
Returns:

params,sd_params,itlim[,fitfunc]

the parameters as defined in function Gauss1d/Lorentz1d/PseudoVoigt1d/

PseudoVoigt1dasym(x, *param). In the case of linear background one more

parameter is included! For every parameter the corresponding errors of the

fit ‘sd_params’ are returned. A boolean flag ‘itlim’, which is False in

the case of a successful fit is added by default. Further the function

used in the fit can be returned (see func_out).

xrayutilities.math.functions module

module with several common function needed in xray data analysis

xrayutilities.math.functions.Debye1(x)[source]

function to calculate the first Debye function as needed for the calculation of the thermal Debye-Waller-factor by numerical integration

for definition see: :http: //en.wikipedia.org/wiki/Debye_function

D1(x) = (1/x) int_0^x t/(exp(t)-1) dt

Parameters:

**x ... argument of the Debye function (float)**

Returns:
D1(x):float value of the Debye function
xrayutilities.math.functions.Gauss1d(x, *p)[source]

function to calculate a general one dimensional Gaussian

Parameters:

**p: list of parameters of the Gaussian**

[XCEN,SIGMA,AMP,BACKGROUND] for information: SIGMA = FWHM / (2*sqrt(2*log(2)))

x:coordinate(s) where the function should be evaluated
Returns:

the value of the Gaussian described by the parameters p

at position x

Examples

Calling with a list of parameters needs a call looking as shown below (note the ‘*’) or explicit listing of the parameters: >>> Gauss1d(x,*p) >>> Gauss1d(numpy.linspace(0,10,100), 5, 1, 1e3, 0)

xrayutilities.math.functions.Gauss1dArea(*p)[source]

function to calculate the area of a Gauss function with neglected background

Parameters:

**p: list of parameters of the Gauss-function**

[XCEN,SIGMA,AMP,BACKGROUND]

Returns:

the area of the Gaussian described by the parameters p

xrayutilities.math.functions.Gauss1d_der_p(x, *p)[source]

function to calculate the derivative of a Gaussian with respect the parameters p

for parameter description see Gauss1d

xrayutilities.math.functions.Gauss1d_der_x(x, *p)[source]

function to calculate the derivative of a Gaussian with respect to x

for parameter description see Gauss1d

xrayutilities.math.functions.Gauss2d(x, y, *p)[source]

function to calculate a general two dimensional Gaussian

Parameters:

**p: list of parameters of the Gauss-function**

[XCEN,YCEN,SIGMAX,SIGMAY,AMP,BACKGROUND,ANGLE] SIGMA = FWHM / (2*sqrt(2*log(2))) ANGLE = rotation of the X,Y direction of the Gaussian in radians

x,y:coordinate(s) where the function should be evaluated
Returns:

the value of the Gaussian described by the parameters p

at position (x,y)

xrayutilities.math.functions.Gauss2dArea(*p)[source]

function to calculate the area of a 2D Gauss function with neglected background

Parameters:

**p: list of parameters of the Gauss-function**

[XCEN,YCEN,SIGMAX,SIGMAY,AMP,ANGLE,BACKGROUND]

Returns:

the area of the Gaussian described by the parameters p

xrayutilities.math.functions.Gauss3d(x, y, z, *p)[source]

function to calculate a general three dimensional Gaussian

Parameters:

**p: list of parameters of the Gauss-function**

[XCEN,YCEN,ZCEN,SIGMAX,SIGMAY,SIGMAZ,AMP,BACKGROUND] SIGMA = FWHM / (2*sqrt(2*log(2)))

x,y,z:coordinate(s) where the function should be evaluated
Returns:

the value of the Gaussian described by the parameters p

at positions (x,y,z)

xrayutilities.math.functions.Lorentz1d(x, *p)[source]

function to calculate a general one dimensional Lorentzian

Parameters:

**p: list of parameters of the Lorentz-function**

[XCEN,FWHM,AMP,BACKGROUND]

x:coordinate(s) where the function should be evaluated
Returns:

the value of the Lorentian described by the parameters p

at position (x,y)

xrayutilities.math.functions.Lorentz1dArea(*p)[source]

function to calculate the area of a Lorentz function with neglected background

Parameters:

**p: list of parameters of the Lorentz-function**

[XCEN,FWHM,AMP,BACKGROUND]

Returns:

the area of the Lorentzian described by the parameters p

xrayutilities.math.functions.Lorentz1d_der_p(x, *p)[source]

function to calculate the derivative of a Gaussian with respect the parameters p

for parameter description see Lorentz1d

xrayutilities.math.functions.Lorentz1d_der_x(x, *p)[source]

function to calculate the derivative of a Gaussian with respect to x

for parameter description see Lorentz1d

xrayutilities.math.functions.Lorentz2d(x, y, *p)[source]

function to calculate a general two dimensional Lorentzian

Parameters:

**p: list of parameters of the Lorentz-function**

[XCEN,YCEN,FWHMX,FWHMY,AMP,BACKGROUND,ANGLE] ANGLE = rotation of the X,Y direction of the Lorentzian in radians

x,y:coordinate(s) where the function should be evaluated
Returns:

the value of the Lorentian described by the parameters p

at position (x,y)

xrayutilities.math.functions.NormGauss1d(x, *p)[source]

function to calculate a normalized one dimensional Gaussian

Parameters:

**p: list of parameters of the Gaussian**

[XCEN,SIGMA] for information: SIGMA = FWHM / (2*sqrt(2*log(2)))

x:coordinate(s) where the function should be evaluated
Returns:

the value of the normalized Gaussian described by the parameters p

at position x

xrayutilities.math.functions.NormLorentz1d(x, *p)[source]

function to calculate a normalized one dimensional Lorentzian

Parameters:

**p: list of parameters of the Lorentzian**

[XCEN,FWHM]

x:coordinate(s) where the function should be evaluated
Returns:

the value of the normalized Lorentzian described by the parameters p

at position x

xrayutilities.math.functions.PseudoVoigt1d(x, *p)[source]

function to calculate a pseudo Voigt function as linear combination of a Gauss and Lorentz peak

Parameters:

**p: list of parameters of the pseudo Voigt-function**

[XCEN,FWHM,AMP,BACKGROUND,ETA] :ETA: 0 ...1 0 means pure Gauss and 1 means pure Lorentz

x:coordinate(s) where the function should be evaluated
Returns:

the value of the PseudoVoigt described by the parameters p

at position ‘x’

xrayutilities.math.functions.PseudoVoigt1dArea(*p)[source]

function to calculate the area of a pseudo Voigt function with neglected background

Parameters:

**p: list of parameters of the Lorentz-function**

[XCEN,FWHM,AMP,BACKGROUND,ETA] :ETA: 0 ...1 0 means pure Gauss and 1 means pure Lorentz

Returns:

the area of the PseudoVoigt described by the parameters p

xrayutilities.math.functions.PseudoVoigt1d_der_p(x, *p)[source]

function to calculate the derivative of a PseudoVoigt with respect the parameters p

for parameter description see PseudoVoigt1d

xrayutilities.math.functions.PseudoVoigt1d_der_x(x, *p)[source]

function to calculate the derivative of a PseudoVoigt with respect to x

for parameter description see PseudoVoigt1d

xrayutilities.math.functions.PseudoVoigt1dasym(x, *p)[source]

function to calculate an asymmetric pseudo Voigt function as linear combination of asymmetric Gauss and Lorentz peak

Parameters:

**p: list of parameters of the pseudo Voigt-function**

[XCEN,FWHMLEFT,FWHMRIGHT,AMP,BACKGROUND,ETA] :ETA: 0 ...1 0 means pure Gauss and 1 means pure Lorentz

x:coordinate(s) where the function should be evaluated
Returns:

the value of the PseudoVoigt described by the parameters p

at position ‘x’

xrayutilities.math.functions.PseudoVoigt1dasym2(x, *p)[source]

function to calculate an asymmetric pseudo Voigt function as linear combination of asymmetric Gauss and Lorentz peak

Parameters:

**p: list of parameters of the pseudo Voigt-function**

[XCEN,FWHMLEFT,FWHMRIGHT,AMP,BACKGROUND,ETALEFT, ETARIGHT] :ETA: 0 ...1 0 means pure Gauss and 1 means pure Lorentz

x:coordinate(s) where the function should be evaluated
Returns:

the value of the PseudoVoigt described by the parameters p

at position ‘x’

xrayutilities.math.functions.PseudoVoigt2d(x, y, *p)[source]

function to calculate a pseudo Voigt function as linear combination of a Gauss and Lorentz peak in two dimensions

Parameters:

**x,y: coordinate(s) where the function should be evaluated**

p:list of parameters of the pseudo Voigt-function [XCEN,YCEN,FWHMX,FWHMY,AMP,BACKGROUND,ANGLE,ETA] :ETA: 0 ...1 0 means pure Gauss and 1 means pure Lorentz
Returns:

the value of the PseudoVoigt described by the parameters p

at position (x,y)

xrayutilities.math.functions.TwoGauss2d(x, y, *p)[source]

function to calculate two general two dimensional Gaussians

Parameters:

**p: list of parameters of the Gauss-function**

[XCEN1,YCEN1,SIGMAX1,SIGMAY1,AMP1,ANGLE1,XCEN2,YCEN2, SIGMAX2,SIGMAY2,AMP2,ANGLE2,BACKGROUND] SIGMA = FWHM / (2*sqrt(2*log(2))) ANGLE = rotation of the X,Y direction of the Gaussian in radians

x,y:coordinate(s) where the function should be evaluated
Returns:

the value of the Gaussian described by the parameters p

at position (x,y)

xrayutilities.math.functions.heaviside(x)[source]

Heaviside step function for numpy arrays

Parameters:

**x: any scalar of ndarray object**

Returns:
s:Heaviside step function evaluated for all values of x
xrayutilities.math.functions.kill_spike(data, threshold=2.0)[source]

function to smooth **single** data points which differ from the average of the neighboring data points by more than the threshold factor. Such spikes will be replaced by the mean value of the next neighbors.

Warning

Use this function carefully not to manipulate your data!

Parameters:

**data: 1d numpy array with experimental data**

threshold:threshold factor to identify strange data points
Returns:

1d data-array with spikes removed

xrayutilities.math.functions.multPeak1d(x, *args)[source]

function to calculate the sum of multiple peaks in 1D. the peaks can be of different type and a background function (polynom) can also be included.

Parameters:

**x: coordinate where the function should be evaluated**

args:list of peak/function types and parameters for every function type two arguments need to be given first the type of function as string with possible values ‘g’: Gaussian, ‘l’: Lorentzian, ‘v’: PseudoVoigt, ‘a’: asym. PseudoVoigt, ‘p’: polynom the second type of arguments is the tuple/list of parameters of the respective function. See documentation of math.Gauss1d, math.Lorentz1d, math.PseudoVoigt1d, math.PseudoVoigt1dasym, and numpy.polyval for details of the different function types.
Returns:

value of the sum of functions at position x

xrayutilities.math.functions.multPeak2d(x, y, *args)[source]

function to calculate the sum of multiple peaks in 2D. the peaks can be of different type and a background function (polynom) can also be included.

Parameters:

**x,y: coordinates where the function should be evaluated**

args:list of peak/function types and parameters for every function type two arguments need to be given first the type of function as string with possible values ‘g’: Gaussian, ‘l’: Lorentzian, ‘v’: PseudoVoigt, ‘c’: constant the second type of arguments is the tuple/list of parameters of the respective function. See documentation of math.Gauss2d, math.Lorentz2d, math.PseudoVoigt2d for details of the different function types. The constant accepts a single float which will be added to the data
Returns:

value of the sum of functions at position (x,y)

xrayutilities.math.functions.smooth(x, n)[source]

function to smooth an array of data by averaging N adjacent data points

Parameters:

**x: 1D data array**

n:number of data points to average
Returns:
xsmooth:smoothed array with same length as x

xrayutilities.math.misc module

xrayutilities.math.misc.center_of_mass(pos, data, background='none', full_output=False)[source]

function to determine the center of mass of an array

Parameters:

**pos: position of the data points**

data:data values
background:type of background, either ‘none’, ‘constant’ or ‘linear’
full_output:return background cleaned data and background-parameters
Returns:

center of mass position (single float)

xrayutilities.math.misc.fwhm_exp(pos, data)[source]

function to determine the full width at half maximum value of experimental data. Please check the obtained value visually (noise influences the result)

Parameters:

**pos: position of the data points**

data:data values
Returns:

fwhm value (single float)

xrayutilities.math.transforms module

class xrayutilities.math.transforms.AxisToZ(newzaxis)[source]

Bases: xrayutilities.math.transforms.CoordinateTransform

Creates a coordinate transformation to move a certain axis to the z-axis. The rotation is done along the great circle. The x-axis of the new coordinate frame is created to be normal to the new and original z-axis. The new y-axis is create in order to obtain a right handed coordinate system.

class xrayutilities.math.transforms.AxisToZ_keepXY(newzaxis)[source]

Bases: xrayutilities.math.transforms.CoordinateTransform

Creates a coordinate transformation to move a certain axis to the z-axis. The rotation is done along the great circle. The x-axis/y-axis of the new coordinate frame is created to be similar to the old x and y directions. This variant of AxisToZ assumes that the new Z-axis has its main component along the Z-direction

class xrayutilities.math.transforms.CoordinateTransform(v1, v2, v3)[source]

Bases: xrayutilities.math.transforms.Transform

Create a Transformation object which transforms a point into a new coordinate frame. The new frame is determined by the three vectors v1/norm(v1), v2/norm(v2) and v3/norm(v3), which need to be orthogonal!

class xrayutilities.math.transforms.Transform(matrix)[source]

Bases: object

inverse(args, rank=1)[source]

performs inverse transformation a vector, matrix or tensor of rank 4

Parameters:

**args: object to transform, list or numpy array of shape**

(...,n) (...,n,n), (...,n,n,n,n) where n is the size of the transformation matrix.

rank:rank of the supplied object. allowed values are 1, 2, and 4
xrayutilities.math.transforms.XRotation(alpha, deg=True)[source]

Returns a transform that represents a rotation about the x-axis by an angle alpha. If deg=True the angle is assumed to be in degree, otherwise the function expects radiants.

xrayutilities.math.transforms.YRotation(alpha, deg=True)[source]

Returns a transform that represents a rotation about the y-axis by an angle alpha. If deg=True the angle is assumed to be in degree, otherwise the function expects radiants.

xrayutilities.math.transforms.ZRotation(alpha, deg=True)[source]

Returns a transform that represents a rotation about the z-axis by an angle alpha. If deg=True the angle is assumed to be in degree, otherwise the function expects radiants.

xrayutilities.math.transforms.mycross(vec, mat)[source]

function implements the cross-product of a vector with each column of a matrix

xrayutilities.math.transforms.rotarb(vec, axis, ang, deg=True)[source]

function implements the rotation around an arbitrary axis by an angle ang positive rotation is anti-clockwise when looking from positive end of axis vector

Parameters:

**vec: numpy.array or list of length 3**

axis:numpy.array or list of length 3
ang:rotation angle in degree (deg=True) or in rad (deg=False)
deg:boolean which determines the input format of ang (default: True)
Returns:
rotvec:rotated vector as numpy.array

Examples

>>> rotarb([1,0,0],[0,0,1],90)
array([  6.12323400e-17,   1.00000000e+00,   0.00000000e+00])
xrayutilities.math.transforms.tensorprod(vec1, vec2)[source]

function implements an elementwise multiplication of two vectors

xrayutilities.math.vector module

module with vector operations for vectors of size 3, since for so short vectors numpy does not give the best performance explicit implementation of the equations is performed together with error checking to ensure vectors of length 3.

xrayutilities.math.vector.VecAngle((v1.v2)/(norm(v1)*norm(v2)))[source]
Parameters:

**v1 .............. vector as numpy array or list**

v2:vector as numpy array or list

**optional keyword arguments:**

deg:(default: false) return result in degree otherwise in radiants
Returns:

float value with the angle inclined by the two vectors

xrayutilities.math.vector.VecCross(v1, v2, out=None)[source]

Calculate the vector cross product.

Parameters:

**v1 .............. vector as numpy array or list**

v2:vector as numpy array or list
out:optional output vector
Returns:

float value

xrayutilities.math.vector.VecDot(v1, v2)[source]

Calculate the vector dot product.

Parameters:

**v1 .............. vector as numpy array or list**

v2:vector as numpy array or list
Returns:

float value

xrayutilities.math.vector.VecNorm(v)[source]

Calculate the norm of a vector.

Parameters:**v .......... vector as list or numpy array**
Returns:float holding the vector norm
xrayutilities.math.vector.VecUnit(v)[source]

Calculate the unit vector of v.

Parameters:**v ........... vector as list or numpy array**
Returns:numpy array with the unit vector
xrayutilities.math.vector.getSyntax(vec)[source]

returns vector direction in the syntax ‘x+’ ‘z-‘ or equivalents therefore works only for principle vectors of the coordinate system like e.g. [1,0,0] or [0,2,0]

Parameters:**vec: vector of length 3**
Returns:[xyz][+-]
xrayutilities.math.vector.getVector(string)[source]

returns unit vector along a rotation axis given in the syntax ‘x+’ ‘z-‘ or equivalents

Parameters:**string [xyz][+-]**
Returns:vector along the given direction as numpy array

Module contents