Skip to content

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

Bound work-limit bookkeeping and verify subset-driver integration (#7)

Cinna Davis7343106b0529
diff --git a/src/aircraft_geoloc/data_access.py b/src/aircraft_geoloc/data_access.py
index 06d1268..d6bb7d7 100644
--- a/src/aircraft_geoloc/data_access.py
+++ b/src/aircraft_geoloc/data_access.py
@@ -19,22 +19,15 @@ import numpy as np
import pandas as pd
import requests
-from .geometry import Track, velocity_ecef
-from .tracks import adsblol_frame
+from .geometry import Track
+from .tracks import _segment_velocity, adsblol_frame
CONUS = (-125.0, 24.0, -66.0, 50.0)
INDEX_VERSION = 2
def _frame_track(name, frame):
- from . import tracks as normalizer
- velocity, inferred = None, {}
- if hasattr(normalizer, "_segment_velocity"):
- velocity, inferred = normalizer._segment_velocity(frame)
- else:
- cols = ["east_mps", "north_mps", "up_mps"]
- if set(cols).issubset(frame) and np.isfinite(frame[cols].to_numpy(dtype=float)).all():
- velocity = velocity_ecef(frame.lat.to_numpy(), frame.lon.to_numpy(), *[frame[c].to_numpy() for c in cols])
+ velocity, inferred = _segment_velocity(frame)
track = Track(name, frame.time_s.to_numpy(), frame.lat.to_numpy(), frame.lon.to_numpy(), frame.alt_m.to_numpy(),
str(frame.typecode.iloc[0] or "") if "typecode" in frame else "", velocity)
track.altitude_basis = frame.altitude_basis.tolist() if "altitude_basis" in frame else ["unspecified"] * len(frame)
@@ -260,7 +253,7 @@ class FlightIndex:
invalid += int((~valid).sum())
clean = frame.loc[valid]
clean = clean.copy()
- clean["aircraft_class"] = clean.typecode.map(class_map).fillna("unclassified") if "typecode" in clean else "unclassified"
+ clean["aircraft_class"] = clean.typecode.map(class_map).astype(object).fillna("unclassified") if "typecode" in clean else "unclassified"
altitude.update(clean.altitude_basis.value_counts().to_dict() if "altitude_basis" in clean else {"unspecified": len(clean)})
# pandas normalizes numpy scalars/NaNs to JSON numbers/null.
records = json.loads(clean.to_json(orient="records", double_precision=15))
diff --git a/src/aircraft_geoloc/tiled_search.py b/src/aircraft_geoloc/tiled_search.py
index 68e5394..b64721b 100644
--- a/src/aircraft_geoloc/tiled_search.py
+++ b/src/aircraft_geoloc/tiled_search.py
@@ -63,12 +63,17 @@ def merge_candidates(rows, top_k):
return sorted(unique.values(), key=lambda row: (-row["score"], _key(row)))[:top_k]
-def grid_tiles(bounds, spacing_m, tile_cells, crs="EPSG:5070"):
- """Half-open integer-cell cores: each global receiver cell has one owner."""
+def _grid_extent(bounds, spacing_m, crs):
forward = Transformer.from_crs("EPSG:4326", crs, always_xy=True)
left, bottom, right, top = forward.transform_bounds(*bounds, densify_pts=101)
xmin, ymin = math.floor(left / spacing_m), math.floor(bottom / spacing_m)
xmax, ymax = math.ceil(right / spacing_m), math.ceil(top / spacing_m)
+ return xmin, ymin, xmax, ymax
+
+
+def grid_tiles(bounds, spacing_m, tile_cells, crs="EPSG:5070"):
+ """Half-open integer-cell cores: each global receiver cell has one owner."""
+ xmin, ymin, xmax, ymax = _grid_extent(bounds, spacing_m, crs)
for iy in range(ymin // tile_cells, ymax // tile_cells + 1):
for ix in range(xmin // tile_cells, xmax // tile_cells + 1):
yield f"{ix}_{iy}", (max(xmin, ix * tile_cells), max(ymin, iy * tile_cells),
@@ -221,7 +226,8 @@ def search_national(indexes, terrain, events, measurements, *, time_range_s, out
raise DataUnavailable("Checkpoint configuration/input/implementation mismatch; use a new output directory")
write_json(manifest, {"run_hash": run_hash, "configuration": identity})
began, ranked, failures, done, resumed, points_searched = time.monotonic(), [], [], 0, 0, 0
- total = sum(1 for _ in grid_tiles(bounds, budget.spacing_m, budget.tile_cells, crs))
+ xmin, ymin, xmax, ymax = _grid_extent(bounds, budget.spacing_m, crs)
+ total = (xmax // budget.tile_cells - xmin // budget.tile_cells + 1) * (ymax // budget.tile_cells - ymin // budget.tile_cells + 1)
radius = max(e["slant_range_m"][1] for e in events)
for number, (tile_id, cells) in enumerate(grid_tiles(bounds, budget.spacing_m, budget.tile_cells, crs)):
if budget.max_tiles is not None and number >= budget.max_tiles:
diff --git a/tests/test_tiled_search.py b/tests/test_tiled_search.py
index da70a90..68cbc0c 100644
--- a/tests/test_tiled_search.py
+++ b/tests/test_tiled_search.py
@@ -103,3 +103,32 @@ def test_ridge_only_adapter_and_exact_recording_clock(tmp_path):
scoring={"mode": "ridge", "recording_start_window_s": [origin, origin]})
assert result["ranked"]
assert all(row["recording_start_time_s"] == origin for row in result["ranked"])
+
+
+def test_work_limit_does_not_enumerate_unsearched_national_grid(tmp_path):
+ index, _, events, measurements, _, times, origin = scenario(tmp_path)
+ result = search_national([index], Terrain(constant_m=0), events, measurements, time_range_s=times,
+ output_dir=tmp_path / "huge-grid",
+ budget=SearchBudget(spacing_m=1, tile_cells=32, max_tiles=1, refine_spacing_m=1),
+ scoring={"mode": "ridge", "time_origin": origin, "time_sweep_s": 0})
+ assert result["status"] == "partial" and result["tiles_completed"] == 1
+ assert result["tiles_total"] > 1000000000
+
+
+def test_subset_matching_keeps_good_events_after_unmatched_proposal(tmp_path):
+ index, _, events, measurements, bounds, times, origin = scenario(tmp_path)
+ for i, event in enumerate(events):
+ event["event_id"] = f"supported-{i}"
+ spurious = dict(events[0], event_id="noise-proposal", start_s=20., end_s=40., cpa_receive_s=30.)
+ t = np.linspace(20, 40, 41)
+ measurements = [(SimpleNamespace(time=t), np.full(len(t), 120.), np.ones(len(t)), {})] + measurements
+ result = search_national([index], Terrain(constant_m=0), [spurious] + events, measurements,
+ time_range_s=times, output_dir=tmp_path / "subsets", bounds=bounds,
+ budget=SearchBudget(spacing_m=1000, tile_cells=4, refine_spacing_m=1000),
+ scoring={"mode": "robust_ridge", "recording_start_window_s": [origin, origin], "time_sweep_s": 0},
+ filters={"association": "subsets", "min_events": 2, "max_events": 2})
+ assert result["ranked"]
+ best = result["ranked"][0]
+ assert best["event_indices"] == [1, 2] and best["unmatched_event_ids"] == ["noise-proposal"]
+ assert best["recording_start_time_s"] == origin
+ assert best["x"] == 0 and best["y"] == 1900000