sloth.math: mathematical utilities

sloth.math.gridxyz: grid (X,Y,Z) to meshgrid

Utilities to work with 2D grids and interpolation

sloth.math.gridxyz.gridxyz(xcol, ycol, zcol, xystep=None, lib='scipy', method='cubic')[source]

Grid (X, Y, Z) 1D data on a 2D regular mesh

Parameters:
  • zcol (xcol, ycol,) –

  • xystep (the step size of the XY grid) –

  • lib (library used for griddata) – [scipy] matplotlib

  • method (interpolation method) –

Returns:

  • xgrid, ygrid (1D arrays giving abscissa and ordinate of the map)

  • zz (2D array with the gridded intensity map)

See also

-()

sloth.math.gridxyz.gridxyz_larch(xcol, ycol, zcol, xystep=None, method='cubic', lib='scipy', _larch=None)[source]

Larch equivalent of gridxyz() Grid (X, Y, Z) 1D data on a 2D regular mesh

Parameters:
  • zcol (xcol, ycol,) –

  • xystep (the step size of the XY grid) –

  • lib (library used for griddata) – [scipy] matplotlib

  • method (interpolation method) –

Returns:

  • xgrid, ygrid (1D arrays giving abscissa and ordinate of the map)

  • zz (2D array with the gridded intensity map)

See also

-()

sloth.math.rotmatrix: 3D rotation matrix

Rotation matrix using the Euler-Rodrigues formula

Rotation using the right hand rule

Code downloaded from http://stackoverflow.com/questions/6802577/python-rotation-of-3d-vector

sloth.math.rotmatrix.rotation_matrix_numpy(axis, theta)[source]

return the rotation matrix using numpy

Parameters:
  • axis (numpy.array([x,y,z])) –

  • theta (rotation angle in radians) –

sloth.math.rotmatrix.rotation_matrix_weave(axis, theta, mat=None)[source]

return the rotation matrix using Weave (~20x faster than numpy)

Parameters:
  • axis (numpy.array([x,y,z])) –

  • theta (rotation angle in radians) –

sloth.math.rotmatrix.rotate(arr, axis, theta, method='numpy')[source]

rotate array around axis by theta with ‘numpy’ or ‘weave’

Parameters:
  • arr (np.array([x,y,z])) –

  • axis (np.array([x,y,z])) –

  • theta (in radians) –

  • method ('numpy' or 'weave') –

Returns:

Return type:

np.array([x,y,z])

sloth.math.convolution1D: XAS convolution

Generic discrete convolution

Description

This is a manual (not optimized!) implementation of discrete 1D convolution intended for spectroscopy analysis. The difference with commonly used methods is the possibility to adapt the convolution kernel for each convolution point, e.g. change the FWHM of the Gaussian kernel as a function of the energy scale.

Resources

sloth.math.convolution1D.gaussian(x, cen=0, sigma=1, fwhm=False, peak=None)[source]

1 dimensional Gaussian function (https://en.wikipedia.org/wiki/Gaussian_function)

Parameters:
  • x (array) –

  • cen ([0] center, x0) –

  • sigma ([1] standard deviation, FWHM = 2*sqrt(2*ln(2)) * sigma =~ 2.35482 * sigma) –

  • fwhm ([False] if True, the given sigma is assumed as fwhm and then converted accordingly) –

  • peak ([None] if None, peak = 1 / math.sqrt(2*math.pi), the distribution integrate to 1) –

sloth.math.convolution1D.lorentzian(x, cen=0, gamma=1, peak=None)[source]

1 dimensional Lorentzian

Parameters:
  • x (array) –

  • cen ([0] center, x0) –

  • gamma ([1] half width at half maximum) –

  • peak ([None] if None, peak = 1 / (math.pi*sigma), the distribution integrate to 1) –

sloth.math.convolution1D.get_ene_index(ene, cen, hwhm)[source]

returns the min/max indexes for array ene at (cen-hwhm) and (cen+hwhm) very similar to index_of in larch

sloth.math.convolution1D.lin_gamma(ene, fwhm=1.0, linbroad=None)[source]

returns constant or linear energy-dependent broadening

Parameters:
  • ene (energy array in eV) –

  • fwhm (first full width at half maximum in eV) –

  • linbroad (list of 3-elements giving) – ‘second full width at half maximum’ ‘energy starting point of the linear increase’ ‘energy ending point of the linear increase’

sloth.math.convolution1D.atan_gamma(ene, gamma_hole, gamma_max=15.0, e0=0, eslope=1.0)[source]

returns arctangent-like broadening, $Gamma(E)$

..math

Gamma(E) = Gamma_{hole} + Gamma_{max} * ( rctan(

rac{E-E_{0}}{E_{slope}} ) / pi + 1/2) )

sloth.math.convolution1D.conv(e, mu, kernel='gaussian', fwhm_e=None, efermi=None)[source]

linear broadening

Parameters:
  • e (x-axis (energy)) –

  • mu (f(x) to convolve with g(x) kernel, mu(energy)) –

  • kernel (convolution kernel, g(x)) – ‘gaussian’ ‘lorentzian’

  • fwhm_e (the full width half maximum in eV for the kernel) – broadening. It is an array of size ‘e’ with constants or an energy-dependent values determined by a function as ‘lin_gamma()’ or ‘atan_gamma()’

sloth.math.convolution1D.glinbroad(e, mu, fwhm_e=None, efermi=None, _larch=None)[source]

