Skip to content

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

5.2 KB
Raw Blame
"""Compare cached and uncached scoring with saved real-recording inputs.

This checks numerical equivalence and runtime at an existing candidate. Its
position and inferred clock are comparison settings, not independent truth.
No audio, trajectories or measurements are generated by this command.
Scorers share track objects; elapsed times can include other warm-cache effects.
Fit-computation counts specifically measure this optimization.
"""
import argparse
import json
from pathlib import Path
import time

import pandas as pd

from dopplergeist import matching
from dopplergeist.finite_search import RetentionLedger
from dopplergeist.tiled_search import SearchBudget, _score
from dopplergeist.tracks import load_tracks


def compact_counts(counts):
    summaries = {"unsupported_cpa_tracks": "unsupported_cpa_track_count",
                 "unclassified_candidate_assessments": "unclassified_assessment_count"}
    return {summaries.get(key, key): len(value) if key in summaries else value
            for key, value in counts.items()}


def retention_rows(ledger):
    # Compare all finite modes and pruning choices without asserting coverage
    # or inventing a lattice spacing for this single-candidate comparison.
    with ledger.connect() as db:
        records = db.execute("SELECT * FROM records ORDER BY stage,mode_id").fetchall()
        stages = db.execute("SELECT name,occurrences,duplicates,kept FROM stages ORDER BY name").fetchall()
    return records, stages


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--discovery", required=True, type=Path)
    parser.add_argument("--tracks", required=True, type=Path)
    parser.add_argument("--candidates", required=True, type=Path)
    parser.add_argument("--candidate-rank", type=int, default=1)
    parser.add_argument("--recording-duration-s", required=True, type=float)
    parser.add_argument("--time-padding-s", type=float, default=120.)
    parser.add_argument("--output", required=True, type=Path)
    args = parser.parse_args()
    if args.candidate_rank < 1 or args.recording_duration_s <= 0 or args.time_padding_s < 0:
        parser.error("Need a positive candidate rank/duration and nonnegative time padding")

    args.output.mkdir(parents=True, exist_ok=False)
    proposals = json.loads(args.discovery.read_text())["events"]
    candidate = json.loads(args.candidates.read_text())[args.candidate_rank - 1]
    clock = candidate["recording_start_time_s"]
    tracks, metadata = load_tracks(args.tracks, time_range=(
        clock - args.time_padding_s, clock + args.recording_duration_s + args.time_padding_s))
    events, measurements = matching.prepare_events(proposals)
    points = pd.DataFrame([{key: candidate[key] for key in
                           ("point_id", "lat", "lon", "elev_m", "x", "y")}])
    settings = {"mode": "robust_ridge", "time_origin": clock, "time_sweep_s": 0}
    summary = {"scope": "fixed-input numerical/performance comparison; no accuracy claim",
               "inputs": {name: str(getattr(args, name)) for name in
                          ("discovery", "tracks", "candidates")},
               "candidate_rank": args.candidate_rank, "recording_duration_s": args.recording_duration_s,
               "time_padding_s": args.time_padding_s, "segments": len(tracks),
               "observations": metadata["rows"], "proposals": len(proposals),
               "groups": len(events), "runs": []}
    compute = matching._fit_ridge_uncached
    calls = 0

    def counted(*values, **options):
        nonlocal calls
        calls += 1
        return compute(*values, **options)

    results = []
    matching._fit_ridge_uncached = counted
    try:
        for name, score in (("uncached", _score.__wrapped__), ("cached", _score)):
            directory = args.output / name
            directory.mkdir()
            ledger = RetentionLedger(directory / "retention.sqlite", "real-fit-reuse",
                                     "matched_group_support_minus_normalized_residual")
            calls = 0
            started = time.perf_counter()
            rows, counts = score(points, tracks, events, measurements, SearchBudget(top_k=5),
                                 settings, {}, ledger=ledger, stage="coarse:reference-point")
            elapsed = time.perf_counter() - started
            retained = retention_rows(ledger)
            results.append((rows, counts, retained))
            result = {"mode": name, "elapsed_s": elapsed, "fit_computations": calls,
                      "counts": compact_counts(counts), "kept": len(rows),
                      "finite_modes": len(retained[0]),
                      "leader_groups": len(rows[0]["event_indices"]) if rows else 0}
            summary["runs"].append(result)
            print(json.dumps(result), flush=True)
    finally:
        matching._fit_ridge_uncached = compute

    summary["identical_results"] = results[0] == results[1]
    (args.output / "comparison.json").write_text(json.dumps(summary, indent=2) + "\n")
    if not summary["identical_results"]:
        raise RuntimeError("Cache changed returned rows, diagnostics, counts or retention evidence")
    print("Exact match: returned candidates, diagnostics, assessment counts and retention evidence")


if __name__ == "__main__":
    main()