Skip to content

Python tool for aircraft Doppler audio geolocation using historical ADS-B flight data (Source-available)

perf(discovery): avoid numpy median allocations in path linking (#221)

Cinna Davis969180774589
diff --git a/docs/PERFORMANCE_AUDIO_DISCOVERY.md b/docs/PERFORMANCE_AUDIO_DISCOVERY.md
index ae70df7..5dff1b0 100644
--- a/docs/PERFORMANCE_AUDIO_DISCOVERY.md
+++ b/docs/PERFORMANCE_AUDIO_DISCOVERY.md
@@ -31,10 +31,22 @@ low-priority process competing with the national search. In that profile,
Intake took 218 s, including 204 s inside reads from FFmpeg's output pipe;
this run does not yet separate decoder CPU from wait time under host load.
-The immediate change makes per-event PNGs opt-in while retaining all ridge
+A separate sequential A/B then ran only `discover_events` over the same cached
+decoded Commons audio, with plots disabled. Replacing NumPy allocation/diff/
+median work for each short slope history with `statistics.median` reduced
+discovery from **111.456 s** to **94.616 s** (**1.18x**), kept all 222 ridge
+records, and preserved the exact JSON SHA-256 above. A smaller microbenchmark
+over 2,731 real ridge windows was also numerically identical and took 0.354 s
+with NumPy versus 0.058 s with the standard-library median. These are paired
+measurements on a loaded host, not an isolated steady-state benchmark; the
+CLI's decode-inclusive runtime was not remeasured after this change.
+
+The plot change makes per-event PNGs opt-in while retaining all ridge
measurements in JSON and CSV. The CLI also makes its summary plot opt-in. The
library keeps its existing summary-plot default for callers that use the image
-as an inspectable artifact. These changes do not reduce event recall.
+as an inspectable artifact. These changes do not reduce event recall. The
+short-history median change preserves the entire recorded discovery JSON on
+the hour-long real clip.
DOPPLERGEIST's application and detector control flow is Python. NumPy/SciPy
provide compiled array, FFT and peak-finding kernels; the repository has no
diff --git a/src/dopplergeist/discovery.py b/src/dopplergeist/discovery.py
index ee75eda..0905839 100644
--- a/src/dopplergeist/discovery.py
+++ b/src/dopplergeist/discovery.py
@@ -6,6 +6,7 @@ aircraft identity. No straight-pass speed/slant fit is required for acceptance.
from dataclasses import asdict, dataclass
import json
from pathlib import Path
+from statistics import median
import numpy as np
from scipy import ndimage, optimize, signal
@@ -92,8 +93,14 @@ def _paths(spec, settings):
speed = delta / dt[:, None]
slopes = []
for path in active:
- tail = np.asarray(path[-8:])
- slopes.append(float(np.median(np.diff(tail[:, 1]) / np.diff(tail[:, 0]))) if len(tail) >= 3 else 0.0)
+ tail = path[-8:]
+ if len(tail) >= 3:
+ slopes.append(float(median(
+ (right[1] - left[1]) / (right[0] - left[0])
+ for left, right in zip(tail, tail[1:])
+ )))
+ else:
+ slopes.append(0.0)
allowed = (speed >= -np.maximum(8, last[:, 1, None] * 0.08)) & (speed <= np.maximum(1.0, last[:, 1, None] * 0.008))
predicted_delta = np.asarray(slopes)[:, None] * dt[:, None]
costs = np.abs(delta - predicted_delta) / np.maximum(1.0, last[:, 1, None] * 0.01)