linear broadening

Parameters:
  • e (x-axis (energy)) –

  • mu (f(x) to convolve with g(x) kernel, mu(energy)) –

  • kernel (convolution kernel, g(x)) – ‘gaussian’ ‘lorentzian’

  • fwhm_e (the full width half maximum in eV for the kernel) – broadening. It is an array of size ‘e’ with constants or an energy-dependent values determined by a function as ‘lin_gamma()’ or ‘atan_gamma()’

class sloth.math.convolution1D.FdmnesConv(opts=None, calcroot=None, fn_in=None, fn_out=None)[source]

Performs convolution with FDMNES within Python

wfdmfile()[source]

write a simple fdmfile.txt to enable the convolution first makes a copy of previous fdmfile.txt if not already done

run()[source]

runs fdmnes

sloth.math.normalization: normalization schemes

Normalization schemes

sloth.math.normalization.norm1D(y, norm=None, logger=None, **kws)[source]

collection of simple normalization methods

Parameters:
  • y (array of float, to normalize) –

  • norm (string, available options) – “max” -> y / np.max(y) “max-min” -> (y - np.min(y)) / (np.max(y) - np.min(y)) “area” -> (y - np.min(y)) / np.trapz(y, x=kws.get(‘x’)) “sum” -> (y - np.min(y)) / np.sum(y) “larch” -> TODO!!!

Returns:

ynorm

Return type:

array of float

sloth.math.geometry3D: simple 3D geometry

Collection of simple 3D geometry utilities

Sources

The following functions are simply grabbed for the net. In this case, their own license apply.

sloth.math.geometry3D.dot(v1, v2)[source]

Dot product of two vectors

sloth.math.geometry3D.cross(v1, v2)[source]

Cross product of two vectors

sloth.math.geometry3D.length(v)[source]

Length of vector

sloth.math.geometry3D.unit(v)[source]

A unit vector in the direction of v

sloth.math.geometry3D.circle_3p(A, B, C)[source]

get center and radius of a circle given 3 points in space

sloth.math.geometry3D.plane_3p(p1, p2, p3)[source]

get the plane ax+by+cz+d=0 given 3 points, p1, p2, p3

sloth.math.geometry3D.circle_radius(point, center)[source]

get circle radius given a point and center as 3D arrays

sloth.math.geometry3D.lines2_intersect(p10, p11, p20, p21)[source]

get intesection point, assuming line1 and line2 intersect and represented by two points each line, respectively, (p10, p11) and (p20, p21)

sloth.math.geometry3D.angle_3p(p0, p1, p2)[source]

get the angle between three 3D points, p0 is the intersection point

sloth.math.geometry3D.point_on_plane_projection(point, plane, test=False)[source]

get the orthogonal projection of a 3d point on plane

Parameters:
  • point (3d P(x,y,z) => np.array([x,y,z])) –

  • plane (Ax+By+Cz+d=0, norm = (A,B,C)) – => np.array([norm_x, norm_y, norm_z, d])

Returns:

proj_pt – => np.array([proj_x, proj_y, proj_z])

Return type:

projected point = point - norm * offset

sloth.math.deglitch: data deglich (=remove spikes)

Deglitch utilities

sloth.math.deglitch.remove_spikes_medfilt1d(y_spiky, backend='silx', kernel_size=3, threshold=0.1)[source]

Remove spikes in a 1D array using medfilt from silx.math

Parameters:
  • y_spiky (array) – spiky data

  • backend (str, optional) – library to use as backend - ‘silx’ -> from silx.math.medianfilter import medfilt1d - ‘pymca’ -> from PyMca5.PyMcaMath.PyMcaSciPy.signal import medfilt1d - ‘pandas’ : TODO

  • kernel_size (int, optional) – kernel size where to calculate median, must be odd [3]

  • threshold (float, optional) – relative difference between filtered and spiky data [0.1]

Returns:

filtered array

Return type:

array

sloth.math.deglitch.remove_spikes_silx(y_spiky, kernel_size=3, threshold=0.1)[source]

Remove spikes in a 1D array using medfilt from silx.math

Parameters:
  • y_spiky (array) – spiky data

  • kernel_size (int, optional) – kernel size where to calculate median, must be odd [3]

  • threshold (float, optional) – difference between filtered and spiky data relative [0.1]

Returns:

filtered array

Return type:

array

sloth.math.deglitch.remove_spikes_pymca(y_spiky, kernel_size=9, threshold=0.66)[source]

Remove spikes in a 1D array using medfilt from PyMca5.PyMcaMath.PyMcaScipy.signal

Parameters:
  • y_spiky (array) – spiky data

  • kernel_size (int, optional) – kernel size where to calculate median, should be odd [9]

  • threshold (float, optional) – difference between filtered and spiky data in sigma units [0.66]

Returns:

filtered array

Return type:

array

sloth.math.deglitch.remove_spikes_pandas(y, window=3, threshold=3)[source]

remove spikes using pandas

Taken from `https://ocefpaf.github.io/python4oceanographers/blog/2015/03/16/outlier_detection/`_

Note

this will not work in pandas > 0.17 one could simply do df.rolling(3, center=True).median(); also df.as_matrix() is deprecated, use df.values instead

Parameters:
  • y (array 1D) –

  • window (int (optional)) – window in rolling median [3]

  • threshold (int (optional)) – number of sigma difference with original data

Returns:

ynew

Return type:

array like x/y