spectrochempy.find_peaks#
- find_peaks(dataset, height=None, window_length=3, threshold=None, distance=None, prominence=None, width=None, wlen=None, rel_height=0.5, plateau_size=None, use_coord=True, as_result=False, *, dims=None)[source]#
Find and analyze peaks in spectroscopic data with advanced filtering options.
This function extends scipy.signal.find_peaks by adding spectroscopy-specific features like coordinate system awareness and unit handling. It performs peak detection through local maxima analysis and supports various filtering criteria to identify significant peaks.
- Parameters:
dataset (NDDataset) – Input dataset containing spectral data. The historical path accepts 1D data (including squeezed singleton data). A true 2D spectrum must explicitly provide both dimensions through
dims.height (float or array-like, optional) – In 1D, minimum and/or maximum peak height criteria. Can be specified as: - Single value for minimum height - Tuple (min, max) for height range - Array matching x for position-dependent criteria
window_length (int, default: 3) – Window size for peak interpolation. Must be odd. Larger values provide smoother interpolation but may miss narrow peaks.
threshold (float or array-like, optional) – Minimum height difference between peak and neighboring points. Useful for filtering out noise-related peaks.
distance (float, optional) – Minimum separation between peaks. In 1D, this follows SciPy peak spacing behavior. In 2D, use
dimsand provide a pair of distances or a mapping from each selected dimension name to a point count or physical distance. 2D suppression uses the documented rectangular anisotropic metric.prominence (float or array-like, optional) – Required prominence (height above surrounding baseline) of peaks.
width (float or array-like, optional) – Required width of peaks. Interpreted as coordinate units if use_coord=True, otherwise as number of points.
wlen (int or float, optional) – Window length for prominence calculation. Affects computation speed for large datasets.
rel_height (float, default: 0.5) – Relative height for width calculation (0-1 range).
plateau_size (float or array-like, optional) – Required size of peak plateau (flat top).
use_coord (bool, default: True) – Whether to use coordinate system units instead of array indices.
as_result (bool, default: False) – If True, return a
PeakFindingResultobject. If False, preserve the historical(peaks, properties)tuple return.dims (sequence of str or int, optional) – Explicit dimensions to analyze.
Nonepreserves historical dispatch. One dimension selects the existing 1D engine; exactly two dimensions select deterministic positive-maximum detection for a true 2D spectrum.
- Returns:
peaks (NDDataset, PeakTable or None) – Dataset containing identified 1D peaks with interpolated positions, or a
PeakTablecontaining discrete 2D peaks. Returned whenas_result=False.properties (dict or None) – In 1D, SciPy peak properties. In 2D,
peak_heightsonly. Values use appropriate units when applicable. Returned whenas_result=False.result (PeakFindingResult) – Structured peak finding result. For 1D,
peaksis an NDDataset; for 2D,peaksandtableare the samePeakTable. Returned whenas_result=True. Result rows includesample_index,sample_positionandsample_heightfor the discrete detector sample in addition to the refinedpositionandheight.
Notes
Peak positions are refined using quadratic interpolation when window_length > 1
The function handles units automatically when use_coord=True
For noisy data, consider preprocessing with smoothing functions
In
PeakFindingResult,indexis the row identifier,sample_indexis the local index returned by the detector for the 1D signal actually analyzed,sample_positionandsample_heightare the coordinate and signal value at that sample, andpositionandheightare the existing refined values. No global index into a parent dataset is reported for sliced inputs.The 2D path is selected only with
dims=(dim0, dim1)and returns rows with native source indices and one position per selected dimension.The 2D path supports positive maxima in real numeric data only. It does not calculate interpolated positions, widths, prominence, volumes, or integrals. A true 2D dataset without explicit
dimsretains the historical dimensionality error.2D intensities are processed in a detached
float64working array. Integer-valued intensities above2**53may lose unit precision during conversion; scale such data before peak detection when that precision is scientifically important.
Examples
Basic peak finding with a synthetic spectrum:
>>> x = scp.Coord.linspace(0.0, 10.0, 501, title="x", units="cm^-1") >>> y = (scp.gaussian(x, ampl=1.0, pos=3.0, width=0.5, normalized=False) ... + scp.gaussian(x, ampl=0.8, pos=7.0, width=0.6, normalized=False)) >>> ds = scp.NDDataset(y, coordset=[x]) >>> peaks, props = ds.find_peaks(height=0.5) >>> len(peaks) 2
Physical-unit spacing constraints are accepted when coordinates carry units:
>>> x = scp.Coord.linspace(0.0, 10.0, 501, title="x", units="cm^-1") >>> y = (scp.gaussian(x, ampl=1.0, pos=3.0, width=0.5, normalized=False) ... + scp.gaussian(x, ampl=0.8, pos=7.0, width=0.6, normalized=False)) >>> ds = scp.NDDataset(y, coordset=[x]) >>> peaks, props = ds.find_peaks(distance="1 cm^-1", width=0.2) >>> len(peaks) 2
Return a structured result when a tabular/export representation is useful:
>>> x = scp.Coord.linspace(0.0, 10.0, 501, title="x", units="cm^-1") >>> y = (scp.gaussian(x, ampl=1.0, pos=3.0, width=0.5, normalized=False) ... + scp.gaussian(x, ampl=0.8, pos=7.0, width=0.6, normalized=False)) >>> ds = scp.NDDataset(y, coordset=[x]) >>> result = ds.find_peaks(height=0.5, as_result=True) >>> rows = result.to_dict() >>> len(rows) 2
Explicit 2D detection with physical, anisotropic coordinate distances:
>>> y = scp.Coord.linspace(-1.0, 1.0, 41, title="y", units="ppm") >>> x = scp.Coord.linspace(0.0, 2.0, 81, title="x", units="ppm") >>> yy, xx = np.meshgrid(y.data, x.data, indexing="ij") >>> z = np.exp(-((yy + 0.4) ** 2 + (xx - 0.6) ** 2) / 0.02) >>> z += 0.8 * np.exp(-((yy - 0.45) ** 2 + (xx - 1.5) ** 2) / 0.03) >>> map2d = scp.NDDataset(z, coordset=[y, x], units="absorbance") >>> result2d = map2d.find_peaks( ... dims=("y", "x"), ... height=0.2, ... distance={"y": 0.2 * scp.ur.ppm, "x": 0.25 * scp.ur.ppm}, ... as_result=True, ... ) >>> result2d.table.columns ('index', 'y_index', 'x_index', 'y_position', 'x_position', 'height')