Python tool for aircraft Doppler audio geolocation using historical ADS-B flight data (Source-available)
Python acoustic-geolocationads-baircraft-trackingairplaneaudio-forensicsdoppler-effectdoppler-shiftgeolocationosintsignal-processing
Include supplied acoustics in ridge fit cache keys
Cinna Davisf0c25f5a6db8
diff --git a/src/dopplergeist/matching.py b/src/dopplergeist/matching.pyindex 42ec288..65f4e31 100644--- a/src/dopplergeist/matching.py+++ b/src/dopplergeist/matching.py@@ -84,6 +84,16 @@ class _RidgeFitCache:try:parameters = tuple(_fit_argument_key(value) for value in(observer, start_time, f0_bounds, c, sigma_hz, drift_fraction))+ if acoustic_result is None:+ acoustic_key = None+ elif (type(acoustic_result) in (tuple, list) and len(acoustic_result) == 2+ and all(type(value) is np.ndarray and value.dtype.kind in "biuf" for value in acoustic_result)):+ acoustic_key = (type(acoustic_result), tuple(+ (value.dtype.str, value.shape, hashlib.sha256(value.tobytes()).digest())+ for value in acoustic_result))+ else:+ # Preserve validation and custom input behavior in the fitter.+ return _fit_ridge_uncached(*arguments, acoustic_result=acoustic_result)except (TypeError, ValueError):# Let the fitter handle custom representations and their validation.return _fit_ridge_uncached(*arguments, acoustic_result=acoustic_result)@@ -95,7 +105,7 @@ class _RidgeFitCache:values = np.asarray(values, dtype=float)observed.update(repr(values.shape).encode())observed.update(values.tobytes())- key = (id(track), parameters, observed.digest())+ key = (id(track), parameters, observed.digest(), acoustic_key)if key in self.entries:self.entries.move_to_end(key)return deepcopy(self.entries[key][1])diff --git a/tests/test_real_acoustic_batch_cache.py b/tests/test_real_acoustic_batch_cache.pyindex 3a39d58..ac8391d 100644--- a/tests/test_real_acoustic_batch_cache.py+++ b/tests/test_real_acoustic_batch_cache.py@@ -10,7 +10,8 @@ import pytestfrom dopplergeist.data_access import FlightIndexfrom dopplergeist.geometry import eceffrom dopplergeist.matching import (_cache_ridge_fits, _ridge_fit_cache,- batch_acoustic_trials, prepare_events)+ _fit_ridge_uncached, _frequency_scale, batch_acoustic_trials,+ fit_ridge, prepare_events)def test_real_acoustic_batches_reuse_exact_inputs_and_preserve_fallback(monkeypatch):@@ -81,3 +82,27 @@ def test_real_acoustic_batches_reuse_exact_inputs_and_preserve_fallback(monkeypabefore = callsevicted()assert calls == before + 3++ fit_calls = 0+ def counted_fit(*args, **kwargs):+ nonlocal fit_calls+ fit_calls += 1+ return _fit_ridge_uncached(*args, **kwargs)+ monkeypatch.setattr("dopplergeist.matching._fit_ridge_uncached", counted_fit)++ @_cache_ridge_fits+ def supplied_acoustics_are_part_of_fit_input():+ factors, emissions = (part[0].copy() for part in baseline)+ arguments = (track, observer, measurement, starts[0], event["f0_range_hz"],+ 343., _frequency_scale(event), event.get("quality", {}).get("source_drift_fraction", .02))+ original_fit = fit_ridge(*arguments, acoustic_result=(factors, emissions))+ assert fit_ridge(*arguments, acoustic_result=(factors.copy(), emissions.copy())) == original_fit+ assert fit_calls == 1+ factors[0] *= 1.01 # Fault-inject a changed precomputed input on real observations.+ expected = _fit_ridge_uncached(*arguments, acoustic_result=(factors, emissions))+ assert expected != original_fit+ assert fit_ridge(*arguments, acoustic_result=(factors, emissions)) == expected+ assert fit_calls == 2+ with pytest.raises(ValueError, match="does not match"):+ fit_ridge(*arguments, acoustic_result=(factors[:-1], emissions))+ supplied_acoustics_are_part_of_fit_input()