Skip to content

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

[skip ci] Connect bounded spectral comparison to real automatic jobs (#10)

Cinna Davisf4d17485b8e0
diff --git a/docs/PRODUCT.md b/docs/PRODUCT.md
index 64975f8..7149ed1 100644
--- a/docs/PRODUCT.md
+++ b/docs/PRODUCT.md
@@ -140,6 +140,24 @@ the operator configuration hash is retained in provenance. This configuration is
not an upload-request field. A chunk-size comparison must rerun detection and the
pipeline; it cannot reuse final candidates as if discovery had run again.
+An operator can add `"spectral_comparison": {}` to save a separate
+`spectral-comparison.json` artifact after refinement. This optional stage reads
+the actual decoded recording and named native flight segments. It compares
+constant source frequencies with one shared clock, using the reference band of
+each prepared flyover group. The existing ridge decision and its uncertainty
+remain unchanged. Spectral scores only compare supplied modes with identical
+event sets; mixed sets have separate `rankings_by_event_set` lists and an empty
+top-level `ranked` list. They do not establish a national optimum or location
+acceptance, and ridge retention bounds do not apply to these scores.
+
+The spectral configuration accepts work limits and grid settings, including
+`max_modes`, `max_spectrogram_bins`, `max_event_clock_trials`,
+`max_curve_samples`, `time_sweep_s`, `time_step_s`, and `frequency_step_hz`.
+Track loading uses `search_budget.max_tracks_per_tile` and
+`search_budget.max_track_samples_per_tile` as total limits for this comparison.
+Missing trajectories, ambiguous segment IDs and exhausted budgets remain
+explicit in the artifact. These controls are private operator settings.
+
Search reports distinguish raw model support from finite assessment. If any
trajectory or scoring envelope lacks support, `model_support_complete` stays false.
`model_support` retains the raw work counts, unknown trials, and reasons. Missing
diff --git a/src/dopplergeist/automatic.py b/src/dopplergeist/automatic.py
index e52bdf2..8e4ecd1 100644
--- a/src/dopplergeist/automatic.py
+++ b/src/dopplergeist/automatic.py
@@ -87,7 +87,9 @@ def analyze(request, output_dir, config=None, progress=lambda *args, **kwargs: N
artifacts = {"provenance": "provenance.json", "request": "request.json"}
for name, path in (("intake", "intake/intake.json"), ("recording", "intake/recording.json"),
("discovery", "discovery/discovery.json"), ("discovery_plot", "discovery/discovery.png"),
- ("search_report", "search/search-report.json"), ("refinement", "refined-candidates.json"), ("data_provenance", "data-provenance.json"), ("decision", "decision.json")):
+ ("search_report", "search/search-report.json"), ("refinement", "refined-candidates.json"),
+ ("spectral_comparison", "spectral-comparison.json"),
+ ("data_provenance", "data-provenance.json"), ("decision", "decision.json")):
if (output / path).is_file():
artifacts[name] = path
for path in sorted((output / "discovery").glob("event-*.png")):
@@ -109,6 +111,13 @@ def analyze(request, output_dir, config=None, progress=lambda *args, **kwargs: N
discovery_settings = DiscoverySettings(**discovery_options)
except (TypeError, ValueError) as exc:
return finish("failed", "invalid_configuration", f"Invalid operator discovery settings: {exc}")
+ spectral_options = config.get("spectral_comparison")
+ spectral_keys = {"time_sweep_s", "time_step_s", "frequency_step_hz", "expansion_s",
+ "window_s", "hop_s", "max_window_s", "max_modes", "max_event_clock_trials",
+ "max_curve_samples", "max_spectrogram_bins"}
+ if spectral_options is not None and (not isinstance(spectral_options, dict)
+ or spectral_options.keys() - spectral_keys):
+ return finish("failed", "invalid_configuration", "Spectral comparison accepts only work limits and spectral grid settings.")
if not resume:
progress("intake", 0.05)
try:
@@ -142,7 +151,8 @@ def analyze(request, output_dir, config=None, progress=lambda *args, **kwargs: N
state["coverage"] = _coverage(request, search, config)
provenance["search_run_hash"] = search.get("run_hash")
progress("refinement", 0.85, **state)
- return _decide(search, events, prepared_events, measurements, indexes, terrain, state, output, config, finish, lambda: progress("decision", 0.95, **state))
+ return _decide(search, events, prepared_events, measurements, indexes, terrain, state, output, config, finish,
+ lambda: progress("decision", 0.95, **state), recording=recording)
class MissingData(ValueError):
@@ -375,7 +385,54 @@ def _comparable_refinement(candidate, original, prepared_events):
return candidate
-def _decide(search, events, prepared_events, measurements, indexes, terrain, state, output, config, finish, decision_progress):
+def _compare_retained_spectra(recording, rows, prepared_events, indexes, config, start_window):
+ """Run the optional physical comparison without transferring ridge evidence."""
+ from .data_access import DataUnavailable
+ from .spectral_matching import SCORE_KIND, compare_spectral_modes
+ options = dict(config["spectral_comparison"])
+ lookup = {"requested_tracks": 0, "loaded_tracks": 0, "loaded_samples": 0,
+ "missing_ids": [], "ambiguous_ids": [], "failures": []}
+ try:
+ if recording is None:
+ raise ValueError("The decoded recording is unavailable for spectral comparison")
+ tracks, ambiguous = {}, set()
+ if len(rows) <= options.get("max_modes", 1000):
+ needed = {identifier for row in rows for identifier in row["flight_ids"]}
+ lookup["requested_tracks"] = len(needed)
+ budget = config.get("search_budget", {})
+ max_tracks = budget.get("max_tracks_per_tile", 2000)
+ max_samples = budget.get("max_track_samples_per_tile", 500000)
+ lookup.update(max_tracks=max_tracks, max_samples=max_samples)
+ for index in indexes:
+ remaining = max_samples-lookup["loaded_samples"]
+ if remaining <= 0:
+ lookup["failures"].append("Flight lookup sample budget exhausted")
+ break
+ try:
+ for track in index.lookup(needed, max_tracks=max_tracks, max_samples=remaining):
+ lookup["loaded_samples"] += len(track.time)
+ if track.id in tracks or track.id in ambiguous:
+ ambiguous.add(track.id)
+ tracks.pop(track.id, None)
+ else:
+ tracks[track.id] = track
+ except DataUnavailable as exc:
+ lookup["failures"].append(str(exc))
+ lookup.update(loaded_tracks=len(tracks), missing_ids=sorted(needed-tracks.keys()),
+ ambiguous_ids=sorted(ambiguous))
+ report = compare_spectral_modes(recording, rows, tracks, prepared_events,
+ recording_start_window_s=start_window, c=config.get("sound_speed_mps", 343.0), **options)
+ if lookup["failures"]:
+ report.update(status="inconclusive", all_modes_compared=False)
+ except (ValueError, TypeError, KeyError, OSError, DataUnavailable) as exc:
+ report = {"status": "inconclusive", "location_decision": "not_evaluated",
+ "score_kind": SCORE_KIND, "all_modes_compared": False,
+ "ranked": [], "rankings_by_event_set": [], "reason": str(exc)}
+ report["track_lookup"] = lookup
+ return report
+
+
+def _decide(search, events, prepared_events, measurements, indexes, terrain, state, output, config, finish, decision_progress, *, recording=None):
# The confidence producer supplies this adapter; missing decision machinery
# can never turn the highest raw score into a supported location.
from .confidence import decide_location
@@ -509,6 +566,9 @@ def _decide(search, events, prepared_events, measurements, indexes, terrain, sta
decision_progress()
decision = decide_location(events, candidates, state["coverage"], matching_diagnostics=diagnostics)
decision.setdefault("candidates", candidates)
+ if config.get("spectral_comparison") is not None:
+ spectral = _compare_retained_spectra(recording, rows, prepared_events, indexes, config, start_window)
+ write_json(output / "spectral-comparison.json", spectral)
write_json(output / "decision.json", decision)
fields = {key: value for key, value in decision.items() if key not in {"schema_version", "job_id", "status", "stage", "progress", "artifacts", "recording", "events", "diagnostics"}}
diff --git a/src/dopplergeist/data_access.py b/src/dopplergeist/data_access.py
index 7eac208..799dc47 100644
--- a/src/dopplergeist/data_access.py
+++ b/src/dopplergeist/data_access.py
@@ -393,6 +393,39 @@ class FlightIndex:
frame = pd.DataFrame(json.loads(gzip.decompress(payload)))
yield _frame_track(name, frame)
+ def lookup(self, identifiers, *, max_tracks=2000, max_samples=500000):
+ """Yield named whole segments without scanning unrelated trajectories.
+
+ Missing names are omitted; callers must compare requested and returned
+ IDs. Limits reject excess work explicitly, never truncate a trajectory.
+ This lookup establishes no spatial, temporal or receiver completeness.
+ """
+ if any(type(value) is not int or value <= 0 for value in (max_tracks, max_samples)):
+ raise ValueError("Flight lookup limits must be positive integers")
+ names = set()
+ for name in identifiers:
+ if not isinstance(name, str) or not name:
+ raise ValueError("Flight lookup requires nonempty string identifiers")
+ names.add(name)
+ if len(names) > max_tracks:
+ raise DataUnavailable("Flight lookup identifier budget exceeded")
+ names, samples = sorted(names), 0
+ with self.connect() as db:
+ # Stay below SQLite's conservative bound on bound SQL parameters.
+ for first in range(0, len(names), 500):
+ batch = names[first:first+500]
+ placeholders = ",".join("?" for _ in batch)
+ sql = ("SELECT name,payload,payload_hash FROM segments WHERE name IN ("
+ + placeholders + ") ORDER BY name")
+ for name, payload, expected in db.execute(sql, batch):
+ if hashlib.sha256(payload).hexdigest() != expected:
+ raise DataUnavailable(f"Corrupt flight payload: {name}")
+ frame = pd.DataFrame(json.loads(gzip.decompress(payload)))
+ samples += len(frame)
+ if samples > max_samples:
+ raise DataUnavailable("Flight lookup sample budget exceeded")
+ yield _frame_track(name, frame)
+
def coverage_for(self, bounds, time_range):
source = self.report["coverage"]
box, times = source["bounds"], source["time_range_s"]