Skip to content

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

Support bounded association anchor partitions (#134)

Cinna Davise0ecd02f10ca
diff --git a/src/dopplergeist/matching.py b/src/dopplergeist/matching.py
index 0a6fce3..f0d1a9c 100644
--- a/src/dopplergeist/matching.py
+++ b/src/dopplergeist/matching.py
@@ -149,18 +149,24 @@ def predict_fitted_ridge(track, observer, receive_times_s, diagnostics, c=343.0)
def association_candidates(table, events, offset_tolerance_s=5.0, time_origin=None,
time_tolerance_s=5.0, silence=None, min_events=1,
- max_events=None, max_candidates=20000):
+ max_events=None, max_candidates=20000, anchor_window=None):
"""Bounded subset hypotheses, retaining explicitly unmatched proposals.
Default to every independent group. Intersect shared-clock intervals before
extending a branch; do not materialize impossible Cartesian products. Index
entries, branch visits and stored assignments share an explicit work budget.
Exceeding it fails visibly so the outer driver reports incomplete coverage.
+ An optional half-open anchor window selects the first matched passage's
+ implied recording start without clipping the shared-clock fit interval.
"""
from .search import filter_association_event
max_events = max(min_events, len(events)) if max_events is None else max_events
if not 1 <= min_events <= max_events or max_candidates < 1:
raise ValueError("Invalid association event/candidate budget")
+ if anchor_window is not None and (len(anchor_window) != 2 or
+ np.isnan(anchor_window[0]) or np.isnan(anchor_window[1]) or
+ anchor_window[0] >= anchor_window[1]):
+ raise ValueError("Anchor window must be an increasing half-open interval")
candidates, examined, work = [], 0, 0
def reserve(units):
nonlocal work
@@ -211,7 +217,8 @@ def association_candidates(table, events, offset_tolerance_s=5.0, time_origin=No
continue
lower, upper = (-np.inf, np.inf) if time_origin is None else (time_origin-time_tolerance_s, time_origin+time_tolerance_s)
path, used = [], set()
- frames = [(0, iter(indexed_options(available[0], point_id, lower, upper)), lower, upper, 0)]
+ option_lo, option_hi = anchor_window if anchor_window is not None else (lower, upper)
+ frames = [(0, iter(indexed_options(available[0], point_id, option_lo, option_hi)), lower, upper, 0)]
while frames:
cursor, options, lower, upper, depth = frames[-1]
while len(path) > depth:
@@ -230,6 +237,8 @@ def association_candidates(table, events, offset_tolerance_s=5.0, time_origin=No
if identity in used:
continue
start = row["receive_time"]-events[index]["cpa_receive_s"]
+ if not path and anchor_window is not None and not anchor_window[0] <= start < anchor_window[1]:
+ continue
tolerance = events[index].get("quality", {}).get("cpa_tolerance_s", 0)+offset_tolerance_s/2
lo, hi = max(lo, start-tolerance), min(hi, start+tolerance)
if lo > hi:
@@ -240,7 +249,8 @@ def association_candidates(table, events, offset_tolerance_s=5.0, time_origin=No
if len(path)+len(available)-following < min_events:
continue
if following < len(available) and len(path) < max_events:
- frames.append((following, iter(indexed_options(available[following], point_id, lo, hi)), lo, hi, len(path)))
+ option_lo, option_hi = anchor_window if not path and anchor_window is not None else (lo, hi)
+ frames.append((following, iter(indexed_options(available[following], point_id, option_lo, option_hi)), lo, hi, len(path)))
continue
if len(path) < min_events:
continue
diff --git a/tests/test_real_association_budget.py b/tests/test_real_association_budget.py
index 72cf6ba..1dc2273 100644
--- a/tests/test_real_association_budget.py
+++ b/tests/test_real_association_budget.py
@@ -1,8 +1,10 @@
"""Regression from actual ADS-B CPAs and automatically proposed real-audio groups."""
import json
+import math
from pathlib import Path
import pandas as pd
+import pytest
from dopplergeist.matching import association_candidates
@@ -40,3 +42,39 @@ def test_real_silence_filter_remains_within_work_budget():
silence={'edge_margin_s': 60})
assert len(candidates) == 1590
assert counts['work_units'] <= 400000
+
+
[email protected]('silence', [None, {'edge_margin_s': 60}])
+def test_real_anchor_partitions_preserve_candidates_and_clock_intervals(silence):
+ fixture = json.loads((Path(__file__).parent / 'data' / 'reference-cpa-real.json').read_text())
+ events = fixture['events']
+ table = pd.DataFrame(fixture['cpa_rows'])
+ kwargs = dict(offset_tolerance_s=5, max_candidates=20000, silence=silence)
+ baseline, _ = association_candidates(table, events, **kwargs)
+
+ def anchor(candidate):
+ return candidate['passes'][0]['receive_time'] - events[candidate['event_indices'][0]]['cpa_receive_s']
+
+ def records(candidates):
+ return sorted(json.dumps(candidate, sort_keys=True) for candidate in candidates)
+
+ anchors = sorted({anchor(candidate) for candidate in baseline})
+ pivot = anchors[len(anchors) // 2]
+ expected = records(baseline)
+ for boundary in (math.nextafter(pivot, -math.inf), pivot, math.nextafter(pivot, math.inf)):
+ left, _ = association_candidates(table, events, anchor_window=(-math.inf, boundary), **kwargs)
+ right, _ = association_candidates(table, events, anchor_window=(boundary, math.inf), **kwargs)
+ assert all(anchor(candidate) < boundary for candidate in left)
+ assert all(anchor(candidate) >= boundary for candidate in right)
+ assert records(left + right) == expected
+
+ clock = table.iloc[0].receive_time - events[0]['cpa_receive_s']
+ fixed = dict(kwargs, time_origin=clock, time_tolerance_s=3)
+ baseline, _ = association_candidates(table, events, **fixed)
+ left, _ = association_candidates(table, events, anchor_window=(-math.inf, clock), **fixed)
+ right, _ = association_candidates(table, events, anchor_window=(clock, math.inf), **fixed)
+ assert baseline and records(left + right) == records(baseline)
+
+ for invalid in ((1, 1), (1, 0), (math.nan, 1)):
+ with pytest.raises(ValueError, match='Anchor window'):
+ association_candidates(table, events, anchor_window=invalid, **kwargs)