Deterministic Peak Detection for Multi-Dimensional Data
A robust Python toolkit for deterministic peak detection in one-dimensional, two-dimensional, and higher-dimensional numerical data.
Peak Locator provides a collection of peak detection algorithms for numerical data represented as arrays or matrices. It supports multiple algorithmic approaches for one-dimensional and two-dimensional inputs, with a unified interface for higher-dimensional data. The library offers automatic algorithm selection based on input characteristics, while allowing explicit configuration when a specific method or performance profile is required.
pip install peak-locator
For visualization support:
pip install peak-locator[viz]
git clone https://github.com/yourusername/peak-locator.git
cd peak-locator
# Install dependencies
pip install -r requirements.txt
# Install in editable mode with development tools
pip install -e ".[dev]"
from peak_locator import PeakDetector
import numpy as np
# 1D peak detection
arr = np.array([1, 3, 2, 5, 4])
detector = PeakDetector(arr)
peak_idx = detector.find_any_peak()
print(f"Peak found at index: {peak_idx}")
# 2D peak detection
matrix = np.array([[1, 2, 3], [4, 9, 5], [6, 7, 8]])
detector = PeakDetector(matrix)
row, col = detector.find_peak_2d()
print(f"Peak found at ({row}, {col})")
arr = np.array([1, 5, 2, 6, 3])
detector = PeakDetector(arr)
all_peaks = detector.find_all_peaks()
print(f"Found {len(all_peaks)} peaks at indices: {all_peaks}")
arr = np.array([1, 5, 2, 6, 3, 4, 2])
detector = PeakDetector(arr)
count = detector.count_peaks()
print(f"Total peaks: {count}")
mode="brute"): Linear scan, O(n) time complexitymode="binary"): O(log n) time complexity, best for arrays without duplicatesmode="hybrid"): Compresses duplicates then uses binary search, handles duplicates robustlymode="auto"): Automatically selects the best algorithm based on data propertiesPeak Locator is designed with the following principles:
Peak Locator performs strict input validation and raises explicit exceptions for:
PeakDetector instances are safe for concurrent read-only use| Algorithm | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Brute Force | O(n) | O(1) | Small arrays, simple cases |
| Binary Search | O(log n) | O(1) | Large arrays without duplicates |
| Hybrid | O(n) worst, O(log n) best | O(n) worst, O(1) best | Arrays with duplicates |
Main interface for peak detection.
detector = PeakDetector(
data, # array-like: Input data
allow_duplicates=True, # bool: Handle duplicate values
mode="auto" # str: Algorithm mode
)
find_any_peak(): Find any peak in the datafind_all_peaks(): Find all peaks (1D only)count_peaks(use_segment_tree=False): Count peaks (1D only)find_peak_2d(): Find peak in 2D datafind_peak_nd(): Find peak in N-dimensional datadata: Underlying numpy arrayshape: Shape of the datandim: Number of dimensionsimport numpy as np
from peak_locator import PeakDetector
# Simulate a signal with noise
signal = np.sin(np.linspace(0, 4*np.pi, 100)) + np.random.normal(0, 0.1, 100)
detector = PeakDetector(signal)
peaks = detector.find_all_peaks()
print(f"Found {len(peaks)} peaks in signal")
import numpy as np
from peak_locator import PeakDetector
# Find peak intensity in an image
image = np.random.rand(100, 100) * 255
detector = PeakDetector(image)
row, col = detector.find_peak_2d()
print(f"Peak intensity at pixel ({row}, {col})")
import numpy as np
from peak_locator import PeakDetector
from peak_locator.visualization import plot_1d_peaks
arr = np.array([1, 5, 2, 6, 3, 4, 2])
detector = PeakDetector(arr)
peaks = detector.find_all_peaks()
plot_1d_peaks(arr, peaks=peaks, show_all=True)
Comprehensive documentation is available in the docs/ directory:
Run the test suite:
pytest
With coverage:
pytest --cov=peak_locator --cov-report=html
Contributions are welcome! Please follow these guidelines:
git clone https://github.com/yourusername/peak-locator.git
cd peak-locator
pip install -r requirements.txt
pip install -e ".[dev]"
This project uses:
black for code formattingruff for lintingmypy for type checkingpytest for testingThe project uses automated GitHub Actions workflows for:
MIT License - see LICENSE file for details.
All notable changes are documented in CHANGELOG.md.
This project adheres to Keep a Changelog.