PeakFinder Classes#
Base Class#
- class GlobalPeakLocator(padding_value=-1, compute_width=False, compute_area=False, threshold=0.5, max_number_of_peaks=1)[source]#
Bases:
BasePeakLocator
A peak detection utility that identifies the maximum value in each row of a 2D array.
Optionally, the peak detection can also compute the width and area of the peak.
The width is computed as the number of contiguous points around the maximum that remain above a specified fraction (threshold) of the maximum value. The area is the sum of the values over that same region.
- Parameters:
padding_value (object, optional) – Value used to pad the output if a row contains no data. Default is -1.
compute_width (bool, optional) – If True, the width of the peak (number of samples) is computed. Default is False.
compute_area (bool, optional) – If True, the area (sum of values) of the peak is computed. Default is False.
threshold (float, optional) – Fraction of the peak value used to determine the boundaries for width and area computation. For example, a threshold of 0.5 means the region above 50% of the maximum is considered. Default is 0.5.
max_number_of_peaks (int)
Examples
>>> import numpy as np >>> data = np.array([ ... [0, 800, 100, 2000, 150, 9000], ... [50, 1000, 200, 3000, 4000, 100] ... ]) >>> # Only compute peak indices: >>> peak_locator = BasicPeakLocator(padding_value=-1) >>> results = peak_locator(data) >>> print("Peak Indices:\n", results["peak_index"]) >>> print("Widths:\n", results["width"]) >>> print("Areas:\n", results["area"]) >>> # Compute peak index, width, and area: >>> peak_locator_metrics = BasicPeakLocator(padding_value=-1, compute_width=True, compute_area=True, threshold=0.5) >>> results = peak_locator_metrics(data) >>> print("Peak Indices:\n", results["peak_index"]) >>> print("Widths:\n", results["width"]) >>> print("Areas:\n", results["area"])
Moving-average Class#
- class SlidingWindowPeakLocator(window_size, max_number_of_peaks=5, padding_value=-1, window_step=-1, compute_width=False, compute_area=False, threshold=0.5)[source]#
Bases:
BasePeakLocator
- SlidingWindowPeakLocator(window_size, window_step=-1, max_number_of_peaks=5, padding_value=-1,
compute_width=False, compute_area=False, threshold=0.5)
A sliding-window-based peak detection utility for 1D signals. This class segments the input signal into fixed-size windows (which can be overlapping if window_step is less than window_size) and identifies the local maximum in each window. Optionally, it computes additional metrics for each peak: - Width: the number of contiguous samples above a specified fraction of the peak’s height. - Area: the sum of signal values under the peak within its window.
The results are returned as a dictionary containing fixed-length arrays. If fewer peaks are detected than max_number_of_peaks, the arrays are padded with padding_value (for indices) or NaN (for numeric values).
- Parameters:
window_size (int) – The size of the sliding window used for local peak detection.
window_step (int, optional) – The step size between consecutive windows. If not provided or set to -1, defaults to window_size (i.e., non-overlapping windows). To create overlapping windows, specify a value less than window_size.
max_number_of_peaks (int, optional) – The maximum number of peaks to report. If fewer peaks are detected, the results are padded. Default is 5.
padding_value (int, optional) – The value used to pad the output array for indices when fewer than max_number_of_peaks peaks are found. Default is -1.
compute_width (bool, optional) – If True, compute and return the width of each detected peak (in samples). Default is False.
compute_area (bool, optional) – If True, compute and return the area (sum of signal values) under each detected peak. Default is False.
threshold (float, optional) – The fraction of the peak’s height used to determine the boundaries for width and area calculations. For example, a threshold of 0.5 uses 50% of the peak height as the cutoff. Default is 0.5.
- Returns:
- A dictionary containing the following keys:
”Index”: A fixed-length array of detected peak indices, padded as necessary.
”Height”: A fixed-length array of the corresponding peak values.
”Width”: (optional) A fixed-length array of computed peak widths (if compute_width is True).
”Area”: (optional) A fixed-length array of computed peak areas (if compute_area is True).
- Return type:
dict
Examples
>>> import numpy as np >>> # Example signal >>> data = np.array([1, 3, 2, 5, 4, 2, 8, 1], dtype=float) >>> # Create a peak locator without additional metrics (non-overlapping windows). >>> locator = SlidingWindowPeakLocator(window_size=2, max_number_of_peaks=4, padding_value=-1) >>> result = locator(data) >>> print("Indices:", result["Index"]) [1, 3, 4, 6] >>> print("Heights:", result["Height"]) [3.0, 5.0, 4.0, 8.0] >>> # Create a peak locator with overlapping windows and with width and area computations. >>> locator_metrics = SlidingWindowPeakLocator(window_size=4, window_step=2, max_number_of_peaks=4, ... padding_value=-1, compute_width=True, compute_area=True, threshold=0.5) >>> result = locator_metrics(data) >>> print("Peak indices:", result["Index"]) >>> print("Heights:", result["Height"]) >>> print("Widths:", result["Width"]) >>> print("Areas:", result["Area"])
Derivative based Class#
- class ScipyPeakLocator(height=2000, distance=1, width=None, prominence=None, max_number_of_peaks=5, padding_value=-1, compute_width=False, compute_area=False)[source]#
Bases:
object
A peak detection utility that uses SciPy’s find_peaks to detect prominent peaks in each row of a 2D NumPy array. Optionally, it can also compute additional metrics such as the peak width and area.
The width is computed using SciPy’s peak_widths function (with rel_height=0.5 by default), and the area is computed as the sum of the signal over the interval defined by the left and right interpolated positions returned by peak_widths.
- Parameters:
height (float, optional) – Minimum height a peak must have to be detected. Default is 2000.
distance (int, optional) – Minimum horizontal distance between detected peaks. Default is 1.
width (float, optional) – Minimum width of detected peaks. If None, width is not explicitly constrained. Default is None.
prominence (float, optional) – Minimum prominence of detected peaks. If None, prominence is not explicitly constrained. Default is None.
max_number_of_peaks (int, optional) – Maximum number of peaks to return per row. If fewer peaks are detected, missing values are padded with the padding_value. Default is 5.
padding_value (object, optional) – Value used to pad missing peaks. Default is -1.
compute_width (bool, optional) – If True, computes the width (in number of samples) of each detected peak. Default is False.
compute_area (bool, optional) – If True, computes the area (sum over the peak region) for each detected peak. Default is False.