core.feature
Module¶
This module contains code for extracting features from data
-
exception
skbeam.core.feature.
PeakRejection
¶ Bases:
Exception
Custom exception class to indicate that the refine function rejected the candidate peak.
This uses the exception handling framework in a method akin to StopIteration to indicate that there will be no return value.
-
skbeam.core.feature.
filter_n_largest
(y, cands, N)¶ Filters the N largest candidate peaks
Return a maximum of N largest candidates. If N > len(cands) then all of the cands will be returned sorted, else the indices of the N largest peaks will be returned in descending order.
- Parameters
- yarray
Independent variable
- candsarray
An array containing the indices of candidate peaks
- Nint
The maximum number of peaks to return, sorted by size. Must be positive
- Returns
- candsarray
An array of the indices of up to the N largest candidates
-
skbeam.core.feature.
filter_peak_height
(y, cands, thresh, window=5)¶ Filter to remove candidate that are too small. This is implemented by looking at the relative height (max - min) of the peak in a window around the candidate peak.
- Parameters
- yarray
Independent variable
- candsarray
An array containing the indices of candidate peaks
- threshint
The minimum peak-to-peak size of the candidate peak to be accepted
- windowint, optional
The size of the window around the peak to consider
- Returns
- candsarray
An array of the indices which pass the filter
-
skbeam.core.feature.
peak_refinement
(x, y, cands, window, refine_function, refine_args=None)¶ Refine candidate locations
- Parameters
- xarray
The independent variable, does not need to be evenly spaced.
- yarray
The dependent variable. Must correspond 1:1 with the values in x
- candsarray
Array of the indices in x (and y) for the candidate peaks.
- refine_functionfunction
A function which takes a section of data with a peak in it and returns the location and height of the peak to sub-sample accuracy. Additional parameters can be passed through via the refine_args kwarg. The function signature must be:
center, height = refine_func(x, y, **kwargs)
This function may raise PeakRejection to indicate no suitable peak was found
- windowint
How many samples to extract on either side of the candidate locations are passed to the refine function. The window will be truncated near the boundaries. The length of the data passed to the refine function will be (2 * window + 1).
- refine_argsdict, optional
The passed to the refine_function
- Returns
- peak_locationsarray
The locations of the peaks
- peak_heightsarray
The heights of the peaks
Examples
>>> x = np.arange(512) >>> tt = np.zeros(512) >>> tt += np.exp(-((x - 150.55)/10)**2) >>> tt += np.exp(-((x - 450.75)/10)**2) >>> cands = scipy.signal.argrelmax(tt)[0]
>>> print(peak_refinement(x, tt, cands, 10, refine_quadratic)) (array([ 150.62286432, 450.7909412 ]), array([ 0.96435832, 0.96491501])) >>> print(peak_refinement(x, tt, cands, 10, refine_log_quadratic)) (array([ 150.55, 450.75]), array([ 1., 1.]))
-
skbeam.core.feature.
refine_log_quadratic
(x, y, Rval_thresh=None)¶ Attempts to refine the peaks by fitting a quadratic to the log of the y-data. This is a linear approximation of fitting a Gaussian.
- Parameters
- xarray
Independent variable
- yarray
Dependent variable
- Rval_threshfloat, optional
Threshold for R^2 value of fit, If the computed R^2 is worse than this threshold PeakRejection will be raised
- Returns
- centerfloat
Refined estimate for center
- heightfloat
Refined estimate for height
- Raises
- PeakRejection
Raised to indicate that no suitable peak was found in the interval
-
skbeam.core.feature.
refine_quadratic
(x, y, Rval_thresh=None)¶ Attempts to refine the peaks by fitting to a quadratic function.
- Parameters
- xarray
Independent variable
- yarray
Dependent variable
- Rval_threshfloat, optional
Threshold for R^2 value of fit, If the computed R^2 is worse than this threshold PeakRejection will be raised
- Returns
- centerfloat
Refined estimate for center
- heightfloat
Refined estimate for height
- Raises
- PeakRejection
Raised to indicate that no suitable peak was found in the interval