Deterministic Peak Detection for Multi-Dimensional Data
Examples for finding peaks in 2D matrices.
A peak in a 2D matrix is an element that is greater than or equal to all its neighbors (up, down, left, right). Edge elements are peaks if they are greater than or equal to their available neighbors.
from peak_locator import PeakDetector
import numpy as np
matrix = np.array([
[1, 2, 3],
[4, 9, 5],
[6, 7, 8]
])
detector = PeakDetector(matrix)
row, col = detector.find_peak_2d()
print(f"Peak at row {row}, column {col}")
print(f"Value: {matrix[row, col]}")
# Output: Peak at row 1, column 1
# Value: 9
For 2D data, find_any_peak() returns a tuple:
matrix = np.array([
[1, 2, 3],
[4, 9, 5],
[6, 7, 8]
])
detector = PeakDetector(matrix)
peak = detector.find_any_peak()
row, col = peak
print(f"Peak at ({row}, {col})")
Find the pixel with maximum intensity:
import numpy as np
from peak_locator import PeakDetector
# Simulate an image (grayscale, 0-255)
image = np.random.rand(100, 100) * 255
image[50, 50] = 255 # Add a bright spot
detector = PeakDetector(image)
row, col = detector.find_peak_2d()
print(f"Brightest pixel at ({row}, {col})")
print(f"Intensity: {image[row, col]}")
Find the highest point in elevation data:
# Simulate elevation data
elevation = np.array([
[100, 150, 200, 180],
[120, 250, 300, 190],
[110, 140, 280, 170],
[105, 130, 160, 175]
])
detector = PeakDetector(elevation)
row, col = detector.find_peak_2d()
print(f"Highest point at ({row}, {col})")
print(f"Elevation: {elevation[row, col]} meters")
Find the hottest region:
# Temperature data
temperatures = np.array([
[20, 22, 25, 23],
[21, 28, 30, 24],
[19, 26, 29, 22],
[18, 20, 23, 21]
])
detector = PeakDetector(temperatures)
row, col = detector.find_peak_2d()
print(f"Hottest region at ({row}, {col})")
print(f"Temperature: {temperatures[row, col]}°C")
Visualize 2D peaks (requires peakfinder[viz]):
from peak_locator import PeakDetector
from peak_locator.visualization import plot_2d_peak
import numpy as np
matrix = np.array([
[1, 2, 3],
[4, 9, 5],
[6, 7, 8]
])
detector = PeakDetector(matrix)
peak = detector.find_peak_2d()
plot_2d_peak(matrix, peak=peak)
matrix = np.array([[5]])
detector = PeakDetector(matrix)
row, col = detector.find_peak_2d()
print(f"Peak at ({row}, {col})") # Output: Peak at (0, 0)
matrix = np.array([[1, 5, 3, 2, 4]])
detector = PeakDetector(matrix)
row, col = detector.find_peak_2d()
print(f"Peak at row {row}, column {col}")
matrix = np.array([[1], [5], [3], [2], [4]])
detector = PeakDetector(matrix)
row, col = detector.find_peak_2d()
print(f"Peak at row {row}, column {col}")
matrix = np.array([[5, 5, 5], [5, 5, 5], [5, 5, 5]])
detector = PeakDetector(matrix)
row, col = detector.find_peak_2d()
print(f"Peak at ({row}, {col})") # Any position is valid
The 2D peak detection uses a divide-and-conquer approach:
Time Complexity: O(n log m) for n×m matrix
Space Complexity: O(log m) for recursion stack