Python tool for aircraft Doppler audio geolocation using historical ADS-B flight data (Source-available)
Python acoustic-geolocationads-baircraft-trackingairplaneaudio-forensicsdoppler-effectdoppler-shiftgeolocationosintsignal-processing
Add independent methodology replay receipts and failure checks (#13)
Cinna Davis472a668df897
diff --git a/docs/verification/CLEAN_REPLAY.md b/docs/verification/CLEAN_REPLAY.mdnew file mode 100644index 0000000..1a74207--- /dev/null+++ b/docs/verification/CLEAN_REPLAY.md@@ -0,0 +1,53 @@+# Independent clean-environment replay++This lane checks the existing methodology from a new clone and isolated virtual+environment. It supports DG-12 / issue #13. The automatic upload acceptance runner+is a separate coordinated lane. Successful replays do not complete automatic+discovery or independent national real-audio validation.++The runner requires Python 3.11+, ffmpeg, the installed project, and restored+inputs. Follow `docs/ARTIFACTS.md` for release acquisition and checksum verification.+Generate the independent synthetic cases before running them:++```bash+python3 -m venv .venv+.venv/bin/pip install -e . pytest+.venv/bin/python -m pytest -q+.venv/bin/python scripts/make_synthetic_cases.py+.venv/bin/python scripts/verify_pipeline_replay.py \+ --case synthetic-two-events --case synthetic-known-minute \+ --output results/clean-synthetic-replay+.venv/bin/python scripts/verify_pipeline_replay.py \+ --case reference-local --case reference-faint \+ --output results/clean-real-regional-replay+```++Use new output paths. Existing directories are refused. With no `--case`, the+runner attempts all four cases; failed/missing cases are recorded and later cases+still run. Missing fixtures are failures, not skips. A timeout terminates the+whole subprocess group and preserves the partial log/output.++Each case runs the real research CLI as a separate subprocess. The runner records+all declared input hashes, including terrain, geoid, type database and supplied+ridge files; implementation hashes; Git revision and working-tree status;+dependency/Python/ffmpeg versions; exact command; subprocess log; elapsed time; and+peak RSS/CPU use when GNU `time` is available. Evaluation references are read only+after the search exits and are separately hashed. This is process separation,+not a filesystem isolation claim.++`baseline_replays_passed` means that the winning flight IDs, position (within 2 m),+score (within 0.0001) and shared recording clock (within 0.05 s) reproduce the frozen+case. `within_100_m` is reported separately. In particular, reproducing the faint+case's approximately 357 m error passes its regression comparison while retaining+its localization failure. `automatic_upload_goal_verified` remains false for all+these manually configured replays.++The two synthetic cases use the independent emission-domain renderer and separate+truth JSON. The real cases retain the source-informed regional search bounds,+event windows and frequency bands. Their comparison coordinate is a public+platform listing with unquantified physical uncertainty, and the faint case's+recording clock comes from the two-event match.++Machine-readable run receipts and measured results will be added here after the+clean replays complete. Large audio/flight arrays and spectral outputs stay in+ignored `data/`, `results/` and checksummed release assets.diff --git a/scripts/verify_pipeline_replay.py b/scripts/verify_pipeline_replay.pynew file mode 100644index 0000000..6c3a71f--- /dev/null+++ b/scripts/verify_pipeline_replay.py@@ -0,0 +1,217 @@+"""Replay frozen methodology cases and write independent verification receipts.++This checks the manually configured research pipeline. It deliberately does not+assert that automatic event discovery, upload, or national validation is complete.+Truth/reference files are opened only after the search subprocess has exited.+"""+import argparse+from datetime import datetime, timezone+import hashlib+import importlib.metadata+import json+import os+from pathlib import Path+import platform+import shutil+import signal+import subprocess+import sys+import time+++ROOT = Path(__file__).resolve().parents[1]+CASES = {+ "synthetic-two-events": {+ "config": "data/synthetic/two-events.json",+ "truth": "data/synthetic/truth.json",+ "baseline": "evidence/results/synthetic-two-events/report.json",+ "scope": "synthetic; manual event windows/bands; three candidate regions",+ },+ "synthetic-known-minute": {+ "config": "data/synthetic/known-time-minute.json",+ "truth": "data/synthetic/truth.json",+ "baseline": "evidence/results/synthetic-known-minute/report.json",+ "scope": "synthetic; manual event window/band; known recording start",+ },+ "reference-local": {+ "config": "data/source/reproduce-local.json",+ "baseline": "evidence/results/reference-local-fast/report.json",+ "scope": "real; source-informed event windows/bands and regional bounds",+ },+ "reference-faint": {+ "config": "data/source/reproduce-faint-alltypes-local.json",+ "baseline": "evidence/results/reference-faint-alltypes-local/report.json",+ "scope": "real; manual faint event window/band; regional; clock from two-event result",+ },+}+++def dump(path, value):+ Path(path).write_text(json.dumps(value, indent=2, allow_nan=False) + "\n")+++def digest(path):+ path = Path(path)+ with path.open("rb") as handle:+ sha256 = hashlib.file_digest(handle, "sha256").hexdigest()+ return {"path": str(path.resolve()), "bytes": path.stat().st_size, "sha256": sha256}+++def input_files(config_path):+ """Include terrain, type data and supplied ridge files omitted by old reports."""+ config_path = Path(config_path).resolve()+ config = json.loads(config_path.read_text())+ paths = [config_path, config_path.parent / config["audio"]]+ paths.extend(config_path.parent / name for name in config["tracks"])+ if config.get("type_database"):+ paths.append(config_path.parent / config["type_database"])+ terrain = config["terrain"]+ paths.extend(config_path.parent / name for name in terrain.get("rasters", []))+ if terrain.get("geoid_raster"):+ paths.append(config_path.parent / terrain["geoid_raster"])+ if terrain.get("copernicus_cache"):+ paths.extend(sorted((config_path.parent / terrain["copernicus_cache"]).rglob("*.tif")))+ paths.extend(config_path.parent / event["ridge_csv"] for event in config["events"]+ if event.get("ridge_csv"))+ return sorted({path.resolve() for path in paths})+++def distance_m(first, second):+ from pyproj import Geod+ return float(Geod(ellps="WGS84").inv(first["lon"], first["lat"],+ second["lon"], second["lat"])[2])+++def evaluate(name, report):+ """Evaluate only a completed run; no truth is passed to the child command."""+ case = CASES[name]+ baseline_path = ROOT / case["baseline"]+ baseline = json.loads(baseline_path.read_text())["best"]+ best = report["best"]+ displacement = distance_m(best, baseline)+ result = {+ "evaluation_inputs": [digest(baseline_path)],+ "baseline_displacement_m": displacement,+ "baseline_flight_ids_match": best["flight_ids"] == baseline["flight_ids"],+ "baseline_score_delta": best["score"] - baseline["score"],+ "baseline_clock_delta_s": best["recording_start_time_s"] - baseline["recording_start_time_s"],+ }+ if case.get("truth"):+ truth_path = ROOT / case["truth"]+ truth = json.loads(truth_path.read_text())+ result["evaluation_inputs"].append(digest(truth_path))+ result["position_error_m"] = distance_m(best, truth)+ result["clock_error_s"] = best["recording_start_time_s"] - truth["recording_start_time_s"]+ expected_flights = truth["flight_ids"][:len(best["flight_ids"])]+ result["flight_ids_correct"] = best["flight_ids"] == expected_flights+ result["within_100_m"] = result["position_error_m"] <= 100+ result["reference_kind"] = "independent synthetic renderer truth"+ else:+ # PROGRESS.md explicitly labels this as a public-platform coordinate,+ # not surveyed ground truth or an independent recording-clock source.+ result["reference_coordinate"] = {"lat": 40.8766, "lon": -73.5247}+ result["position_error_m"] = distance_m(best, result["reference_coordinate"])+ result["within_100_m"] = result["position_error_m"] <= 100+ result["reference_kind"] = "published public-platform coordinate; physical uncertainty unquantified"+ result["baseline_reproduced"] = bool(+ displacement <= 2 and result["baseline_flight_ids_match"]+ and abs(result["baseline_score_delta"]) <= 1e-4+ and abs(result["baseline_clock_delta_s"]) <= 0.05)+ return result+++def execute(command, output, timeout_s):+ """Preserve logs and kill the whole subprocess group on timeout."""+ metrics = output / "resource-usage.txt"+ timer = shutil.which("time")+ wrapped = ([timer, "-f", "max_rss_kib=%M\nuser_cpu_s=%U\nsystem_cpu_s=%S", "-o", str(metrics), "--"]+ if timer else []) + command+ environment = dict(os.environ, MPLBACKEND="Agg", OPENBLAS_NUM_THREADS="1", OMP_NUM_THREADS="1")+ began = time.monotonic()+ timed_out = False+ with (output / "pipeline.log").open("wb") as log:+ child = subprocess.Popen(wrapped, stdout=log, stderr=subprocess.STDOUT,+ cwd=ROOT, env=environment, start_new_session=True)+ try:+ child.wait(timeout=timeout_s)+ except subprocess.TimeoutExpired:+ timed_out = True+ os.killpg(child.pid, signal.SIGTERM)+ try:+ child.wait(timeout=5)+ except subprocess.TimeoutExpired:+ os.killpg(child.pid, signal.SIGKILL)+ child.wait()+ usage = {}+ if metrics.exists():+ for line in metrics.read_text().splitlines():+ if "=" in line:+ key, value = line.split("=", 1)+ usage[key] = float(value)+ return {"command": command, "exit_code": child.returncode, "timed_out": timed_out,+ "elapsed_s": time.monotonic() - began, "resource_usage": usage}+++def verify_case(name, output, timeout_s):+ output.mkdir()+ receipt = {"case": name, "scope": CASES[name]["scope"], "status": "failed"}+ try:+ config = ROOT / CASES[name]["config"]+ receipt["search_inputs"] = [digest(path) for path in input_files(config)]+ command = [sys.executable, "-m", "aircraft_geoloc.cli", "run", str(config),+ "--output", str(output / "pipeline")]+ receipt.update(execute(command, output, timeout_s))+ if receipt["exit_code"] == 0:+ report_path = output / "pipeline/report.json"+ report = json.loads(report_path.read_text())+ receipt["report"] = digest(report_path)+ receipt["evaluation"] = evaluate(name, report)+ receipt["status"] = "passed" if receipt["evaluation"]["baseline_reproduced"] else "baseline_changed"+ except Exception as exc:+ # A missing fixture or failed case must not suppress later cases.+ receipt["error"] = f"{type(exc).__name__}: {exc}"+ dump(output / "verification.json", receipt)+ return receipt+++def runtime():+ def git(*args):+ return subprocess.check_output(["git", *args], cwd=ROOT, text=True).strip()+ sources = [digest(path) for path in sorted((ROOT / "src").rglob("*.py"))]+ return {"git_commit": git("rev-parse", "HEAD"), "git_status": git("status", "--short"),+ "python": sys.version, "platform": platform.platform(),+ "packages": {name: importlib.metadata.version(name) for name in+ ["numpy", "scipy", "pandas", "soundfile", "pyproj", "rasterio", "matplotlib", "requests", "pyarrow"]},+ "ffmpeg": subprocess.check_output(["ffmpeg", "-version"], text=True).splitlines()[0],+ "sources": sources, "runner": digest(__file__)}+++def main(argv=None):+ parser = argparse.ArgumentParser(description=__doc__)+ parser.add_argument("--case", action="append", choices=CASES, dest="cases")+ parser.add_argument("--output", type=Path, required=True, help="New directory; existing paths are refused")+ parser.add_argument("--timeout-s", type=float, default=1800)+ args = parser.parse_args(argv)+ if args.timeout_s <= 0:+ parser.error("--timeout-s must be positive")+ output = args.output.resolve()+ output.mkdir(parents=True, exist_ok=False)+ summary = {"schema_version": 1, "started_utc": datetime.now(timezone.utc).isoformat(),+ "runtime": runtime(), "cases": [], "automatic_upload_goal_verified": False,+ "limitations": ["Manual event settings remain search inputs in these replays.",+ "Synthetic and regional reference replays do not establish national real-audio accuracy.",+ "The faint reference case is an expected localization failure at the 100 m target.",+ "Evaluation inputs are read after search; filesystem sandboxing is not asserted."]}+ for name in dict.fromkeys(args.cases or CASES):+ receipt = verify_case(name, output / name, args.timeout_s)+ summary["cases"].append(receipt)+ dump(output / "verification.json", summary)+ print(json.dumps({"case": name, "status": receipt["status"],+ "evaluation": receipt.get("evaluation"), "error": receipt.get("error")}), flush=True)+ summary["baseline_replays_passed"] = all(row["status"] == "passed" for row in summary["cases"])+ dump(output / "verification.json", summary)+ return 0 if summary["baseline_replays_passed"] else 1+++if __name__ == "__main__":+ raise SystemExit(main())diff --git a/tests/test_pipeline_replay.py b/tests/test_pipeline_replay.pynew file mode 100644index 0000000..a945549--- /dev/null+++ b/tests/test_pipeline_replay.py@@ -0,0 +1,67 @@+"""Failure and provenance boundaries for the independent replay runner."""+import importlib.util+import json+from pathlib import Path+import sys++import pytest+++spec = importlib.util.spec_from_file_location(+ "verify_pipeline_replay", Path(__file__).resolve().parents[1] / "scripts/verify_pipeline_replay.py")+replay = importlib.util.module_from_spec(spec)+spec.loader.exec_module(replay)+++def test_real_child_failure_keeps_output_and_exit_status(tmp_path):+ result = replay.execute([sys.executable, "-c", "print('failure evidence'); raise SystemExit(7)"],+ tmp_path, 10)+ assert result["exit_code"] == 7+ assert result["timed_out"] is False+ assert "failure evidence" in (tmp_path / "pipeline.log").read_text()+++def test_real_child_timeout_cannot_be_reported_as_success(tmp_path):+ result = replay.execute([sys.executable, "-u", "-c",+ "import time; print('started'); time.sleep(60)"], tmp_path, 0.25)+ assert result["timed_out"] is True+ assert result["exit_code"] != 0+ assert "started" in (tmp_path / "pipeline.log").read_text()+ assert result["elapsed_s"] < 10+++def test_missing_case_is_recorded_and_later_cases_still_run(tmp_path, monkeypatch):+ monkeypatch.setattr(replay, "ROOT", tmp_path)+ monkeypatch.setattr(replay, "runtime", lambda: {})+ output = tmp_path / "receipts"+ status = replay.main(["--case", "synthetic-two-events", "--case", "reference-local",+ "--output", str(output)])+ summary = json.loads((output / "verification.json").read_text())+ assert status == 1+ assert len(summary["cases"]) == 2+ assert all(case["status"] == "failed" for case in summary["cases"])+ assert summary["automatic_upload_goal_verified"] is False+ with pytest.raises(FileExistsError):+ replay.main(["--output", str(output)])+++def test_evaluation_failure_preserves_successful_child_receipt(tmp_path, monkeypatch):+ config_dir = tmp_path / "data/synthetic"+ config_dir.mkdir(parents=True)+ config = config_dir / "two-events.json"+ config.write_text(json.dumps({"audio": "recording.wav", "tracks": [], "terrain": {}, "events": []}))+ (config_dir / "recording.wav").write_bytes(b"source fixture")+ monkeypatch.setattr(replay, "ROOT", tmp_path)++ def completed(command, output, timeout_s):+ report = output / "pipeline/report.json"+ report.parent.mkdir()+ report.write_text('{"best":{}}')+ return {"exit_code": 0, "command": command, "timed_out": False}++ monkeypatch.setattr(replay, "execute", completed)+ result = replay.verify_case("synthetic-two-events", tmp_path / "case", 10)+ assert result["exit_code"] == 0+ assert result["status"] == "failed"+ assert result["report"]["sha256"] == replay.digest(tmp_path / "case/pipeline/report.json")["sha256"]+ assert "FileNotFoundError" in result["error"]