functions¶
Peak functions
find_local_maxima(y, yerror=None)
¶
Find local maxima in 1d arrays, returns index of local maximums, plus estimation of the peak power for each maxima and a classification of whether the maxima is greater than the standard deviation of the error.
E.G. index, power, isgood = find_local_maxima(ydata) maxima = ydata[index[isgood]] maxima_power = power[isgood]
Peak Power: peak power for each maxima is calculated using the peak_ratio algorithm for each maxima and adjacent points Good Peaks: Maxima are returned Good if: power > (max(y) - min(y)) / std(yerror)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
ndarray
|
array(n) of data |
required |
yerror
|
ndarray | None
|
array(n) of errors on data, or None to use default error function (sqrt(abs(y)+1)) |
None
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray, ndarray]
|
bool array(m) where True elements have power > power of the array |
Source code in mmg_toolbox/fitting/functions.py
find_peaks(y, yerror=None, min_peak_power=None, peak_distance_idx=6)
¶
Find peak shaps in linear-spaced 1d arrays with poisson like numerical values
E.G. index, power = find_peaks(ydata, yerror, min_peak_power=None, peak_distance_idx=10) peak_centres = xdata[index] # ordered by peak strength
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
ndarray
|
array(n) of data |
required |
yerror
|
ndarray | None
|
array(n) of errors on data, or None to use default error function (sqrt(abs(y)+1)) |
None
|
min_peak_power
|
float | None
|
float, only return peaks with power greater than this. If None compare against std(y) |
None
|
peak_distance_idx
|
int
|
int, group adjacent maxima if closer in index than this |
6
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray]
|
array(m) of estimated power of each peak |
Source code in mmg_toolbox/fitting/functions.py
find_peaks_str(x, y, yerror=None, min_peak_power=None, peak_distance_idx=6)
¶
Find peak shaps in linear-spaced 1d arrays with poisson like numerical values
E.G. index, power = find_peaks(ydata, yerror, min_peak_power=None, peak_distance_idx=10) peak_centres = xdata[index] # ordered by peak strength
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
array(n) of data |
required |
y
|
ndarray
|
array(n) of data |
required |
yerror
|
ndarray | None
|
array(n) of errors on data, or None to use default error function (sqrt(abs(y)+1)) |
None
|
min_peak_power
|
float | None
|
float, only return peaks with power greater than this. If None compare against std(y) |
None
|
peak_distance_idx
|
int
|
int, group adjacent maxima if closer in index than this |
6
|
Returns:
| Type | Description |
|---|---|
str
|
array(m) of estimated power of each peak |
Source code in mmg_toolbox/fitting/functions.py
gauss(x, y=None, height=1, cen=0, fwhm=0.5, bkg=0, cen_y=None, fwhm_y=None)
¶
Define Gaussian distribution in 1 or 2 dimensions
y[1xn] = gauss(x[1xn], height=10, cen=0, fwhm=1, bkg=0)
- OR -
Z[nxm] = gauss(x[1xn], y[1xm], height=100, cen=4, fwhm=5, bkg=30)
From http://fityk.nieto.pl/model.html
height is related to amplitude (area) by: height = (area / fwhm) * sqrt(4ln(2)/pi) ~ 0.94 * area / fwhm area = height * fwhm / sqrt(4ln(2)/pi) ~ 1.06 * height * fwhm - or for 2D - area = height * fwhm_x * fwhm_y * (pi / 4ln(2)) sigma is related to fwhm by: fwhm = 2 * sqrt(2ln(2)) * sigma sigma = fwhm / (2sqrt(2ln(2)))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
[1xn] array of values, defines size of gaussian in dimension 1 |
required |
y
|
ndarray | None
|
None* or [1xm] array of values, defines size of gaussian in dimension 2 |
None
|
height
|
float
|
peak height |
1
|
cen
|
float
|
peak centre |
0
|
fwhm
|
float
|
peak full width at half-max |
0.5
|
bkg
|
float
|
background |
0
|
cen_y
|
float | None
|
peak centre in y-axis (None to use cen) |
None
|
fwhm_y
|
float | None
|
peak full width in y-axis (None to use fwhm) |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
[nxm array] 2D Gaussian distribution |
Source code in mmg_toolbox/fitting/functions.py
gen_weights(yerrors=None)
¶
Generate weights for fitting routines
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yerrors
|
array(n) or None |
None
|
Returns:
| Type | Description |
|---|---|
ndarray | None
|
array(n) or None |
Source code in mmg_toolbox/fitting/functions.py
group_adjacent(values, close=10)
¶
Average adjacent values in array, return grouped array and indexes to return groups to original array
E.G. grp, idx = group_adjacent([1,2,3,10,12,31], close=3) grp -> [2, 11, 31] idx -> [[0,1,2], [3,4], [5]]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ndarray
|
array of values to be grouped |
required |
close
|
float
|
float |
10
|
Returns:
| Type | Description |
|---|---|
|
[n] list of lists, each item relates to an averaged group, with indexes from values |
Source code in mmg_toolbox/fitting/functions.py
local_maxima_1d(y)
¶
Find local maxima in 1d array Returns points with central point higher than neighboring points.
Copied from scipy.signal._peak_finding_utils https://github.com/scipy/scipy/blob/v1.7.1/scipy/signal/_peak_finding_utils.pyx
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
ndarray
|
list or array |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
array of peak indexes |
Source code in mmg_toolbox/fitting/functions.py
max_index(array)
¶
peak_ratio(y, yerror=None)
¶
Return the ratio signal / error for given dataset From Blessing, J. Appl. Cryst. (1997). 30, 421-426 Equ: (1) + (6) peak_ratio = (sum((y-bkg)/dy2)/sum(1/dy2)) / sqrt(i/sum(1/dy^2))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y
|
ndarray
|
array of y data |
required |
yerror
|
ndarray | None
|
array of errors on data, or None to calcualte np.sqrt(y+0.001) |
None
|
Returns:
| Type | Description |
|---|---|
float
|
float ratio signal / err |