Skip to content

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

perf: reuse identical ridge fits within bounded scoring batches [skip ci]

Cinna Davis3c53538818af
diff --git a/src/aircraft_geoloc/matching.py b/src/aircraft_geoloc/matching.py
index 3f95898..a1c685b 100644
--- a/src/aircraft_geoloc/matching.py
+++ b/src/aircraft_geoloc/matching.py
@@ -3,7 +3,12 @@
Scores are penalized residuals, never probabilities. Geometry from a straight-pass
audio fit is a proposal diagnostic, not a reliable exclusion bound for real tracks.
"""
+from collections import OrderedDict
+from contextvars import ContextVar
+from copy import deepcopy
from dataclasses import dataclass
+from functools import wraps
+import hashlib
from itertools import chain
import numpy as np
@@ -18,6 +23,81 @@ class RidgeTimes:
time: np.ndarray
+_ridge_fit_cache = ContextVar("ridge_fit_cache", default=None)
+
+
+def _fit_argument_key(value):
+ """Preserve scalar precision and array shape without rounding fit inputs."""
+ if type(value) is float:
+ return (float, value.hex())
+ if type(value) in (int, bool):
+ return (type(value), value)
+ if type(value) in (list, tuple):
+ return (type(value), tuple(_fit_argument_key(item) for item in value))
+ if type(value) is np.ndarray or isinstance(value, np.generic):
+ array = np.asarray(value)
+ if array.dtype.kind in "biuf":
+ return (type(value), array.dtype.str, array.shape, array.tobytes())
+ raise TypeError("Unsupported fit-cache argument representation")
+
+
+class _RidgeFitCache:
+ """Bounded reuse for an immutable set of tracks within one scoring batch."""
+
+ def __init__(self, max_entries):
+ self.max_entries = max_entries
+ self.entries = OrderedDict()
+
+ def fit(self, track, observer, measurement, start_time, f0_bounds, c, sigma_hz, drift_fraction):
+ arguments = (track, observer, measurement, start_time, f0_bounds, c, sigma_hz, drift_fraction)
+ try:
+ parameters = tuple(_fit_argument_key(value) for value in
+ (observer, start_time, f0_bounds, c, sigma_hz, drift_fraction))
+ except (TypeError, ValueError):
+ # Let the fitter handle custom representations and their validation.
+ return _fit_ridge_uncached(*arguments)
+ # Measurements may be equivalent copies or modified arrays. Hash the
+ # numeric inputs actually used by the fit, without retaining their data.
+ spec, ridge, weight, _ = measurement
+ observed = hashlib.sha256()
+ for values in (spec.time, ridge, weight):
+ values = np.asarray(values, dtype=float)
+ observed.update(repr(values.shape).encode())
+ observed.update(values.tobytes())
+ key = (id(track), parameters, observed.digest())
+ if key in self.entries:
+ self.entries.move_to_end(key)
+ return deepcopy(self.entries[key][1])
+ result = _fit_ridge_uncached(*arguments)
+ # Keep the track alive so a different trajectory cannot reuse its id.
+ # The enclosing batch already owns these tracks. Exceptions are not cached.
+ self.entries[key] = (track, deepcopy(result))
+ if len(self.entries) > self.max_entries:
+ self.entries.popitem(last=False)
+ return result
+
+
+def _cache_ridge_fits(function, *, max_entries=4096):
+ """Reuse fits only while this batch's tracks stay unchanged.
+
+ Each call has at most 4,096 entries by default. Context-local state prevents
+ reuse between concurrent jobs, nested batches or later calls. Cached results
+ are copied so candidate diagnostics remain independently mutable.
+ """
+ if type(max_entries) is not int or max_entries < 1:
+ raise ValueError("Fit-cache capacity must be a positive integer")
+
+ @wraps(function)
+ def run(*args, **kwargs):
+ token = _ridge_fit_cache.set(_RidgeFitCache(max_entries))
+ try:
+ return function(*args, **kwargs)
+ finally:
+ _ridge_fit_cache.reset(token)
+
+ return run
+
+
def _frequency_scale(event):
quality = event.get("quality", {})
return quality.get("frequency_loss_scale_hz", quality.get("frequency_sigma_hz",
@@ -228,8 +308,8 @@ def prepare_events(detected_events, c=343.0, max_distance_m=20000.0):
return [settings[i] for i in order], [measurements[i] for i in order]
-def fit_ridge(track, observer, measurement, start_time, f0_bounds, c=343.0,
- sigma_hz=1.0, drift_fraction=0.02):
+def _fit_ridge_uncached(track, observer, measurement, start_time, f0_bounds, c=343.0,
+ sigma_hz=1.0, drift_fraction=0.02):
"""Bounded linear source drift at emission time, with robust residual loss.
Drift is bounded to a fraction of source frequency over the measured event.
@@ -276,6 +356,14 @@ def fit_ridge(track, observer, measurement, start_time, f0_bounds, c=343.0,
"outlier_fraction": float(np.average(np.abs(residual) > 3 * sigma_hz, weights=w))}
+def fit_ridge(track, observer, measurement, start_time, f0_bounds, c=343.0,
+ sigma_hz=1.0, drift_fraction=0.02):
+ """Fit bounded source drift, optionally reusing an identical batch input."""
+ cache = _ridge_fit_cache.get()
+ fit = _fit_ridge_uncached if cache is None else cache.fit
+ return fit(track, observer, measurement, start_time, f0_bounds, c, sigma_hz, drift_fraction)
+
+
def score_ridges(tracks, events, measurements, observer, recording_start_s, c=343.0):
"""Score all events at exactly one recording clock; report nuisance fits."""
if not events or len(tracks) != len(events) or len(measurements) != len(events):
diff --git a/src/aircraft_geoloc/tiled_search.py b/src/aircraft_geoloc/tiled_search.py
index 2d5ffef..e9e3dec 100644
--- a/src/aircraft_geoloc/tiled_search.py
+++ b/src/aircraft_geoloc/tiled_search.py
@@ -19,6 +19,7 @@ from pyproj import Transformer
from .data_access import CONUS, DataUnavailable, content_hash, sha256, write_json
from .finite_search import RetentionLedger, classified_assessment, identify_mode, mode_seed
+from .matching import _cache_ridge_fits
from .search import cpa_table, filter_event, pair_candidates, score_candidate
@@ -129,6 +130,7 @@ def _tracks(indexes, bounds, time_range, budget):
return tracks
+@_cache_ridge_fits
def _score(points, tracks, events, measurements, budget, parameters, filters, start_window=None, *, ledger=None, stage=None):
radius = max(e["slant_range_m"][1] for e in events)
tables, count = [], 0
diff --git a/tests/test_ridge_fit_cache.py b/tests/test_ridge_fit_cache.py
new file mode 100644
index 0000000..efa976b
--- /dev/null
+++ b/tests/test_ridge_fit_cache.py
@@ -0,0 +1,188 @@
+"""Behavioral controls for bounded, batch-local reuse of physical ridge fits."""
+from types import SimpleNamespace
+
+import numpy as np
+import pytest
+
+from aircraft_geoloc import matching
+from aircraft_geoloc.finite_search import RetentionLedger
+from aircraft_geoloc.geometry import Track, ecef
+from aircraft_geoloc.terrain import Terrain
+from aircraft_geoloc.tiled_search import SearchBudget, _score, tile_points
+from test_tiled_search import scenario
+
+
+def test_six_event_batch_reuses_fits_without_changing_modes_or_assessments(tmp_path, monkeypatch):
+ _, tracks, events, measurements, bounds, _, origin = scenario(
+ tmp_path, count=6, receiver_offset=(250, 250))
+ points = tile_points((1, 7601, 2, 7602), 250, bounds, Terrain(constant_m=0), "EPSG:5070")
+ budget = SearchBudget(top_k=5)
+ settings = {"mode": "robust_ridge", "time_origin": origin, "time_sweep_s": 0}
+ compute_name = "_fit_ridge_uncached" if hasattr(matching, "_fit_ridge_uncached") else "fit_ridge"
+ compute = getattr(matching, compute_name)
+ calls = 0
+
+ def counted(*args, **kwargs):
+ nonlocal calls
+ calls += 1
+ return compute(*args, **kwargs)
+
+ monkeypatch.setattr(matching, compute_name, counted)
+ results, evidence, computations = [], [], []
+ for name, score in [("reference", getattr(_score, "__wrapped__", _score)), ("cached", _score)]:
+ directory = tmp_path / name
+ directory.mkdir()
+ ledger = RetentionLedger(directory / "retention.sqlite", "cache-control",
+ "matched_group_support_minus_normalized_residual")
+ calls = 0
+ rows, counts = score(points, tracks, events, measurements, budget, settings, {},
+ ledger=ledger, stage="coarse:point")
+ results.append((rows, counts))
+ evidence.append(ledger.evidence(rows, complete=True, spacing_m=250, refine_spacing_m=250,
+ max_event_groups=6, expected_stages={"coarse": 1}))
+ computations.append(calls)
+ assert results[0] == results[1]
+ assert evidence[0] == evidence[1]
+ rows, counts = results[1]
+ assert counts["candidates"] == 63 and counts["unclassified_score_candidates"] == 0
+ assert len(rows) == 5 and len(rows[0]["event_indices"]) == 6
+ assert evidence[1]["complete"] and evidence[1]["evaluated_modes"] == 63
+ assert evidence[1]["discarded_count"] == 58
+ assert computations == [384, 6]
+
+
+def fit_inputs(tmp_path):
+ _, tracks, _, measurements, _, _, origin = scenario(tmp_path)
+ observer = ecef(tracks[0].lat[len(tracks[0].lat) // 2],
+ tracks[0].lon[len(tracks[0].lon) // 2], 0)
+ return tracks[0], observer, measurements[0], origin, (115., 125.)
+
+
+def test_fit_cache_keys_include_physical_inputs_and_measurement_contents(tmp_path, monkeypatch):
+ base = fit_inputs(tmp_path)
+ track, observer, measurement, start, frequency_bounds = base
+ compute = matching._fit_ridge_uncached
+ calls = 0
+
+ def counted(*args, **kwargs):
+ nonlocal calls
+ calls += 1
+ return compute(*args, **kwargs)
+
+ monkeypatch.setattr(matching, "_fit_ridge_uncached", counted)
+
+ @matching._cache_ridge_fits
+ def evaluate():
+ arguments = dict(track=track, observer=observer, measurement=measurement, start_time=start,
+ f0_bounds=frequency_bounds, c=343., sigma_hz=1., drift_fraction=.02)
+ assert matching.fit_ridge(**arguments) == compute(**arguments)
+ assert matching.fit_ridge(**arguments) == compute(**arguments)
+ assert calls == 1
+ alternate = Track(track.id, track.time, track.lat, track.lon + .001, track.altitude)
+ nearby = observer.copy()
+ nearby[0] = np.nextafter(nearby[0], np.inf)
+ variants = [("track", alternate), ("observer", nearby), ("start_time", start + .1),
+ ("f0_bounds", (116., 126.)), ("c", 342.), ("sigma_hz", 1.1), ("drift_fraction", .01)]
+ for field, value in variants:
+ changed = dict(arguments, **{field: value})
+ assert matching.fit_ridge(**changed) == compute(**changed)
+ # Reusing the same arrays after an in-place measurement edit must miss.
+ for field in range(3):
+ if field == 0:
+ measurement[0].time[0] += .001
+ else:
+ measurement[field][0] += .1
+ assert matching.fit_ridge(**arguments) == compute(**arguments)
+ assert calls == 1 + len(variants) + 3
+
+ evaluate()
+
+
+def test_cache_eviction_copies_results_and_releases_scope_on_error(tmp_path, monkeypatch):
+ arguments = fit_inputs(tmp_path)
+ compute = matching._fit_ridge_uncached
+ expected = compute(*arguments)
+ calls = 0
+
+ def counted(*args, **kwargs):
+ nonlocal calls
+ calls += 1
+ return compute(*args, **kwargs)
+
+ monkeypatch.setattr(matching, "_fit_ridge_uncached", counted)
+
+ def batch():
+ first = matching.fit_ridge(*arguments)
+ first["f0_hz"] = -999.
+ assert matching.fit_ridge(*arguments) == expected
+ for shift in (.1, .2):
+ changed = (*arguments[:3], arguments[3] + shift, arguments[4])
+ assert matching.fit_ridge(*changed) == compute(*changed)
+ assert len(matching._ridge_fit_cache.get().entries) == 2
+ assert matching.fit_ridge(*arguments) == expected
+ assert calls == 4
+ raise RuntimeError("end this batch")
+
+ cached = matching._cache_ridge_fits(batch, max_entries=2)
+ with pytest.raises(RuntimeError, match="end this batch"):
+ cached()
+ assert matching._ridge_fit_cache.get() is None
+ assert matching.fit_ridge(*arguments) == expected
+ assert calls == 5
+
+
+def test_typed_support_errors_are_never_replaced_with_cached_fit(tmp_path, monkeypatch):
+ from aircraft_geoloc.geometry import AcousticSupportError
+
+ arguments = fit_inputs(tmp_path)
+ original = matching._fit_ridge_uncached
+ calls = 0
+
+ def unavailable(*args, **kwargs):
+ nonlocal calls
+ calls += 1
+ raise AcousticSupportError(arguments[0].id, "test_unavailable_source")
+
+ monkeypatch.setattr(matching, "_fit_ridge_uncached", unavailable)
+
+ @matching._cache_ridge_fits
+ def batch():
+ for _ in range(2):
+ with pytest.raises(AcousticSupportError, match="test_unavailable_source"):
+ matching.fit_ridge(*arguments)
+ assert len(matching._ridge_fit_cache.get().entries) == 0
+
+ batch()
+ assert calls == 2
+ monkeypatch.setattr(matching, "_fit_ridge_uncached", original)
+ assert np.isfinite(matching.fit_ridge(*arguments)["score"])
+
+
+def test_invalid_scalar_measurement_does_not_reuse_a_one_sample_fit(tmp_path):
+ track, observer, _, start, bounds = fit_inputs(tmp_path)
+ one_sample = (SimpleNamespace(time=np.array([10.])), np.array([120.]), np.array([1.]), {})
+ scalar = (SimpleNamespace(time=np.array(10.)), np.array(120.), np.array(1.), {})
+
+ @matching._cache_ridge_fits
+ def batch():
+ assert matching.fit_ridge(track, observer, one_sample, start, bounds)["support_fraction"] == 0
+ with pytest.raises(ValueError, match="one-dimensional arrays"):
+ matching.fit_ridge(track, observer, scalar, start, bounds)
+
+ batch()
+
+
+def test_scalar_precision_is_preserved_in_the_fit_cache_key(tmp_path):
+ arguments = fit_inputs(tmp_path)
+ ridge = arguments[2][1]
+ ridge += .4 * (-1.) ** np.arange(len(ridge))
+ single = np.float32(1.234567)
+ double = float(single)
+ expected = [matching._fit_ridge_uncached(*arguments, sigma_hz=value) for value in (single, double)]
+ assert expected[0] != expected[1], "The fixture must expose scalar arithmetic precision"
+
+ @matching._cache_ridge_fits
+ def batch():
+ assert [matching.fit_ridge(*arguments, sigma_hz=value) for value in (single, double)] == expected
+
+ batch()