PyMcaMath Package¶
PyMcaMath
Package¶
ImageRegistration
Module¶
This is a python module to measure image offsets
-
PyMca5.PyMcaMath.ImageRegistration.
get_crop_indices
(shape, shifts0, shifts1)[source]¶ Get the indices of the valid region to be used when aligning a set of images :param shitfs0: Shifts applied to the first dimension :param shitfs1: Shifts applied to the second dimension
-
PyMca5.PyMcaMath.ImageRegistration.
measure_offset
(img1, img2, method='fft', withLog=False)[source]¶ Measure the actual offset between 2 images. The first one is the reference. That means, if the image to be shifted is the second one, the shift has to be multiplied byt -1. :param img1: ndarray, first image :param img2: ndarray, second image, same shape as img1 :param withLog: shall we return logs as well ? boolean :return: tuple of floats with the offsets of the second respect to the first
-
PyMca5.PyMcaMath.ImageRegistration.
measure_offset_from_ffts
(img0_fft2, img1_fft2, withLog=False)[source]¶ Convenience method to measure the actual offset between 2 images taing their FFTs as inpuy The first FFT one is the one of the reference. That means, if the image to be shifted is the second one, the shift has to be multiplied byt -1. :param img1: ndarray, FFT of first image :param img2: ndarray, FFT of the second image, same shape as img1 :param withLog: shall we return logs as well ? boolean :return: tuple of floats with the offsets of the second respect to the first
-
PyMca5.PyMcaMath.ImageRegistration.
shiftBilinear
(img, shift)[source]¶ Shift an array like scipy.ndimage.interpolation.shift(input, shift, mode=”wrap”, order=”infinity”) :param input: 2d numpy array :param shift: 2-tuple of float :return: shifted image
-
PyMca5.PyMcaMath.ImageRegistration.
shiftFFT
(img, shift)[source]¶ Shift an array using FFTs :param input: 2d numpy array :param shift: 2-tuple of float :return: shifted image
-
PyMca5.PyMcaMath.ImageRegistration.
shiftImage
(img, shift, method=None)[source]¶ Shift an array like scipy.ndimage.interpolation.shift(input, shift, mode=”wrap”, order=”infinity”) :param input: 2d numpy array :param shift: 2-tuple of float :param method: string set to PYMCA, SCIPY or FFT. Default is to try the first of those that is possible. :return: shifted image
SGModule
Module¶
-
PyMca5.PyMcaMath.SGModule.
calc_coeff
(num_points, pol_degree, diff_order=0)[source]¶ calculates filter coefficients for symmetric savitzky-golay filter. see: http://www.nrbook.com/a/bookcpdf/c14-8.pdf
- num_points means that 2*num_points+1 values contribute to the
- smoother.
pol_degree is degree of fitting polynomial
- diff_order is degree of implicit differentiation.
0 means that filter results in smoothing of function 1 means that filter results in smoothing the first
derivative of function.and so on ...
SNIPModule
Module¶
-
PyMca5.PyMcaMath.SNIPModule.
getImageBackground
(image, width, roi_min=None, roi_max=None, smoothing=1)[source]¶
-
PyMca5.PyMcaMath.SNIPModule.
getSnip1DBackground
(spectrum, width, roi_min=None, roi_max=None, smoothing=1)¶
-
PyMca5.PyMcaMath.SNIPModule.
getSnip2DBackground
(image, width, roi_min=None, roi_max=None, smoothing=1)¶
-
PyMca5.PyMcaMath.SNIPModule.
getSpectrumBackground
(spectrum, width, roi_min=None, roi_max=None, smoothing=1)[source]¶
-
PyMca5.PyMcaMath.SNIPModule.
replaceStackWithSnip1DBackground
(stack, width, roi_min=None, roi_max=None, smoothing=1)[source]¶
SimpleMath
Module¶
-
class
PyMca5.PyMcaMath.SimpleMath.
SimpleMath
[source]¶ Bases:
object
-
average
(xarr, yarr, x=None)[source]¶ :param xarr : List containing x values in 1-D numpy arrays :param yarr : List containing y Values in 1-D numpy arrays :param x: x values of the final average spectrum (or None) :return: Average spectrum. In case of invalid input (None, None) tuple is returned.
From the spectra given in xarr & yarr, the method determines the overlap in the x-range. For spectra with unequal x-ranges, the method interpolates all spectra on the values given in x if provided or the first curve and averages them.
-
SpecArithmetic
Module¶
- This module implements several mathematical functions:
- peak search
- center of mass search
- fwhm search
WARNING : array are numpy.ndarray objects.
-
PyMca5.PyMcaMath.SpecArithmetic.
search_com
(xdata, ydata)[source]¶ Return the center of mass in arrays xdata and ydata
-
PyMca5.PyMcaMath.SpecArithmetic.
search_fwhm
(xdata, ydata, peak=None, index=None)[source]¶ Search a fwhm and its center in arrays xdata and ydatas. If no fwhm is found, (0,0) is returned. peak and index which are coming from search_peak result, may accelerate calculation
linalg
Module¶
Similar function to the numpy lstsq function with a more rigorous uncertainty treatement besides other optimizations in view of simultaneously solving several equations of the form a x = b. Hopefully licensed under the same terms as numpy itself (BSD license).
-
PyMca5.PyMcaMath.linalg.
getModelMatrixFromFunction
(model_function, dummy_parameters, xdata, derivative=None)[source]¶
-
PyMca5.PyMcaMath.linalg.
lstsq
(a, b, rcond=None, sigma_b=None, weight=False, uncertainties=True, covariances=False, digested_output=False, svd=True, last_svd=None)[source]¶ Return the least-squares solution to a linear matrix equation.
Solves the equation a x = b by computing a vector x that minimizes the Euclidean 2-norm || b - a x ||^2. The equation may be under-, well-, or over- determined (i.e., the number of linearly independent rows of a can be less than, equal to, or greater than its number of linearly independent columns). If a is square and of full rank, then x (but for round-off error) is the “exact” solution of the equation.
- a : array_like, shape (M, N)
- “Model” matrix.
- b : array_like, shape (M,) or (M, K)
- Ordinate or “dependent variable” values. If b is two-dimensional, the least-squares solution is calculated for each of the K columns of b.
- sigma_b : uncertainties on the b values or None. If sigma_b has shape (M,) or (M, 1) and
- b has dimension (M, K), the uncertainty will be the same for all spectra.
- weight: 0 - No data weighting.
- If required, uncertainties will be calculated using either the supplied experimental uncertainties or an experimental uncertainty of 1 for each data point.
- 1 - Statistical weight.
- Weighted fit using the supplied experimental uncertainties or the square root of the b values.
- svd: If not true, a simple matrix inversion will be used in case of weighting with unequal
- data weights. Ignored in any other cases.
- last_svd: Tuple containing U, s, V of the weighted model matrix or None. This is to
- prevent recalculation on repeated fits.
- uncertainties: If False, no uncertainties will be calculated unless the covariance
- matrix is requested.
covariances: If True, an array of covariance matrix/matrices will be returned.
digested_output: If True, returns a dictionnary with explicit keys
- x : ndarray, shape (N,) or (N, K)
- Least-squares solution. The shape of x depends on the shape of b.
uncertainties: ndarray, shape (N,) or (N, K)
covariances: ndarray, shape (N, N) or (K, N, N)
Fit a line,
y = mx + c
, through some noisy data-points:>>> x = np.array([0, 1, 2, 3]) >>> y = np.array([-1, 0.2, 0.9, 2.1])
By examining the coefficients, we see that the line should have a gradient of roughly 1 and cut the y-axis at, more or less, -1.
We can rewrite the line equation as
y = Ap
, whereA = [[x 1]]
andp = [[m], [c]]
. Now use lstsq to solve for p:>>> A = np.vstack([x, np.ones(len(x))]).T >>> A array([[ 0., 1.], [ 1., 1.], [ 2., 1.], [ 3., 1.]])
>>> m, c = np.linalg.lstsq(A, y)[0] >>> print m, c 1.0 -0.95
Plot the data along with the fitted line:
>>> import matplotlib.pyplot as plt >>> plt.plot(x, y, 'o', label='Original data', markersize=10) >>> plt.plot(x, m*x + c, 'r', label='Fitted line') >>> plt.legend() >>> plt.show()