Skip to content

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

Reduce repeated acoustic-screen preparation and validate native fit evidence

Cinna Davis448ef64ee006
diff --git a/.github/workflows/real-performance.yml b/.github/workflows/real-performance.yml
index 9893f56..64ee681 100644
--- a/.github/workflows/real-performance.yml
+++ b/.github/workflows/real-performance.yml
@@ -57,6 +57,7 @@ jobs:
python - <<'PY'
from dopplergeist import matching, native_acoustic
assert callable(matching._native_fit_irls), "Native ridge extension did not build"
+ assert callable(getattr(matching._native_ridge_module, "event_fit_reason", None)), "Native evidence validator did not build"
assert matching.RIDGE_FIT_BACKEND.startswith("native-c-")
assert native_acoustic._native is not None, "Native acoustic extension did not build"
assert native_acoustic.ACOUSTIC_BACKEND.startswith("native-c-")
@@ -93,7 +94,7 @@ jobs:
DOPPLERGEIST_REAL_CPA_FIXTURE: benchmark-inputs/point-120.json
DOPPLERGEIST_REAL_REFERENCE_ROOT: benchmark-inputs/reference-checkout
DOPPLERGEIST_REAL_REFERENCE_DISCOVERY: evidence/audio/w1-sensitivity-20261102/reference/discovery.json
- run: python -m pytest -q tests/test_real_prepared_measurements.py tests/test_real_geometry_kinematics.py tests/test_real_batch_scalar_parity.py tests/test_real_motion_support_batch.py tests/test_real_single_pass_sieve.py tests/test_real_refinement_lookup.py tests/test_real_cpa_preparation.py tests/test_real_native_ridge.py tests/test_real_ridge_backend.py tests/test_real_legacy_batch_cache.py tests/test_real_native_acoustic.py tests/test_real_native_acoustic_pipeline.py tests/test_real_batch_ceilings.py tests/test_real_acoustic_screen_cache.py tests/test_real_cpa_spatial_sieve.py
+ run: python -m pytest -q tests/test_real_prepared_measurements.py tests/test_real_geometry_kinematics.py tests/test_real_batch_scalar_parity.py tests/test_real_motion_support_batch.py tests/test_real_single_pass_sieve.py tests/test_real_refinement_lookup.py tests/test_real_cpa_preparation.py tests/test_real_native_ridge.py tests/test_real_native_ridge_long.py tests/test_real_native_validation.py tests/test_real_ridge_backend.py tests/test_real_legacy_batch_cache.py tests/test_real_native_acoustic.py tests/test_real_native_acoustic_pipeline.py tests/test_real_batch_ceilings.py tests/test_real_acoustic_screen_cache.py tests/test_real_cpa_spatial_sieve.py
- name: Verify the explicit Python fallback on recorded arithmetic
env:
DOPPLERGEIST_REAL_CPA_FIXTURE: benchmark-inputs/point-120.json
@@ -183,6 +184,7 @@ jobs:
python - <<'PY'
from dopplergeist import matching, native_acoustic
assert callable(matching._native_fit_irls), "Native ridge extension did not build"
+ assert callable(getattr(matching._native_ridge_module, "event_fit_reason", None)), "Native evidence validator did not build"
assert matching.RIDGE_FIT_BACKEND.startswith("native-c-")
assert native_acoustic._native is not None, "Native acoustic extension did not build"
assert native_acoustic.ACOUSTIC_BACKEND.startswith("native-c-")
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 3a0d11e..9757aa8 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -39,19 +39,27 @@ jobs:
run: python -m compileall -q src scripts
- name: CLI smoke check
run: dopplergeist --help
- - name: Check installed modules and packaged browser assets
+ - name: Check installed native validator and packaged browser assets
run: |
python -I - <<'PY'
from importlib import resources
+ import json
from pathlib import Path
import dopplergeist
- from dopplergeist import automatic, jobs, service
+ from dopplergeist import _native_ridge, automatic, jobs, service
package = Path(dopplergeist.__file__).resolve().parent
assert "site-packages" in package.parts, package
+ assert Path(_native_ridge.__file__).resolve().parent == package
+ assert callable(getattr(_native_ridge, "event_fit_reason", None)), "Native evidence validator did not build"
+ fixture = json.loads(Path("tests/data/real-ridge-batch-arithmetic.json").read_text())
+ fits = fixture["legacy_diagnostics"]
+ assert _native_ridge.event_fit_reason(
+ fits, [fit["score"] for fit in fits], [fit["f0_hz"] for fit in fits],
+ [fixture["event"]] * len(fits)) == 0
web = resources.files("dopplergeist.web")
for name in ("index.html", "app.js", "style.css"):
assert web.joinpath(name).is_file(), name
assert web.joinpath(name).read_bytes(), name
- print("Installed package imports and browser assets passed; no audio or tracks generated.")
+ print("Installed native validation of recorded fits and browser assets passed; no audio or tracks generated.")
PY
diff --git a/scripts/benchmark_prepared_measurements.py b/scripts/benchmark_prepared_measurements.py
index cd7b44e..424ba03 100644
--- a/scripts/benchmark_prepared_measurements.py
+++ b/scripts/benchmark_prepared_measurements.py
@@ -97,7 +97,9 @@ def prepare(args):
if not tables:
raise ValueError("The real-data point has no supported CPA rows")
fixture = {
- "scope": "Recorded-input computation comparison; source-informed receiver/clock, no accuracy claim",
+ "scope": ("Recorded-input computation comparison; source-informed receiver, no recording-clock prior or accuracy claim"
+ if args.full_day else
+ "Recorded-input computation comparison; source-informed receiver/clock, no accuracy claim"),
"discovery_source": str(args.discovery.resolve()),
"reference_report": str(args.reference_report.resolve()),
"real_index": str(args.index.resolve()), "index_input_hash": index.report["input_hash"],
@@ -113,6 +115,9 @@ def prepare(args):
"ridge_samples": sum(len(measurement[1]) for measurement in measurements),
"cpa_rows": row_count, "unsupported_cpa_tracks": len(unsupported)},
}
+ if args.anchor_window_s is not None:
+ fixture["anchor_window_s"] = args.anchor_window_s
+ fixture["scope"] += ". One association-anchor partition. Physical fit intervals remain unchanged."
write_new(args.output, fixture)
print(json.dumps({"fixture": str(args.output), **fixture["counts"],
"scope": fixture["scope"]}), flush=True)
@@ -131,16 +136,16 @@ def worker(args):
if Path(dopplergeist.__file__).resolve().parent != source / "dopplergeist":
raise RuntimeError("Comparison imported a different source tree")
native_fit = getattr(matching, "_native_fit_irls", None)
- backend = getattr(matching, "RIDGE_FIT_BACKEND", "python-irls-v1")
+ ridge_backend = getattr(matching, "RIDGE_FIT_BACKEND", "python-irls-v1")
acoustic_backend = getattr(geometry, "ACOUSTIC_BACKEND", "python-acoustic-v1")
try:
from dopplergeist.native_acoustic import _native as native_acoustic
except ImportError:
native_acoustic = None
if args.require_native and not callable(native_fit):
- raise RuntimeError("The source-tree native fitter is unavailable; build it in place before timing")
+ raise RuntimeError("The selected source's native ridge fitter is unavailable")
if args.require_native_acoustic and native_acoustic is None:
- raise RuntimeError("The source-tree native acoustic solver is unavailable; build it in place before timing")
+ raise RuntimeError("The selected source's native acoustic solver is unavailable")
fixture = json.loads(args.fixture.read_text())
index = FlightIndex(args.index or fixture["real_index"])
if index.report["input_hash"] != fixture["index_input_hash"]:
@@ -164,6 +169,41 @@ def worker(args):
all_rows = []
original_fit, original_acoustic = matching._fit_ridge_uncached, Track.acoustic
original_score = tiled_search.score_candidate
+ phase_seconds, phase_patches = {}, []
+
+ def timed_phase(name, function):
+ def run(*values, **options):
+ began, cpu = time.perf_counter(), time.process_time()
+ try:
+ return function(*values, **options)
+ finally:
+ phase = phase_seconds.setdefault(name, {"wall_seconds": 0.0, "cpu_seconds": 0.0, "calls": 0})
+ phase["wall_seconds"] += time.perf_counter() - began
+ phase["cpu_seconds"] += time.process_time() - cpu
+ phase["calls"] += 1
+ return run
+
+ if args.stage_timing:
+ phase_targets = [
+ (Track, "outside_observer_radius", "distance_sieve"),
+ (matching, "association_candidates", "association_enumeration"),
+ (tiled_search, "merge_candidates", "retention"),
+ ]
+ if callable(getattr(tiled_search, "cpa_table", None)):
+ phase_targets.append((tiled_search, "cpa_table", "closest_approach"))
+ else:
+ prepared_cpa = getattr(tiled_search, "_PreparedCPATrack", None)
+ if prepared_cpa is not None:
+ phase_targets.extend([
+ (prepared_cpa, "__init__", "closest_approach_preparation"),
+ (prepared_cpa, "raw_rows", "closest_approach"),
+ ])
+ for owner, attribute, name in phase_targets:
+ function = getattr(owner, attribute, None)
+ if function is not None:
+ phase_patches.append((owner, attribute, function))
+ setattr(owner, attribute, timed_phase(name, function))
+ score_function = timed_phase("candidate_scoring", original_score) if args.stage_timing else original_score
def counted_fit(*values, **options):
operations["scalar_fit_calls"] += 1
@@ -183,7 +223,7 @@ def worker(args):
def captured_score(*values, **options):
operations["scored_candidates"] += 1
try:
- result = original_score(*values, **options)
+ result = score_function(*values, **options)
except AcousticSupportError as error:
all_rows.append({"support_failure": tiled_search._support_error(error)})
raise
@@ -191,23 +231,27 @@ def worker(args):
return result
matching._fit_ridge_uncached, Track.acoustic = counted_fit, counted_acoustic
+ tiled_search.score_candidate = captured_score
if callable(native_fit):
matching._native_fit_irls = counted_native
- tiled_search.score_candidate = captured_score
native_acoustic_before = native_acoustic.successful_calls() if native_acoustic is not None else 0
began, cpu = time.perf_counter(), time.process_time()
try:
+ partition = ({"anchor_window": fixture["anchor_window_s"]}
+ if "anchor_window_s" in fixture else {})
ranked, counts = tiled_search._score(
points, tracks, events, measurements, budget, fixture["scoring"], {},
- start_window=fixture["recording_start_window_s"], ledger=ledger, stage=stage)
+ start_window=fixture["recording_start_window_s"], ledger=ledger, stage=stage, **partition)
finally:
elapsed, cpu_seconds = time.perf_counter() - began, time.process_time() - cpu
- if native_acoustic is not None:
- operations["native_acoustic_successes"] = native_acoustic.successful_calls() - native_acoustic_before
matching._fit_ridge_uncached, Track.acoustic = original_fit, original_acoustic
+ tiled_search.score_candidate = original_score
if callable(native_fit):
matching._native_fit_irls = native_fit
- tiled_search.score_candidate = original_score
+ if native_acoustic is not None:
+ operations["native_acoustic_successes"] = native_acoustic.successful_calls() - native_acoustic_before
+ for owner, attribute, function in reversed(phase_patches):
+ setattr(owner, attribute, function)
expected_counts = fixture.get("expected_counts")
if expected_counts is not None:
actual_counts = {"cpa_rows": counts["cpa_rows"],
@@ -226,9 +270,9 @@ def worker(args):
if not operations["scored_candidates"]:
raise AssertionError("The benchmark did not score any real associations")
if args.require_native and not operations["native_fit_successes"]:
- raise AssertionError("The native fitter was available but no successful native fit was timed")
+ raise AssertionError("No successful native ridge fit occurred inside the timed scoring call")
if args.require_native_acoustic and not operations["native_acoustic_successes"]:
- raise AssertionError("The native acoustic solver was available but no successful native solve was timed")
+ raise AssertionError("No successful native acoustic solve occurred inside the timed scoring call")
with ledger.connect() as database:
retention = {name: database.execute(f"SELECT * FROM {name} ORDER BY 1,2").fetchall()
for name in ("records", "bounds", "stages")}
@@ -236,12 +280,17 @@ def worker(args):
"all_scored_candidates": all_rows, "retention": retention})
summary = {"wall_seconds": elapsed, "cpu_seconds": cpu_seconds,
"load_seconds_excluded": load_seconds, "operations": operations,
- "ridge_fit_backend": backend, "native_extension_available": callable(native_fit),
- "acoustic_backend": acoustic_backend,
+ "ridge_fit_backend": ridge_backend, "acoustic_backend": acoustic_backend,
+ "native_extension_available": callable(native_fit),
"peak_rss_mib": peak_rss_mib,
"peak_rss_scope": ("Worker lifetime through scoring, including imports and input loading"
if peak_rss_mib is not None else "Unavailable on this platform"),
"retained": len(ranked), "independent_groups": len(events), "scope": fixture["scope"]}
+ if "anchor_window_s" in fixture:
+ summary["anchor_window_s"] = fixture["anchor_window_s"]
+ if args.stage_timing:
+ summary["phase_seconds"] = phase_seconds
+ summary["phase_timing_scope"] = "Inclusive instrumented call times; overlapping phases must not be summed."
write_new(args.output / "summary.json", summary)
print(json.dumps(summary), flush=True)
@@ -261,6 +310,8 @@ def compare(args):
"--fixture", str(args.fixture.resolve()), "--output", str(output.resolve())]
if args.index:
command.extend(["--index", str(args.index.resolve())])
+ if args.stage_timing:
+ command.append("--stage-timing")
if name == "current" and args.require_current_native:
command.append("--require-native")
if name == "current" and args.require_current_native_acoustic:
@@ -299,6 +350,8 @@ def main():
default=ROOT / "evidence/results/reference-local/report.json")
preparation.add_argument("--clock-half-width-s", type=float, default=120.0)
preparation.add_argument("--full-day", action="store_true", help="Use all indexed times with an unknown clock")
+ preparation.add_argument("--anchor-window-s", nargs=2, type=float, metavar=("LOW", "HIGH"),
+ help="Optional half-open association ownership partition; physical fit windows remain unchanged")
preparation.add_argument("--max-tracks", type=int, default=5000)
preparation.add_argument("--max-samples", type=int, default=1000000)
preparation.add_argument("--max-candidates", type=int, default=20000)
@@ -312,21 +365,28 @@ def main():
comparison.add_argument("--repeats", type=int, default=3)
comparison.add_argument("--timeout-s", type=float, default=600.0)
comparison.add_argument("--output", required=True, type=Path)
+ comparison.add_argument("--stage-timing", action="store_true",
+ help="Collect inclusive phase times in a separate diagnostic run")
comparison.add_argument("--require-current-native", action="store_true",
- help="Fail unless the timed current worker actually uses its native fitter")
+ help="Require successful native ridge fits inside each current scoring run")
comparison.add_argument("--require-current-native-acoustic", action="store_true",
- help="Fail unless the timed current worker actually uses its native acoustic solver")
+ help="Require successful native acoustic solves inside each current scoring run")
internal = commands.add_parser("_worker", help=argparse.SUPPRESS)
internal.add_argument("--source", required=True, type=Path)
internal.add_argument("--fixture", required=True, type=Path)
internal.add_argument("--index", type=Path)
internal.add_argument("--output", required=True, type=Path)
+ internal.add_argument("--stage-timing", action="store_true", help=argparse.SUPPRESS)
internal.add_argument("--require-native", action="store_true", help=argparse.SUPPRESS)
internal.add_argument("--require-native-acoustic", action="store_true", help=argparse.SUPPRESS)
args = parser.parse_args()
if args.command == "prepare":
if args.clock_half_width_s < 0:
parser.error("--clock-half-width-s must be nonnegative")
+ if (args.anchor_window_s is not None
+ and (not all(math.isfinite(value) for value in args.anchor_window_s)
+ or args.anchor_window_s[0] >= args.anchor_window_s[1])):
+ parser.error("--anchor-window-s requires finite increasing endpoints")
sys.path.insert(0, str(ROOT / "src"))
prepare(args)
elif args.command == "compare":
@@ -339,4 +399,3 @@ def main():
if __name__ == "__main__":
main()
-
diff --git a/src/dopplergeist/_native_ridge.c b/src/dopplergeist/_native_ridge.c
index c424624..5ba0523 100644
--- a/src/dopplergeist/_native_ridge.c
+++ b/src/dopplergeist/_native_ridge.c
@@ -3,6 +3,17 @@
#include <math.h>
#include <string.h>
+typedef struct {
+ PyObject *validation_keys[16];
+} RidgeModuleState;
+
+static const char *validation_names[] = {
+ "support_fraction", "score", "f0_hz", "source_drift_hz", "source_drift_hz_s",
+ "source_reference_emission_time_s", "source_span_s", "drift_bound_hz", "rmse_hz",
+ "outlier_fraction", "sample_count", "independent_sample_count", "optimizer_success",
+ "f0_range_hz", "quality", "source_drift_fraction"
+};
+
static int
get_vector(PyObject *object, Py_buffer *view)
{
@@ -267,17 +278,165 @@ done:
return result;
}
+/* Only ordinary string-keyed dictionaries enter the fast validator. A custom
+ * key or container can execute Python during lookup and keeps the Python path. */
+static int
+plain_dictionary(PyObject *object)
+{
+ if (!PyDict_CheckExact(object)) return 0;
+ Py_ssize_t position = 0;
+ PyObject *key, *value;
+ while (PyDict_Next(object, &position, &key, &value)) {
+ if (!PyUnicode_CheckExact(key)) return 0;
+ }
+ return 1;
+}
+
+static PyObject *
+sequence_item(PyObject *sequence, Py_ssize_t index)
+{
+ return PyList_CheckExact(sequence) ? PyList_GET_ITEM(sequence, index) :
+ PyTuple_GET_ITEM(sequence, index);
+}
+
+/* Return the first per-event evidence failure, or None for the Python path.
+ * The numeric codes match search._EVENT_FIT_FAILURES. No evidence is cached or
+ * trusted because it came from a fitter: every call checks the current values.
+ * Score summation and association arithmetic stay in Python, including Python's
+ * version-specific float sum behavior. */
+static PyObject *
+event_fit_reason(PyObject *self, PyObject *args)
+{
+ PyObject *diagnostics, *scores, *frequencies, *events;
+ RidgeModuleState *state = PyModule_GetState(self);
+ if (state == NULL) return NULL;
+ if (!PyArg_ParseTuple(args, "OOOO", &diagnostics, &scores, &frequencies, &events)) {
+ return NULL;
+ }
+ if (!PyList_CheckExact(diagnostics) || !PyList_CheckExact(scores) ||
+ !PyList_CheckExact(frequencies) ||
+ !(PyList_CheckExact(events) || PyTuple_CheckExact(events))) Py_RETURN_NONE;
+ Py_ssize_t count = PyList_GET_SIZE(diagnostics);
+ Py_ssize_t event_count = PyList_CheckExact(events) ? PyList_GET_SIZE(events) :
+ PyTuple_GET_SIZE(events);
+ if (PyList_GET_SIZE(scores) != count || PyList_GET_SIZE(frequencies) != count ||
+ event_count != count) Py_RETURN_NONE;
+
+ for (Py_ssize_t i = 0; i < count; ++i) {
+ PyObject *diagnostic = PyList_GET_ITEM(diagnostics, i);
+ PyObject *event = sequence_item(events, i);
+ PyObject *score_object = PyList_GET_ITEM(scores, i);
+ PyObject *frequency_object = PyList_GET_ITEM(frequencies, i);
+ if (!plain_dictionary(diagnostic) || !plain_dictionary(event) ||
+ !PyFloat_CheckExact(score_object) || !PyFloat_CheckExact(frequency_object)) {
+ Py_RETURN_NONE;
+ }
+ double values[10];
+ for (int j = 0; j < 10; ++j) {
+ PyObject *value = PyDict_GetItem(diagnostic, state->validation_keys[j]);
+ if (value == NULL || !PyFloat_CheckExact(value)) Py_RETURN_NONE;
+ values[j] = PyFloat_AS_DOUBLE(value);
+ }
+ PyObject *samples = PyDict_GetItem(diagnostic, state->validation_keys[10]);
+ PyObject *independent = PyDict_GetItem(diagnostic, state->validation_keys[11]);
+ if (samples == NULL || independent == NULL ||
+ !PyLong_CheckExact(samples) || !PyLong_CheckExact(independent)) Py_RETURN_NONE;
+ long long sample_count = PyLong_AsLongLong(samples);
+ if (PyErr_Occurred()) { PyErr_Clear(); Py_RETURN_NONE; }
+ long long independent_count = PyLong_AsLongLong(independent);
+ if (PyErr_Occurred()) { PyErr_Clear(); Py_RETURN_NONE; }
+ PyObject *optimizer = PyDict_GetItem(diagnostic, state->validation_keys[12]);
+ if (optimizer != NULL && !PyBool_Check(optimizer)) Py_RETURN_NONE;
+ PyObject *bounds = PyDict_GetItem(event, state->validation_keys[13]);
+ if (bounds == NULL || !(PyList_CheckExact(bounds) || PyTuple_CheckExact(bounds))) {
+ Py_RETURN_NONE;
+ }
+ Py_ssize_t size = PyList_CheckExact(bounds) ? PyList_GET_SIZE(bounds) : PyTuple_GET_SIZE(bounds);
+ if (size != 2) Py_RETURN_NONE;
+ PyObject *low_object = sequence_item(bounds, 0), *high_object = sequence_item(bounds, 1);
+ if (!PyFloat_CheckExact(low_object) || !PyFloat_CheckExact(high_object)) Py_RETURN_NONE;
+ double low = PyFloat_AS_DOUBLE(low_object), high = PyFloat_AS_DOUBLE(high_object);
+ double fraction = 0.02;
+ PyObject *quality = PyDict_GetItem(event, state->validation_keys[14]);
+ if (quality != NULL) {
+ if (!plain_dictionary(quality)) Py_RETURN_NONE;
+ PyObject *configured = PyDict_GetItem(quality, state->validation_keys[15]);
+ if (configured != NULL) {
+ if (!PyFloat_CheckExact(configured)) Py_RETURN_NONE;
+ fraction = PyFloat_AS_DOUBLE(configured);
+ }
+ }
+ double score = PyFloat_AS_DOUBLE(score_object), frequency = PyFloat_AS_DOUBLE(frequency_object);
+ if (!isfinite(values[0]) || values[0] != 1.0) return PyLong_FromLong(1);
+ int finite = isfinite(score) && isfinite(frequency);
+ for (int j = 1; j < 10; ++j) finite = finite && isfinite(values[j]);
+ if (!finite) return PyLong_FromLong(2);
+ if (independent_count < 1 || independent_count > sample_count || sample_count < 10 ||
+ values[6] <= 0.0 || values[7] < 0.0 || values[8] < 0.0 ||
+ !(0.0 <= values[9] && values[9] <= 1.0) || values[2] <= 0.0 || values[1] > 0.0) {
+ return PyLong_FromLong(3);
+ }
+ if (optimizer == Py_False) return PyLong_FromLong(4);
+ if (score != values[1] || frequency != values[2]) return PyLong_FromLong(5);
+ if (!isfinite(low) || !isfinite(high) || !(0.0 < low && low <= high) ||
+ !(low <= frequency && frequency <= high) || !isfinite(fraction) ||
+ !(0.0 <= fraction && fraction <= 0.1)) return PyLong_FromLong(6);
+ double drift = values[3], cap = values[7];
+ if (!(low * fraction <= cap && cap <= high * fraction) || fabs(drift) > cap) {
+ return PyLong_FromLong(7);
+ }
+ if (fabs(values[4] * values[6] - drift) > 1e-12 + 1e-12 * fabs(drift)) {
+ return PyLong_FromLong(8);
+ }
+ }
+ return PyLong_FromLong(0);
+}
+
static PyMethodDef methods[] = {
{"fit_irls", fit_irls, METH_VARARGS, "Run six robust ridge-fit iterations in C."},
+ {"event_fit_reason", event_fit_reason, METH_VARARGS, "Validate canonical per-event fit evidence, or request Python fallback."},
{NULL, NULL, 0, NULL}
};
+static int
+ridge_traverse(PyObject *module, visitproc visit, void *arg)
+{
+ RidgeModuleState *state = PyModule_GetState(module);
+ for (int i = 0; i < 16; ++i) Py_VISIT(state->validation_keys[i]);
+ return 0;
+}
+
+static int
+ridge_clear(PyObject *module)
+{
+ RidgeModuleState *state = PyModule_GetState(module);
+ for (int i = 0; i < 16; ++i) Py_CLEAR(state->validation_keys[i]);
+ return 0;
+}
+
+static void
+ridge_free(void *module)
+{
+ ridge_clear((PyObject *)module);
+}
+
static struct PyModuleDef module = {
- PyModuleDef_HEAD_INIT, "_native_ridge", NULL, -1, methods
+ PyModuleDef_HEAD_INIT, "_native_ridge", NULL, sizeof(RidgeModuleState), methods,
+ NULL, ridge_traverse, ridge_clear, ridge_free
};
PyMODINIT_FUNC
PyInit__native_ridge(void)
{
- return PyModule_Create(&module);
+ PyObject *created = PyModule_Create(&module);
+ if (created == NULL) return NULL;
+ RidgeModuleState *state = PyModule_GetState(created);
+ for (int i = 0; i < 16; ++i) {
+ state->validation_keys[i] = PyUnicode_InternFromString(validation_names[i]);
+ if (state->validation_keys[i] == NULL) {
+ Py_DECREF(created);
+ return NULL;
+ }
+ }
+ return created;
}
diff --git a/src/dopplergeist/acoustic_screen.py b/src/dopplergeist/acoustic_screen.py
index f1711d7..cac05ab 100644
--- a/src/dopplergeist/acoustic_screen.py
+++ b/src/dopplergeist/acoustic_screen.py
@@ -44,22 +44,13 @@ def ridge_trial_rejections(measurement, event, factors, emissions, *, score_floo
times, ridge, weight = (np.asarray(a, dtype=float) for a in (spec.time, ridge, weight))
except (ValueError, TypeError, KeyError, AttributeError):
return result()
- if (not np.isfinite(sigma) or sigma <= 0 or not np.isfinite(fraction) or not 0 <= fraction <= .1
- or not np.isfinite(low) or not np.isfinite(high) or not 0 < low <= high
- or times.ndim != 1 or times.shape != ridge.shape or times.shape != weight.shape
- or not np.all(np.isfinite(weight)) or np.any(weight < 0)):
- return result()
- measured = np.isfinite(times) & np.isfinite(ridge) & (weight > 0)
- if measured.sum() < 10 or factors.shape[1] != measured.sum():
- return result()
- times, ridge, weight = times[measured], ridge[measured], weight[measured]
- evidence = _cached_loss_lower_bounds(times, ridge, weight, factors, emissions, sigma, fraction, high)
+ evidence = _cached_loss_lower_bounds(times, ridge, weight, factors, emissions,
+ sigma, fraction, low, high)
if evidence is None:
return result()
- active, valid, lower = evidence
+ active, valid, lower, residual_rejections = evidence
+ rejected[active] = residual_rejections
with np.errstate(over="ignore", invalid="ignore", divide="ignore"):
- rejected[active] = (valid & np.isfinite(lower)
- & (lower > (3*sigma)**2*(1+1e-8)+1e-9))
if (score_floor is not None and type(group_count) is int and group_count > 0
and np.isfinite(score_floor) and 0 <= group_count-score_floor < 1):
threshold = (3*sigma*(group_count-score_floor))**2*(1+1e-8)+1e-9
@@ -72,27 +63,55 @@ def ridge_trial_rejections(measurement, event, factors, emissions, *, score_floo
return result()
-def _cached_loss_lower_bounds(times, ridge, weight, factors, emissions, sigma, fraction, high):
- """Reuse exact numerical inputs under the scoring context's memory limit."""
+def _validated_loss_lower_bounds(times, ridge, weight, factors, emissions, sigma, fraction, low, high):
+ """Validate and select measured samples before computing a screen's bound."""
+ if (not np.isfinite(sigma) or sigma <= 0 or not np.isfinite(fraction) or not 0 <= fraction <= .1
+ or not np.isfinite(low) or not np.isfinite(high) or not 0 < low <= high
+ or times.ndim != 1 or times.shape != ridge.shape or times.shape != weight.shape
+ or not np.all(np.isfinite(weight)) or np.any(weight < 0)):
+ return None
+ measured = np.isfinite(times) & np.isfinite(ridge) & (weight > 0)
+ count = int(measured.sum())
+ if count < 10 or factors.shape[1] != count:
+ return None
+ evidence = _loss_lower_bounds(times[measured], ridge[measured], weight[measured],
+ factors, emissions, sigma, fraction, high)
+ if evidence is None:
+ return None
+ active, valid, lower = evidence
+ with np.errstate(over="ignore", invalid="ignore", divide="ignore"):
+ rejected = valid & np.isfinite(lower) & (lower > (3*sigma)**2*(1+1e-8)+1e-9)
+ return active, valid, lower, rejected
+
+
+def _cached_loss_lower_bounds(times, ridge, weight, factors, emissions, sigma, fraction, low, high):
+ """Reuse validated raw inputs under the scoring context's memory limit.
+
+ Fingerprint complete input arrays before selecting samples. A hit therefore
+ reuses validation, masking and residual rejections as well as loss evidence.
+ Any changed or invalid raw sample, including one previously masked out,
+ gets checked again. Retention ceilings still use the caller's current floor.
+ """
cache = _ridge_fit_cache.get()
arrays = times, ridge, weight, factors, emissions
if cache is None:
- return _loss_lower_bounds(*arrays, sigma, fraction, high)
+ return _validated_loss_lower_bounds(*arrays, sigma, fraction, low, high)
entries = cache.screen_entries
if cache.screen_limit <= 0 or cache.max_entries <= 0:
entries.clear()
cache.screen_bytes = 0
- return _loss_lower_bounds(*arrays, sigma, fraction, high)
+ return _validated_loss_lower_bounds(*arrays, sigma, fraction, low, high)
if np.geterr()["under"] != "ignore":
# The numerical kernel inherits underflow handling. Preserve any
# requested warning/exception instead of hiding it behind a cache hit.
- return _loss_lower_bounds(*arrays, sigma, fraction, high)
+ return _validated_loss_lower_bounds(*arrays, sigma, fraction, low, high)
# Layout affects reduction order. Keys retain input bytes and layout, but
# never the much larger propagation arrays themselves. Parameters have
- # already been converted to finite Python floats by the public validator.
- key = (_loss_lower_bounds, tuple(value.hex() for value in (sigma, fraction, high)),
+ # already been converted to Python floats; misses validate their domain.
+ key = (_validated_loss_lower_bounds, _loss_lower_bounds,
+ tuple(value.hex() for value in (sigma, fraction, low, high)),
tuple((value.dtype.str, value.shape, value.strides, value.flags.aligned,
- hashlib.sha256(value.tobytes()).digest()) for value in arrays))
+ hashlib.sha256(value if value.flags.c_contiguous else value.tobytes()).digest()) for value in arrays))
# A caller may lower a budget between requests; enforce that budget on hits
# as well as inserts rather than retaining an oversized previous context.
while entries and (len(entries) > cache.max_entries or
@@ -103,7 +122,7 @@ def _cached_loss_lower_bounds(times, ridge, weight, factors, emissions, sigma, f
if previous is not None:
entries.move_to_end(key)
return previous[0]
- evidence = _loss_lower_bounds(*arrays, sigma, fraction, high)
+ evidence = _validated_loss_lower_bounds(*arrays, sigma, fraction, low, high)
if evidence is None:
return None
diff --git a/src/dopplergeist/search.py b/src/dopplergeist/search.py
index 9ef7604..dc73f85 100644
--- a/src/dopplergeist/search.py
+++ b/src/dopplergeist/search.py
@@ -89,7 +89,9 @@ def cpa_table(tracks,points,max_distance,c=343.0,step_s=2.0,chunk_size=512, *, _
point_ids=points.point_id.to_numpy(dtype=int)
rows = _cpa_rows(tracks, observers, point_ids, max_distance, c, step_s, chunk_size,
_sample_cache=_sample_cache)
- return pd.DataFrame(rows, columns=_CPA_COLUMNS) if rows else _EMPTY_CPA_TABLE.copy()
+ if not rows:
+ return _EMPTY_CPA_TABLE.copy()
+ return pd.DataFrame(rows, columns=_CPA_COLUMNS)
def _cpa_rows(tracks, observers, point_ids, max_distance, c=343.0, step_s=2.0, chunk_size=512, *,
@@ -320,6 +322,13 @@ def event_score(track, observer, spectrum, ridge, weight, start_time, f0_bounds,
return best_frequency(spectrum, times, factor, f0_bounds, frequency_step)
+_EVENT_FIT_FAILURES = (
+ None, "incomplete_acoustic_support", "nonfinite_fit_evidence", "invalid_fit_evidence",
+ "failed_optimizer", "inconsistent_event_fit_evidence", "fit_outside_declared_frequency_domain",
+ "fit_outside_declared_drift_domain", "inconsistent_source_drift",
+)
+
+
def _ridge_trial_classification(result, events, association):
"""Classify recorded core fits, without changing their score or rejection rule.
@@ -337,37 +346,43 @@ def _ridge_trial_classification(result, events, association):
if (not isinstance(diagnostics, list) or not isinstance(scores, list) or not isinstance(frequencies, list)
or not len(diagnostics) == len(scores) == len(frequencies) == len(events)):
return "unknown", "missing_event_fit_evidence"
- fields = ("score", "f0_hz", "source_drift_hz", "source_drift_hz_s",
- "source_reference_emission_time_s", "source_span_s", "drift_bound_hz", "rmse_hz", "outlier_fraction")
- for diagnostic, score, frequency, event in zip(diagnostics, scores, frequencies, events):
- if (not isinstance(diagnostic, dict) or not finite(diagnostic.get("support_fraction"))
- or diagnostic["support_fraction"] != 1.0):
- return "unknown", "incomplete_acoustic_support"
- if (not all(finite(diagnostic.get(name)) for name in fields)
- or not finite(score) or not finite(frequency)):
- return "unknown", "nonfinite_fit_evidence"
- count, independent = diagnostic.get("sample_count"), diagnostic.get("independent_sample_count")
- if (type(count) is not int or type(independent) is not int or not 1 <= independent <= count
- or count < 10 or diagnostic["source_span_s"] <= 0 or diagnostic["drift_bound_hz"] < 0
- or diagnostic["rmse_hz"] < 0 or not 0 <= diagnostic["outlier_fraction"] <= 1
- or diagnostic["f0_hz"] <= 0 or diagnostic["score"] > 0):
- return "unknown", "invalid_fit_evidence"
- if "optimizer_success" in diagnostic and diagnostic["optimizer_success"] is not True:
- return "unknown", "failed_optimizer"
- if score != diagnostic["score"] or frequency != diagnostic["f0_hz"]:
- return "unknown", "inconsistent_event_fit_evidence"
- bounds = event.get("f0_range_hz")
- fraction = event.get("quality", {}).get("source_drift_fraction", 0.02)
- if (not isinstance(bounds, (list, tuple)) or len(bounds) != 2
- or not all(finite(value) for value in bounds) or not 0 < bounds[0] <= bounds[1]
- or not bounds[0] <= frequency <= bounds[1]
- or not finite(fraction) or not 0 <= fraction <= 0.1):
- return "unknown", "fit_outside_declared_frequency_domain"
- drift, cap = diagnostic["source_drift_hz"], diagnostic["drift_bound_hz"]
- if not bounds[0] * fraction <= cap <= bounds[1] * fraction or abs(drift) > cap:
- return "unknown", "fit_outside_declared_drift_domain"
- if abs(diagnostic["source_drift_hz_s"] * diagnostic["source_span_s"] - drift) > 1e-12 + 1e-12 * abs(drift):
- return "unknown", "inconsistent_source_drift"
+ from .matching import _native_ridge_module
+ validator = getattr(_native_ridge_module, "event_fit_reason", None)
+ reason = None if validator is None else validator(diagnostics, scores, frequencies, events)
+ if reason is not None and reason:
+ return "unknown", _EVENT_FIT_FAILURES[reason]
+ if reason is None:
+ fields = ("score", "f0_hz", "source_drift_hz", "source_drift_hz_s",
+ "source_reference_emission_time_s", "source_span_s", "drift_bound_hz", "rmse_hz", "outlier_fraction")
+ for diagnostic, score, frequency, event in zip(diagnostics, scores, frequencies, events):
+ if (not isinstance(diagnostic, dict) or not finite(diagnostic.get("support_fraction"))
+ or diagnostic["support_fraction"] != 1.0):
+ return "unknown", "incomplete_acoustic_support"
+ if (not all(finite(diagnostic.get(name)) for name in fields)
+ or not finite(score) or not finite(frequency)):
+ return "unknown", "nonfinite_fit_evidence"
+ count, independent = diagnostic.get("sample_count"), diagnostic.get("independent_sample_count")
+ if (type(count) is not int or type(independent) is not int or not 1 <= independent <= count
+ or count < 10 or diagnostic["source_span_s"] <= 0 or diagnostic["drift_bound_hz"] < 0
+ or diagnostic["rmse_hz"] < 0 or not 0 <= diagnostic["outlier_fraction"] <= 1
+ or diagnostic["f0_hz"] <= 0 or diagnostic["score"] > 0):
+ return "unknown", "invalid_fit_evidence"
+ if "optimizer_success" in diagnostic and diagnostic["optimizer_success"] is not True:
+ return "unknown", "failed_optimizer"
+ if score != diagnostic["score"] or frequency != diagnostic["f0_hz"]:
+ return "unknown", "inconsistent_event_fit_evidence"
+ bounds = event.get("f0_range_hz")
+ fraction = event.get("quality", {}).get("source_drift_fraction", 0.02)
+ if (not isinstance(bounds, (list, tuple)) or len(bounds) != 2
+ or not all(finite(value) for value in bounds) or not 0 < bounds[0] <= bounds[1]
+ or not bounds[0] <= frequency <= bounds[1]
+ or not finite(fraction) or not 0 <= fraction <= 0.1):
+ return "unknown", "fit_outside_declared_frequency_domain"
+ drift, cap = diagnostic["source_drift_hz"], diagnostic["drift_bound_hz"]
+ if not bounds[0] * fraction <= cap <= bounds[1] * fraction or abs(drift) > cap:
+ return "unknown", "fit_outside_declared_drift_domain"
+ if abs(diagnostic["source_drift_hz_s"] * diagnostic["source_span_s"] - drift) > 1e-12 + 1e-12 * abs(drift):
+ return "unknown", "inconsistent_source_drift"
if "optimizer_success" in result and result["optimizer_success"] is not True:
return "unknown", "failed_optimizer"
residual = float(sum(scores))
diff --git a/tests/test_real_acoustic_screen_cache.py b/tests/test_real_acoustic_screen_cache.py
index a0ead7f..0cfc68f 100644
--- a/tests/test_real_acoustic_screen_cache.py
+++ b/tests/test_real_acoustic_screen_cache.py
@@ -178,3 +178,32 @@ def test_recorded_underflow_policy_is_not_hidden_by_a_cache_hit(recorded_batches
assert calls == before + 1
compare()
+
+
[email protected]("changed_field", ["low", "weight", "time", "ridge", "shape"])
+def test_real_screen_cache_revalidates_changed_raw_inputs(recorded_batches, changed_field):
+ """A previously valid cache hit must not hide a now-invalid input."""
+ base = deepcopy(recorded_batches[0])
+ changed = deepcopy(base)
+ if changed_field == "low":
+ changed[1]["f0_range_hz"][0] = float("nan")
+ elif changed_field == "weight":
+ changed[0][2][0] = -1.
+ elif changed_field == "time":
+ changed[0][0].time[0] = float("nan")
+ elif changed_field == "ridge":
+ changed[0][1][0] = float("nan")
+ else:
+ measurement = changed[0]
+ changed = ((measurement[0], measurement[1][1:], measurement[2], measurement[3]),
+ *changed[1:])
+ options = {"score_floor": .7, "group_count": 1}
+ expected = screen.ridge_trial_rejections(*changed, **options)
+
+ @_cache_ridge_fits
+ def compare():
+ screen.ridge_trial_rejections(*base, **options)
+ assert _ridge_fit_cache.get().screen_entries
+ same(screen.ridge_trial_rejections(*changed, **options), expected)
+
+ compare()
diff --git a/tests/test_real_native_ridge_long.py b/tests/test_real_native_ridge_long.py
new file mode 100644
index 0000000..29998f5
--- /dev/null
+++ b/tests/test_real_native_ridge_long.py
@@ -0,0 +1,72 @@
+"""Check the native reduction boundary on a long recorded Doppler ridge."""
+import json
+import os
+from pathlib import Path
+
+import numpy as np
+import pandas as pd
+import pytest
+
+from dopplergeist import matching
+from dopplergeist.data_access import FlightIndex
+from dopplergeist.geometry import ecef
+from dopplergeist.search import filter_association_event
+
+
+def test_recorded_ridge_crosses_native_pairwise_reduction_boundary(monkeypatch):
+ native = matching._native_fit_irls
+ if native is None:
+ pytest.skip("optional native ridge extension is not installed")
+ source = os.environ.get("DOPPLERGEIST_REAL_CPA_FIXTURE")
+ if not source:
+ pytest.skip("set DOPPLERGEIST_REAL_CPA_FIXTURE to the real point120 diagnostic")
+ fixture = json.loads(Path(source).read_text())
+ events, measurements = matching.prepare_events(fixture["events"])
+ index = max(range(len(measurements)), key=lambda i: len(measurements[i][1]))
+ event, measurement = events[index], measurements[index]
+ if len(measurement[1]) < 141:
+ pytest.skip("this recorded fixture has no ridge crossing the 128-sample boundary")
+ rows = filter_association_event(pd.DataFrame(fixture["cpa_rows"]), event, 5., None, 5.)
+ tracks = {track.id: track for track in FlightIndex(fixture["real_index"]).lookup(
+ list(dict.fromkeys(rows.flight_id)), max_tracks=2000, max_samples=2000000)}
+ point = fixture["point"]
+ observer = ecef(point["lat"], point["lon"], point["elev_m"])
+ checked = passages = 0
+ native_successes = 0
+
+ def counted(*arguments):
+ nonlocal native_successes
+ result = native(*arguments)
+ native_successes += result is not None and all(np.isfinite(value) for value in result)
+ return result
+
+ monkeypatch.setattr(matching, "_native_fit_irls", counted)
+ for row in rows.itertuples():
+ # These are shared-clock hypotheses for actual measured observations;
+ # no recording or trajectory samples are generated.
+ starts = row.receive_time - event["cpa_receive_s"] + np.array([-2.5, 0., 2.5])
+ batch = matching.batch_acoustic_trials(
+ [tracks[row.flight_id]], [measurement], observer, starts)[0]
+ if batch is None or not all(np.all(np.isfinite(array)) for array in batch):
+ continue
+ for count in (127, 128, 129, 136, 141):
+ selected = (matching.RidgeTimes(measurement[0].time[:count]),
+ measurement[1][:count], measurement[2][:count], {})
+ for factor, emitted in zip(batch[0][:, :count], batch[1][:, :count]):
+ for sigma in (0.5, 1.0, 3.7, 18.001973492902533):
+ for fraction in (0.0, 0.02, 0.1):
+ arguments = (None, None, selected, 0., event["f0_range_hz"],
+ 343., sigma, fraction)
+ with monkeypatch.context() as patch:
+ patch.setattr(matching, "_native_fit_irls", None)
+ expected = matching._fit_ridge_uncached(
+ *arguments, acoustic_result=(factor, emitted))
+ actual = matching._fit_ridge_uncached(
+ *arguments, acoustic_result=(factor, emitted))
+ assert expected["support_fraction"] == 1. and np.isfinite(expected["score"])
+ assert json.dumps(actual, sort_keys=True) == json.dumps(expected, sort_keys=True)
+ checked += 1
+ passages += 1
+ if passages == 3:
+ break
+ assert passages == 3 and checked == native_successes == 540
diff --git a/tests/test_real_native_validation.py b/tests/test_real_native_validation.py
new file mode 100644
index 0000000..48954ed
--- /dev/null
+++ b/tests/test_real_native_validation.py
@@ -0,0 +1,201 @@
+"""Native evidence checks on archived fits of real audio and ADS-B observations.
+
+Malformed scalar metadata exercise validation and fallback behavior; these tests
+do not generate audio, trajectories, or a field-validation corpus.
+"""
+from copy import deepcopy
+import json
+from pathlib import Path
+from types import SimpleNamespace
+
+import numpy as np
+import pytest
+
+from dopplergeist import matching, search
+
+
+def recorded():
+ fixture = json.loads((Path(__file__).parent / "data/real-ridge-batch-arithmetic.json").read_text())
+ return fixture["legacy_diagnostics"], fixture["event"]
+
+
+def result_for(diagnostics, events, association=False):
+ result = {
+ "score_kind": "negative_robust_ridge_loss_hz",
+ "score": float(sum(fit["score"] for fit in diagnostics)),
+ "event_scores": [fit["score"] for fit in diagnostics],
+ "f0_hz": [fit["f0_hz"] for fit in diagnostics],
+ "event_diagnostics": diagnostics,
+ }
+ return matching.association_score(result, events) if association else result
+
+
+def outcome(function, *args):
+ try:
+ return ("result", function(*args))
+ except Exception as error:
+ return ("error", type(error), str(error))
+
+
+def assert_python_parity(monkeypatch, result, events, association=False):
+ with monkeypatch.context() as patch:
+ patch.setattr(matching, "_native_ridge_module", None)
+ expected = outcome(search._ridge_trial_classification, result, events, association)
+ actual = outcome(search._ridge_trial_classification, result, events, association)
+ assert actual == expected
+
+
+def native():
+ function = getattr(matching._native_ridge_module, "event_fit_reason", None)
+ if function is None:
+ pytest.skip("optional native evidence validator is not installed")
+ return function
+
+
+def test_native_recorded_event_evidence_and_final_score_parity(native, monkeypatch):
+ diagnostics, event = recorded()
+ for selected in ([diagnostic] for diagnostic in diagnostics):
+ for association in (False, True):
+ events = [deepcopy(event) for _ in selected]
+ result = result_for(selected, events, association)
+ assert native(result["event_diagnostics"], result["event_scores"], result["f0_hz"], events) == 0
+ assert_python_parity(monkeypatch, result, events, association)
+ # The outer classifier still performs Python's exact score summation and
+ # association arithmetic, including its first failure and optimizer flag.
+ events = [deepcopy(event) for _ in diagnostics]
+ for association in (False, True):
+ result = result_for(diagnostics, events, association)
+ assert_python_parity(monkeypatch, result, events, association)
+ for field in ("score", "residual_score", "score_kind", "optimizer_success"):
+ for value in (None, False, True, -float("inf"), 0.0, "invalid"):
+ changed = dict(result, **{field: value})
+ assert_python_parity(monkeypatch, changed, events, association)
+
+
+def test_native_rechecks_every_recorded_diagnostic_field(native, monkeypatch):
+ diagnostics, event = recorded()
+ checked = 0
+ for diagnostic in diagnostics:
+ original = result_for([diagnostic], [event])
+ for field, previous in diagnostic.items():
+ values = (None, False, True, float("nan"), float("inf"), -float("inf"),
+ 0, 0.0, -1.0, 1e200, np.float32(previous), np.float64(previous),
+ np.longdouble(previous), 2**80)
+ for value in values:
+ changed = deepcopy(original)
+ changed["event_diagnostics"][0][field] = value
+ assert_python_parity(monkeypatch, changed, [event])
+ checked += 1
+ changed = deepcopy(original)
+ del changed["event_diagnostics"][0][field]
+ assert_python_parity(monkeypatch, changed, [event])
+ checked += 1
+ for key in ("event_scores", "f0_hz"):
+ for value in (np.float64(original[key][0]), 0.0, None, True, float("nan")):
+ changed = deepcopy(original)
+ changed[key][0] = value
+ assert_python_parity(monkeypatch, changed, [event])
+ checked += 1
+ assert checked > 800
+
+
+def test_native_recorded_frequency_and_drift_domain_parity(native, monkeypatch):
+ diagnostics, event = recorded()
+ for diagnostic in diagnostics:
+ result = result_for([diagnostic], [event])
+ for bounds in (None, [], [0.0, 1.0], [-1.0, 1.0], [1.0, 0.0],
+ [1.0, float("inf")], [float("nan"), 1000.0], [1, 1000],
+ np.asarray(event["f0_range_hz"]), tuple(event["f0_range_hz"])):
+ configured = dict(event, f0_range_hz=bounds)
+ assert_python_parity(monkeypatch, result, [configured])
+ for fraction in (None, False, True, -1.0, -0.0, 0.1, 0.2, float("nan"),
+ float("inf"), np.float32(.02), np.float64(.02), np.longdouble(.02)):
+ configured = dict(event, quality=dict(event["quality"], source_drift_fraction=fraction))
+ assert_python_parity(monkeypatch, result, [configured])
+ for quality in (None, {}, []):
+ assert_python_parity(monkeypatch, result, [dict(event, quality=quality)])
+ without_quality = {key: value for key, value in event.items() if key != "quality"}
+ assert_python_parity(monkeypatch, result, [without_quality])
+
+
+def test_native_reason_order_and_in_place_mutation(native, monkeypatch):
+ diagnostics, event = recorded()
+ fit = deepcopy(diagnostics[0])
+ result = result_for([fit], [event])
+ arguments = (result["event_diagnostics"], result["event_scores"], result["f0_hz"], [event])
+ assert native(*arguments) == 0
+ changes = (
+ ("support_fraction", 0.0, 1),
+ ("rmse_hz", float("nan"), 2),
+ ("sample_count", 1, 3),
+ ("optimizer_success", False, 4),
+ ("score", float(np.nextafter(fit["score"], np.inf)), 5),
+ ("source_drift_hz", fit["drift_bound_hz"] + 1.0, 7),
+ ("source_drift_hz_s", fit["source_drift_hz_s"] + 1.0, 8),
+ )
+ for field, value, code in changes:
+ fit.clear()
+ fit.update(diagnostics[0])
+ fit[field] = value
+ assert native(*arguments) == code
+ assert_python_parity(monkeypatch, result, [event])
+ fit.clear()
+ fit.update(diagnostics[0])
+ configured = dict(event, f0_range_hz=[1.0, 2.0])
+ assert native(*arguments[:-1], [configured]) == 6
+ assert_python_parity(monkeypatch, result, [configured])
+ # A later event's invalid data cannot replace the earlier failure reason.
+ fits = deepcopy(diagnostics[:2])
+ fits[0]["support_fraction"] = 0.0
+ fits[1]["rmse_hz"] = -1.0
+ result = result_for(fits, [event, event])
+ assert_python_parity(monkeypatch, result, [event, event])
+
+
+def test_native_source_drift_tolerance_boundary_keeps_python_arithmetic(native, monkeypatch):
+ diagnostics, event = recorded()
+ for diagnostic in diagnostics:
+ drift, span = diagnostic["source_drift_hz"], diagnostic["source_span_s"]
+ tolerance = 1e-12 + 1e-12 * abs(drift)
+ for direction in (-1.0, 1.0):
+ edge = (drift + direction * tolerance) / span
+ for rate in (edge, float(np.nextafter(edge, -np.inf)), float(np.nextafter(edge, np.inf))):
+ fit = dict(diagnostic, source_drift_hz_s=rate)
+ result = result_for([fit], [event])
+ assert_python_parity(monkeypatch, result, [event])
+
+
+def test_native_custom_types_and_older_extension_keep_python_fallback(native, monkeypatch):
+ class CustomDict(dict):
+ pass
+
+ class CustomList(list):
+ pass
+
+ class CustomString(str):
+ pass
+
+ diagnostics, event = recorded()
+ result = result_for(diagnostics, [event] * len(diagnostics))
+ standard = [diagnostics, result["event_scores"], result["f0_hz"], [event] * len(diagnostics)]
+ cases = []
+ for index in range(4):
+ changed = standard.copy()
+ changed[index] = CustomList(changed[index])
+ cases.append(changed)
+ for altered in (CustomDict(diagnostics[0]),
+ dict(diagnostics[0], score=np.float64(diagnostics[0]["score"])),
+ dict(diagnostics[0], sample_count=2**80)):
+ cases.append([[altered] + diagnostics[1:], *standard[1:]])
+ keyed = {CustomString(key): value for key, value in diagnostics[0].items()}
+ cases.append([[keyed] + diagnostics[1:], *standard[1:]])
+ cases.append([*standard[:3], [CustomDict(event)] * len(diagnostics)])
+ cases.append([*standard[:3], [dict(event, quality=CustomDict(event["quality"]))] * len(diagnostics)])
+ for arguments in cases:
+ assert native(*arguments) is None
+ altered_result = dict(result, event_diagnostics=arguments[0], event_scores=arguments[1], f0_hz=arguments[2])
+ assert_python_parity(monkeypatch, altered_result, arguments[3])
+ with monkeypatch.context() as patch:
+ patch.setattr(matching, "_native_ridge_module", SimpleNamespace())
+ assert_python_parity(monkeypatch, result, standard[3])