Skip to content

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

Integrate measured scoring-cache and flight-sieve acceleration

Cinna Davisb7fc01f8bc3e
diff --cc .github/workflows/real-performance.yml
index afd1d3b,5ef6711,d727775..95b8a1c
--- a/.github/workflows/real-performance.yml
+++ b/.github/workflows/real-performance.yml
@@@@ -11,7 -11,7 -11,7 +11,7 @@@@ on
type: boolean
default: true
push:
- branches: [perf/measured-hotpath-20261103, perf/real-pipeline-throughput-20261103, perf/single-pass-motion-20261103, perf/flight-sieve-reuse-20261103, perf/flight-sieve-94a6]
- branches: [perf/measured-hotpath-20261103, perf/real-pipeline-throughput-20261103, perf/single-pass-motion-20261103, perf/prepared-flight-scoring-20261103, perf/flight-sieve-reuse-20261103]
- branches: [perf/measured-hotpath-20261103, perf/real-pipeline-throughput-20261103, perf/single-pass-motion-20261103, perf/flight-sieve-reuse-20261103]
+++ branches: [perf/measured-hotpath-20261103, perf/real-pipeline-throughput-20261103, perf/single-pass-motion-20261103, perf/flight-sieve-reuse-20261103, perf/flight-sieve-94a6, perf/flight-sieve-combined-94a6, perf/prepared-flight-scoring-20261103]
paths:
- .github/workflows/real-performance.yml
- src/dopplergeist/**
@@@@ -38,7 -39,7 -38,7 +38,7 @@@@ env
NUMEXPR_NUM_THREADS: '1'
CC: gcc
PYTHONPATH: src
- BASELINE_REF: ${{ inputs.baseline || (github.ref == 'refs/heads/perf/flight-sieve-94a6' && 'b5ff48fe0e9d6e6feac7a4faf5268630f0c85d8b') || '27da08a878ac2ec90cd7e2c07f43765ceeea609b' }}
- BASELINE_REF: ${{ inputs.baseline || '27da08a878ac2ec90cd7e2c07f43765ceeea609b' }}
+ + BASELINE_REF: ${{ inputs.baseline || 'b4cd00ad46f0299baa716950fabe062d74f019f0' }}
jobs:
compare:
if: github.event_name != 'push' || github.ref != 'refs/heads/perf/real-pipeline-throughput-20261103'
@@@@ -56,9 -57,10 -56,9 +56,11 @@@@
python -m pip install 'setuptools>=68'
python -m pip install --editable '.[test]'
python - <<'PY'
-- from dopplergeist import matching, native_acoustic
++ from dopplergeist import matching, native_acoustic, native_cpa
assert callable(matching._native_fit_irls), "Native ridge extension did not build"
- assert callable(getattr(matching._native_ridge_module, "event_fit_reason", None)), "Native event_fit_reason did not build"
- assert callable(matching._native_validate_ridge_events), "Native valid_ridge_events did not build"
+++ assert callable(matching._native_fit_irls_batch), "Native batch fitter did not build"
+ assert callable(matching._native_validate_ridge_events), "Native evidence validator did not build"
+++ assert callable(matching._native_ridge_module.event_fit_reason), "Native diagnostic 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-")
@@@@ -101,18 -102,15 -100,15 +103,18 @@@@
- name: Real-input regression tests
env:
DOPPLERGEIST_REAL_CPA_FIXTURE: benchmark-inputs/point-120.json
++ DOPPLERGEIST_REAL_DENSE_QUERY: benchmark-inputs/dense-query.json
++ DOPPLERGEIST_REAL_DEVELOPMENT_INDEX: benchmark-inputs/reference-region.sqlite
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_partition_query.py tests/test_real_native_cpa.py tests/test_real_native_ridge.py tests/test_real_native_ridge_batch.py tests/test_real_native_ridge_long.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 tests/test_real_native_validation.py tests/test_real_index_columns.py tests/test_real_track_cache.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 tests/test_real_scalar_terrain.py tests/test_real_scoring_inputs.py tests/test_real_trial_prefix.py tests/test_real_candidate_setup.py tests/test_real_index_columns.py tests/test_real_track_cache.py tests/test_real_retention_batch.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_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 tests/test_real_native_validation.py tests/test_real_index_columns.py tests/test_real_track_cache.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_partition_query.py tests/test_real_native_cpa.py tests/test_real_native_ridge.py tests/test_real_native_ridge_batch.py tests/test_real_native_prefix_integration.py tests/test_real_native_ridge_long.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 tests/test_real_native_validation.py tests/test_real_index_columns.py tests/test_real_track_cache.py tests/test_real_scalar_terrain.py tests/test_real_scoring_inputs.py tests/test_real_trial_prefix.py tests/test_real_candidate_setup.py tests/test_real_retention_batch.py
- name: Verify the explicit Python fallback on recorded arithmetic
env:
DOPPLERGEIST_REAL_CPA_FIXTURE: benchmark-inputs/point-120.json
DOPPLERGEIST_RIDGE_BACKEND: python
DOPPLERGEIST_ACOUSTIC_BACKEND: python
- run: python -m pytest -q tests/test_real_batch_scalar_parity.py tests/test_real_legacy_batch_cache.py
++ DOPPLERGEIST_CPA_BACKEND: python
- run: python -m pytest -q tests/test_real_batch_scalar_parity.py tests/test_real_legacy_batch_cache.py
+ + run: python -m pytest -q tests/test_real_batch_scalar_parity.py tests/test_real_legacy_batch_cache.py tests/test_real_scoring_inputs.py tests/test_real_trial_prefix.py tests/test_real_candidate_setup.py
- name: Compare real CPA preparation
run: python scripts/benchmark_real_cpa_rows.py --baseline-source "$RUNNER_TEMP/baseline/src" --output results/cpa-rows.json --repeats 5 --iterations 20
- name: Compare complete payloads and timings
diff --cc .github/workflows/tests.yml
index 71210f0,e69a4c9,3a0d11e..f482815
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@@@ -39,24 -39,31 -39,19 +39,35 @@@@ jobs
run: python -m compileall -q src scripts
- name: CLI smoke check
run: dopplergeist --help
- - name: Check installed native acceleration and packaged browser assets
- - 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 _native_ridge, automatic, jobs, service
- from dopplergeist import automatic, jobs, service
++ from dopplergeist import _native_cpa, _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 Path(_native_cpa.__file__).resolve().parent == package
++ assert callable(_native_ridge.fit_irls_batch), "Native batch fitter did not build"
++ assert callable(_native_ridge.fit_statistics), "Native fit counters did not build"
++ assert callable(_native_cpa.select), "Native CPA reducer did not build"
+ + assert callable(getattr(_native_ridge, "event_fit_reason", None)), "Native evidence validator did not build"
+ + assert callable(getattr(_native_ridge, "valid_ridge_events", None)), "Native boolean 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
+ + assert _native_ridge.valid_ridge_events(
+ + fits, [fit["score"] for fit in fits], [fit["f0_hz"] for fit in fits],
+ + [fixture["event"]] * len(fits)) is True
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 native acceleration and browser assets passed; no audio or tracks generated.")
- 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 --cc docs/FLIGHT_SIEVE_PERFORMANCE.md
index 73c538e,0000000,0000000..0eb19ad
mode 100644,000000,000000..100644
--- a/docs/FLIGHT_SIEVE_PERFORMANCE.md
+++ b/docs/FLIGHT_SIEVE_PERFORMANCE.md
@@@@ -1,149 -1,0 -1,0 +1,183 @@@@
++# Flight checking and sieve performance
++
++## Measured result
++
+++The final integrated version combines these changes with the prepared-input and
+++prefix-cache work from PR #256, pinned at
+++`5eaee3a43120af13c9bc16ef7f03b782251cdac0`. Three alternating recorded-input pairs
+++measured **1.70x faster query-through-retention processing** and **41% less CPU
+++time**. These are direct measurements of the combined implementation; the
+++individual speedups below are not multiplied.
+++
+++The timed source combined PR #256 at
+++`4c213fa36777806bb24f4541eff3bc302fa24f6d` with these changes. The subsequent
+++`5eaee3a` update preserves exact immutable NumPy `float64` metadata from live
+++discovery, allowing prepared-input and prefix caches to remain eligible.
+++Recorded JSON inputs do not retain that scalar type. Its recorded-input tests
+++verify the live metadata behavior; the timing series was not rerun for this update.
+++
+++| Final integrated scope | Baseline | Integrated version | Improvement |
+++| --- | ---: | ---: | ---: |
+++| Complete measured CPU time | 33.451 s | 19.724 s | 1.70x |
+++| Complete measured wall time | 33.505 s | 19.760 s | 1.70x |
+++| Query and distance-sieve CPU time | 6.276 s | 0.345 s | 18.20x |
+++| CPA, association scoring, and retention CPU time | 27.215 s | 19.379 s | 1.40x |
+++| Worker peak resident memory | 498.375 MiB | 374.262 MiB | 24.9% less |
+++
+++Worker memory uses Linux `/proc/self/status` `VmHWM` after exec. It includes
+++imports and input loading. It excludes the comparison parent's memory, which
+++can otherwise contaminate `ru_maxrss` across fork/exec.
+++
+++The integrated code retains the existing scoring-cache implementation and adds
+++native-batch identity to prepared-input, fit-snapshot, and prefix-cache guards.
+++Changed custom hooks therefore invalidate reused fits and eliminated prefixes.
+++The recorded-input integration suite passed 143 checks plus four subtests;
+++three additional recorded hook regressions also passed.
+++
+++### Isolated contribution of this change
+++
++The clock-partition query bound removes impossible whole flight segments before
++JSON decoding, spline construction, distance checks, and closest-approach work.
++The dense recorded replay queried **197 segments instead of 4,694**. It preserved
++all **16,101 scored candidate payloads**, the **100 retained results**, and the
++complete retention records and trial evidence.
++
++Three alternating baseline/current pairs gave these median CPU times:
++
++| Scope | Baseline | This change | Speedup |
++| --- | ---: | ---: | ---: |
++| Index query and conservative distance sieve | 6.413 s | 0.398 s | 16.11x |
++| CPA, association scoring, and retention | 26.500 s | 24.964 s | 1.06x |
++| Complete measured hot path | 34.073 s | 25.356 s | 1.34x |
++
++Each row has its own median; stage medians need not sum to the total median.
++Total wall-time medians were 34.146 and 25.400 seconds. The baseline is
++`b5ff48fe0e9d6e6feac7a4faf5268630f0c85d8b`, which already includes native ridge and
++acoustic solvers and numeric flight decoding. Earlier gains are not credited here.
++
++This replay uses the original recording's 16 prepared event groups and 724 ridge
++samples, one source-informed receiver, a full-day regional observation index, and one existing
++348-second association ownership partition. It is a computation benchmark, not
++an independent location-accuracy or nationwide-throughput result.
++
++Timing includes index traversal, track decoding, the distance sieve, CPA
++processing, association scoring, and retention. It excludes imports, index
++metadata, event preparation, downloads, terrain sampling, audio discovery, and
++refinement. Both sources used the same Python 3.12 runtime, dependencies,
++single-thread numerical settings, CPU affinity, recorded inputs, and GCC flags.
++The measured numerical functions were not replaced by counting wrappers.
++
++## What changed
++
++### Select possible source times before decoding tracks
++
++Let the first matched passage imply recording start `A` inside the existing
++half-open partition `[L, U)`. Its clock interval has tolerance `t_first`.
++Each effective tolerance is `quality.cpa_tolerance_s + offset_tolerance_s / 2`.
++A later passage with implied start `B` and tolerance `t_event` can participate
++only if its interval intersects the first interval. Thus it lies within the
++partition expanded by both tolerances. Any event may be first, so the bound uses
++the maximum effective tolerance across every event.
++
++Add each event's audio-relative CPA time to bound receive time. Subtract at most
++`maximum_CPA_radius / sound_speed` to bound emission time. Every inverse operation
++rounds outward, including subtraction at Unix timestamp precision.
++
++The query selects **complete segments** touching this envelope. It never clips a
++trajectory, removes an interior sample, or changes its spline. Serial and worker
++paths derive the same anchor once and use it for both querying and scoring.
++Silence filters, noncanonical settings, invalid tolerances, and disjoint query
++windows keep the original broad query.
++
++On the dense replay:
++
++| Work | Baseline | This change |
++| --- | ---: | ---: |
++| Decoded flight segments | 4,694 | 197 |
++| Decoded source samples | 713,100 | 40,690 |
++| CPA rows | 944 | 95 |
++| Association work units | 302,818 | 147,985 |
++| Unsupported CPA track evaluations | 62 | 4 |
++
++Every removed unsupported track's complete emission interval misses the proven
++query envelope. All four retained unsupported reports remain identical. The
++590,900 unknown trials and 15,777 unclassified candidate assessments also remain
++unchanged. The replay remains incomplete where its original evidence was incomplete.
++
++### Fuse closest-approach neighbor calculations
++
++An optional C extension traverses the existing bounded spatial-neighbor graph.
++It combines squared distances, local-minimum selection, and adjacent-segment
++projection without creating expanded observer-index and coordinate arrays.
++It preserves arithmetic order, asymmetric minima, first-edge ties, and row order.
++Python still performs endpoint spline optimization and final row construction.
++
++Five alternating trials of ten iterations on eight archived ADS-B segments gave
++these full CPA-stage results, including preparation:
++
++| Receiver points | Baseline CPU per iteration | Native CPA | Speedup |
++| ---: | ---: | ---: | ---: |
++| 1 | 1.411 ms | 1.091 ms | 1.29x |
++| 32 | 3.598 ms | 2.767 ms | 1.30x |
++| 128 | 4.977 ms | 3.572 ms | 1.39x |
++| 1,024 | 15.017 ms | 10.183 ms | 1.47x |
++| 4,096 | 48.600 ms | 38.321 ms | 1.27x |
++
++All 205 baseline comparisons matched exactly. This small real-track workload
++measures the CPA implementation, not complete search throughput.
++
++The extension is optional. Unsupported layouts, floating-point policies, or
++exceptional arithmetic retain the Python path. Set
++`DOPPLERGEIST_CPA_BACKEND=python` before starting the process to select it explicitly.
++Checkpoints record the active backend. Existing implementation-mismatch checks
++require a new output directory for ordinary checkpoints from older code.
++The separately supported legacy continuation requires Python ridge, acoustic,
++and CPA backends.
++
++### Batch the unchanged native ridge fit
++
++The native ridge extension now accepts a matrix of clock trials. It reuses scratch
++storage and releases the GIL once per batch. Each row keeps the existing six IRLS
++iterations, pairwise reductions, box constraints, and output arithmetic.
++Custom scalar hooks, old extension binaries, unusual inputs, and failed probes
++retain the established fallback. Native output may not overlap its inputs.
++
++The dense replay uses 5,421 native batches for 227,378 trial rows, plus 862 scalar
++fits. Batching alone measured a modest 1.06x dense improvement; its sparse replay
++was approximately neutral. The main sieve gain comes from doing less impossible
++work before decoding.
++
++## Reproduce and validate
++
++Build the optional extensions with `CC=gcc python setup.py build_ext --inplace`.
++Use the recorded-input preparation in `.github/workflows/real-performance.yml`;
++it restores public observations and creates `benchmark-inputs/dense-query.json`.
++Build the same existing native extensions in the pinned baseline checkout.
++
++```bash
++OPENBLAS_NUM_THREADS=1 OMP_NUM_THREADS=1 MKL_NUM_THREADS=1 \
++python scripts/benchmark_prepared_measurements.py compare \
++ --baseline-source /path/to/baseline/src --current-source src \
++ --fixture benchmark-inputs/dense-query.json \
++ --index benchmark-inputs/reference-region.sqlite \
++ --include-track-query --clock-partition-query \
++ --require-current-native --require-current-native-acoustic \
++ --require-current-native-cpa --repeats 3 --output results/flight-sieve
++
++python scripts/benchmark_real_cpa_rows.py \
++ --baseline-source /path/to/baseline/src --points 1 32 128 1024 4096 \
++ --repeats 5 --iterations 10 --skip-reuse-comparison \
++ --output results/flight-cpa.json
++```
++
++Use one available CPU affinity for both arms when comparing elapsed time.
++The normal comparison requires exact complete output equality. Partition-query
++mode also requires exact scored outcomes, rankings, trial evidence, and retention.
++Only declared early-work counts may decrease; each removed unsupported interval
++must be outside the query. Repeated outputs must be deterministic within each arm.
++Profiler runs and numerical wrapper instrumentation cannot produce speed claims.
++
++The added recorded-input tests cover native arithmetic, buffer overlap, fallback
++behavior, outward clock boundaries, complete-track preservation, serial/worker
++query integration, and checkpoint backend identity. No generated recordings or
++aircraft tracks are used. The remaining dominant cost is association scoring.
diff --cc src/dopplergeist/_native_ridge.c
index b343ab3,4e3b5aa,6dfc964..b360b01
--- a/src/dopplergeist/_native_ridge.c
+++ b/src/dopplergeist/_native_ridge.c
@@@@ -1,14 -1,19 -1,8 +1,22 @@@@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <math.h>
++#include <stdint.h>
#include <string.h>
+typedef struct {
+ + PyObject *validation_keys[16];
++ unsigned long long scalar_calls, scalar_successes;
++ unsigned long long batch_calls, batch_rows, batch_successful_rows;
- } ridge_state;
+ +} 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)
{
@@@@ -224,60 -264,9 -253,9 +232,60 @@@@ fit_row(const Py_buffer *views, Py_ssiz
loss = -sqrt(loss + penalty);
valid = isfinite(loss) && isfinite(rmse) && isfinite(outlier_fraction);
}
-- Py_END_ALLOW_THREADS
++ output[0] = loss;
++ output[1] = f0;
++ output[2] = drift;
++ output[3] = rmse;
++ output[4] = outlier_fraction;
++ return valid;
++}
++
++static PyObject *
++fit_irls(PyObject *self, PyObject *args)
++{
- ridge_state *state = PyModule_GetState(self);
+++ RidgeModuleState *state = PyModule_GetState(self);
++ ++state->scalar_calls;
++ PyObject *objects[4];
++ double lower, upper, cap, sigma, output[5];
++ Py_ssize_t independent_samples;
++ Py_buffer views[4] = {{0}};
++ int acquired = 0;
++ double *effective = NULL;
++ PyObject *result = NULL;
-- if (valid) result = Py_BuildValue("(ddddd)", loss, f0, drift, rmse, outlier_fraction);
++ if (!PyArg_ParseTuple(args, "OOOOddddn", &objects[0], &objects[1],
++ &objects[2], &objects[3], &lower, &upper, &cap,
++ &sigma, &independent_samples)) return NULL;
++ for (int i = 0; i < 4; ++i) {
++ if (get_vector(objects[i], &views[i]) < 0) goto done;
++ ++acquired;
++ }
++ Py_ssize_t count = views[0].shape[0];
++ if (count == 0 || views[1].shape[0] != count || views[2].shape[0] != count ||
++ views[3].shape[0] != count || !isfinite(lower) || !isfinite(upper) ||
++ lower > upper || !isfinite(cap) || cap < 0.0 || !isfinite(sigma) ||
++ sigma <= 0.0 || independent_samples < 1) {
++ PyErr_SetString(PyExc_ValueError, "invalid robust ridge-fit inputs");
++ goto done;
++ }
++ if ((size_t)count > SIZE_MAX / (6 * sizeof(double))) {
++ PyErr_NoMemory();
++ goto done;
++ }
++ effective = PyMem_Malloc((size_t)count * 6 * sizeof(double));
++ if (effective == NULL) {
++ PyErr_NoMemory();
++ goto done;
++ }
++ int valid;
++ Py_BEGIN_ALLOW_THREADS
++ valid = fit_row(views, count, lower, upper, cap, sigma, independent_samples,
++ effective, output);
++ Py_END_ALLOW_THREADS
++ if (valid) {
++ ++state->scalar_successes;
++ result = Py_BuildValue("(ddddd)", output[0], output[1], output[2], output[3], output[4]);
++ }
else {
Py_INCREF(Py_None);
result = Py_None;
@@@@ -289,256 -278,179 -267,113 +297,322 @@@@ done
return result;
}
-/* This is a positive-only shortcut for the Python evidence validator. Any
- * noncanonical or invalid value returns False, so Python retains its original
- * error reason, ordering, subclass behavior and unbounded-integer semantics.
- * No fit, score threshold or association arithmetic is performed here. */
+ static int
-plain_string_dict(PyObject *object)
++get_matrix(PyObject *object, Py_buffer *view, int writable)
++{
++ int flags = PyBUF_FORMAT | PyBUF_ND | PyBUF_STRIDES;
++ if (writable) flags |= PyBUF_WRITABLE;
++ if (PyObject_GetBuffer(object, view, flags) < 0) return -1;
++ if (view->ndim != 2 || view->itemsize != (Py_ssize_t)sizeof(double) ||
++ view->shape == NULL || view->strides == NULL || view->format == NULL ||
++ strcmp(view->format, "d") != 0 || !PyBuffer_IsContiguous(view, 'C')) {
++ PyBuffer_Release(view);
++ view->obj = NULL;
++ PyErr_SetString(PyExc_TypeError, "expected a contiguous native float64 matrix");
++ return -1;
++ }
++ return 0;
++}
++
++/* Include the holes between strided elements. A conservative span is enough to
++ * prove that writing a result cannot change a later row's input. */
++static int
++buffer_span(const Py_buffer *view, uintptr_t *begin, uintptr_t *end)
++{
++ uintptr_t low = (uintptr_t)view->buf, high = low;
++ if (view->buf == NULL || view->itemsize <= 0) return 0;
++ for (int axis = 0; axis < view->ndim; ++axis) {
++ if (view->shape[axis] <= 0 ||
++ (view->suboffsets != NULL && view->suboffsets[axis] >= 0)) return 0;
++ Py_ssize_t stride = view->strides[axis];
++ uintptr_t magnitude = stride >= 0 ? (uintptr_t)stride :
++ (uintptr_t)(-(stride + 1)) + 1;
++ uintptr_t steps = (uintptr_t)(view->shape[axis] - 1);
++ if (magnitude != 0 && steps > UINTPTR_MAX / magnitude) return 0;
++ uintptr_t offset = steps * magnitude;
++ if (stride < 0) {
++ if (low < offset) return 0;
++ low -= offset;
++ } else {
++ if (high > UINTPTR_MAX - offset) return 0;
++ high += offset;
++ }
++ }
++ if (high > UINTPTR_MAX - (uintptr_t)view->itemsize) return 0;
++ *begin = low;
++ *end = high + (uintptr_t)view->itemsize;
++ return 1;
++}
++
++static PyObject *
++fit_irls_batch(PyObject *self, PyObject *args)
++{
- ridge_state *state = PyModule_GetState(self);
+++ RidgeModuleState *state = PyModule_GetState(self);
++ ++state->batch_calls;
++ PyObject *objects[6];
++ Py_buffer views[6] = {{0}};
++ double lower, upper, sigma;
++ Py_ssize_t independent_samples;
++ int acquired = 0, valid = 1;
++ double *scratch = NULL;
++ PyObject *result = NULL;
++ if (!PyArg_ParseTuple(args, "OOOOOdddnO", &objects[0], &objects[1], &objects[2],
++ &objects[3], &objects[4], &lower, &upper, &sigma,
++ &independent_samples, &objects[5])) return NULL;
++ for (int i = 0; i < 6; ++i) {
++ int status = (i < 2 || i == 5) ? get_matrix(objects[i], &views[i], i == 5) :
++ get_vector(objects[i], &views[i]);
++ if (status < 0) goto done;
++ ++acquired;
++ }
++ Py_ssize_t rows = views[0].shape[0], count = views[0].shape[1];
++ if (rows == 0 || count == 0 || views[1].shape[0] != rows || views[1].shape[1] != count ||
++ views[2].shape[0] != count || views[3].shape[0] != count || views[4].shape[0] != rows ||
++ views[5].shape[0] != rows || views[5].shape[1] != 5 ||
++ !isfinite(lower) || !isfinite(upper) || lower > upper ||
++ !isfinite(sigma) || sigma <= 0.0 || independent_samples < 1) {
++ PyErr_SetString(PyExc_ValueError, "invalid robust ridge-fit batch inputs");
++ goto done;
++ }
++ uintptr_t output_begin, output_end;
++ if (!buffer_span(&views[5], &output_begin, &output_end)) {
++ PyErr_SetString(PyExc_ValueError, "unsupported robust ridge-fit output buffer");
++ goto done;
++ }
++ for (int i = 0; i < 5; ++i) {
++ uintptr_t input_begin, input_end;
++ if (!buffer_span(&views[i], &input_begin, &input_end) ||
++ (output_begin < input_end && input_begin < output_end)) {
++ PyErr_SetString(PyExc_ValueError, "robust ridge-fit output must not overlap inputs");
++ goto done;
++ }
++ }
++ if ((size_t)count > SIZE_MAX / (6 * sizeof(double))) {
++ PyErr_NoMemory();
++ goto done;
++ }
++ scratch = PyMem_Malloc((size_t)count * 6 * sizeof(double));
++ if (scratch == NULL) {
++ PyErr_NoMemory();
++ goto done;
++ }
++ Py_buffer row_views[4] = {views[0], views[1], views[2], views[3]};
++ row_views[0].strides = views[0].strides + 1;
++ row_views[1].strides = views[1].strides + 1;
++ state->batch_rows += (unsigned long long)rows;
++ Py_BEGIN_ALLOW_THREADS
++ for (Py_ssize_t row = 0; row < rows; ++row) {
++ double cap = at(&views[4], row), output[5];
++ if (!isfinite(cap) || cap < 0.0) {
++ valid = 0;
++ break;
++ }
++ row_views[0].buf = (char *)views[0].buf + row * views[0].strides[0];
++ row_views[1].buf = (char *)views[1].buf + row * views[1].strides[0];
++ if (!fit_row(row_views, count, lower, upper, cap, sigma,
++ independent_samples, scratch, output)) {
++ valid = 0;
++ break;
++ }
++ memcpy((char *)views[5].buf + row * views[5].strides[0], output, sizeof(output));
++ }
++ Py_END_ALLOW_THREADS
++ if (valid) state->batch_successful_rows += (unsigned long long)rows;
++ result = PyBool_FromLong(valid);
++done:
++ PyMem_Free(scratch);
++ for (int i = 0; i < acquired; ++i) PyBuffer_Release(&views[i]);
++ return result;
++}
++
++static PyObject *
++fit_statistics(PyObject *self, PyObject *unused)
++{
- ridge_state *state = PyModule_GetState(self);
+++ RidgeModuleState *state = PyModule_GetState(self);
++ return Py_BuildValue("{sK,sK,sK,sK,sK}",
++ "scalar_calls", state->scalar_calls,
++ "scalar_successes", state->scalar_successes,
++ "batch_calls", state->batch_calls,
++ "batch_rows", state->batch_rows,
++ "batch_successful_rows", state->batch_successful_rows);
++}
++
- /* This is a positive-only shortcut for the Python evidence validator. Any
- * noncanonical or invalid value returns False, so Python retains its original
- * error reason, ordering, subclass behavior and unbounded-integer semantics.
- * No fit, score threshold or association arithmetic is performed here. */
+ +/* 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_string_dict(PyObject *object)
+ +plain_dictionary(PyObject *object)
{
if (!PyDict_CheckExact(object)) return 0;
- - PyObject *key, *value;
Py_ssize_t position = 0;
+ + PyObject *key, *value;
while (PyDict_Next(object, &position, &key, &value)) {
if (!PyUnicode_CheckExact(key)) return 0;
}
return 1;
}
- -static int
- -exact_finite_float(PyObject *object, double *value)
+ +static PyObject *
+ +sequence_item(PyObject *sequence, Py_ssize_t index)
{
- - if (object == NULL || !PyFloat_CheckExact(object)) return 0;
- - *value = PyFloat_AS_DOUBLE(object);
- - return isfinite(*value);
+ + return PyList_CheckExact(sequence) ? PyList_GET_ITEM(sequence, index) :
+ + PyTuple_GET_ITEM(sequence, index);
}
- -static PyObject *
- -valid_ridge_events(PyObject *self, PyObject *args)
+ +/* Return 0 for valid evidence, a positive first-failure code, or -1 for
+ + * Python fallback. Both public native interfaces share the same interned keys
+ + * and exact-type guards. No evidence is cached or trusted because a fitter
+ + * produced it; every call checks its current values. */
+ +static int
+ +ridge_event_reason(RidgeModuleState *state, PyObject *diagnostics, PyObject *scores,
+ + PyObject *frequencies, PyObject *events)
{
- - PyObject *diagnostics, *scores, *frequencies, *events;
- - if (!PyArg_ParseTuple(args, "OOOO", &diagnostics, &scores, &frequencies, &events)) {
- - return NULL;
- - }
if (!PyList_CheckExact(diagnostics) || !PyList_CheckExact(scores) ||
- - !PyList_CheckExact(frequencies) || !PyList_CheckExact(events)) Py_RETURN_FALSE;
- - Py_ssize_t count = PyList_GET_SIZE(events);
- - if (PyList_GET_SIZE(diagnostics) != count || PyList_GET_SIZE(scores) != count ||
- - PyList_GET_SIZE(frequencies) != count) Py_RETURN_FALSE;
- -
- - static const char *fields[] = {
- - "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"
- - };
- - for (Py_ssize_t index = 0; index < count; ++index) {
- - PyObject *diagnostic = PyList_GET_ITEM(diagnostics, index);
- - PyObject *event = PyList_GET_ITEM(events, index);
- - if (!plain_string_dict(diagnostic) || !plain_string_dict(event)) Py_RETURN_FALSE;
- - double values[10], score, frequency;
- - for (int field = 0; field < 10; ++field) {
- - if (!exact_finite_float(PyDict_GetItemString(diagnostic, fields[field]),
- - &values[field])) Py_RETURN_FALSE;
+ + !PyList_CheckExact(frequencies) ||
+ + !(PyList_CheckExact(events) || PyTuple_CheckExact(events))) return -1;
+ + 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) return -1;
+ +
+ + 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)) {
+ + return -1;
}
- - if (!exact_finite_float(PyList_GET_ITEM(scores, index), &score) ||
- - !exact_finite_float(PyList_GET_ITEM(frequencies, index), &frequency)) Py_RETURN_FALSE;
- - if (values[0] != 1.0 || values[6] <= 0.0 || values[7] < 0.0 || values[8] < 0.0 ||
- - values[9] < 0.0 || values[9] > 1.0 || values[2] <= 0.0 || values[1] > 0.0 ||
- - score != values[1] || frequency != values[2]) Py_RETURN_FALSE;
- -
- - PyObject *samples = PyDict_GetItemString(diagnostic, "sample_count");
- - PyObject *independent = PyDict_GetItemString(diagnostic, "independent_sample_count");
- - if (samples == NULL || independent == NULL || !PyLong_CheckExact(samples) ||
- - !PyLong_CheckExact(independent)) Py_RETURN_FALSE;
- - Py_ssize_t n = PyLong_AsSsize_t(samples);
- - if (n == -1 && PyErr_Occurred()) {
- - PyErr_Clear();
- - Py_RETURN_FALSE;
+ + 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)) return -1;
+ + values[j] = PyFloat_AS_DOUBLE(value);
}
- - Py_ssize_t k = PyLong_AsSsize_t(independent);
- - if (k == -1 && PyErr_Occurred()) {
- - PyErr_Clear();
- - Py_RETURN_FALSE;
+ + 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)) return -1;
+ + Py_ssize_t sample_count = PyLong_AsSsize_t(samples);
+ + if (PyErr_Occurred()) { PyErr_Clear(); return -1; }
+ + Py_ssize_t independent_count = PyLong_AsSsize_t(independent);
+ + if (PyErr_Occurred()) { PyErr_Clear(); return -1; }
+ + PyObject *optimizer = PyDict_GetItem(diagnostic, state->validation_keys[12]);
+ + if (optimizer != NULL && !PyBool_Check(optimizer)) return -1;
+ + PyObject *bounds = PyDict_GetItem(event, state->validation_keys[13]);
+ + if (bounds == NULL || !(PyList_CheckExact(bounds) || PyTuple_CheckExact(bounds))) {
+ + return -1;
}
- - if (n < 10 || k < 1 || k > n) Py_RETURN_FALSE;
- - PyObject *optimizer = PyDict_GetItemString(diagnostic, "optimizer_success");
- - if (optimizer != NULL && optimizer != Py_True) Py_RETURN_FALSE;
- -
- - PyObject *bounds = PyDict_GetItemString(event, "f0_range_hz");
- - if (bounds == NULL || !(PyList_CheckExact(bounds) || PyTuple_CheckExact(bounds))) Py_RETURN_FALSE;
- - int tuple_bounds = PyTuple_CheckExact(bounds);
- - Py_ssize_t bounds_count = tuple_bounds ? PyTuple_GET_SIZE(bounds) : PyList_GET_SIZE(bounds);
- - if (bounds_count != 2) Py_RETURN_FALSE;
- - double lower, upper, fraction = 0.02;
- - if (!exact_finite_float(tuple_bounds ? PyTuple_GET_ITEM(bounds, 0) : PyList_GET_ITEM(bounds, 0), &lower) ||
- - !exact_finite_float(tuple_bounds ? PyTuple_GET_ITEM(bounds, 1) : PyList_GET_ITEM(bounds, 1), &upper)) Py_RETURN_FALSE;
- - PyObject *quality = PyDict_GetItemString(event, "quality");
+ + Py_ssize_t size = PyList_CheckExact(bounds) ? PyList_GET_SIZE(bounds) : PyTuple_GET_SIZE(bounds);
+ + if (size != 2) return -1;
+ + PyObject *low_object = sequence_item(bounds, 0), *high_object = sequence_item(bounds, 1);
+ + if (!PyFloat_CheckExact(low_object) || !PyFloat_CheckExact(high_object)) return -1;
+ + 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_string_dict(quality)) Py_RETURN_FALSE;
- - PyObject *configured = PyDict_GetItemString(quality, "source_drift_fraction");
- - if (configured != NULL && !exact_finite_float(configured, &fraction)) Py_RETURN_FALSE;
+ + if (!plain_dictionary(quality)) return -1;
+ + PyObject *configured = PyDict_GetItem(quality, state->validation_keys[15]);
+ + if (configured != NULL) {
+ + if (!PyFloat_CheckExact(configured)) return -1;
+ + 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 1;
+ + int finite = isfinite(score) && isfinite(frequency);
+ + for (int j = 1; j < 10; ++j) finite = finite && isfinite(values[j]);
+ + if (!finite) return 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 3;
}
- - if (!(0.0 < lower && lower <= upper && lower <= frequency && frequency <= upper &&
- - 0.0 <= fraction && fraction <= 0.1)) Py_RETURN_FALSE;
+ + if (optimizer == Py_False) return 4;
+ + if (score != values[1] || frequency != values[2]) return 5;
+ + if (!isfinite(low) || !isfinite(high) || !(0.0 < low && low <= high) ||
+ + !(low <= frequency && frequency <= high) || !isfinite(fraction) ||
+ + !(0.0 <= fraction && fraction <= 0.1)) return 6;
double drift = values[3], cap = values[7];
- - if (!(lower * fraction <= cap && cap <= upper * fraction) || fabs(drift) > cap) Py_RETURN_FALSE;
- - if (fabs(values[4] * values[6] - drift) > 1e-12 + 1e-12 * fabs(drift)) Py_RETURN_FALSE;
+ + if (!(low * fraction <= cap && cap <= high * fraction) || fabs(drift) > cap) {
+ + return 7;
+ + }
+ + if (fabs(values[4] * values[6] - drift) > 1e-12 + 1e-12 * fabs(drift)) {
+ + return 8;
+ + }
+ + }
+ + return 0;
+ +}
+ +
+ +/* Diagnostic compatibility API: preserve first-failure codes and tuple events.
+ + * Score summation and association arithmetic remain in Python. */
+ +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;
+ }
- Py_RETURN_TRUE;
+ + int reason = ridge_event_reason(state, diagnostics, scores, frequencies, events);
+ + if (reason < 0) Py_RETURN_NONE;
+ + return PyLong_FromLong(reason);
+ +}
+ +
+ +/* Positive-only upstream API. Invalid or noncanonical inputs retain Python's
+ + * original failure ordering, custom behavior, and unbounded-integer semantics.
+ + * Its outer event container remains an exact list, as on upstream main. */
+ +static PyObject *
+ +valid_ridge_events(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;
+ }
- Py_RETURN_TRUE;
+ + if (!PyList_CheckExact(events)) Py_RETURN_FALSE;
+ + return PyBool_FromLong(ridge_event_reason(state, diagnostics, scores, frequencies, events) == 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."},
++ {"fit_irls_batch", fit_irls_batch, METH_VARARGS,
++ "Run the same robust fit for contiguous clock rows with shared scratch storage."},
++ {"fit_statistics", fit_statistics, METH_NOARGS, "Read native fit operation counters."},
{"valid_ridge_events", valid_ridge_events, METH_VARARGS,
"Recognize valid canonical ridge evidence; other inputs retain Python validation."},
{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, sizeof(ridge_state), methods
- 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
diff --cc src/dopplergeist/batch_ridge.py
index f1caf0f,3eae4b6,d9bc57a..3553bdc
--- a/src/dopplergeist/batch_ridge.py
+++ b/src/dopplergeist/batch_ridge.py
@@@@ -93,11 -105,11 -93,11 +105,14 @@@@ def _cached_first_event_fits(measuremen
if cache is None or cache.max_entries < 1 or cache.batch_fit_limit <= 0:
result = kernel(measurement, event, factors, emissions, **options)
return result if ceiling_limit is None else (result, None)
--- # A changed fitter must not reuse its predecessor's snapshots.
--- if cache.batch_fit_function is not kernel:
+++ # A changed Python or native fitter must not reuse its predecessor's snapshots.
+++ if (cache.batch_fit_function is not kernel
+++ or cache.batch_fit_backends[0] is not matching._native_fit_irls
+++ or cache.batch_fit_backends[1] is not matching._native_fit_irls_batch):
cache.batch_fit_entries.clear()
cache.batch_fit_bytes = 0
cache.batch_fit_function = kernel
+++ cache.batch_fit_backends = (matching._native_fit_irls, matching._native_fit_irls_batch)
try:
key = _batch_fit_key(measurement, event, factors, emissions)
if legacy_arithmetic:
diff --cc src/dopplergeist/checkpoint_resume.py
index 0e58d94,5ff63b4,31fbf1a..69bb4ec
--- a/src/dopplergeist/checkpoint_resume.py
+++ b/src/dopplergeist/checkpoint_resume.py
@@@@ -47,8 -47,6 -78,12 +78,14 @@@@ def upgrade_legacy_job(directory)
raise ValueError("Legacy checkpoint continuation requires the Python ridge fitter")
if geometry.ACOUSTIC_BACKEND != "python-acoustic-v1":
raise ValueError("Legacy checkpoint continuation requires the Python acoustic solver")
++ if native_cpa.CPA_BACKEND != "python-cpa-v1":
++ raise ValueError("Legacy checkpoint continuation requires Python closest-approach processing")
++ from .provenance import implementation_files
++ package_root = Path(__file__).parent
++ code = {p.relative_to(package_root).as_posix(): _digest(p)
++ for p in implementation_files(package_root, recursive=False)}
++ if previous_upgrade and any(code.get(name) != digest for name, digest in THROUGHPUT_SOURCES.items()):
++ return None
if (old["scoring"].get("mode") != "robust_ridge" or old.get("coarse_floor_policy") != "per_tile"
or old["scoring"].get("time_origin") is not None
or old.get("filters", {}).get("association") != "subsets"):
diff --cc src/dopplergeist/matching.py
index f3baa7c,3c408d5,88afdcd..869c9a7
--- a/src/dopplergeist/matching.py
+++ b/src/dopplergeist/matching.py
@@@@ -40,8 -40,7 -40,6 +40,9 @@@@ else
RIDGE_FIT_BACKEND = f"native-c-irls-v2-pairwise:{_native_binary_hash}"
_native_validate_ridge_events = getattr(_native_ridge_module, "valid_ridge_events", None)
+ +_CANONICAL_NATIVE_EVENT_VALIDATOR = _native_validate_ridge_events
++_native_fit_irls_batch = getattr(_native_ridge_module, "fit_irls_batch", None)
++_CANONICAL_NATIVE_FIT_IRLS = _native_fit_irls
_NATIVE_RIDGE_ERROR_POLICY = {"divide": "warn", "over": "warn", "under": "ignore", "invalid": "warn"}
@@@@ -85,6 -84,6 -83,6 +86,7 @@@@ class _RidgeFitCache
self.batch_fit_bytes = 0
self.batch_fit_limit = 4 * 1024 * 1024
self.batch_fit_function = None
+++ self.batch_fit_backends = (None, None)
self.ceiling_entries = OrderedDict()
self.screen_entries = OrderedDict()
self.screen_bytes = 0
@@@@ -867,3 -883,19 -865,3 +886,20 @@@@ def profile_candidate(candidate, tracks
"clock_bounds_s": [lo, hi]}}
return dict(nominal, sensitivity=sensitivity)
+ +
+ +
+ +# Captured when the module initializes, before callers can install custom hooks.
+ +_CANONICAL_SCORE_INPUT_FUNCTIONS = (_frequency_scale, _ridge_fit_inputs,
+ + _prepare_ridge_fit_inputs, _fit_argument_key)
+ +_CANONICAL_FIT_RIDGE = fit_ridge
+ +_CANONICAL_SCALAR_FITTER = _fit_ridge_uncached
+ +_CANONICAL_ACOUSTIC_TRIALS = _iter_acoustic_trials
+ +_CANONICAL_NATIVE_IRLS = _native_fit_irls
+++_CANONICAL_NATIVE_IRLS_BATCH = _native_fit_irls_batch
+ +_CANONICAL_PREFIX_FUNCTIONS = (
+ + fit_ridge, _fit_ridge_uncached, _iter_acoustic_trials, _source_fit,
- _RidgeFitCache.fit, _RidgeFitCache.acoustic_batch, _native_fit_irls,
+++ _RidgeFitCache.fit, _RidgeFitCache.acoustic_batch, _native_fit_irls, _native_fit_irls_batch,
+ + _native_ridge_module, getattr(_native_ridge_module, "event_fit_reason", None))
+ +
+ +from .scoring_inputs import (_prepare_score_inputs, _prepared_score_input,
+ + _prepared_score_token, _prepared_score_tokens)
diff --cc src/dopplergeist/scoring_inputs.py
index 0000000,bf477c6,0000000..5cadc3b
mode 000000,100644,000000..100644
--- a/src/dopplergeist/scoring_inputs.py
+++ b/src/dopplergeist/scoring_inputs.py
@@@@ -1,0 -1,197 -1,0 +1,198 @@@@
+ +"""Private immutable numerical inputs for one bounded point-scoring call."""
+ +import hashlib
+ +import math
+ +import pickle
+ +import sys
+ +
+ +import numpy as np
+ +
+ +_MISSING = object()
+ +_FLOAT64 = np.dtype("float64")
+ +_SETTING_KEYS = ("frequency_loss_scale_hz", "frequency_sigma_hz",
+ + "frequency_uncertainty_hz", "source_drift_fraction")
+ +
+ +
+ +def _plain_copy(value, remaining):
+ + """Copy JSON-like builtin containers without invoking custom copy hooks."""
+ + # Live discovery's support_fraction can be an immutable NumPy float64.
+ + # Keep its exact type and bits; serializing it first would hide this case.
+ + if type(value) not in (str, float, int, bool, type(None), np.float64, list, tuple, dict):
+ + raise TypeError("Custom scoring input retains its original path")
+ + remaining[0] -= sys.getsizeof(value)
+ + if remaining[0] < 0:
+ + raise TypeError("Prepared scoring input budget exceeded")
+ + if type(value) in (str, float, int, bool, type(None), np.float64):
+ + return value
+ + if type(value) in (list, tuple):
+ + copied = [_plain_copy(item, remaining) for item in value]
+ + return copied if type(value) is list else tuple(copied)
+ + if type(value) is dict and all(type(key) is str for key in value):
+ + return {_plain_copy(key, remaining): _plain_copy(item, remaining) for key, item in value.items()}
+ + raise TypeError("Custom scoring input retains its original path")
+ +
+ +
+ +def _immutable(values):
+ + # A write-disabled owning array can be made writable again. Bytes backing
+ + # prevents that, so mutable callers cannot alter a prepared numeric value.
+ + return np.frombuffer(values.tobytes(), dtype=values.dtype).reshape(values.shape)
+ +
+ +
+ +class _PreparedScoreInput:
+ + def __init__(self, event, measurement, sigma, fraction, size, argument_key):
+ + self.event, self.measurement = event, measurement
+ + self.arrays = (measurement[0].time, measurement[1], measurement[2])
+ + self.shapes = tuple(array.shape for array in self.arrays)
+ + self.strides = tuple(array.strides for array in self.arrays)
+ + self.bases = tuple(array.base for array in self.arrays)
+ + self.bounds = event["f0_range_hz"]
+ + self.bound_values = tuple(self.bounds)
+ + self.quality = event["quality"]
+ + self.settings = tuple(self.quality.get(key, _MISSING) for key in _SETTING_KEYS)
+ + self.sigma, self.fraction, self.size = sigma, fraction, size
+ + times, ridge, weight = self.arrays
+ + self.measured = _immutable(np.isfinite(times) & np.isfinite(ridge) & (weight > 0))
+ + self.selected = tuple(_immutable(array[self.measured]) for array in self.arrays)
+ + self.sample_count = len(self.selected[0])
+ + self.independent = len(np.unique(self.selected[0]))
+ + # Preserve the existing keys byte for byte. Only their invariant prefix
+ + # is prepared; each mutable acoustic batch is still hashed in full.
+ + observed, batch = hashlib.sha256(), hashlib.sha256()
+ + settings = tuple(argument_key(value) for value in (sigma, fraction, self.bounds))
+ + batch.update(pickle.dumps(settings, protocol=5))
+ + for array in self.arrays:
+ + values = array.tobytes()
+ + observed.update(repr(array.shape).encode())
+ + observed.update(values)
+ + layout = (array.dtype.str, array.shape, array.strides, array.flags.aligned)
+ + batch.update(pickle.dumps(layout, protocol=5))
+ + batch.update(values)
+ + self.measurement_key, self.batch_digest = observed.digest(), batch
+ +
+ + def current(self, measurement, event):
+ + if (measurement is not self.measurement or event is not self.event
+ + or measurement[0].time is not self.arrays[0]
+ + or event.get("quality") is not self.quality
+ + or event.get("f0_range_hz") is not self.bounds or len(self.bounds) != 2
+ + or any(value is not original for value, original in zip(self.bounds, self.bound_values))
+ + or any(self.quality.get(key, _MISSING) is not original
+ + for key, original in zip(_SETTING_KEYS, self.settings))):
+ + return False
+ + # NumPy permits changing view metadata even when the bytes are immutable.
+ + # Retain layout guards as well as the immutable backing and object IDs.
+ + return all(array.dtype == _FLOAT64 and array.shape == shape and array.strides == stride
+ + and array.base is base and not array.flags.writeable
+ + for array, shape, stride, base in zip(self.arrays, self.shapes, self.strides, self.bases))
+ +
+ +
+ +def _canonical_functions(matching):
+ + return (all(current is original for current, original in zip(
+ + (matching._frequency_scale, matching._ridge_fit_inputs,
+ + matching._prepare_ridge_fit_inputs, matching._fit_argument_key),
+ + matching._CANONICAL_SCORE_INPUT_FUNCTIONS))
+ + and matching.fit_ridge is matching._CANONICAL_FIT_RIDGE
+ + and matching._fit_ridge_uncached is matching._CANONICAL_SCALAR_FITTER
+ + and matching._iter_acoustic_trials is matching._CANONICAL_ACOUSTIC_TRIALS
+ + and matching._source_fit is matching._CANONICAL_PREFIX_FUNCTIONS[3]
- and matching._native_fit_irls is matching._CANONICAL_NATIVE_IRLS)
+++ and matching._native_fit_irls is matching._CANONICAL_NATIVE_IRLS
+++ and matching._native_fit_irls_batch is matching._CANONICAL_NATIVE_IRLS_BATCH)
+ +
+ +
+ +def _prepare_score_inputs(events, measurements):
+ + """Snapshot supported ordinary inputs; other inputs keep public behavior.
+ +
+ + Preparation never raises a validation error before the original scorer
+ + would reach that input. A malformed/custom input simply stays on its old
+ + path. Memory is bounded by the existing scoring context's separate budget.
+ + """
+ + # A lazy dependency also supports importing scoring_inputs first.
+ + from . import matching
+ +
+ + cache = matching._ridge_fit_cache.get()
+ + if (cache is None or type(events) not in (list, tuple)
+ + or type(measurements) not in (list, tuple) or len(events) != len(measurements)
+ + or not _canonical_functions(matching) or np.geterr() != matching._NATIVE_RIDGE_ERROR_POLICY):
+ + return events, measurements
+ + copied_events, copied_measurements = list(events), list(measurements)
+ + for index, (event, measurement) in enumerate(zip(events, measurements)):
+ + if (type(event) is not dict or not all(type(key) is str for key in event)
+ + or type(event.get("quality")) is not dict
+ + or not all(type(key) is str for key in event["quality"])
+ + or any(type(event["quality"].get(key, 0.0)) not in (float, int, bool)
+ + for key in _SETTING_KEYS)
+ + or type(measurement) not in (list, tuple) or len(measurement) != 4
+ + or type(measurement[0]) is not matching.RidgeTimes):
+ + continue
+ + bounds = event.get("f0_range_hz")
+ + if (type(bounds) not in (list, tuple) or len(bounds) != 2
+ + or not all(type(value) is float and math.isfinite(value) for value in bounds)
+ + or not 0 < bounds[0] <= bounds[1]):
+ + continue
+ + arrays = (measurement[0].time, measurement[1], measurement[2])
+ + if any(type(array) is not np.ndarray or array.dtype != _FLOAT64 or array.ndim != 1
+ + or array.strides != (array.itemsize,)
+ + or not array.flags.c_contiguous or not array.flags.aligned for array in arrays):
+ + continue
+ + available = cache.score_input_limit - cache.score_input_bytes
+ + size = 4 * sum(array.nbytes for array in arrays) + 2048
+ + if size > available or len(cache.score_inputs) >= cache.max_entries:
+ + continue
+ + try:
+ + sigma = matching._frequency_scale(event)
+ + fraction = event["quality"].get("source_drift_fraction", .02)
+ + if (type(sigma) is not float or type(fraction) is not float
+ + or not math.isfinite(sigma) or not math.isfinite(3*sigma) or sigma <= 0
+ + or not math.isfinite(fraction) or not 0 <= fraction <= .1):
+ + continue
+ + matching._ridge_fit_inputs(measurement, sigma, fraction)
+ + remaining = [available - size]
+ + copied_event = _plain_copy(event, remaining)
+ + metadata = _plain_copy(measurement[3], remaining)
+ + frozen = tuple(_immutable(array) for array in arrays)
+ + copied = (matching.RidgeTimes(frozen[0]), frozen[1], frozen[2], metadata)
+ + size += available - size - remaining[0]
+ + prepared = _PreparedScoreInput(copied_event, copied, sigma, fraction, size,
+ + matching._fit_argument_key)
+ + except (TypeError, ValueError, KeyError, AttributeError, OverflowError, RecursionError):
+ + continue
+ + cache.score_inputs[id(copied)] = prepared
+ + cache.score_input_bytes += size
+ + copied_events[index], copied_measurements[index] = copied_event, copied
+ + return copied_events, copied_measurements
+ +
+ +
+ +def _prepared_score_input(measurement, event=None):
+ + from . import matching
+ +
+ + cache = matching._ridge_fit_cache.get()
+ + if cache is None:
+ + return None
+ + prepared = cache.score_inputs.get(id(measurement))
+ + if (prepared is not None and _canonical_functions(matching)
+ + and np.geterr() == matching._NATIVE_RIDGE_ERROR_POLICY
+ + and prepared.current(measurement, prepared.event if event is None else event)):
+ + return prepared
+ + return None
+ +
+ +
+ +def _prepared_score_token(measurement, event):
+ + """Return an immutable identity only for this scope's unchanged snapshots."""
+ + return _prepared_score_input(measurement, event)
+ +
+ +
+ +def _prepared_score_tokens(measurements, events):
+ + """Validate every snapshot while checking shared execution settings once."""
+ + from . import matching
+ +
+ + cache = matching._ridge_fit_cache.get()
+ + if (cache is None or not cache.score_inputs
+ + or type(measurements) not in (list, tuple) or type(events) not in (list, tuple)
+ + or len(measurements) != len(events) or not _canonical_functions(matching)
+ + or np.geterr() != matching._NATIVE_RIDGE_ERROR_POLICY):
+ + return None
+ + tokens = []
+ + for measurement, event in zip(measurements, events):
+ + prepared = cache.score_inputs.get(id(measurement))
+ + if prepared is None or not prepared.current(measurement, event):
+ + return None
+ + tokens.append(prepared)
+ + return tuple(tokens)
diff --cc src/dopplergeist/search.py
index 45430f1,5271dda,54f689f..362e3dc
--- a/src/dopplergeist/search.py
+++ b/src/dopplergeist/search.py
@@@@ -8,9 -9,8 -8,8 +9,9 @@@@ from scipy.optimize import minimize_sca
from scipy.signal import find_peaks
from scipy.spatial import cKDTree
- -from .geometry import AcousticSupportError, ecef
- -from .matching import _native_validate_ridge_events
+ +from .geometry import AcousticSupportError, _CANONICAL_ECEF, ecef
+ +from .matching import _CANONICAL_NATIVE_EVENT_VALIDATOR, _native_validate_ridge_events
++from .native_cpa import try_cpa_edges
_CPA_COLUMNS = ["point_id", "flight_id", "emission_time", "receive_time", "distance_m", "speed_mps"]
_CPA_DTYPE = np.dtype([(name, "int64" if name == "point_id" else object if name == "flight_id" else "float64")
diff --cc src/dopplergeist/trial_prefix.py
index 0000000,0f3651e,0000000..47bab79
mode 000000,100644,000000..100644
--- a/src/dopplergeist/trial_prefix.py
+++ b/src/dopplergeist/trial_prefix.py
@@@@ -1,0 -1,228 -1,0 +1,228 @@@@
+ +"""Bounded reuse of fully eliminated association prefixes in one score call."""
+ +from collections import OrderedDict
+ +import inspect
+ +import math
+ +import sys
+ +from types import GeneratorType
+ +
+ +import numpy as np
+ +
+ +
+ +_MAX_ENTRIES = 4096
+ +_MAX_BYTES = 8 * 1024 * 1024
+ +_COUNTS = dict(lookups=0, hits=0, residual_hits=0, stored=0, evicted=0,
+ + saved_event_steps=0, saved_trials=0)
+ +_TRACK_METHODS = ("acoustic", "position", "speed_vector", "_position_velocity",
+ + "_check_motion_support", "_check_motion_support_scalar",
+ + "_unsupported_motion_intervals")
+ +_OUTCOME_SIZE = sys.getsizeof((None, 0.0, None)) + sys.getsizeof(0.0) + 2 * sys.getsizeof(None)
+ +
+ +
+ +def cache_stats():
+ + """Read process-local operation counters without wrapping numerical code."""
+ + from .matching import _ridge_fit_cache
+ +
+ + owner = _ridge_fit_cache.get()
+ + memo = getattr(owner, "prefix_memo", None)
+ + return dict(_COUNTS, entries=0 if memo is None else len(memo.entries),
+ + bytes=0 if memo is None else memo.size + sys.getsizeof(memo.entries))
+ +
+ +
+ +def _canonical():
+ + # Import inside the call so direct imports of the helper introduce no
+ + # search/matching initialization cycle. Originals live in their providers.
+ + from . import acoustic_screen, batch_ridge, geometry, matching, native_acoustic, search
+ +
+ + current = (matching.fit_ridge, matching._fit_ridge_uncached,
+ + matching._iter_acoustic_trials, matching._source_fit,
+ + matching._RidgeFitCache.fit, matching._RidgeFitCache.acoustic_batch,
- matching._native_fit_irls, matching._native_ridge_module,
+++ matching._native_fit_irls, matching._native_fit_irls_batch, matching._native_ridge_module,
+ + getattr(matching._native_ridge_module, "event_fit_reason", None))
+ + batch = (batch_ridge.first_event_fits, batch_ridge._first_event_fits_with_ceilings,
+ + batch_ridge._cached_first_event_fits, batch_ridge._first_event_fits_uncached,
+ + batch_ridge._fit_diagnostics, batch_ridge._batch_fit_key,
+ + batch_ridge._frequency_scale, batch_ridge._ridge_fit_inputs,
+ + batch_ridge._fit_ridge_uncached)
+ + screen = (acoustic_screen.ridge_trial_rejections, acoustic_screen._cached_loss_lower_bounds,
+ + acoustic_screen._validated_loss_lower_bounds, acoustic_screen._loss_lower_bounds,
+ + acoustic_screen._frequency_scale)
+ + track = tuple(getattr(geometry.Track, name) for name in _TRACK_METHODS)
+ + track += (geometry._try_native_acoustic, geometry._packed_kinematics)
+ + ceiling = (search._association_event_ceiling, search._association_event_ceiling_uncached,
+ + search._ridge_trial_classification)
+ + groups = ((current, getattr(matching, "_CANONICAL_PREFIX_FUNCTIONS", ())),
+ + (batch, getattr(batch_ridge, "_CANONICAL_PREFIX_FUNCTIONS", ())),
+ + (screen, acoustic_screen._CANONICAL_SCREEN_FUNCTIONS),
+ + (track, geometry._CANONICAL_ACOUSTIC_FUNCTIONS),
+ + (ceiling, search._CANONICAL_CEILING_FUNCTIONS))
+ + return (all(len(now) == len(original) and all(a is b for a, b in zip(now, original))
+ + for now, original in groups)
+ + # Importing a consumer while matching is patched can retain a
+ + # custom alias after matching itself has been restored. Compare
+ + # imported helpers with the defining provider's original as well.
+ + and acoustic_screen._frequency_scale is matching._CANONICAL_SCORE_INPUT_FUNCTIONS[0]
+ + and batch_ridge._frequency_scale is matching._CANONICAL_SCORE_INPUT_FUNCTIONS[0]
+ + and batch_ridge._ridge_fit_inputs is matching._CANONICAL_SCORE_INPUT_FUNCTIONS[1]
+ + and batch_ridge._fit_argument_key is matching._CANONICAL_SCORE_INPUT_FUNCTIONS[3]
+ + and batch_ridge._fit_ridge_uncached is matching._CANONICAL_SCALAR_FITTER
+ + and matching._native_validate_ridge_events is matching._CANONICAL_NATIVE_EVENT_VALIDATOR
+ + and search._native_validate_ridge_events is matching._CANONICAL_NATIVE_EVENT_VALIDATOR
+ + and getattr(matching._native_ridge_module, "valid_ridge_events", None)
+ + is matching._CANONICAL_NATIVE_EVENT_VALIDATOR
+ + and native_acoustic._native is native_acoustic._CANONICAL_NATIVE
+ + and getattr(native_acoustic._native, "solve", None) is native_acoustic._CANONICAL_SOLVE
+ + and native_acoustic.try_acoustic is geometry._try_native_acoustic
+ + and matching._EMISSION_ITERATION_TOLERANCE_S == geometry._EMISSION_ITERATION_TOLERANCE_S == 1e-7)
+ +
+ +
+ +def _array_key(value, shape=None):
+ + if (type(value) is not np.ndarray or value.dtype != np.float64
+ + or value.ndim != 1 or not value.flags.c_contiguous or not value.flags.aligned
+ + or (shape is not None and value.shape != shape) or not np.all(np.isfinite(value))):
+ + return None
+ + return value.shape, value.strides, value.tobytes()
+ +
+ +
+ +def _size(value):
+ + # Tokens and tracks belong to the enclosing score scope already. Charge
+ + # their references, but not their existing numerical arrays a second time.
+ + return sys.getsizeof(value) + (sum(map(_size, value)) if type(value) is tuple else 0)
+ +
+ +
+ +class _PrefixMemo:
+ + def __init__(self):
+ + self.entries = OrderedDict()
+ + self.size = 0
+ +
+ + def trim(self, capacity, limit, extra=0):
+ + while self.entries and (len(self.entries) > capacity
+ + or self.size + sys.getsizeof(self.entries) + extra > limit):
+ + _, (_, _, removed) = self.entries.popitem(last=False)
+ + self.size -= removed
+ + _COUNTS["evicted"] += 1
+ +
+ +
+ +class _Request:
+ + def __init__(self, owner, common, pairs, tracks):
+ + self.owner, self.common, self.pairs, self.tracks = owner, common, pairs, tracks
+ + self.memo = owner.prefix_memo
+ + self.floor = float.fromhex(common[4])
+ + self.residual_common = common[:4] + (None, None) + common[6:]
+ +
+ + def lookup(self):
+ + _COUNTS["lookups"] += 1
+ + for length in range(1, len(self.pairs) + 1):
+ + prefix = self.pairs[:length]
+ + key = self.common, prefix
+ + previous = self.memo.entries.get(key)
+ + residual = False
+ + if previous is None and length < len(self.pairs) and len(self.pairs) - length >= self.floor:
+ + key = self.residual_common, prefix
+ + previous = self.memo.entries.get(key)
+ + residual = previous is not None
+ + if previous is not None:
+ + self.memo.entries.move_to_end(key)
+ + outcomes, rejected, bounded = previous[0]
+ + _COUNTS["hits"] += 1
+ + _COUNTS["residual_hits"] += residual
+ + _COUNTS["saved_event_steps"] += length
+ + _COUNTS["saved_trials"] += len(outcomes)
+ + # Only scalar-only rejection tuples are shared. The caller owns
+ + # the outer list, and no fit dictionary or exception is stored.
+ + return list(outcomes), rejected, bounded
+ + return None
+ +
+ + def remember(self, processed, outcomes, rejected, bounded):
+ + if (not processed or not outcomes
+ + or not all(type(outcome) is tuple and len(outcome) == 3
+ + and outcome[0] is None and outcome[2] is None
+ + and type(outcome[1]) is float and outcome[1] < math.inf
+ + for outcome in outcomes)):
+ + return
+ + common = self.common
+ + if (processed < len(self.pairs) and len(self.pairs) - processed >= self.floor
+ + and bounded == 0 and all(outcome[1] == -math.inf for outcome in outcomes)):
+ + # Each finite contribution is nonnegative. While the unperformed
+ + # group count is at least the floor, neither a finite prefix bound
+ + # nor the dual floor screen can eliminate a clock. These -inf
+ + # outcomes therefore contain only residual rejections, independent
+ + # of the total group count/floor inside this same domain. Keeping
+ + # the prefix nonfinal also preserves the batched fit/ceiling path.
+ + common = self.residual_common
+ + key = common, self.pairs[:processed]
+ + value = tuple(outcomes), rejected, bounded
+ + # The cache shares the existing immutable-track contract of _score.
+ + # Retain tracks strongly so IDs cannot be recycled during this scope.
+ + retained = tuple(self.tracks[:processed])
+ + # Every outcome was checked above to have the same scalar-only schema.
+ + # This is the recursive charge without visiting each None/float again.
+ + value_size = (sys.getsizeof(value) + sys.getsizeof(value[0]) + len(outcomes) * _OUTCOME_SIZE
+ + + sys.getsizeof(rejected) + sys.getsizeof(bounded))
+ + size = _size(key) + value_size + sys.getsizeof(retained) + 256
+ + capacity = min(self.owner.max_entries, _MAX_ENTRIES)
+ + if capacity < 1 or size + sys.getsizeof(self.memo.entries) > _MAX_BYTES:
+ + return
+ + previous = self.memo.entries.pop(key, None)
+ + if previous is not None:
+ + self.memo.size -= previous[2]
+ + self.memo.trim(capacity - 1, _MAX_BYTES, size)
+ + self.memo.entries[key] = value, retained, size
+ + self.memo.size += size
+ + self.memo.trim(capacity, _MAX_BYTES)
+ + _COUNTS["stored"] += 1
+ +
+ +
+ +def request(tracks, events, measurements, observer, starts, c, acoustic_batches,
+ + score_floor, use_dual, legacy_first_batch):
+ + """Prepare exact prefix keys only for the private immutable scoring scope.
+ +
+ + A complete eliminated prefix is independent of the remaining events' data.
+ + General entries retain the exact group count and floor. Residual-only
+ + entries may omit them only while every prefix still has enough unperformed
+ + groups to reach the floor. Every clock's original bound and both screen
+ + counters remain unchanged.
+ + Other iterators, custom numerical hooks and mutable public inputs keep the
+ + original traversal. No entry survives its enclosing fit-cache scope.
+ + """
+ + from . import geometry, matching
+ +
+ + owner = matching._ridge_fit_cache.get()
+ + memo = getattr(owner, "prefix_memo", None)
+ + if memo is not None:
+ + memo.trim(min(owner.max_entries, _MAX_ENTRIES), _MAX_BYTES)
+ + if (owner is None or type(owner) is not matching._RidgeFitCache or owner.max_entries < 1
+ + or _MAX_ENTRIES < 1 or _MAX_BYTES < 1 or not owner.score_inputs
+ + or type(score_floor) is not float or not math.isfinite(score_floor)
+ + or type(c) is not float or not math.isfinite(c) or c <= 0
+ + or type(use_dual) is not bool or type(legacy_first_batch) is not bool
+ + or type(tracks) not in (list, tuple) or type(events) not in (list, tuple)
+ + or type(measurements) not in (list, tuple)
+ + or not len(tracks) == len(events) == len(measurements)
+ + or type(acoustic_batches) is not GeneratorType
+ + or acoustic_batches.gi_code is not matching._CANONICAL_ACOUSTIC_TRIALS.__code__
+ + or inspect.getgeneratorstate(acoustic_batches) != inspect.GEN_CREATED
+ + or any(name in owner.__dict__ for name in ("fit", "acoustic_batch"))
+ + or not _canonical()):
+ + return None
+ + arguments = acoustic_batches.gi_frame.f_locals
+ + if (arguments["tracks"] is not tracks or arguments["measurements"] is not measurements
+ + or arguments["observer"] is not observer or arguments["c"] is not c
+ + or type(arguments["max_samples"]) is not int or arguments["max_samples"] < 1):
+ + return None
+ + observer_key, starts_key = _array_key(observer, (3,)), _array_key(starts)
+ + if observer_key is None or starts_key is None or _array_key(arguments["starts"]) != starts_key:
+ + return None
+ + tokens = matching._prepared_score_tokens(measurements, events)
+ + if tokens is None:
+ + return None
+ + pairs = []
+ + for track, token in zip(tracks, tokens):
+ + if type(track) is not geometry.Track or any(name in track.__dict__ for name in _TRACK_METHODS):
+ + return None
+ + pairs.append((id(track), token))
+ + if not hasattr(owner, "prefix_memo"):
+ + owner.prefix_memo = _PrefixMemo()
+ + owner.prefix_memo.trim(min(owner.max_entries, _MAX_ENTRIES), _MAX_BYTES)
+ + common = (observer_key, starts_key, type(c), c, score_floor.hex(), len(events),
+ + use_dual, legacy_first_batch, arguments["max_samples"])
+ + return _Request(owner, common, tuple(pairs), tracks)
diff --cc tests/test_real_native_prefix_integration.py
index 0000000,0000000,0000000..84b772a
new file mode 100644
--- /dev/null
+++ b/tests/test_real_native_prefix_integration.py
@@@@ -1,0 -1,0 -1,0 +1,148 @@@@
+++"""Native batch-hook controls on recorded ridge and association trials."""
+++from copy import deepcopy
+++import json
+++import os
+++from pathlib import Path
+++
+++import numpy as np
+++import pytest
+++
+++from dopplergeist import batch_ridge, geometry, matching, search
+++from dopplergeist.data_access import FlightIndex
+++
+++trial_prefix = pytest.importorskip("dopplergeist.trial_prefix")
+++ROOT = Path(trial_prefix.__file__).resolve().parents[2]
+++
+++
+++def recorded_batch():
+++ if getattr(matching, "_native_fit_irls_batch", None) is None:
+++ pytest.skip("optional native batch fitter is unavailable")
+++ data = json.loads((ROOT / "tests/data/real-ridge-batch-arithmetic.json").read_text())
+++ measurement = (matching.RidgeTimes(np.asarray(data["times"], dtype=float)),
+++ np.asarray(data["ridge"], dtype=float), np.asarray(data["weight"], dtype=float), {})
+++ return measurement, deepcopy(data["event"]), np.asarray(data["factors"]), np.asarray(data["emissions"])
+++
+++
+++def identity(value):
+++ def scalar(item):
+++ return vars(item) if isinstance(item, geometry.AcousticSupportError) else item.tolist()
+++ return json.dumps(value, sort_keys=True, allow_nan=True, default=scalar)
+++
+++
+++def test_changed_native_batch_hook_keeps_original_public_inputs(recorded_batch, monkeypatch):
+++ measurement, event, _, _ = recorded_batch
+++ original = matching._native_fit_irls_batch
+++
+++ @matching._cache_ridge_fits
+++ def run():
+++ events, measurements = [event], [measurement]
+++ prepared_events, prepared_measurements = matching._prepare_score_inputs(events, measurements)
+++ assert prepared_measurements[0] is not measurement
+++ assert matching._prepared_score_tokens(prepared_measurements, prepared_events) is not None
+++ monkeypatch.setattr(matching, "_native_fit_irls_batch", lambda *args: original(*args))
+++ actual_events, actual_measurements = matching._prepare_score_inputs(events, measurements)
+++ assert actual_events is events
+++ assert actual_measurements is measurements
+++ assert matching._prepared_score_tokens(prepared_measurements, prepared_events) is None
+++ run()
+++
+++
+++def test_changed_native_batch_hook_cannot_reuse_predecessor_fit_snapshot(recorded_batch, monkeypatch):
+++ original = matching._native_fit_irls_batch
+++ calls = []
+++
+++ def custom(*args):
+++ calls.append(1)
+++ success = original(*args)
+++ if success:
+++ # Change computed diagnostics through the hook, keeping all recorded
+++ # ridge and track observations untouched.
+++ args[-1][:, 0] = np.nextafter(args[-1][:, 0], -np.inf)
+++ return success
+++
+++ @matching._cache_ridge_fits
+++ def run():
+++ expected_original = batch_ridge.first_event_fits(*recorded_batch)
+++ assert expected_original is not None
+++ assert matching._ridge_fit_cache.get().batch_fit_entries
+++ with monkeypatch.context() as patch:
+++ patch.setattr(matching, "_native_fit_irls_batch", custom)
+++ expected = batch_ridge._first_event_fits_uncached(*recorded_batch)
+++ assert identity(expected) != identity(expected_original)
+++ calls.clear()
+++ actual = batch_ridge.first_event_fits(*recorded_batch)
+++ assert calls, "The previous native batch's cached fit hid the changed hook"
+++ assert identity(actual) == identity(expected)
+++ # Reuse within the same hook identity retains the established fit
+++ # cache contract; switching back must invalidate that snapshot.
+++ assert identity(batch_ridge.first_event_fits(*recorded_batch)) == identity(expected)
+++ restored = batch_ridge.first_event_fits(*recorded_batch)
+++ assert identity(restored) == identity(expected_original)
+++ run()
+++
+++
[email protected](scope="module")
+++def recorded_prefix():
+++ source = os.environ.get("DOPPLERGEIST_REAL_CPA_FIXTURE")
+++ if not source:
+++ pytest.skip("set DOPPLERGEIST_REAL_CPA_FIXTURE to the recorded CPA diagnostic")
+++ fixture = json.loads(Path(source).read_text())
+++ witness = json.loads((ROOT / "tests/data/real-trial-prefix.json").read_text())["calls"]
+++ events, measurements = matching.prepare_events(fixture["events"])
+++ names = sorted({name for item in witness for name in item["track_ids"]})
+++ tracks = {track.id: track for track in FlightIndex(fixture["real_index"]).lookup(
+++ names, max_tracks=2000, max_samples=2000000)}
+++ assert set(tracks) == set(names)
+++ return events, measurements, tracks, witness
+++
+++
+++def test_changed_native_batch_hook_bypasses_previously_eliminated_prefix(
+++ recorded_batch, recorded_prefix, monkeypatch):
+++ events, measurements, tracks, witness = recorded_prefix
+++ original = matching._native_fit_irls_batch
+++ calls = []
+++
+++ def custom(*args):
+++ calls.append(1)
+++ return original(*args)
+++
+++ @matching._cache_ridge_fits
+++ def run():
+++ prepared_events, prepared_measurements = matching._prepare_score_inputs(events, measurements)
+++ by_id = {event["event_id"]: (event, measured)
+++ for event, measured in zip(prepared_events, prepared_measurements)}
+++ options = []
+++ for item in witness:
+++ selected = [by_id[name] for name in item["event_ids"]]
+++ options.append(dict(tracks=[tracks[name] for name in item["track_ids"]],
+++ events=[pair[0] for pair in selected], measurements=[pair[1] for pair in selected],
+++ observer=np.asarray(item["observer"]), starts=np.asarray(item["starts"]),
+++ c=item["c"], score_floor=item["score_floor"], use_dual=item["use_dual"],
+++ legacy_first_batch=item["legacy_first_batch"]))
+++
+++ def acoustic(call):
+++ return matching._iter_acoustic_trials(call["tracks"], call["measurements"],
+++ call["observer"], call["starts"], call["c"])
+++
+++ def evaluate(call):
+++ return search._batched_ridge_trials(**call, acoustic_batches=acoustic(call))
+++
+++ first, second = options
+++ assert trial_prefix._canonical()
+++ evaluate(first)
+++ assert matching._ridge_fit_cache.get().prefix_memo.entries
+++ before = trial_prefix.cache_stats()["hits"]
+++ with monkeypatch.context() as patch:
+++ patch.setattr(matching, "_native_fit_irls_batch", custom)
+++ assert not trial_prefix._canonical()
+++ assert trial_prefix.request(**second, acoustic_batches=acoustic(second)) is None
+++ actual = evaluate(second)
+++ assert calls, "A cached eliminated prefix or fit snapshot hid the batch hook"
+++ assert trial_prefix.cache_stats()["hits"] == before
+++ patch.setattr(trial_prefix, "_MAX_ENTRIES", 0)
+++ patch.setattr(matching._ridge_fit_cache.get(), "batch_fit_limit", 0)
+++ expected = evaluate(second)
+++ assert identity(actual) == identity(expected)
+++ assert trial_prefix._canonical()
+++ run()