Skip to content

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

Reuse CPA schemas and track samples across bounded tile chunks (#248)

Cinna Davisc37128bb08f1
diff --git a/src/dopplergeist/search.py b/src/dopplergeist/search.py
index b1e5759..c4fad38 100644
--- a/src/dopplergeist/search.py
+++ b/src/dopplergeist/search.py
@@ -11,6 +11,11 @@ from scipy.spatial import cKDTree
from .geometry import AcousticSupportError, ecef
+# Most national track/receiver chunks are empty; construct their schema once.
+_EMPTY_CPA_TABLE = pd.DataFrame(columns=[
+ "point_id", "flight_id", "emission_time", "receive_time", "distance_m", "speed_mps"])
+
+
def cpa_table_dense(tracks, points, max_distance, c=343.0, step_s=2.0, chunk_size=256):
"""Vectorized piecewise-linear CPA prefilter; supports repeated local passes.
@@ -71,22 +76,31 @@ def cpa_table_dense(tracks, points, max_distance, c=343.0, step_s=2.0, chunk_siz
return pd.DataFrame(rows,columns=["point_id","flight_id","emission_time","receive_time","distance_m","speed_mps"])
-def cpa_table(tracks,points,max_distance,c=343.0,step_s=2.0,chunk_size=512):
+def cpa_table(tracks,points,max_distance,c=343.0,step_s=2.0,chunk_size=512, *, _sample_cache=None):
"""Spatial-radius CPA graph, retaining every local pass without dense tensors.
The radius includes two complete sample steps beyond the audible envelope,
so both neighbours of an in-range sampled minimum are present. Each minimum
is refined on its two actual adjacent segments. The dense reference above
provides an independent regression oracle for this search optimization.
+ The private sample cache lives only across one track's bounded tile chunks.
"""
observers=ecef(points.lat.to_numpy(),points.lon.to_numpy(),points.elev_m.to_numpy())
point_ids=points.point_id.to_numpy(dtype=int)
rows=[]
for track in tracks:
track.validate_motion_support(c)
- t=np.unique(np.r_[np.arange(track.time[0],track.time[-1],step_s),track.time[-1]])
- pos=track.position(t)
- max_step=np.max(np.linalg.norm(np.diff(pos,axis=0),axis=1))
+ key = (id(track), step_s)
+ sampled = None if _sample_cache is None else _sample_cache.get(key)
+ if sampled is None:
+ t=np.unique(np.r_[np.arange(track.time[0],track.time[-1],step_s),track.time[-1]])
+ pos=track.position(t)
+ max_step=np.max(np.linalg.norm(np.diff(pos,axis=0),axis=1))
+ if _sample_cache is not None:
+ # Retain the track too, so its object ID cannot be reused.
+ _sample_cache[key] = (track, t, pos, max_step)
+ else:
+ _, t, pos, max_step = sampled
near=np.flatnonzero(np.all((observers>=pos.min(axis=0)-max_distance)&
(observers<=pos.max(axis=0)+max_distance),axis=1))
if not len(near):continue
@@ -161,6 +175,8 @@ def cpa_table(tracks,points,max_distance,c=343.0,step_s=2.0,chunk_size=512):
rows.append({"point_id":int(point_ids[chosen[oi[j]]]),"flight_id":track.id,
"emission_time":float(when[j]),"receive_time":float(when[j]+distance[j]/c),
"distance_m":float(distance[j]),"speed_mps":float(speed[j])})
+ if not rows:
+ return _EMPTY_CPA_TABLE.copy()
result=pd.DataFrame(rows,columns=["point_id","flight_id","emission_time","receive_time","distance_m","speed_mps"])
if not result.empty:
# A CPA exactly at a sample can be proposed twice under roundoff.
diff --git a/src/dopplergeist/tiled_search.py b/src/dopplergeist/tiled_search.py
index ef508e1..bd88f91 100644
--- a/src/dopplergeist/tiled_search.py
+++ b/src/dopplergeist/tiled_search.py
@@ -391,11 +391,14 @@ def _score(points, tracks, events, measurements, budget, parameters, filters, st
# of minima before we inspect the returned table.
chunk_points = max(1, budget.max_cpa_rows // samples)
track_tables, track_rows = [], 0
+ sample_cache = {}
try:
if track.outside_observer_radius(observers, radius, parameters.get("c", 343.0)):
continue
for first in range(0, len(points), chunk_points):
- table = cpa_table([track], points.iloc[first:first + chunk_points], radius, parameters.get("c", 343.0), budget.cpa_step_s)
+ table = cpa_table([track], points.iloc[first:first + chunk_points], radius,
+ parameters.get("c", 343.0), budget.cpa_step_s,
+ _sample_cache=sample_cache)
track_rows += len(table)
if count + track_rows > budget.max_cpa_rows:
raise DataUnavailable("Tile CPA-row memory budget exceeded")
diff --git a/tests/test_real_cpa_spatial_sieve.py b/tests/test_real_cpa_spatial_sieve.py
index 9d8bc87..0a054ed 100644
--- a/tests/test_real_cpa_spatial_sieve.py
+++ b/tests/test_real_cpa_spatial_sieve.py
@@ -66,15 +66,28 @@ def test_real_multi_point_sieve_does_not_discard_cpas():
for track in tracks:
try:
outside = track.outside_observer_radius(observers, 20000)
+ table = cpa_table([track], points, 20000)
except AcousticSupportError:
unsupported += 1
with pytest.raises(AcousticSupportError):
cpa_table([track], points, 20000)
continue
- table = cpa_table([track], points, 20000)
if outside:
excluded += 1
assert table.empty
+ if excluded == 1:
+ expected = table.copy()
+ table.drop(columns=["flight_id"], inplace=True)
+ repeated = cpa_table([track], points, 20000)
+ pd.testing.assert_frame_equal(repeated, expected)
else:
+ if retained_rows == 0 and len(table):
+ cache = {}
+ step = max(1, len(points) // 2)
+ chunks = [cpa_table([track], points.iloc[i:i + step], 20000,
+ _sample_cache=cache) for i in range(0, len(points), step)]
+ repeated = pd.concat(chunks, ignore_index=True).sort_values(
+ ["point_id", "flight_id", "emission_time"]).reset_index(drop=True)
+ pd.testing.assert_frame_equal(repeated, table)
retained_rows += len(table)
assert excluded > 0 and retained_rows > 0 and unsupported > 0