Skip to content

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

Reuse immutable scoring inputs and eliminated association prefixes

Cinna Davis4c213fa36777
diff --cc .github/workflows/real-performance.yml
index 64ee681,d727775..5ef6711
--- a/.github/workflows/real-performance.yml
+++ b/.github/workflows/real-performance.yml
@@@ -4,14 -4,14 +4,14 @@@ on
inputs:
baseline:
description: Exact baseline commit for both real-input comparisons
- default: c37128bb08f12b8a68cf1f1cbf86adc2436f60fd
- default: 27da08a878ac2ec90cd7e2c07f43765ceeea609b
++ default: b4cd00ad46f0299baa716950fabe062d74f019f0
required: true
regional:
description: Also compare the complete regional HTTP workflow
type: boolean
default: true
push:
- branches: [perf/measured-hotpath-20261103, perf/real-pipeline-throughput-20261103, perf/single-pass-motion-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/prepared-flight-scoring-20261103, perf/flight-sieve-reuse-20261103]
paths:
- .github/workflows/real-performance.yml
- src/dopplergeist/**
@@@ -27,6 -27,6 +27,7 @@@
- pyproject.toml
- tests/test_real_*.py
- tests/data/real-ridge-batch-arithmetic.json
++ - tests/data/real-trial-prefix.json
- tests/data/reference-empty-tiles-real.json
- tests/data/reference-retention-real.json
permissions:
@@@ -38,7 -38,7 +39,7 @@@ env
NUMEXPR_NUM_THREADS: '1'
CC: gcc
PYTHONPATH: src
- BASELINE_REF: ${{ inputs.baseline || 'c37128bb08f12b8a68cf1f1cbf86adc2436f60fd' }}
- 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'
@@@ -57,7 -58,7 +59,8 @@@
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 callable(matching._native_validate_ridge_events), "Native evidence validator 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 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-")
@@@ -94,13 -102,13 +104,13 @@@
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_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
- 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_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
- 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
++ 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
@@@ -113,11 -122,16 +124,16 @@@
native_option+=(--require-current-native-acoustic)
fi
for width in 10 120; do
- python scripts/benchmark_prepared_measurements.py compare --fixture "benchmark-inputs/point-$width.json" --baseline-source "$RUNNER_TEMP/baseline/src" --current-source src --repeats 2 --timeout-s 600 --output "results/performance-$width" "${native_option[@]}"
+ taskset -c "$benchmark_cpu" python scripts/benchmark_prepared_measurements.py compare --fixture "benchmark-inputs/point-$width.json" --baseline-source "$RUNNER_TEMP/baseline/src" --current-source src --repeats 3 --timeout-s 600 --output "results/performance-$width" "${native_option[@]}"
done
+ taskset -c "$benchmark_cpu" python scripts/benchmark_prepared_measurements.py compare \
+ --fixture benchmark-inputs/dense-query.json --include-track-query \
+ --baseline-source "$RUNNER_TEMP/baseline/src" --current-source src \
+ --repeats 3 --timeout-s 600 --output results/performance-dense-query \
+ "${native_option[@]}"
- name: Profile remaining point work outside the timing comparison
run: |
-- python -m cProfile -o results/current-point.prof scripts/benchmark_prepared_measurements.py _worker --source src --fixture benchmark-inputs/point-120.json --output results/current-point-profile --require-native --require-native-acoustic
++ python -m cProfile -o results/current-point.prof scripts/benchmark_prepared_measurements.py diagnose --source src --fixture benchmark-inputs/point-120.json --output results/current-point-profile --require-native --require-native-acoustic
python - <<'PY'
import json
from pathlib import Path
@@@ -146,6 -160,6 +162,17 @@@
print(json.dumps({"profile_summary": str(output), "excluded_from_timing_medians": True,
"top_10": records[:10]}, indent=2))
PY
++ - name: Count numerical calls outside the timing comparison
++ run: |
++ python scripts/benchmark_prepared_measurements.py diagnose --source src --fixture benchmark-inputs/point-120.json --output results/current-point-operations --instrument-numerics --require-native --require-native-acoustic
++ python - <<'PY'
++ import json
++ from pathlib import Path
++ canonical = json.loads(Path("results/performance-120/1-current/results.json").read_text())
++ diagnostic = json.loads(Path("results/current-point-operations/results.json").read_text())
++ assert json.dumps(canonical, sort_keys=True) == json.dumps(diagnostic, sort_keys=True), "Numerical instrumentation changed scientific outputs"
++ print("Diagnostic counters preserve outputs; their timings and disabled optimizations are excluded from performance comparisons")
++ PY
- uses: actions/upload-artifact@v4
if: always()
with:
@@@ -157,6 -171,6 +184,7 @@@
results/performance-*/*/summary.json
results/current-point.prof
results/current-point-profile-stats.json
++ results/current-point-operations/summary.json
if-no-files-found: ignore
retention-days: 14
- uses: actions/upload-artifact@v4
@@@ -167,7 -181,7 +195,7 @@@
if-no-files-found: ignore
retention-days: 3
regional:
-- if: github.ref == 'refs/heads/perf/single-pass-motion-20261103' || (github.event_name == 'workflow_dispatch' && inputs.regional)
++ if: github.ref == 'refs/heads/perf/single-pass-motion-20261103' || github.ref == 'refs/heads/perf/prepared-flight-scoring-20261103' || (github.event_name == 'workflow_dispatch' && inputs.regional)
# Keep the reference FFmpeg family; a new encoder can change detected ridges.
runs-on: ubuntu-24.04
timeout-minutes: 30
@@@ -184,7 -199,6 +213,8 @@@
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 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 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 --cc .github/workflows/tests.yml
index 9757aa8,3a0d11e..e69a4c9
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@@ -50,13 -49,6 +50,17 @@@ jobs
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"
++ 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
diff --cc docs/PERFORMANCE.md
index d3324ae,9d99f2d..f9765f8
--- a/docs/PERFORMANCE.md
+++ b/docs/PERFORMANCE.md
@@@ -1,5 -1,174 +1,177 @@@
# Measured pipeline performance
+ ## Indexed flight queries and repeated evidence checks
+
+ [PR258](https://github.com/1337isnot1337/DOPPLERGEIST/pull/258) reduces repeated work in indexed flight loading and ridge-evidence validation.
+ The measured production changes are pinned at [`a82f4b0`](https://github.com/1337isnot1337/DOPPLERGEIST/commit/a82f4b02a5a4097693954ddec8e2c2386189e63a).
+ The incremental baseline, [`27da08a`](https://github.com/1337isnot1337/DOPPLERGEIST/commit/27da08a878ac2ec90cd7e2c07f43765ceeea609b), already includes PR237's native fitting, acoustic computation, single-pass queries, and CPA improvements.
+ Those existing improvements are not attributed to PR258.
+ The [measurement record](../benchmarks/results/query-scoring-20261103.json) preserves samples, source pins, runtime, and comparison scopes.
+
+ ### Compared with the implementation at task start
+
+ A separate three-pair comparison measures the complete dense query/sieve/scoring unit against starting main [`18e29d0`](https://github.com/1337isnot1337/DOPPLERGEIST/commit/18e29d0e91b7b450aeca29915ceb72ee987349be).
+ Median CPU time falls from **63.798 to 32.507 seconds: 1.963× faster, with 49.05% less CPU time**.
+ Wall medians are **63.893 → 32.566 seconds**, or **1.962×**.
+ All six executions preserve the same complete scientific output and logical retention rows.
+
+ This measures the final version's combined improvement, including PR237's native kernels and single-pass queries, which merged while PR258 was being developed.
+ The incremental PR258 measurements below separate this patch's contribution from those earlier changes.
+ The starting query makes two traversals and yields 6,695 track objects / 1,047,000 samples across them; the final query makes one traversal and yields 4,694 / 713,100.
+ These operational counts include repeated yields. Both versions query the same unique inventory and select the same flights.
+ The combined speedup is measured directly; it is not a product of component measurements or a full-upload timing claim.
+
+ ### Incremental measurements
+
+ Each row uses three alternating fresh baseline/current pairs on the same host.
+ Speedup is the baseline median divided by the current median.
+
+ | Measured scope | Baseline CPU median | Current CPU median | Speedup | Time reduction |
+ | --- | ---: | ---: | ---: | ---: |
+ | Broad point scoring, loaded tracks, ±120 s clock | 6.218 s | 5.682 s | 1.094× | 8.63% |
+ | Initial flight query, distance sieve, and dense scoring | 37.419 s | 36.161 s | 1.035× | 3.36% |
+ | Query and distance sieve within that dense run | 9.524 s | 6.960 s | 1.368× | 26.92% |
+ | Separate cold query and distance sieve, track cache disabled | 10.459 s | 7.471 s | 1.400× | 28.57% |
+
+ Dense whole-path wall medians are **37.473 → 36.220 seconds**.
+ Its query-plus-scoring CPU samples, in baseline/current pairs, are **39.870/36.161**, **37.419/38.104**, and **35.826/31.773 seconds**.
+ One whole-path pair is slower, despite a faster query stage in every pair.
+ Scoring-stage medians in this series are 27.895 and 28.996 seconds; the shared host introduces material timing variation.
+ Keep the modest whole-path result when evaluating this patch; do not replace it with a product of separate component ratios.
+ Worker-lifetime median peak RSS is **516.02 → 498.16 MiB**, including imports and input preparation.
+
+ The dense workload queries the full indexed day: **4,694 trajectories and 713,100 samples**.
+ The production overload path selects the same ordered **1,354 trajectories and 357,324 samples** in both versions.
+ Scoring evaluates **16,101 associations**, uses **16 independent ridge groups**, and retains the same **100 candidates**.
+ All six executions produce exactly equal queried and selected inventories, candidates, diagnostics, support accounting, ranking, and logical retention rows.
+ Both arms perform **228,240 successful native ridge fits** and **5,731 successful native acoustic solves** per dense execution.
+ The preloaded broad-point workload likewise preserves all **3,905 scored associations**, including diagnostics and retention.
+
+ ### Independent CI replication
+
+ The [recorded-input CI run](https://github.com/1337isnot1337/DOPPLERGEIST/actions/runs/41695355631) at `52db126` repeats the same comparisons against `27da08a` on a separate runner.
+ This commit adds a workflow build-dependency fix; its production code is identical to `a82f4b0`.
+ All three paired executions of each workload preserve exact outputs.
+
+ | CI workload | Baseline CPU median | Current CPU median | Speedup |
+ | --- | ---: | ---: | ---: |
+ | Point scoring, ±10 s clock | 0.579 s | 0.553 s | 1.048× |
+ | Point scoring, ±120 s clock | 3.759 s | 3.433 s | 1.095× |
+ | Initial query, sieve, and dense scoring | 23.694 s | 19.969 s | 1.187× |
+
+ CI dense-query wall medians are **23.827 → 20.104 seconds**, or **1.185×**.
+ The query/sieve component improves **1.528×**, and the CPA/scoring/retention component improves **1.097×**.
+ The whole-path improvement is measured directly, not derived by multiplying those components.
+ This runner uses Ubuntu 26.04, Python 3.12.16, NumPy 2.5.4, SciPy 1.18.1, and pandas 3.1.0.
+ Its absolute times should not be compared with the local runner's times; retain both matched comparisons.
+
+ The same run passes **99 recorded-input regressions and four explicit Python-fallback tests, with zero skips**.
+ The [installation checks](https://github.com/1337isnot1337/DOPPLERGEIST/actions/runs/41695366731) pass on Python 3.11 and 3.14.
+ Local decoder verification additionally compares all 4,694 actual indexed payloads against the pandas path, including exact arrays, spline coefficients, derivatives, and labels.
+ The initial CI attempt failed before benchmarks because the baseline extension build lacked runtime `setuptools`; the workflow now installs its declared build dependency explicitly.
+ Regional and bounded-HTTP CI jobs are not enabled by this branch's push conditions, so these CI results do not establish upload performance.
+
+ ### Complete HTTP upload check
+
+ Three alternating complete MP3-upload pairs compare against [`494b874`](https://github.com/1337isnot1337/DOPPLERGEIST/commit/494b874a42d8599e5950a05ef60f06a96ef4d1b9).
+ This baseline has the same numerical implementation as `27da08a`, plus the intervening job-startup/cancellation change, which both HTTP arms include.
+ All six executions complete upload, decoding, discovery, search, refinement, and decision verification.
+ Median elapsed time in the HTTP verification receipt is **51.211 → 49.500 seconds**, or **1.035× faster / 3.34% less time**.
+ Baseline samples are **51.349, 51.211, 51.094 seconds**; current samples are **54.366, 48.963, 49.500 seconds**.
+ The first pair is slower and remains in the measurement record. This is a modest incremental upload gain, not the roughly twofold dense-workload gain above.
+ The reported search timer has medians **22.789 → 21.621 seconds**; its scope is contained within the full workflow.
+
+ Every pair has zero scientific or array differences after the declared run-metadata normalization, without a numerical tolerance.
+ The comparison includes 20 scientific JSON artifacts, three root JSON files, the logical SQLite ledger, decoded PCM, 17 CSV files, and PNG pixels.
+ Each ledger preserves 29,738 bounds, 852 records, 12 stages, and one run.
+ Each upload retains 17 detected event records and 23 candidates; its decision remains `inconclusive / search_incomplete`.
+ Both arms use the same original MP3, request, operator configuration, index, terrain, numerical thread count, and CPU affinity.
+ The small source-informed regional request is an integration/performance control; it does not establish independent location accuracy.
+
+ ### Where the time is removed
+
+ **Construct the representation needed by the next stage.** Indexed segments already contain canonical numeric JSON records.
+ Previously, each cache miss built a pandas table and immediately converted its columns into NumPy arrays for track normalization.
+ The new private column adapter supplies those arrays directly.
+ The existing `_frame_track` and `_segment_velocity` routines still perform normalization, velocity inference, and spline construction.
+ Noncanonical or mixed representations retain pandas.
+ The index schema, compressed payloads, integrity checks, cache keys, mutation isolation, and sample-budget checks are unchanged.
+ No index rebuild is required.
+
+ **Validate canonical evidence without repeatedly rebuilding Python objects.** Association scoring often checks complete, valid fit evidence with the same structure.
+ The optional C helper recognizes that narrow case and avoids the repeated Python field loop and per-event cache-key construction.
+ It only returns success for valid evidence in exact built-in containers and numeric types.
+ Mutations are rechecked on every call.
+ Invalid, incomplete, or custom inputs retain the original Python validation, including failure precedence and custom-object behavior.
+ Aggregate scores, Python summation, NumPy boundary handling, fit iterations, thresholds, and whole-batch ceiling reuse are unchanged.
+
+ These changes avoid work without reducing observed flights, event samples, search resolution, or candidate budgets.
+ A direct-distance alternative to the spatial tree was measured and rejected: its roughly 0.06-second geometry saving did not justify changing that path.
+
+ ### Measurement method
+
+ The comparison harness accepts `--include-track-query` for preparation and comparison.
+ Each worker opens a fresh `FlightIndex` and starts timing before the initial traversal and conservative distance sieve.
+ The production 32 MiB track-cache budget remains enabled for the combined run; no track lookup occurs before its timer starts.
+ The default still measures complete scoring after loading selected tracks, so older commands keep their scope.
+
+ The dense run includes CPA preparation, association generation, physical scoring, and retention.
+ It excludes imports, index metadata, event preparation, downloads, terrain sampling, audio discovery, and refinement.
+ It queries all indexed times without a recording-clock prior, but scores one declared association-anchor partition, `[1788864888.7151732, 1788865236.7214916)`.
+ This partition controls association ownership; physical fit intervals are unchanged.
+ This is a measured dense search unit, not a measurement of every partition or receiver location.
+
+ Baseline/current order alternates as B/C, C/B, B/C.
+ Each arm runs in a new interpreter with the same inputs, one numerical thread, and CPU affinity fixed to CPU 2.
+ The separate cache-disabled query experiment uses CPU 0 and fresh index objects.
+ Operating-system file caches are not purged; “cold” describes the application track cache, not cold storage.
+ The host is shared, so CPU affinity does not imply exclusive machine access.
+ Separate profiling and native-validator hit-rate diagnostics are excluded from timing medians.
-The comparison harness retains its lightweight operation counters and candidate capture in both timed arms.
++The reported PR258 runs used numerical operation counters and candidate capture in both timed arms.
++Current comparisons preserve numerical function identities; separate diagnostic workers collect wrapped counters, as described below.
+
+ The local runtime is Python 3.12.14, NumPy 2.5.4, SciPy 1.17.0, pandas 2.2.3, pyproj 3.8.0, pyarrow 26.0.1, and SQLite 3.53.1 on Linux x86-64.
+ Native extensions use GCC with the existing strict floating-point build options.
+ Archived source-informed receiver and clock controls freeze computational workloads; they do not establish independent localization accuracy or nationwide throughput.
+
+ ### Reproduce the query-plus-scoring comparison
+
+ Run these Linux commands from the optimized checkout, with GCC available.
+ Choose unused output paths because the harness refuses to overwrite prior results.
+
+ ```bash
+ CC=gcc python -m pip install 'setuptools>=68' --editable '.[test]'
+ export PYTHONPATH=src
+ export OPENBLAS_NUM_THREADS=1 OMP_NUM_THREADS=1 MKL_NUM_THREADS=1 NUMEXPR_NUM_THREADS=1
+ python -c 'from dopplergeist import matching, native_acoustic; assert callable(matching._native_validate_ridge_events); assert native_acoustic._native is not None'
+ python scripts/restore_artifacts.py --bundle data-adsb-derived --bundle data-terrain
+ python scripts/prepare_reference_upload.py --data-root data --output benchmark-inputs
+ git worktree add --detach ../dopplergeist-baseline 27da08a878ac2ec90cd7e2c07f43765ceeea609b
+ (cd ../dopplergeist-baseline && CC=gcc python setup.py build_ext --inplace)
+
+ python scripts/benchmark_prepared_measurements.py prepare \
+ --discovery evidence/audio/w1-sensitivity-20261102/reference/discovery.json \
+ --index benchmark-inputs/reference-region.sqlite \
+ --reference-report evidence/results/reference-local/report.json \
+ --full-day --max-tracks 2000 --max-samples 500000 --max-candidates 20000 \
+ --anchor-window-s 1788864888.7151732 1788865236.7214916 \
+ --include-track-query --output benchmark-inputs/dense-query.json
+
+ benchmark_cpu=$(python -c 'import os; print(min(os.sched_getaffinity(0)))')
+ taskset -c "$benchmark_cpu" python scripts/benchmark_prepared_measurements.py compare \
+ --fixture benchmark-inputs/dense-query.json --include-track-query \
+ --baseline-source ../dopplergeist-baseline/src --current-source src \
+ --repeats 3 --timeout-s 600 --output results/performance-dense-query \
+ --require-current-native --require-current-native-acoustic
+ ```
+
+ Check native availability after installation; source updates require rebuilding the extension and restarting a running service.
+ The runtime remains usable through its Python fallback, but that fallback does not receive the native evidence-validation saving.
-The comparison requires successful native operations in the measured current arm and refuses to report a speedup if any exact-output comparison fails.
++Current comparisons require the native ridge backend and successful native acoustic operations.
++A separate diagnostic worker verifies successful native ridge execution.
++The harness refuses to report a speedup if any exact-output comparison fails.
+ The `Real performance comparison` workflow runs recorded regressions and these paired measurements, and preserves summaries as artifacts.
+
## Measured native computation and bounded reuse
The implementation at [`41d0646`](https://github.com/1337isnot1337/DOPPLERGEIST/commit/41d06469935f776988e1a4f04ab386d0355b433b) completed the regional HTTP replay in a median **37.188 seconds**, compared with **62.031 seconds** for the already optimized main baseline [`fec518e5`](https://github.com/1337isnot1337/DOPPLERGEIST/commit/fec518e51f14afe8d36203e6c5e1be1277dea14e).
@@@ -986,7 -1155,7 +1158,21 @@@ don
The comparison alternates arm order in fresh worker processes.
It compares every candidate, diagnostic, support record, ranking, and logical retention row exactly before reporting a speedup.
--Native success counters must increase inside the timed scoring call.
++Timed runs preserve the original numerical functions so guarded production optimizations remain active.
++Unmeasured scalar and native-ridge call counts are `null`, not zero.
++The native acoustic solver exposes an internal success counter without wrapping its numerical functions.
++Native ridge calls are checked in a separate diagnostic worker:
++
++```bash
++python scripts/benchmark_prepared_measurements.py diagnose \
++ --source src --fixture benchmark-inputs/point-120.json \
++ --instrument-numerics --require-native --require-native-acoustic \
++ --output results/native-scoring-operations
++```
++
++Numerical wrappers can disable guarded caches, so diagnostic counts describe that execution path only.
++Diagnostic timings do not establish a production speedup.
++The workflow checks that this diagnostic worker preserves the canonical worker's complete scientific outputs.
Input loading is reported separately, and the RSS measurement covers the worker lifetime through scoring.
The complete-fit cache shares its existing 4 MiB digest-key and serialized-payload budget with validated batch ceilings.
diff --cc scripts/benchmark_prepared_measurements.py
index 424ba03,038c87a..c5583b7
--- a/scripts/benchmark_prepared_measurements.py
+++ b/scripts/benchmark_prepared_measurements.py
@@@ -1,8 -1,8 +1,11 @@@
--"""Compare complete point scoring on recorded ridges and historical trajectories.
++"""Compare canonical point scoring on recorded ridges and historical trajectories.
The archived research candidate supplies workload coordinates and a clock, not
independent truth. Each source runs in a separate interpreter with bounded work
--and identical observed inputs. This script generates no audio or trajectories.
++and identical observed inputs. Timed comparisons preserve numerical function
++identities. Collect numerical counters in a separate diagnostic worker, because
++wrapping those functions can disable guarded production optimizations.
++This script generates no audio or trajectories.
"""
from __future__ import annotations
@@@ -142,20 -188,22 +191,31 @@@ def worker(args)
from dopplergeist.native_acoustic import _native as native_acoustic
except ImportError:
native_acoustic = None
++ try:
++ from dopplergeist.trial_prefix import cache_stats as prefix_cache_stats
++ except ModuleNotFoundError as error:
++ if error.name != "dopplergeist.trial_prefix":
++ raise
++ prefix_cache_stats = None
if args.require_native and not callable(native_fit):
raise RuntimeError("The selected source's native ridge fitter is unavailable")
if args.require_native_acoustic and native_acoustic is None:
raise RuntimeError("The selected source's native acoustic solver is unavailable")
fixture = json.loads(args.fixture.read_text())
++ index_started = time.perf_counter()
index = FlightIndex(args.index or fixture["real_index"])
++ index_metadata_seconds = time.perf_counter() - index_started
if index.report["input_hash"] != fixture["index_input_hash"]:
raise ValueError("The real trajectory index no longer matches the fixture")
budget = tiled_search.SearchBudget(**fixture["budget"])
loaded_at = time.perf_counter()
- tracks = list(index.lookup(fixture["track_ids"], max_tracks=budget.max_tracks_per_tile,
- max_samples=budget.max_track_samples_per_tile))
- if sorted(track.id for track in tracks) != sorted(fixture["track_ids"]):
- raise ValueError("The index is missing a recorded trajectory")
+ tracks = None
+ if not args.include_track_query:
+ tracks = list(index.lookup(fixture["track_ids"], max_tracks=budget.max_tracks_per_tile,
+ max_samples=budget.max_track_samples_per_tile))
+ if sorted(track.id for track in tracks) != sorted(fixture["track_ids"]):
+ raise ValueError("The index is missing a recorded trajectory")
++ track_load_seconds = time.perf_counter() - loaded_at if tracks is not None else None
events, measurements = matching.prepare_events(fixture["events"])
points = pd.DataFrame([fixture["point"]])
load_seconds = time.perf_counter() - loaded_at
@@@ -230,24 -278,49 +290,54 @@@
all_rows.append(result)
return result
-- matching._fit_ridge_uncached, Track.acoustic = counted_fit, counted_acoustic
++ if args.instrument_numerics:
++ matching._fit_ridge_uncached, Track.acoustic = counted_fit, counted_acoustic
++ else:
++ for name in ("scalar_fit_calls", "acoustic_calls", "native_fit_calls", "native_fit_successes"):
++ operations[name] = None
tiled_search.score_candidate = captured_score
-- if callable(native_fit):
++ if args.instrument_numerics and callable(native_fit):
matching._native_fit_irls = counted_native
native_acoustic_before = native_acoustic.successful_calls() if native_acoustic is not None else 0
+ query_accounting, query_operations, execution_stages = None, None, {}
began, cpu = time.perf_counter(), time.process_time()
try:
+ if args.include_track_query:
+ query_began, query_cpu = time.perf_counter(), time.process_time()
+ tracks, points, query_operations, query_accounting = query_tracks(
+ index, points, events, budget, fixture["scoring"],
+ fixture["query_bounds"], fixture["query_time_s"])
+ execution_stages["track_query_and_distance_sieve"] = {
+ "wall_seconds": time.perf_counter() - query_began,
+ "cpu_seconds": time.process_time() - query_cpu,
+ }
+ if query_accounting["selected_track_ids"] != fixture["track_ids"]:
+ raise AssertionError("The real query changed the ordered selected-track inventory")
+ if query_accounting["selected_trajectory_samples"] != fixture["counts"]["trajectory_samples"]:
+ raise AssertionError("The real query changed the selected trajectory sample count")
+ prepared_query = fixture.get("track_query_preparation")
+ if prepared_query is not None:
+ if (len(query_accounting["queried_track_ids"]) != prepared_query["queried_trajectories"]
+ or query_accounting["queried_trajectory_samples"] != prepared_query["queried_trajectory_samples"]):
+ raise AssertionError("The real query changed the complete observed-track inventory")
+ scoring_began, scoring_cpu = time.perf_counter(), time.process_time()
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, **partition)
+ if args.include_track_query:
+ execution_stages["cpa_association_scoring_and_retention"] = {
+ "wall_seconds": time.perf_counter() - scoring_began,
+ "cpu_seconds": time.process_time() - scoring_cpu,
+ }
finally:
elapsed, cpu_seconds = time.perf_counter() - began, time.process_time() - cpu
-- 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
++ if args.instrument_numerics:
++ matching._fit_ridge_uncached, Track.acoustic = original_fit, original_acoustic
++ if callable(native_fit):
++ matching._native_fit_irls = native_fit
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):
@@@ -269,17 -342,20 +359,32 @@@
raise AssertionError("The scoring pass produced an invalid retention ledger")
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"]:
++ if args.require_native and args.instrument_numerics and not operations["native_fit_successes"]:
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("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")}
- write_new(args.output / "results.json", {"ranked": ranked, "counts": counts,
- "all_scored_candidates": all_rows, "retention": retention})
+ results = {"ranked": ranked, "counts": counts,
+ "all_scored_candidates": all_rows, "retention": retention}
+ if query_accounting is not None:
+ results["track_query_accounting"] = query_accounting
+ write_new(args.output / "results.json", results)
summary = {"wall_seconds": elapsed, "cpu_seconds": cpu_seconds,
"load_seconds_excluded": load_seconds, "operations": operations,
++ "index_metadata_seconds_excluded": index_metadata_seconds,
++ "track_load_seconds_excluded": track_load_seconds,
++ "numerical_functions_canonical": not (args.instrument_numerics or args.stage_timing),
++ "performance_timing_eligible": (args.command != "diagnose"
++ and not (args.instrument_numerics or args.stage_timing)),
++ "measurement_mode": ("numerical_counter_diagnostic" if args.instrument_numerics else
++ "phase_timing_diagnostic" if args.stage_timing else
++ "canonical_diagnostic" if args.command == "diagnose" else "canonical_timing"),
++ "ridge_execution_check": ("Successful-call counter in a separate diagnostic worker"
++ if args.instrument_numerics else
++ "Backend selection and availability; successful-call count is unmeasured"),
++ "trial_prefix_cache": prefix_cache_stats() if prefix_cache_stats is not None else None,
"ridge_fit_backend": ridge_backend, "acoustic_backend": acoustic_backend,
"native_extension_available": callable(native_fit),
"peak_rss_mib": peak_rss_mib,
@@@ -299,7 -382,7 +411,7 @@@ def compare(args)
args.output.mkdir(parents=True, exist_ok=False)
sources = {"baseline": args.baseline_source.resolve(), "current": args.current_source.resolve()}
environment = dict(os.environ)
-- for variable in ("OPENBLAS_NUM_THREADS", "OMP_NUM_THREADS", "MKL_NUM_THREADS"):
++ for variable in ("OPENBLAS_NUM_THREADS", "OMP_NUM_THREADS", "MKL_NUM_THREADS", "NUMEXPR_NUM_THREADS"):
environment[variable] = "1"
runs, expected = [], None
for repetition in range(args.repeats):
@@@ -327,15 -412,25 +441,29 @@@
medians = {}
for name in sources:
medians[name] = {}
-- for metric in ("wall_seconds", "cpu_seconds", "peak_rss_mib"):
++ for metric in ("wall_seconds", "cpu_seconds", "peak_rss_mib",
++ "load_seconds_excluded", "index_metadata_seconds_excluded",
++ "track_load_seconds_excluded"):
values = [row[metric] for row in runs if row["source"] == name and row[metric] is not None]
medians[name][metric] = statistics.median(values) if values else None
report = {"identical_results": True, "repeats": args.repeats,
"fixture": str(args.fixture.resolve()),
"sources": {name: str(path) for name, path in sources.items()}, "medians": medians,
-- "speedup": {metric: medians["baseline"][metric] / medians["current"][metric]
-- for metric in ("wall_seconds", "cpu_seconds")},
++ "performance_timing_eligible": not args.stage_timing,
++ "speedup": ({metric: medians["baseline"][metric] / medians["current"][metric]
++ for metric in ("wall_seconds", "cpu_seconds")}
++ if not args.stage_timing else None),
"scope": runs[0]["scope"], "runs": runs}
+ if args.include_track_query:
+ report["timing_scope"] = runs[0]["timing_scope"]
+ report["track_query_included"] = True
+ report["execution_stage_medians"] = {
+ name: {stage: {metric: statistics.median(
+ row["execution_stage_seconds"][stage][metric]
+ for row in runs if row["source"] == name)
+ for metric in ("wall_seconds", "cpu_seconds")}
+ for stage in runs[0]["execution_stage_seconds"]}
+ for name in sources}
write_new(args.output / "comparison.json", report)
print(json.dumps({key: report[key] for key in ("identical_results", "medians", "speedup", "scope")}), flush=True)
@@@ -366,19 -463,22 +496,30 @@@ def main()
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")
++ help="Collect diagnostic phase times; no performance speedup is reported")
+ comparison.add_argument("--include-track-query", action="store_true",
+ help="Include initial index traversal and the production overload sieve in each timed worker")
comparison.add_argument("--require-current-native", action="store_true",
-- help="Require successful native ridge fits inside each current scoring run")
++ help="Require the native ridge backend; verify successful calls in a separate diagnose run")
comparison.add_argument("--require-current-native-acoustic", action="store_true",
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("--include-track-query", 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)
++ for name in ("diagnose", "_worker"):
++ internal = commands.add_parser(name, help=("Collect diagnostic measurements outside performance comparisons"
++ if name == "diagnose" else 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="Collect inclusive phase times; overlapping phases must not be summed")
++ internal.add_argument("--include-track-query", action="store_true",
++ help="Include initial index traversal and the production overload sieve")
++ internal.add_argument("--instrument-numerics", action="store_true",
++ help="Wrap numerical methods for counters; this can disable guarded optimizations")
++ internal.add_argument("--require-native", action="store_true",
++ help="Require the native ridge backend and, with counters, successful native calls")
++ internal.add_argument("--require-native-acoustic", action="store_true",
++ help="Require successful native acoustic calls")
args = parser.parse_args()
if args.command == "prepare":
if args.clock_half_width_s < 0:
diff --cc src/dopplergeist/_native_ridge.c
index 5ba0523,6dfc964..4e3b5aa
--- a/src/dopplergeist/_native_ridge.c
+++ b/src/dopplergeist/_native_ridge.c
@@@ -292,109 -283,92 +292,137 @@@ plain_dictionary(PyObject *object
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);
}
- /* 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)
-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;
- 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)) 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;
+ !PyList_CheckExact(frequencies) ||
- !(PyList_CheckExact(events) || PyTuple_CheckExact(events))) Py_RETURN_NONE;
++ !(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) Py_RETURN_NONE;
++ event_count != count) return -1;
- 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;
+ 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;
++ 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)) Py_RETURN_NONE;
++ 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)) 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; }
++ !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)) Py_RETURN_NONE;
++ 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))) {
- Py_RETURN_NONE;
++ 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) Py_RETURN_NONE;
++ 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)) Py_RETURN_NONE;
++ 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_dictionary(quality)) Py_RETURN_NONE;
- 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)) Py_RETURN_NONE;
++ 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 PyLong_FromLong(1);
++ 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 PyLong_FromLong(2);
++ 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 PyLong_FromLong(3);
++ return 3;
}
- if (optimizer == Py_False) return PyLong_FromLong(4);
- if (score != values[1] || frequency != values[2]) return PyLong_FromLong(5);
- 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 PyLong_FromLong(6);
++ !(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 PyLong_FromLong(7);
++ return 7;
+ }
+ if (fabs(values[4] * values[6] - drift) > 1e-12 + 1e-12 * fabs(drift)) {
- return PyLong_FromLong(8);
++ return 8;
+ }
+ }
- return PyLong_FromLong(0);
++ 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;
++ }
++ 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."},
+ {"valid_ridge_events", valid_ridge_events, METH_VARARGS,
+ "Recognize valid canonical ridge evidence; other inputs retain Python validation."},
{NULL, NULL, 0, NULL}
};
diff --cc src/dopplergeist/acoustic_screen.py
index cac05ab,f1711d7..37f1d96
--- a/src/dopplergeist/acoustic_screen.py
+++ b/src/dopplergeist/acoustic_screen.py
@@@ -194,3 -175,3 +194,7 @@@ def _loss_lower_bounds(times, ridge, we
+source_box+drift_box)
lower = np.maximum(0., value-error)/np.sum(weight)
return active, valid, lower
++
++
++_CANONICAL_SCREEN_FUNCTIONS = (ridge_trial_rejections, _cached_loss_lower_bounds,
++ _validated_loss_lower_bounds, _loss_lower_bounds, _frequency_scale)
diff --cc src/dopplergeist/batch_ridge.py
index d9bc57a,d9bc57a..3eae4b6
--- a/src/dopplergeist/batch_ridge.py
+++ b/src/dopplergeist/batch_ridge.py
@@@ -23,12 -23,12 +23,20 @@@ def _batch_fit_key(measurement, event,
raise TypeError("Uncached custom measurement time container")
if type(event) is not dict or type(event.get("quality", {})) is not dict:
raise TypeError("Uncached custom event container")
-- digest = hashlib.sha256()
-- settings = tuple(_fit_argument_key(value) for value in
-- (_frequency_scale(event), event.get("quality", {}).get("source_drift_fraction", .02),
-- event["f0_range_hz"]))
-- digest.update(pickle.dumps(settings, protocol=5))
-- for array in (measurement[0].time, measurement[1], measurement[2], factors, emissions):
++ private = (matching._prepared_score_input(measurement, event)
++ if _fit_argument_key is matching._CANONICAL_SCORE_INPUT_FUNCTIONS[3]
++ and _frequency_scale is matching._CANONICAL_SCORE_INPUT_FUNCTIONS[0] else None)
++ if private is None:
++ digest = hashlib.sha256()
++ settings = tuple(_fit_argument_key(value) for value in
++ (_frequency_scale(event), event.get("quality", {}).get("source_drift_fraction", .02),
++ event["f0_range_hz"]))
++ digest.update(pickle.dumps(settings, protocol=5))
++ arrays = (measurement[0].time, measurement[1], measurement[2], factors, emissions)
++ else:
++ digest = private.batch_digest.copy()
++ arrays = (factors, emissions)
++ for array in arrays:
if type(array) is not np.ndarray or array.dtype.kind not in "biuf":
raise TypeError("Uncached custom batch input")
layout = (array.dtype.str, array.shape, array.strides, array.flags.aligned)
@@@ -67,6 -67,6 +75,7 @@@ def _first_event_fits_with_ceilings(mea
bounds = event.get("f0_range_hz") if type(event) is dict else None
quality = event.get("quality", {}) if type(event) is dict else None
fraction = quality.get("source_drift_fraction", .02) if type(quality) is dict else None
++ native_validator = matching._CANONICAL_NATIVE_EVENT_VALIDATOR
canonical = (
first_event_fits is _CANONICAL_FIRST_EVENT_FITS
and _first_event_fits_uncached is _CANONICAL_BATCH_FITTER
@@@ -74,6 -74,6 +83,9 @@@
and all(current is original for current, original in zip(
(search._association_event_ceiling, search._association_event_ceiling_uncached,
search._ridge_trial_classification), search._CANONICAL_CEILING_FUNCTIONS))
++ and matching._native_validate_ridge_events is native_validator
++ and search._native_validate_ridge_events is native_validator
++ and getattr(matching._native_ridge_module, "valid_ridge_events", None) is native_validator
and type(limit) is float and math.isfinite(limit) and limit > 0
and type(bounds) in (list, tuple) and len(bounds) == 2
and all(type(value) is float for value in (*bounds, fraction))
@@@ -121,8 -121,8 +133,30 @@@ def _cached_first_event_fits(measuremen
ceilings = None
if result is not None:
if ceiling_limit is not None:
-- from .search import _association_event_ceiling
-- ceilings = [_association_event_ceiling(fit, fit["score"], fit["f0_hz"], event, ceiling_limit)
++ from . import search
++
++ validator = search._association_event_ceiling
++ native_module, native_reason = matching._CANONICAL_PREFIX_FUNCTIONS[-2:]
++ native_validator = matching._CANONICAL_NATIVE_EVENT_VALIDATOR
++ if (native_validator is None
++ and matching._native_validate_ridge_events is native_validator
++ and search._native_validate_ridge_events is native_validator
++ and getattr(matching._native_ridge_module, "valid_ridge_events", None) is native_validator
++ and first_event_fits is _CANONICAL_FIRST_EVENT_FITS
++ and kernel is _CANONICAL_BATCH_FITTER
++ and _fit_diagnostics is _CANONICAL_FIT_DIAGNOSTICS
++ and matching._native_fit_irls is matching._CANONICAL_NATIVE_IRLS
++ and matching._native_ridge_module is native_module
++ and getattr(native_module, "event_fit_reason", None) is native_reason
++ and all(current is original for current, original in zip(
++ (validator, search._association_event_ceiling_uncached,
++ search._ridge_trial_classification), search._CANONICAL_CEILING_FUNCTIONS))
++ and np.geterr() == matching._NATIVE_RIDGE_ERROR_POLICY):
++ # Without the native boolean fast path, this vector's own cache
++ # makes a second scalar evidence-key lookup unnecessary. Native
++ # validation instead uses the public scalar shortcut above.
++ validator = search._association_event_ceiling_uncached
++ ceilings = [validator(fit, fit["score"], fit["f0_hz"], event, ceiling_limit)
for fit in result]
saved = result if ceiling_limit is None else _CeilingFitBatch(result, ceiling_limit, ceilings)
snapshot = pickle.dumps(saved, protocol=5)
@@@ -151,11 -151,11 +185,18 @@@ def _first_event_fits_uncached(measurem
"""
sigma = _frequency_scale(event)
fraction = event.get("quality", {}).get("source_drift_fraction", 0.02)
-- times, ridge, weights = _ridge_fit_inputs(measurement, sigma, fraction)
-- measured = np.isfinite(times) & np.isfinite(ridge) & (weights > 0)
++ private = (matching._prepared_score_input(measurement, event)
++ if _ridge_fit_inputs is matching._CANONICAL_SCORE_INPUT_FUNCTIONS[1]
++ and _frequency_scale is matching._CANONICAL_SCORE_INPUT_FUNCTIONS[0] else None)
++ if private is None:
++ times, ridge, weights = _ridge_fit_inputs(measurement, sigma, fraction)
++ measured = np.isfinite(times) & np.isfinite(ridge) & (weights > 0)
++ sample_count = measured.sum()
++ else:
++ sample_count = private.sample_count
factors, emissions = np.asarray(factors, dtype=float), np.asarray(emissions, dtype=float)
-- if (measured.sum() < 10 or factors.ndim != 2 or emissions.shape != factors.shape
-- or factors.shape[1] != measured.sum() or not len(factors)
++ if (sample_count < 10 or factors.ndim != 2 or emissions.shape != factors.shape
++ or factors.shape[1] != sample_count or not len(factors)
or not np.all(np.isfinite(factors)) or not np.all(factors > 0)
or not np.all(np.isfinite(emissions)) or fraction <= 0):
return None
@@@ -163,8 -163,8 +204,12 @@@
# Keep scalar numeric promotion for noncanonical parameter types.
if not legacy_arithmetic and any(type(value) not in (int, float) for value in (sigma, fraction, low, high)):
return None
-- y, w = ridge[measured], weights[measured]
-- independent = len(np.unique(times[measured]))
++ if private is None:
++ y, w = ridge[measured], weights[measured]
++ independent = len(np.unique(times[measured]))
++ else:
++ _, y, w = private.selected
++ independent = private.independent
diagnostic_error_policy = np.geterr()
with np.errstate(over="ignore", invalid="ignore", divide="ignore", under="ignore"):
w = w * independent / w.sum()
@@@ -283,3 -283,3 +328,7 @@@ def _fit_diagnostics(score, alpha, beta
_CANONICAL_BATCH_FITTER = _first_event_fits_uncached
_CANONICAL_FIT_DIAGNOSTICS = _fit_diagnostics
++_CANONICAL_PREFIX_FUNCTIONS = (
++ first_event_fits, _first_event_fits_with_ceilings, _cached_first_event_fits,
++ _first_event_fits_uncached, _fit_diagnostics, _batch_fit_key, _frequency_scale,
++ _ridge_fit_inputs, _fit_ridge_uncached)
diff --cc src/dopplergeist/geometry.py
index 737a83f,737a83f..75b2c04
--- a/src/dopplergeist/geometry.py
+++ b/src/dopplergeist/geometry.py
@@@ -52,6 -52,6 +52,9 @@@ def ecef(lat, lon, height)
(n * (1 - e2) + height) * np.sin(lat)), axis=-1)
++_CANONICAL_ECEF = ecef
++
++
def velocity_ecef(lat, lon, east, north, up):
lat, lon = np.deg2rad(lat), np.deg2rad(lon)
return np.stack((-np.sin(lon) * east - np.sin(lat) * np.cos(lon) * north + np.cos(lat) * np.cos(lon) * up,
@@@ -360,6 -360,6 +363,9 @@@ class Track
_NATIVE_POSITION = Track.position
_NATIVE_POSITION_VELOCITY = Track._position_velocity
++_CANONICAL_ACOUSTIC_FUNCTIONS = (Track.acoustic, Track.position, Track.speed_vector,
++ Track._position_velocity, Track._check_motion_support, Track._check_motion_support_scalar,
++ Track._unsupported_motion_intervals, _try_native_acoustic, _packed_kinematics)
def straight_doppler(t_receive, f0, speed, slant, cpa_receive, c=343.0):
diff --cc src/dopplergeist/matching.py
index d099d99,88afdcd..3c408d5
--- a/src/dopplergeist/matching.py
+++ b/src/dopplergeist/matching.py
@@@ -39,6 -39,8 +39,9 @@@ else
_native_binary_hash = hashlib.sha256(Path(_native_ridge_module.__file__).read_bytes()).hexdigest()
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_RIDGE_ERROR_POLICY = {"divide": "warn", "over": "warn", "under": "ignore", "invalid": "warn"}
@@@ -85,6 -87,6 +88,9 @@@ class _RidgeFitCache
self.screen_entries = OrderedDict()
self.screen_bytes = 0
self.screen_limit = 8 * 1024 * 1024
++ self.score_inputs = {}
++ self.score_input_bytes = 0
++ self.score_input_limit = 4 * 1024 * 1024
def prepared_measurement(self, measurement, sigma_hz, drift_fraction, key):
"""Reuse selected samples only after checking their current content key."""
@@@ -150,13 -152,13 +156,17 @@@
return _fit_ridge_uncached(*arguments, acoustic_result=acoustic_result)
# Measurements may be equivalent copies or modified arrays. Hash the
# numeric inputs actually used by the fit, without retaining their data.
-- spec, ridge, weight, _ = measurement
-- observed = hashlib.sha256()
-- for values in (spec.time, ridge, weight):
-- values = np.asarray(values, dtype=float)
-- observed.update(repr(values.shape).encode())
-- observed.update(values.tobytes())
-- measurement_key = observed.digest()
++ private = _prepared_score_input(measurement)
++ if private is None:
++ spec, ridge, weight, _ = measurement
++ observed = hashlib.sha256()
++ for values in (spec.time, ridge, weight):
++ values = np.asarray(values, dtype=float)
++ observed.update(repr(values.shape).encode())
++ observed.update(values.tobytes())
++ measurement_key = observed.digest()
++ else:
++ measurement_key = private.measurement_key
key = (id(track), parameters, measurement_key, acoustic_key)
if key in self.entries:
self.entries.move_to_end(key)
@@@ -180,7 -182,7 +190,8 @@@ def _cache_ridge_fits(function, *, max_
Each fit cache has at most 4,096 entries by default. Serialized batch
snapshots, including validated ceiling vectors, and digest bytes are bounded
to 4 MiB; Python object/container overhead is additional. Prepared measurements
-- use their own 4 MiB bound and acoustic arrays use 32 MiB. Acoustic screening
++ use their own 4 MiB bound; private immutable scoring inputs have another
++ 4 MiB budget, and acoustic arrays use 32 MiB. Acoustic screening
evidence has an additional 8 MiB budget, including conservatively charged
keys, arrays and containers. Context-local state prevents
reuse between concurrent jobs, nested batches or later calls. Cached results
@@@ -499,6 -501,6 +510,10 @@@ def prepare_events(detected_events, c=3
def _ridge_fit_inputs(measurement, sigma_hz, drift_fraction):
"""Validate the same measurement/model domain for fits and score ceilings."""
++ private = _prepared_score_input(measurement)
++ if (private is not None and type(sigma_hz) is float and type(drift_fraction) is float
++ and sigma_hz == private.sigma and drift_fraction == private.fraction):
++ return private.arrays
spec, ridge, weight, _ = measurement
times, ridge, weight = (np.asarray(a, dtype=float) for a in (spec.time, ridge, weight))
if times.shape != ridge.shape or times.shape != weight.shape or times.ndim != 1:
@@@ -530,6 -532,6 +545,11 @@@ class _PreparedRidge
def _prepare_ridge_fit_inputs(measurement, sigma_hz, drift_fraction):
++ private = _prepared_score_input(measurement)
++ if (private is not None and type(sigma_hz) is float and type(drift_fraction) is float
++ and sigma_hz == private.sigma and drift_fraction == private.fraction):
++ # Keep normalization lazy and owned by the existing scalar cache.
++ return _PreparedRidge(*private.selected)
times, ridge, weight = _ridge_fit_inputs(measurement, sigma_hz, drift_fraction)
measured = np.isfinite(times) & np.isfinite(ridge) & (weight > 0)
return _PreparedRidge(times[measured], ridge[measured], weight[measured])
@@@ -863,3 -865,3 +883,19 @@@ 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_PREFIX_FUNCTIONS = (
++ fit_ridge, _fit_ridge_uncached, _iter_acoustic_trials, _source_fit,
++ _RidgeFitCache.fit, _RidgeFitCache.acoustic_batch, _native_fit_irls,
++ _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/native_acoustic.py
index 2427b41,2427b41..56b64a7
--- a/src/dopplergeist/native_acoustic.py
+++ b/src/dopplergeist/native_acoustic.py
@@@ -62,3 -62,3 +62,7 @@@ def try_acoustic(track, t, observer, c
return None
track._check_motion_support(t, observer, c)
return output
++
++
++_CANONICAL_NATIVE = _native
++_CANONICAL_SOLVE = getattr(_native, "solve", None)
diff --cc src/dopplergeist/scoring_inputs.py
index 0000000,0000000..4c79b9b
new file mode 100644
--- /dev/null
+++ b/src/dopplergeist/scoring_inputs.py
@@@ -1,0 -1,0 +1,195 @@@
++"""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."""
++ if type(value) not in (str, float, int, bool, type(None), 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)):
++ 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)
++
++
++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 dc73f85,54f689f..5271dda
--- a/src/dopplergeist/search.py
+++ b/src/dopplergeist/search.py
@@@ -1,4 -1,4 +1,5 @@@
"""Geometric event filters, silence constraints and locked multi-event scoring."""
++from collections import OrderedDict
from itertools import product
import math
@@@ -8,7 -8,8 +9,8 @@@ 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
_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")
@@@ -423,6 -415,14 +418,18 @@@ def _association_event_ceiling(diagnost
mutation checks while avoiding repeated validation of the same clock fit.
Noncanonical numeric types retain the original validation and arithmetic.
"""
++ if _native_validate_ridge_events is not _CANONICAL_NATIVE_EVENT_VALIDATOR:
++ # A custom hook can change validity without changing scalar evidence.
++ # Preserve the full classifier and do not reuse an earlier hook's key.
++ return _association_event_ceiling_uncached(diagnostic, score, frequency, event, limit)
+ if (_native_validate_ridge_events is not None
+ and _ridge_trial_classification is _CANONICAL_CEILING_FUNCTIONS[2]
+ and _association_event_ceiling_uncached is _CANONICAL_CEILING_FUNCTIONS[1]
+ and type(limit) is float and math.isfinite(limit) and limit > 0
+ and _native_validate_ridge_events([diagnostic], [score], [frequency], [event])):
+ if -score > limit:
+ return -np.inf
+ return float(np.nextafter(1 - (-score) / limit, np.inf))
from .matching import _ridge_fit_cache
cache = _ridge_fit_cache.get()
if (cache is None or cache.max_entries <= 0 or type(diagnostic) is not dict
@@@ -508,7 -508,7 +515,9 @@@ def _association_trial_bound(result, ev
def _validate_score_ceiling_inputs(events, measurements):
"""Do not hide malformed fit inputs behind an unperformed-fit certificate."""
-- from .matching import _frequency_scale, _ridge_fit_inputs
++ from .matching import _frequency_scale, _prepared_score_tokens, _ridge_fit_inputs
++ if _prepared_score_tokens(measurements, events) is not None:
++ return
for event, measurement in zip(events, measurements):
value = _frequency_scale(event)
if not isinstance(value, (int, float, np.number)) or isinstance(value, (bool, np.bool_)):
@@@ -581,6 -581,6 +590,14 @@@ def _batched_ridge_trials(tracks, event
from .acoustic_screen import ridge_trial_rejections
from .batch_ridge import first_event_fits, _first_event_fits_with_ceilings
from .matching import _frequency_scale, fit_ridge
++ from .trial_prefix import request
++
++ reusable = request(tracks, events, measurements, observer, starts, c, acoustic_batches,
++ score_floor, use_dual, legacy_first_batch)
++ if reusable is not None:
++ previous = reusable.lookup()
++ if previous is not None:
++ return previous
diagnostics = [[] for _ in starts]
outcomes = [None] * len(starts)
@@@ -590,10 -590,10 +607,12 @@@
limits = [3 * _frequency_scale(event) for event in events] if score_floor is not None else []
bound_enabled = bool(limits) and all(np.isfinite(value) and value > 0 for value in limits)
batches = iter(acoustic_batches)
++ processed = 0
for index, (track, event, measurement) in enumerate(zip(tracks, events, measurements)):
if not active:
break
batch = next(batches)
++ processed = index + 1
if use_dual and batch is not None:
rejected, upper = ridge_trial_rejections(measurement, event, *batch,
score_floor=score_floor, group_count=len(events))
@@@ -656,9 -656,9 +675,40 @@@
else:
outcomes[trial] = (_ridge_result(diagnostics[trial]), None, None)
active = next_active
++ if reusable is not None:
++ reusable.remember(processed, outcomes, rejected_count, floor_count)
return outcomes, rejected_count, floor_count
++def _candidate_observer(lat, lon, height):
++ """Reuse the scalar ECEF operation for private, ordinary receiver inputs."""
++ from .matching import _NATIVE_RIDGE_ERROR_POLICY, _ridge_fit_cache
++
++ cache = _ridge_fit_cache.get()
++ if (cache is None or not cache.score_inputs or cache.max_entries < 1
++ or ecef is not _CANONICAL_ECEF
++ or not all(type(value) is float and math.isfinite(value) for value in (lat, lon, height))
++ or not (-90 <= lat <= 90 and -180 <= lon <= 180 and abs(height) <= 1e7)
++ or np.geterr() != _NATIVE_RIDGE_ERROR_POLICY):
++ return ecef(lat, lon, height)
++ entries = getattr(cache, "observer_entries", None)
++ if entries is None:
++ entries = cache.observer_entries = OrderedDict()
++ # Hex preserves signed zero as well as every bit of each finite coordinate.
++ key = lat.hex(), lon.hex(), height.hex()
++ value = entries.get(key)
++ if value is None:
++ result = ecef(lat, lon, height)
++ value = np.frombuffer(result.tobytes(), dtype=result.dtype).reshape(result.shape)
++ entries[key] = value
++ entries.move_to_end(key)
++ while len(entries) > min(cache.max_entries, 256):
++ entries.popitem(last=False)
++ # Each scalar ECEF call used to return a fresh array. Keep that ownership
++ # contract, including for a caller that mutates its observer later.
++ return value.copy()
++
++
def score_candidate(candidate, point, tracks, events, measurements, mode="brightness",
time_origin=None, time_sweep_s=5.0, time_step_s=0.5, c=343.0,
score_floor=None, legacy_first_batch=False):
@@@ -694,12 -694,12 +744,20 @@@
if not np.isfinite(score_floor):
raise ValueError("score_floor must be finite")
_validate_score_ceiling_inputs(events, measurements)
-- observer = ecef(point.lat,point.lon,point.elev_m)
++ observer = _candidate_observer(point.lat,point.lon,point.elev_m)
# Derive one recording start from the first event; all other events retain
# their true absolute geometry/time. This implements the locked offset.
anchor = time_origin if time_origin is not None else candidate["passes"][0]["receive_time"]-events[0]["cpa_receive_s"]
-- shifts = np.linspace(-time_sweep_s,time_sweep_s,max(1,int(np.ceil(2*time_sweep_s/time_step_s)))+1)
-- if mode == "robust_ridge" and "recording_start_bounds_s" in candidate:
++ default_count = max(1,int(np.ceil(2*time_sweep_s/time_step_s)))+1
++ bounded_clock = mode == "robust_ridge" and "recording_start_bounds_s" in candidate
++ # The bounded clock below replaces this entire array. Retain its original
++ # arithmetic/validation for unusual scalar types, ranges and error policies.
++ ordinary_clock = (bounded_clock and type(time_sweep_s) in (float, int) and type(time_step_s) in (float, int)
++ and 0 <= time_sweep_s <= 86400 and 1e-6 <= time_step_s <= 86400
++ and default_count <= 4096
++ and np.geterr() == {"divide": "warn", "over": "warn", "under": "ignore", "invalid": "warn"})
++ shifts = None if bounded_clock and ordinary_clock else np.linspace(-time_sweep_s,time_sweep_s,default_count)
++ if bounded_clock:
lower, upper = candidate["recording_start_bounds_s"]
# CPA intervals propose a shared clock; the physical model may move it
# within the same declared sweep used by legacy scoring.
diff --cc src/dopplergeist/terrain.py
index 608a803,608a803..6703db0
--- a/src/dopplergeist/terrain.py
+++ b/src/dopplergeist/terrain.py
@@@ -1,4 -1,4 +1,5 @@@
"""Terrain sampling and common projected grids."""
++from collections import OrderedDict
from functools import lru_cache
from pathlib import Path
@@@ -18,9 -18,9 +19,91 @@@ class Terrain
self.rasters = [rasterio.open(p) for p in (paths or [])]
self.constant = constant_m
self.geoid = Terrain([geoid_path]) if geoid_path else None
++ # Each sampler, including the optional geoid, retains at most 4 MiB
++ # of exact pixels and masks for scalar optimizer evaluations.
++ self._scalar_blocks = OrderedDict()
++ self._scalar_block_bytes = 0
++ self._scalar_cache_limit = 4 * 1024 * 1024
++ self._scalar_sources = {}
if not self.rasters and constant_m is None:
raise ValueError("Supply DEM raster paths or an explicit constant ground elevation")
++ @staticmethod
++ def _source_signature(paths):
++ result = []
++ for path in paths:
++ stat = path.stat()
++ result.append((stat.st_dev, stat.st_ino, stat.st_size,
++ stat.st_mtime_ns, stat.st_ctime_ns))
++ return tuple(result)
++
++ def _scalar_pixel(self, raster, row, col):
++ """Return an unchanged pixel and mask, with bounded local-file reuse."""
++ from rasterio.io import DatasetReader
++ from rasterio.windows import Window
++
++ # Custom readers, writable/closed datasets and virtual sources retain
++ # their normal read behavior. A closed reader must not return old pixels.
++ cacheable = (self._scalar_cache_limit > 0 and
++ type(raster) is DatasetReader and not raster.closed and
++ raster.mode == "r" and raster.driver == "GTiff")
++ if cacheable and raster not in self._scalar_sources:
++ paths = tuple(Path(path) for path in raster.files)
++ try:
++ signature = self._source_signature(paths) if paths else None
++ except OSError:
++ signature = None
++ self._scalar_sources[raster] = ((paths, signature)
++ if signature is not None else None)
++ source = self._scalar_sources.get(raster) if cacheable else None
++ if source is None:
++ data = raster.read(1, window=Window(col, row, 1, 1), masked=True)
++ return np.asarray(data)[0, 0], np.ma.getmaskarray(data)[0, 0]
++
++ paths, signature = source
++ try:
++ unchanged = self._source_signature(paths) == signature
++ except OSError:
++ unchanged = False
++ if not unchanged:
++ raise ValueError(f"Terrain raster changed after caching: {raster.name}")
++
++ # Keep windows inside the same underlying storage block as the requested
++ # pixel, including striped TIFFs and blocks not divisible by 64.
++ size = 64
++ block_h, block_w = raster.block_shapes[0]
++ block_r, block_c = row // block_h * block_h, col // block_w * block_w
++ r0 = block_r + (row-block_r) // size * size
++ c0 = block_c + (col-block_c) // size * size
++ key = (raster, r0, c0)
++ entry = self._scalar_blocks.get(key)
++ if entry is None:
++ height = min(size, block_r+block_h-r0, raster.height-r0)
++ width = min(size, block_c+block_w-c0, raster.width-c0)
++ data = raster.read(1, window=Window(c0, r0, width, height), masked=True)
++ pixels, mask = np.asarray(data), np.ma.getmaskarray(data)
++ try:
++ unchanged = self._source_signature(paths) == signature
++ except OSError:
++ unchanged = False
++ if not unchanged:
++ raise ValueError(f"Terrain raster changed while reading: {raster.name}")
++ byte_count = pixels.nbytes + mask.nbytes
++ if byte_count <= self._scalar_cache_limit:
++ while self._scalar_blocks and (
++ self._scalar_block_bytes + byte_count > self._scalar_cache_limit or
++ len(self._scalar_blocks) >= 256):
++ _, old = self._scalar_blocks.popitem(last=False)
++ self._scalar_block_bytes -= old[0].nbytes + old[1].nbytes
++ pixels.flags.writeable = False
++ mask.flags.writeable = False
++ self._scalar_blocks[key] = (pixels, mask)
++ self._scalar_block_bytes += byte_count
++ else:
++ self._scalar_blocks.move_to_end(key)
++ pixels, mask = entry
++ return pixels[row-r0, col-c0], mask[row-r0, col-c0]
++
def sample(self, lat, lon):
from rasterio.windows import Window
@@@ -31,6 -31,6 +114,13 @@@
x, y = transform.transform(lon.ravel(), lat.ravel())
col,row=(~raster.transform) @ (np.asarray(x),np.asarray(y))
col,row=np.floor(col).astype(int),np.floor(row).astype(int)
++ if lat.size == 1:
++ c, r = int(col[0]), int(row[0])
++ if 0 <= c < raster.width and 0 <= r < raster.height:
++ value, masked = self._scalar_pixel(raster, r, c)
++ if not masked:
++ elevation.ravel()[0] = value
++ continue
inside=(col>=0)&(col<raster.width)&(row>=0)&(row<raster.height)
values=np.ma.masked_all(len(col))
if np.any(inside):
diff --cc src/dopplergeist/tiled_search.py
index a502613,a502613..e407128
--- a/src/dopplergeist/tiled_search.py
+++ b/src/dopplergeist/tiled_search.py
@@@ -445,6 -445,6 +445,9 @@@ def _score(points, tracks, events, meas
if ledger is not None:
ledger.record(stage, [], [], 0)
return [], {"cpa_rows": 0, "candidates": 0, "unsupported_cpa_tracks": unsupported}
++ if parameters.get("mode") == "robust_ridge":
++ from .matching import _prepare_score_inputs
++ events, measurements = _prepare_score_inputs(events, measurements)
table = pd.DataFrame({name: np.concatenate([batch[name] for batch in cpa_batches])
for name in _CPA_DTYPE.names}, copy=False)
del cpa_batches, track_batches
diff --cc src/dopplergeist/trial_prefix.py
index 0000000,0000000..0f3651e
new file mode 100644
--- /dev/null
+++ b/src/dopplergeist/trial_prefix.py
@@@ -1,0 -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,
++ 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/data/real-trial-prefix.json
index 0000000,0000000..a0f7546
new file mode 100644
--- /dev/null
+++ b/tests/data/real-trial-prefix.json
@@@ -1,0 -1,0 +1,117 @@@
++{
++ "source": "Recorded point120 association traversal; real September 8 reference ridges and ADS-B observations.",
++ "calls": [
++ {
++ "track_ids": [
++ "a1cecb:5",
++ "ab50b9:11",
++ "a63e21:2",
++ "aa638e:0",
++ "ab53cf:3",
++ "a70a89:17",
++ "ab29ff:4",
++ "a9d741:16"
++ ],
++ "event_ids": [
++ "event-0003",
++ "event-0004",
++ "event-0005",
++ "event-0007",
++ "event-0008",
++ "event-0011",
++ "event-0013",
++ "event-0016"
++ ],
++ "observer": [
++ 1369639.355836627,
++ -4631239.307755372,
++ 4152078.460917809
++ ],
++ "starts": [
++ 1788898730.2187524,
++ 1788898730.7160869,
++ 1788898731.213421,
++ 1788898731.7107556,
++ 1788898732.20809,
++ 1788898732.7054243,
++ 1788898733.2027588,
++ 1788898733.7000933,
++ 1788898734.1974275,
++ 1788898734.694762,
++ 1788898735.1920965,
++ 1788898735.6894307,
++ 1788898736.1867652,
++ 1788898736.6840997,
++ 1788898737.181434,
++ 1788898737.6787684,
++ 1788898738.1761029,
++ 1788898738.673437,
++ 1788898739.1707716,
++ 1788898739.668106,
++ 1788898740.1654403,
++ 1788898740.6627748
++ ],
++ "c": 343.0,
++ "score_floor": 0.0,
++ "use_dual": true,
++ "legacy_first_batch": false,
++ "processed": 2
++ },
++ {
++ "track_ids": [
++ "a1cecb:5",
++ "ab50b9:11",
++ "a63e21:2",
++ "aa638e:0",
++ "ab53cf:3",
++ "a70a89:17",
++ "ab29ff:4",
++ "a9d741:16"
++ ],
++ "event_ids": [
++ "event-0003",
++ "event-0004",
++ "event-0005",
++ "event-0007",
++ "event-0008",
++ "event-0011",
++ "event-0014",
++ "event-0016"
++ ],
++ "observer": [
++ 1369639.355836627,
++ -4631239.307755372,
++ 4152078.460917809
++ ],
++ "starts": [
++ 1788898730.2187524,
++ 1788898730.7160869,
++ 1788898731.213421,
++ 1788898731.7107556,
++ 1788898732.20809,
++ 1788898732.7054243,
++ 1788898733.2027588,
++ 1788898733.7000933,
++ 1788898734.1974275,
++ 1788898734.694762,
++ 1788898735.1920965,
++ 1788898735.6894307,
++ 1788898736.1867652,
++ 1788898736.6840997,
++ 1788898737.181434,
++ 1788898737.6787684,
++ 1788898738.1761029,
++ 1788898738.673437,
++ 1788898739.1707716,
++ 1788898739.668106,
++ 1788898740.1654403,
++ 1788898740.6627748
++ ],
++ "c": 343.0,
++ "score_floor": 0.0,
++ "use_dual": true,
++ "legacy_first_batch": false,
++ "processed": 2
++ }
++ ]
++}
diff --cc tests/test_real_batch_ceilings.py
index c9b1ff1,c9b1ff1..36184ba
--- a/tests/test_real_batch_ceilings.py
+++ b/tests/test_real_batch_ceilings.py
@@@ -32,11 -32,11 +32,19 @@@ class RecordedBatchCeilings(unittest.Te
limit = 3 * matching._frequency_scale(event)
expected = batch_ridge.first_event_fits(measurement, event, factors, emissions)
expected_ceilings = ceilings(expected, event, limit)
-- calls = []
++ calls = {"scalar_cache": 0, "validation": 0}
++ native = matching._CANONICAL_NATIVE_EVENT_VALIDATOR
++ if native is not None:
++ self.assertTrue(all(native([fit], [fit["score"]], [fit["f0_hz"]], [event]) for fit in expected))
++ expected_calls = ({"scalar_cache": len(expected), "validation": 0} if native is not None else
++ {"scalar_cache": 0, "validation": len(expected)})
def profile(frame, action, unused):
-- if action == "call" and frame.f_code is search._association_event_ceiling.__code__:
-- calls.append(1)
++ if action == "call":
++ if frame.f_code is search._association_event_ceiling.__code__:
++ calls["scalar_cache"] += 1
++ elif frame.f_code is search._association_event_ceiling_uncached.__code__:
++ calls["validation"] += 1
@matching._cache_ridge_fits
def run():
@@@ -49,13 -49,13 +57,14 @@@
fits, upper = batch_ridge._first_event_fits_with_ceilings(
measurement, event, factors, emissions, limit)
self.assertEqual((fits, upper), (expected, expected_ceilings))
-- self.assertEqual(len(calls), len(fits))
++ self.assertEqual(calls, expected_calls)
++ self.assertFalse(cache.ceiling_entries)
fits[0].clear()
upper[0] = -123.
again = batch_ridge._first_event_fits_with_ceilings(
deepcopy(measurement), deepcopy(event), factors.copy(), emissions.copy(), limit)
self.assertEqual(again, (expected, expected_ceilings))
-- self.assertEqual(len(calls), len(expected))
++ self.assertEqual(calls, expected_calls)
self.assertEqual(batch_ridge.first_event_fits(measurement, event, factors, emissions), expected)
finally:
sys.setprofile(before)
@@@ -67,6 -67,6 +76,62 @@@
run()
self.assertIsNone(matching._ridge_fit_cache.get())
++ def test_changed_native_fitter_keeps_public_scalar_path_for_new_vectors(self):
++ measurement, event, factors, emissions = recorded()
++ limit = 3 * matching._frequency_scale(event)
++ expected = batch_ridge.first_event_fits(measurement, event, factors, emissions)
++ expected_ceilings = ceilings(expected, event, limit)
++ hooks = []
++ if matching._native_fit_irls is not None:
++ hooks.append((matching, "_native_fit_irls"))
++ for owner, name in hooks:
++ with self.subTest(hook=name):
++ @matching._cache_ridge_fits
++ def run():
++ with patch.object(owner, name, wraps=getattr(owner, name)) as hook:
++ actual = batch_ridge._first_event_fits_with_ceilings(
++ measurement, event, factors, emissions, limit)
++ self.assertEqual(actual, (expected, expected_ceilings))
++ self.assertGreater(hook.call_count, 0)
++ self.assertEqual(bool(matching._ridge_fit_cache.get().ceiling_entries),
++ matching._CANONICAL_NATIVE_EVENT_VALIDATOR is None)
++ # A complete cached vector retains its original behavior.
++ self.assertEqual(batch_ridge._first_event_fits_with_ceilings(
++ measurement, event, factors, emissions, limit), actual)
++ run()
++
++ def test_changed_boolean_aliases_and_export_do_not_reuse_a_ceiling_vector(self):
++ measurement, event, factors, emissions = recorded()
++ limit = 3 * matching._frequency_scale(event)
++ expected = batch_ridge.first_event_fits(measurement, event, factors, emissions)
++ expected_ceilings = ceilings(expected, event, limit)
++ hooks = [(matching, "_native_validate_ridge_events"), (search, "_native_validate_ridge_events")]
++ native_module = matching._native_ridge_module
++ if native_module is not None and hasattr(native_module, "valid_ridge_events"):
++ hooks.append((native_module, "valid_ridge_events"))
++ for owner, name in hooks:
++ with self.subTest(owner=owner.__name__, hook=name):
++ @matching._cache_ridge_fits
++ def run():
++ previous = batch_ridge._first_event_fits_with_ceilings(
++ measurement, event, factors, emissions, limit)
++ self.assertEqual(previous, (expected, expected_ceilings))
++ original = getattr(owner, name)
++ def custom(*args):
++ return False if original is None else original(*args)
++ with patch.object(owner, name, side_effect=custom) as hook:
++ for _ in range(2):
++ fits, upper = batch_ridge._first_event_fits_with_ceilings(
++ measurement, event, factors, emissions, limit)
++ self.assertEqual(fits, expected)
++ self.assertIsNone(upper)
++ self.assertEqual(ceilings(fits, event, limit), expected_ceilings)
++ if owner is search:
++ self.assertGreater(hook.call_count, 0)
++ self.assertEqual(batch_ridge._first_event_fits_with_ceilings(
++ measurement, event, factors, emissions, limit), previous)
++ run()
++
def test_event_limit_and_legacy_changes_keep_exact_scalar_ceilings(self):
measurement, event, factors, emissions = recorded()
limit = 3 * matching._frequency_scale(event)
diff --cc tests/test_real_candidate_setup.py
index 0000000,0000000..d2cba2e
new file mode 100644
--- /dev/null
+++ b/tests/test_real_candidate_setup.py
@@@ -1,0 -1,0 +1,119 @@@
++"""Candidate preparation controls from recorded reference inputs."""
++import json
++from pathlib import Path
++import subprocess
++import sys
++
++import numpy as np
++import pytest
++
++from dopplergeist import matching, search
++
++
++def recorded():
++ directory = Path(__file__).parent / "data"
++ ridge = json.loads((directory / "real-ridge-batch-arithmetic.json").read_text())
++ point = json.loads((directory / "real-search-workloads.json").read_text())["cases"]["previous-fit"]["point"]
++ measurement = (matching.RidgeTimes(np.asarray(ridge["times"])),
++ np.asarray(ridge["ridge"]), np.asarray(ridge["weight"]), {})
++ return ridge["event"], measurement, tuple(point[name] for name in ("lat", "lon", "elev_m"))
++
++
++def test_recorded_scalar_observer_reuse_preserves_bits_and_array_ownership():
++ event, measurement, coordinates = recorded()
++
++ @matching._cache_ridge_fits
++ def run():
++ matching._prepare_score_inputs([event], [measurement])
++ expected = search.ecef(*coordinates)
++ first, second = search._candidate_observer(*coordinates), search._candidate_observer(*coordinates)
++ assert first.tobytes() == second.tobytes() == expected.tobytes()
++ assert first is not second and first.flags.writeable and second.flags.writeable
++ first[:] = 0.
++ assert search._candidate_observer(*coordinates).tobytes() == expected.tobytes()
++ cache = matching._ridge_fit_cache.get()
++ assert len(cache.observer_entries) == 1
++ cache.max_entries = 1
++ for index in range(3):
++ changed = list(coordinates)
++ changed[index] = float(np.nextafter(changed[index], np.inf))
++ actual = search._candidate_observer(*changed)
++ assert actual.tobytes() == search.ecef(*changed).tobytes()
++ assert len(cache.observer_entries) == 1
++ run()
++
++
++def test_recorded_observer_custom_function_remains_stateful(monkeypatch):
++ event, measurement, coordinates = recorded()
++ original = search.ecef
++ calls = 0
++
++ def custom(*values):
++ nonlocal calls
++ calls += 1
++ return original(*values) + calls
++
++ @matching._cache_ridge_fits
++ def run():
++ matching._prepare_score_inputs([event], [measurement])
++ search._candidate_observer(*coordinates)
++ monkeypatch.setattr(search, "ecef", custom)
++ first, second = search._candidate_observer(*coordinates), search._candidate_observer(*coordinates)
++ assert calls == 2
++ np.testing.assert_array_equal(second - first, np.ones(3))
++ run()
++
++
++def test_recorded_observer_respects_a_hook_installed_before_search_import():
++ code = """
++import json
++from pathlib import Path
++import sys
++import numpy as np
++from dopplergeist import geometry
++original, calls = geometry.ecef, 0
++def custom(*values):
++ global calls
++ calls += 1
++ return original(*values) + calls
++geometry.ecef = custom
++from dopplergeist import matching, search
++directory = Path(sys.argv[1])
++ridge = json.loads((directory / 'real-ridge-batch-arithmetic.json').read_text())
++point = json.loads((directory / 'real-search-workloads.json').read_text())['cases']['previous-fit']['point']
++measurement = (matching.RidgeTimes(np.asarray(ridge['times'])), np.asarray(ridge['ridge']),
++ np.asarray(ridge['weight']), {})
++@matching._cache_ridge_fits
++def run():
++ matching._prepare_score_inputs([ridge['event']], [measurement])
++ values = tuple(point[name] for name in ('lat', 'lon', 'elev_m'))
++ first, second = search._candidate_observer(*values), search._candidate_observer(*values)
++ assert calls == 2
++ np.testing.assert_array_equal(second - first, np.ones(3))
++run()
++"""
++ subprocess.run([sys.executable, "-c", code, str(Path(__file__).parent / "data")], check=True)
++
++
[email protected]("changed", ["bounds", "scale", "shape"])
++def test_recorded_score_floor_validation_keeps_changed_input_failures(changed, monkeypatch):
++ event, measurement, _ = recorded()
++
++ @matching._cache_ridge_fits
++ def run():
++ events, measurements = matching._prepare_score_inputs([event], [measurement])
++ assert matching._prepared_score_tokens(measurements, events) is not None
++ assert search._validate_score_ceiling_inputs(events, measurements) is None
++ if changed == "bounds":
++ events[0]["f0_range_hz"][0] = float("nan")
++ elif changed == "scale":
++ events[0]["quality"]["frequency_loss_scale_hz"] = -1.
++ else:
++ measurements[0][0].time.shape = (1, -1)
++ with pytest.raises(ValueError) as actual:
++ search._validate_score_ceiling_inputs(events, measurements)
++ monkeypatch.setattr(matching, "_prepared_score_tokens", lambda *args: None)
++ with pytest.raises(ValueError) as expected:
++ search._validate_score_ceiling_inputs(events, measurements)
++ assert str(actual.value) == str(expected.value)
++ run()
diff --cc tests/test_real_native_validation.py
index 48954ed,c115f0c..3a8ea22
--- a/tests/test_real_native_validation.py
+++ b/tests/test_real_native_validation.py
@@@ -6,7 -6,7 +6,9 @@@ recordings, trajectories or location-ac
from copy import deepcopy
import json
from pathlib import Path
+from types import SimpleNamespace
++import sys
+ import warnings
import numpy as np
import pytest
@@@ -19,183 -20,180 +22,422 @@@ def recorded()
return fixture["legacy_diagnostics"], fixture["event"]
+ @pytest.fixture
+ def native():
+ function = search._native_validate_ridge_events
+ if function is None:
+ pytest.skip("optional native ridge extension is not installed")
+ return function
+
+
+ def outcome(function):
+ with warnings.catch_warnings(record=True) as caught:
+ warnings.simplefilter("always")
+ try:
+ value = ("result", function())
+ except Exception as error:
+ value = ("error", type(error), str(error))
+ return value, [(type(item.message), str(item.message)) for item in caught]
+
+
+ def compare(monkeypatch, native, result, events, association=False):
+ with monkeypatch.context() as patch:
+ patch.setattr(search, "_native_validate_ridge_events", None)
+ expected = outcome(lambda: search._ridge_trial_classification(result, events, association))
+ assert outcome(lambda: search._ridge_trial_classification(result, events, association)) == expected
+ return expected[0]
+
+
+ def test_recorded_fits_take_native_path_and_preserve_aggregate_checks(recorded, native, monkeypatch):
+ fits, event = recorded
+ for fit in fits:
+ result = search._ridge_result([fit])
+ assert native([fit], result["event_scores"], result["f0_hz"], [event]) is True
+ assert compare(monkeypatch, native, result, [event]) == ("result", ("evaluated", None))
+ association = matching.association_score(result, [event])
+ compare(monkeypatch, native, association, [event], True)
+ for key, value in (("score", 1.0), ("residual_score", 0.0),
+ ("score_kind", "unrecognized"), ("optimizer_success", False)):
+ changed = dict(association, **{key: value})
+ compare(monkeypatch, native, changed, [event], True)
+
+
+ def test_recorded_field_mutations_keep_all_python_failure_reasons(recorded, native, monkeypatch):
+ fits, event = recorded
+ expected_reasons = {
+ "support_fraction": (0.0, "incomplete_acoustic_support"),
+ "source_reference_emission_time_s": (np.inf, "nonfinite_fit_evidence"),
+ "sample_count": (9, "invalid_fit_evidence"),
+ "optimizer_success": (False, "failed_optimizer"),
+ "score": (-123.0, "inconsistent_event_fit_evidence"),
+ "source_drift_hz": (fits[0]["drift_bound_hz"] + 1.0, "fit_outside_declared_drift_domain"),
+ "source_drift_hz_s": (123.0, "inconsistent_source_drift"),
+ }
+ for field, (value, reason) in expected_reasons.items():
+ result = search._ridge_result([deepcopy(fits[0])])
+ result["event_diagnostics"][0][field] = value
+ assert compare(monkeypatch, native, result, [event]) == ("result", ("unknown", reason))
+ result = search._ridge_result([deepcopy(fits[0])])
+ changed = dict(event, f0_range_hz=[1.0, 2.0])
+ assert compare(monkeypatch, native, result, [changed]) == (
+ "result", ("unknown", "fit_outside_declared_frequency_domain"))
+
+ # Invalid or noncanonical replacements must reach the original validator.
+ for field in search._CEILING_FLOAT_FIELDS:
+ for value in (None, True, 1, np.float32(1), np.float64(1), np.nan, np.inf, -np.inf):
+ result = search._ridge_result([deepcopy(fits[0])])
+ result["event_diagnostics"][0][field] = value
+ assert native(result["event_diagnostics"], result["event_scores"], result["f0_hz"], [event]) is False
+ compare(monkeypatch, native, result, [event])
+ result = search._ridge_result([deepcopy(fits[0])])
+ del result["event_diagnostics"][0][field]
+ compare(monkeypatch, native, result, [event])
+
+
+ def test_native_fallback_preserves_integer_range_and_custom_types(recorded, native, monkeypatch):
+ fits, event = recorded
+
+ class Dictionary(dict):
+ pass
+
+ class Sequence(list):
+ pass
+
+ class Number(float):
+ pass
+
+ mutations = []
+ for field in ("sample_count", "independent_sample_count"):
+ for value in (True, 31.0, np.int64(31), 10**100, -1, None):
+ result = search._ridge_result([deepcopy(fits[0])])
+ result["event_diagnostics"][0][field] = value
+ mutations.append((result, [event]))
+ result = search._ridge_result([deepcopy(fits[0])])
+ result["event_diagnostics"][0]["sample_count"] = 10**100
+ result["event_diagnostics"][0]["independent_sample_count"] = 10**90
+ mutations.append((result, [event]))
+ for field in search._CEILING_FLOAT_FIELDS:
+ result = search._ridge_result([deepcopy(fits[0])])
+ result["event_diagnostics"][0][field] = Number(result["event_diagnostics"][0][field])
+ mutations.append((result, [event]))
+ result = search._ridge_result([Dictionary(fits[0])])
+ mutations.append((result, [event]))
+ for field in ("event_diagnostics", "event_scores", "f0_hz"):
+ result = search._ridge_result([deepcopy(fits[0])])
+ result[field] = Sequence(result[field])
+ mutations.append((result, [event]))
+ for changed in (Dictionary(event), dict(event, quality=Dictionary(event["quality"])),
+ dict(event, f0_range_hz=Sequence(event["f0_range_hz"]))):
+ mutations.append((search._ridge_result([deepcopy(fits[0])]), [changed]))
+ mutations.append((search._ridge_result([deepcopy(fits[0])]), Sequence([event])))
+ for result, events in mutations:
+ assert native(result["event_diagnostics"], result["event_scores"], result["f0_hz"], events) is False
+ compare(monkeypatch, native, result, events)
+
+
+ def test_native_fallback_preserves_custom_key_and_getter_calls(recorded, native, monkeypatch):
+ fits, event = recorded
+ calls = []
+
+ class Dictionary(dict):
+ def get(self, *args):
+ calls.append(args)
+ return super().get(*args)
+
+ result = search._ridge_result([Dictionary(fits[0])])
+ assert native(result["event_diagnostics"], result["event_scores"], result["f0_hz"], [event]) is False
+ assert calls == []
+ compare(monkeypatch, native, result, [event])
+ midpoint = len(calls) // 2
+ assert calls[:midpoint] == calls[midpoint:]
+
+ class Key(str):
+ pass
+
+ for key in (17, Key("extra")):
+ result = search._ridge_result([dict(fits[0], extra=1)])
+ result["event_diagnostics"][0][key] = result["event_diagnostics"][0].pop("extra")
+ assert native(result["event_diagnostics"], result["event_scores"], result["f0_hz"], [event]) is False
+ compare(monkeypatch, native, result, [event])
+
+
+ def test_default_fraction_and_current_mutable_values_are_rechecked(recorded, native, monkeypatch):
+ fits, event = recorded
+ result = search._ridge_result([deepcopy(fits[0])])
+ for changed in (event, {"f0_range_hz": tuple(event["f0_range_hz"])},
+ dict(event, quality={"source_drift_fraction": 0.02})):
+ assert native(result["event_diagnostics"], result["event_scores"], result["f0_hz"], [changed]) is True
+ compare(monkeypatch, native, result, [changed])
+ for field in ("score", "support_fraction", "f0_hz"):
+ original = result["event_diagnostics"][0][field]
+ result["event_diagnostics"][0][field] = np.nextafter(original, np.inf).item()
+ compare(monkeypatch, native, result, [event])
+ result["event_diagnostics"][0][field] = original
+ assert native(result["event_diagnostics"], result["event_scores"], result["f0_hz"], [event]) is True
+
+
+ def test_native_ceiling_preserves_numpy_policies_and_custom_classifier(recorded, native, monkeypatch):
+ fits, event = recorded
+ fit = fits[0]
+ # Equality with the observed loss gives a zero contribution. nextafter
+ # then exercises the original NumPy underflow warning/exception behavior.
+ limits = (-fit["score"], np.nextafter(-fit["score"], np.inf).item(), 3.0,
+ np.float64(3.0), 3, 0.0, np.inf, np.nan)
+ for policy in ("ignore", "warn", "raise"):
+ for limit in limits:
+ arguments = (fit, fit["score"], fit["f0_hz"], event, limit)
+ with np.errstate(under=policy):
+ with monkeypatch.context() as patch:
+ patch.setattr(search, "_native_validate_ridge_events", None)
+ expected = outcome(lambda: search._association_event_ceiling(*arguments))
+ actual = outcome(lambda: search._association_event_ceiling(*arguments))
+ # NaN ceilings compare via their serialized value.
+ assert repr(actual) == repr(expected)
+ with monkeypatch.context() as patch:
+ patch.setattr(search, "_ridge_trial_classification", lambda *args: ("unknown", "custom"))
+ assert search._association_event_ceiling(fit, fit["score"], fit["f0_hz"], event, 3.0) == 1.0
+ with monkeypatch.context() as patch:
+ patch.setattr(search, "_association_event_ceiling_uncached", lambda *args: 0.125)
+ assert search._association_event_ceiling(fit, fit["score"], fit["f0_hz"], event, 3.0) == 0.125
++
++
++def load_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):
++def reason_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)
++ patch.setattr(search, "_native_validate_ridge_events", None)
++ expected = reason_outcome(search._ridge_trial_classification, result, events, association)
++ actual = reason_outcome(search._ridge_trial_classification, result, events, association)
+ assert actual == expected
+
+
- def native():
++def native_reason():
+ 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()
++def test_native_recorded_event_evidence_and_final_score_parity(native_reason, monkeypatch):
++ diagnostics, event = load_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 native_reason(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()
++def test_native_rechecks_every_recorded_diagnostic_field(native_reason, monkeypatch):
++ diagnostics, event = load_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()
++def test_native_recorded_frequency_and_drift_domain_parity(native_reason, monkeypatch):
++ diagnostics, event = load_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()
++def test_native_reason_order_and_in_place_mutation(native_reason, monkeypatch):
++ diagnostics, event = load_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
++ assert native_reason(*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 native_reason(*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 native_reason(*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()
++def test_native_source_drift_tolerance_boundary_keeps_python_arithmetic(native_reason, monkeypatch):
++ diagnostics, event = load_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):
++def test_native_custom_types_and_older_extension_keep_python_fallback(native_reason, monkeypatch):
+ class CustomDict(dict):
+ pass
+
+ class CustomList(list):
+ pass
+
+ class CustomString(str):
+ pass
+
- diagnostics, event = recorded()
++ diagnostics, event = load_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
++ assert native_reason(*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])
++ expected = reason_outcome(search._ridge_trial_classification, result, standard[3])
+ with monkeypatch.context() as patch:
+ patch.setattr(matching, "_native_ridge_module", SimpleNamespace())
- assert_python_parity(monkeypatch, result, standard[3])
++ patch.setattr(search, "_native_validate_ridge_events", None)
++ assert reason_outcome(search._ridge_trial_classification, result, standard[3]) == expected
++
++
++def test_both_native_interfaces_preserve_container_and_integer_fallbacks(recorded, native, native_reason):
++ fits, event = recorded
++ result = search._ridge_result([deepcopy(fits[0])])
++ arguments = (result["event_diagnostics"], result["event_scores"], result["f0_hz"])
++ assert native(*arguments, [event]) is True
++ assert native_reason(*arguments, [event]) == 0
++ assert native(*arguments, (event,)) is False
++ assert native_reason(*arguments, (event,)) == 0
++ # The boolean API retains upstream's platform-sized integer gate. Larger
++ # Python integers must still reach the original unbounded validator.
++ result["event_diagnostics"][0]["sample_count"] = sys.maxsize + 1
++ assert native(*arguments, [event]) is False
++ assert native_reason(*arguments, [event]) is None
++
++
++def test_custom_boolean_validator_preserves_nonfinite_aggregate_ceiling(recorded, monkeypatch):
++ fits, event = recorded
++ fit = dict(fits[0], score=float("nan"))
++ arguments = (fit, fit["score"], fit["f0_hz"], event, 3.0)
++ monkeypatch.setattr(search, "_native_validate_ridge_events", lambda *args: True)
++ expected = outcome(lambda: search._association_event_ceiling_uncached(*arguments))
++ assert expected == (("result", 1.0), [])
++ assert outcome(lambda: search._association_event_ceiling(*arguments)) == expected
++
++
++def test_changed_boolean_validator_does_not_reuse_cached_ceiling(recorded, monkeypatch):
++ fits, event = recorded
++ fit = dict(fits[0], support_fraction=0.0)
++ arguments = (fit, fit["score"], fit["f0_hz"], event, 3.0)
++ calls = []
++
++ def custom_validator(*args):
++ calls.append(args)
++ return True
++
++ @matching._cache_ridge_fits
++ def exercise():
++ cache = matching._ridge_fit_cache.get()
++ assert search._association_event_ceiling(*arguments) == 1.0
++ assert len(cache.ceiling_entries) == 1
++ with monkeypatch.context() as patch:
++ patch.setattr(search, "_native_validate_ridge_events", custom_validator)
++ expected = search._association_event_ceiling_uncached(*arguments)
++ assert expected != 1.0
++ calls.clear()
++ assert search._association_event_ceiling(*arguments) == expected
++ assert len(calls) == 1
++ assert len(cache.ceiling_entries) == 1
++ assert search._association_event_ceiling(*arguments) == 1.0
++
++ exercise()
diff --cc tests/test_real_scalar_terrain.py
index 0000000,0000000..5dcb65c
new file mode 100644
--- /dev/null
+++ b/tests/test_real_scalar_terrain.py
@@@ -1,0 -1,0 +1,139 @@@
++"""Exact scalar terrain reuse on restored rasters and recorded candidate points."""
++import json
++import os
++from pathlib import Path
++import shutil
++
++import numpy as np
++import pytest
++import rasterio
++
++from dopplergeist.terrain import Terrain
++
++
[email protected](scope="module")
++def observed_terrain():
++ root = Path(os.environ.get("DOPPLERGEIST_REAL_REFERENCE_ROOT",
++ Path(__file__).resolve().parents[1]))
++ dem = root / "data/terrain/Copernicus_DSM_COG_10_N40_00_W074_00_DEM.tif"
++ geoid = root / "data/terrain/us_nga_egm96_15.tif"
++ if not dem.exists() or not geoid.exists():
++ pytest.skip("Restore the real reference DEM and geoid rasters")
++ trace = os.environ.get("DOPPLERGEIST_REAL_TERRAIN_TRACE")
++ if trace:
++ rows = json.loads(Path(trace).read_text())["rows"]
++ points = np.asarray(rows, dtype=float)[:, :2]
++ # Retain actual requests throughout the optimizer trace, without
++ # constructing new coordinates or a simulated search workload.
++ points = points[np.linspace(0, len(points)-1, min(256, len(points)), dtype=int)]
++ else:
++ choices = [root / "results/reference-local-fast/ranked.json",
++ root / "evidence/results/reference-local-fast/ranked.json"]
++ source = next((path for path in choices if path.exists()), None)
++ if source is None:
++ pytest.skip("Restore the archived real reference candidate results")
++ rows = json.loads(source.read_text())
++ points = np.asarray([(row["lat"], row["lon"]) for row in rows])
++ assert len(points) > 1
++ return dem, geoid, points
++
++
++def _close(sampler):
++ for raster in sampler.rasters:
++ raster.close()
++ if sampler.geoid is not None:
++ _close(sampler.geoid)
++
++
[email protected]("with_geoid", [False, True])
++def test_scalar_cache_preserves_real_vector_samples_and_result_ownership(observed_terrain, with_geoid):
++ dem, geoid, points = observed_terrain
++ sampler = Terrain([dem], geoid_path=geoid if with_geoid else None)
++ try:
++ expected = sampler.sample(points[:, 0], points[:, 1])
++ for _ in range(2):
++ actual = np.asarray([sampler.sample(lat, lon).item() for lat, lon in points])
++ np.testing.assert_array_equal(actual, expected)
++ assert sampler._scalar_blocks
++ lat, lon = points[0]
++ result = sampler.sample([[lat]], [[lon]])
++ assert result.shape == (1, 1)
++ assert result.item() == expected[0]
++ result[...] = np.nan
++ assert sampler.sample(lat, lon).item() == expected[0]
++ for pixels, mask in sampler._scalar_blocks.values():
++ assert not pixels.flags.writeable and not mask.flags.writeable
++ finally:
++ _close(sampler)
++
++
++def test_scalar_cache_stays_bounded_on_actual_points(observed_terrain):
++ dem, _, points = observed_terrain
++ sampler = Terrain([dem])
++ sampler._scalar_cache_limit = 21_000
++ try:
++ expected = sampler.sample(points[:, 0], points[:, 1])
++ for point, height in zip(points, expected):
++ assert sampler.sample(*point).item() == height
++ assert sampler._scalar_block_bytes <= sampler._scalar_cache_limit
++ assert sampler._scalar_block_bytes == sum(
++ pixels.nbytes + mask.nbytes for pixels, mask in sampler._scalar_blocks.values())
++ # The same observed points also take the explicit uncached read path.
++ sampler._scalar_cache_limit = 0
++ np.testing.assert_array_equal(
++ [sampler.sample(*point).item() for point in points], expected)
++ finally:
++ _close(sampler)
++
++
++def test_changed_real_raster_is_not_served_from_cache(observed_terrain, tmp_path):
++ dem, _, points = observed_terrain
++ copy = tmp_path / dem.name
++ shutil.copy2(dem, copy)
++ sampler = Terrain([copy])
++ try:
++ sampler.sample(*points[0])
++ stat = copy.stat()
++ # Change file metadata, keeping the real measured terrain untouched.
++ os.utime(copy, ns=(stat.st_atime_ns, stat.st_mtime_ns + 1_000_000))
++ with pytest.raises(ValueError, match="Terrain raster changed after caching"):
++ sampler.sample(*points[0])
++ finally:
++ _close(sampler)
++
++
++def test_closed_real_reader_does_not_serve_cached_pixels(observed_terrain):
++ dem, _, points = observed_terrain
++ sampler = Terrain([dem])
++ sampler.sample(*points[0])
++ sampler.rasters[0].close()
++ with pytest.raises(rasterio.errors.RasterioIOError):
++ sampler.sample(*points[0])
++
++
++def test_custom_reader_keeps_its_read_behavior(observed_terrain):
++ dem, _, points = observed_terrain
++
++ class ObservedReader:
++ def __init__(self, reader):
++ self.reader, self.windows = reader, []
++
++ def __getattr__(self, name):
++ return getattr(self.reader, name)
++
++ def read(self, *args, **kwargs):
++ self.windows.append(kwargs["window"])
++ return self.reader.read(*args, **kwargs)
++
++ sampler = Terrain([dem])
++ reader = ObservedReader(sampler.rasters[0])
++ sampler.rasters = [reader]
++ try:
++ first = sampler.sample(*points[0])
++ second = sampler.sample(*points[0])
++ np.testing.assert_array_equal(first, second)
++ assert len(reader.windows) == 2
++ assert all(window.width == window.height == 1 for window in reader.windows)
++ assert not sampler._scalar_blocks
++ finally:
++ _close(sampler)
diff --cc tests/test_real_scoring_inputs.py
index 0000000,0000000..d4d53f8
new file mode 100644
--- /dev/null
+++ b/tests/test_real_scoring_inputs.py
@@@ -1,0 -1,0 +1,239 @@@
++"""Private scoring snapshots using archived real ridge/flight fit inputs."""
++from copy import deepcopy
++import json
++from pathlib import Path
++import subprocess
++import sys
++
++import numpy as np
++import pytest
++
++from dopplergeist import batch_ridge, matching
++
++
++def recorded():
++ fixture = json.loads((Path(__file__).parent / "data/real-ridge-batch-arithmetic.json").read_text())
++ measurement = (matching.RidgeTimes(np.asarray(fixture["times"])),
++ np.asarray(fixture["ridge"]), np.asarray(fixture["weight"]), {})
++ return fixture["event"], measurement
++
++
++def arrays(measurement):
++ return measurement[0].time, measurement[1], measurement[2]
++
++
++def test_recorded_snapshot_is_private_and_immutable():
++ event, measurement = recorded()
++ original = deepcopy((event, measurement))
++
++ @matching._cache_ridge_fits
++ def run():
++ events, measurements = matching._prepare_score_inputs([event], [measurement])
++ copied_event, copied = events[0], measurements[0]
++ token = matching._prepared_score_token(copied, copied_event)
++ assert token is not None and matching._prepared_score_token(measurement, event) is None
++ assert copied_event == event and copied_event is not event
++ for new, previous in zip(arrays(copied), arrays(measurement)):
++ np.testing.assert_array_equal(new, previous)
++ assert new.dtype == previous.dtype and new.strides == previous.strides
++ with pytest.raises(ValueError):
++ new.setflags(write=True)
++ event["f0_range_hz"][0] = 0.0
++ measurement[1][0] = -1.0
++ assert copied_event == original[0]
++ np.testing.assert_array_equal(copied[1], original[1][1])
++ assert matching._prepared_score_token(copied, copied_event) is token
++ cache = matching._ridge_fit_cache.get()
++ assert 0 < cache.score_input_bytes <= cache.score_input_limit
++
++ run()
++ assert matching._ridge_fit_cache.get() is None
++
++
[email protected]("changed", ["bounds", "fraction", "scale", "time", "shape", "dtype"])
++def test_recorded_snapshot_token_rejects_changed_settings_or_view_metadata(changed):
++ event, measurement = recorded()
++
++ @matching._cache_ridge_fits
++ def run():
++ events, measurements = matching._prepare_score_inputs([event], [measurement])
++ copied_event, copied = events[0], measurements[0]
++ assert matching._prepared_score_token(copied, copied_event) is not None
++ if changed == "bounds":
++ copied_event["f0_range_hz"][0] = float(np.nextafter(copied_event["f0_range_hz"][0], np.inf))
++ elif changed in ("fraction", "scale"):
++ copied_event["quality"]["source_drift_fraction" if changed == "fraction" else "frequency_loss_scale_hz"] = 0.0
++ elif changed == "time":
++ copied[0].time = copied[0].time.copy()
++ elif changed == "shape":
++ copied[1].shape = (1, -1)
++ else:
++ copied[1].dtype = np.int64
++ assert matching._prepared_score_token(copied, copied_event) is None
++
++ run()
++
++
[email protected]("kind", ["weight", "bounds", "noncontiguous", "custom", "budget"])
++def test_unsupported_recorded_inputs_keep_the_original_path(kind):
++ event, measurement = recorded()
++ if kind == "weight":
++ measurement[2][0] = -1.0
++ elif kind == "bounds":
++ event["f0_range_hz"][0] = float("nan")
++ elif kind == "noncontiguous":
++ measurement = (matching.RidgeTimes(measurement[0].time[::2]),
++ measurement[1][::2], measurement[2][::2], {})
++ elif kind == "custom":
++ class Custom(dict):
++ def __deepcopy__(self, memo):
++ raise AssertionError("Custom copy hook executed")
++ def __sizeof__(self):
++ raise AssertionError("Custom size hook executed")
++ event = dict(event, ignored_custom_metadata=Custom())
++
++ @matching._cache_ridge_fits
++ def run():
++ if kind == "budget":
++ matching._ridge_fit_cache.get().score_input_limit = 1
++ events, measurements = matching._prepare_score_inputs([event], [measurement])
++ assert events[0] is event and measurements[0] is measurement
++ assert matching._prepared_score_token(measurement, event) is None
++ assert not matching._ridge_fit_cache.get().score_inputs
++
++ run()
++
++
++def test_custom_validation_hooks_and_nested_contexts_do_not_share_tokens(monkeypatch):
++ event, measurement = recorded()
++
++ @matching._cache_ridge_fits
++ def outer():
++ events, measurements = matching._prepare_score_inputs([event], [measurement])
++ copied_event, copied = events[0], measurements[0]
++ token = matching._prepared_score_token(copied, copied_event)
++ assert token is not None
++
++ @matching._cache_ridge_fits
++ def inner():
++ assert matching._prepared_score_token(copied, copied_event) is None
++ inner()
++ assert matching._prepared_score_token(copied, copied_event) is token
++ with monkeypatch.context() as patch:
++ patch.setattr(matching, "_ridge_fit_inputs", lambda *args: None)
++ assert matching._prepared_score_token(copied, copied_event) is None
++ events, measurements = matching._prepare_score_inputs([event], [measurement])
++ assert events[0] is event and measurements[0] is measurement
++ with monkeypatch.context() as patch:
++ patch.setattr(matching, "_fit_ridge_uncached", lambda *args, **kwargs: None)
++ assert matching._prepared_score_token(copied, copied_event) is None
++ events, measurements = matching._prepare_score_inputs([event], [measurement])
++ assert events[0] is event and measurements[0] is measurement
++
++ outer()
++
++
++def test_recorded_snapshot_does_not_evaluate_custom_settings_or_nondefault_errors():
++ event, measurement = recorded()
++
++ class CustomScale(float):
++ def __gt__(self, other):
++ raise AssertionError("Custom comparison executed")
++
++ def __float__(self):
++ raise AssertionError("Custom conversion executed")
++
++ @matching._cache_ridge_fits
++ def run():
++ events, measurements = matching._prepare_score_inputs([event], [measurement])
++ assert matching._prepared_score_token(measurements[0], events[0]) is not None
++ with np.errstate(over="raise"):
++ assert matching._prepared_score_token(measurements[0], events[0]) is None
++ unchanged_events, unchanged_measurements = matching._prepare_score_inputs([event], [measurement])
++ assert unchanged_events[0] is event and unchanged_measurements[0] is measurement
++ event["quality"]["frequency_uncertainty_hz"] = CustomScale(1.0)
++ unchanged_events, unchanged_measurements = matching._prepare_score_inputs([event], [measurement])
++ assert unchanged_events[0] is event and unchanged_measurements[0] is measurement
++
++ run()
++
++
++def test_direct_scoring_input_import_order():
++ subprocess.run([sys.executable, "-c",
++ "import dopplergeist.scoring_inputs; import dopplergeist.matching; "
++ "import dopplergeist.batch_ridge"], check=True)
++
++
[email protected]("changed_index", [0, 1, 2])
++def test_bulk_tokens_validate_every_recorded_snapshot(changed_index, monkeypatch):
++ observed = [recorded() for _ in range(3)]
++ original_events = [pair[0] for pair in observed]
++ original_measurements = [pair[1] for pair in observed]
++
++ @matching._cache_ridge_fits
++ def run():
++ events, measurements = matching._prepare_score_inputs(original_events, original_measurements)
++ tokens = matching._prepared_score_tokens(measurements, events)
++ assert tokens == tuple(matching._prepared_score_token(measurement, event)
++ for measurement, event in zip(measurements, events))
++ assert len(tokens) == 3 and all(token is not None for token in tokens)
++ assert matching._prepared_score_tokens(original_measurements, original_events) is None
++ assert matching._prepared_score_tokens(measurements[:-1], events) is None
++ with np.errstate(under="raise"):
++ assert matching._prepared_score_tokens(measurements, events) is None
++ with monkeypatch.context() as patch:
++ original = matching._source_fit
++ patch.setattr(matching, "_source_fit", lambda *args, **kwargs: original(*args, **kwargs))
++ assert matching._prepared_score_tokens(measurements, events) is None
++ assert matching._prepared_score_tokens(measurements, events) == tokens
++ events[changed_index]["f0_range_hz"][0] = float("nan")
++ assert matching._prepared_score_tokens(measurements, events) is None
++ return measurements, events
++
++ measurements, events = run()
++ assert matching._prepared_score_tokens(measurements, events) is None
++
++
[email protected]("layout", ["C", "F", "reversed"])
++def test_recorded_prepared_keys_and_every_batch_fit_are_exact(layout):
++ event, measurement = recorded()
++ fixture = json.loads((Path(__file__).parent / "data/real-ridge-batch-arithmetic.json").read_text())
++ factors, emissions = np.asarray(fixture["factors"]), np.asarray(fixture["emissions"])
++ if layout == "F":
++ factors, emissions = np.asfortranarray(factors), np.asfortranarray(emissions)
++ elif layout == "reversed":
++ factors, emissions = factors[::-1], emissions[::-1]
++ expected_key = batch_ridge._batch_fit_key(measurement, event, factors, emissions)
++ expected = batch_ridge._first_event_fits_uncached(measurement, event, factors, emissions)
++ assert expected is not None
++ sigma = matching._frequency_scale(event)
++ fraction = event["quality"].get("source_drift_fraction", .02)
++ expected_scalar = [matching._fit_ridge_uncached(None, None, measurement, 0.,
++ event["f0_range_hz"], 343., sigma, fraction,
++ acoustic_result=(factor, emission))
++ for factor, emission in zip(factors, emissions)]
++
++ @matching._cache_ridge_fits
++ def run():
++ events, measurements = matching._prepare_score_inputs([event], [measurement])
++ copied_event, copied = events[0], measurements[0]
++ token = matching._prepared_score_token(copied, copied_event)
++ assert token is not None
++ assert batch_ridge._batch_fit_key(copied, copied_event, factors, emissions) == expected_key
++ assert batch_ridge._first_event_fits_uncached(copied, copied_event, factors, emissions) == expected
++ prepared = matching._prepare_ridge_fit_inputs(copied, sigma, fraction)
++ assert all(actual is original for actual, original in
++ zip((prepared.times, prepared.ridge, prepared.weight), token.selected))
++ assert prepared.normalized_weight is None
++ for index, (factor, emission) in enumerate(zip(factors, emissions)):
++ assert matching.fit_ridge(None, None, copied, 0., copied_event["f0_range_hz"],
++ 343., sigma, fraction, acoustic_result=(factor, emission)) == expected_scalar[index]
++ # Invalid model parameters retain their original validation even when
++ # the numerical measurement itself is an unchanged private snapshot.
++ with pytest.raises(ValueError, match="drift fraction"):
++ matching._ridge_fit_inputs(copied, sigma, .2)
++ copied_event["quality"]["source_drift_fraction"] = .01
++ assert matching._prepared_score_token(copied, copied_event) is None
++ assert batch_ridge._batch_fit_key(copied, copied_event, factors, emissions) != expected_key
++
++ run()
diff --cc tests/test_real_trial_prefix.py
index 0000000,0000000..49df7a9
new file mode 100644
--- /dev/null
+++ b/tests/test_real_trial_prefix.py
@@@ -1,0 -1,0 +1,337 @@@
++"""Eliminated-prefix reuse on recorded ridge/ADS-B association trials."""
++import json
++import os
++from pathlib import Path
++import subprocess
++import sys
++
++import numpy as np
++import pytest
++
++from dopplergeist import acoustic_screen, batch_ridge, geometry, matching, search, trial_prefix
++from dopplergeist.data_access import FlightIndex
++
++
[email protected](scope="module")
++def recorded():
++ source = os.environ.get("DOPPLERGEIST_REAL_CPA_FIXTURE")
++ if not source:
++ pytest.skip("set DOPPLERGEIST_REAL_CPA_FIXTURE to the recorded reference CPA diagnostic")
++ fixture = json.loads(Path(source).read_text())
++ witness = json.loads((Path(__file__).parent / "data/real-trial-prefix.json").read_text())
++ events, measurements = matching.prepare_events(fixture["events"])
++ names = sorted({name for call in witness["calls"] for name in call["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["calls"]
++
++
++def prepared_calls(recorded):
++ events, measurements, tracks, specifications = recorded
++ events, measurements = matching._prepare_score_inputs(events, measurements)
++ by_id = {event["event_id"]: (event, measurement) for event, measurement in zip(events, measurements)}
++ calls = []
++ for specification in specifications:
++ selected = [by_id[name] for name in specification["event_ids"]]
++ calls.append(dict(tracks=[tracks[name] for name in specification["track_ids"]],
++ events=[pair[0] for pair in selected], measurements=[pair[1] for pair in selected],
++ observer=np.asarray(specification["observer"], dtype=float),
++ starts=np.asarray(specification["starts"], dtype=float), c=specification["c"],
++ score_floor=specification["score_floor"], use_dual=specification["use_dual"],
++ legacy_first_batch=specification["legacy_first_batch"]))
++ return calls
++
++
++def acoustic(call, max_samples=262144):
++ return matching._iter_acoustic_trials(call["tracks"], call["measurements"], call["observer"],
++ call["starts"], call["c"], max_samples=max_samples)
++
++
++def evaluate(call, batches=None):
++ return search._batched_ridge_trials(**call, acoustic_batches=acoustic(call) if batches is None else batches)
++
++
++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_real_eliminated_prefix_reuses_exact_outcomes_for_a_different_suffix(recorded, monkeypatch):
++ @matching._cache_ridge_fits
++ def run():
++ first, second = prepared_calls(recorded)
++ with monkeypatch.context() as patch:
++ patch.setattr(trial_prefix, "_MAX_ENTRIES", 0)
++ expected = [identity(evaluate(call)) for call in (first, second)]
++ assert expected[0] == expected[1]
++ before = trial_prefix.cache_stats()
++ initial = evaluate(first)
++ assert identity(initial) == expected[0]
++ memo = matching._ridge_fit_cache.get().prefix_memo
++ assert len(memo.entries) == 1
++ assert memo.size + sys.getsizeof(memo.entries) <= trial_prefix._MAX_BYTES
++ key, (value, retained, size) = next(iter(memo.entries.items()))
++ assert size == trial_prefix._size(key) + trial_prefix._size(value) + sys.getsizeof(retained) + 256
++ initial[0][0] = (None, 12345., None)
++ assert identity(evaluate(second)) == expected[1]
++ after = trial_prefix.cache_stats()
++ assert after["hits"] == before["hits"] + 1
++ assert after["saved_event_steps"] == before["saved_event_steps"] + 2
++ assert after["saved_trials"] == before["saved_trials"] + len(first["starts"])
++ return first
++
++ previous = run()
++ assert trial_prefix.cache_stats()["entries"] == 0
++
++ @matching._cache_ridge_fits
++ def separate_scope():
++ assert trial_prefix.request(**previous, acoustic_batches=acoustic(previous)) is None
++ separate_scope()
++
++
++def test_real_prefix_keeps_finite_unknown_bounds_and_support_exceptions(recorded, monkeypatch):
++ @matching._cache_ridge_fits
++ def bounded():
++ call = prepared_calls(recorded)[0]
++ call["score_floor"] = float(len(call["events"])) - .05
++ with monkeypatch.context() as patch:
++ patch.setattr(trial_prefix, "_MAX_ENTRIES", 0)
++ expected = evaluate(call)
++ assert all(result is None and error is None for result, upper, error in expected[0])
++ assert any(np.isfinite(upper) for result, upper, error in expected[0])
++ assert identity(evaluate(call)) == identity(expected)
++ before = trial_prefix.cache_stats()["hits"]
++ assert identity(evaluate(call)) == identity(expected)
++ assert trial_prefix.cache_stats()["hits"] == before + 1
++ bounded()
++
++ @matching._cache_ridge_fits
++ def unsupported():
++ call = prepared_calls(recorded)[0]
++ # The recorded aircraft motion cannot support this sound-speed control.
++ # Its typed physical-model failures must be produced afresh, not stored.
++ call["c"] = 1.0
++ first, second = evaluate(call), evaluate(call)
++ assert identity(first) == identity(second)
++ errors = [(a[2], b[2]) for a, b in zip(first[0], second[0]) if a[2] is not None]
++ assert errors and all(a is not b for a, b in errors)
++ assert not matching._ridge_fit_cache.get().prefix_memo.entries
++ unsupported()
++
++
[email protected]("groups,direction", [(2, 0), (3, -1), (3, 0), (3, 1), (8, -1), (8, 0), (8, 1)])
++def test_real_residual_prefix_domain_keeps_outward_boundary_and_final_event(recorded, monkeypatch, groups, direction):
++ @matching._cache_ridge_fits
++ def run():
++ source = prepared_calls(recorded)[0]
++ target = dict(source)
++ for key in ("tracks", "events", "measurements"):
++ target[key] = source[key][:groups]
++ floor = float(groups - 2) # This recorded prefix eliminates all clocks after two groups.
++ if direction:
++ floor = float(np.nextafter(floor, np.inf if direction > 0 else -np.inf))
++ target["score_floor"] = floor
++ with monkeypatch.context() as patch:
++ patch.setattr(trial_prefix, "_MAX_ENTRIES", 0)
++ expected = identity(evaluate(target))
++ evaluate(source)
++ before = trial_prefix.cache_stats()["residual_hits"]
++ assert identity(evaluate(target)) == expected
++ eligible = groups > 2 and floor <= groups - 2
++ assert trial_prefix.cache_stats()["residual_hits"] == before + eligible
++ run()
++
++
++def test_real_residual_prefix_can_originate_at_a_positive_safe_floor(recorded, monkeypatch):
++ @matching._cache_ridge_fits
++ def run():
++ target = prepared_calls(recorded)[0]
++ source = dict(target, score_floor=float(len(target["events"]) - 2))
++ with monkeypatch.context() as patch:
++ patch.setattr(trial_prefix, "_MAX_ENTRIES", 0)
++ expected = identity(evaluate(target))
++ assert identity(evaluate(source)) == expected
++ before = trial_prefix.cache_stats()["residual_hits"]
++ assert identity(evaluate(target)) == expected
++ assert trial_prefix.cache_stats()["residual_hits"] == before + 1
++ run()
++
++
[email protected]("changed", ["floor", "observer", "clock", "c", "groups", "dual", "legacy", "track"])
++def test_real_prefix_keys_preserve_changed_scoring_controls(recorded, monkeypatch, changed):
++ @matching._cache_ridge_fits
++ def run():
++ original = prepared_calls(recorded)[0]
++ altered = dict(original)
++ if changed in ("floor", "c"):
++ key = "score_floor" if changed == "floor" else "c"
++ altered[key] = float(np.nextafter(original[key], np.inf))
++ elif changed in ("observer", "clock"):
++ key = "observer" if changed == "observer" else "starts"
++ altered[key] = original[key].copy()
++ altered[key][0] = np.nextafter(altered[key][0], np.inf)
++ elif changed == "groups":
++ for key in ("events", "measurements", "tracks"):
++ altered[key] = original[key][:-1]
++ elif changed in ("dual", "legacy"):
++ key = "use_dual" if changed == "dual" else "legacy_first_batch"
++ altered[key] = not original[key]
++ else:
++ altered["tracks"] = list(original["tracks"])
++ altered["tracks"][0] = original["tracks"][1]
++ with monkeypatch.context() as patch:
++ patch.setattr(trial_prefix, "_MAX_ENTRIES", 0)
++ expected = identity(evaluate(altered))
++ evaluate(original)
++ before = trial_prefix.cache_stats()["hits"]
++ assert identity(evaluate(altered)) == expected
++ assert trial_prefix.cache_stats()["hits"] == before + (changed in ("floor", "groups"))
++ run()
++
++
[email protected]("changed", ["bounds", "weight_shape", "clock_budget", "iterator", "policy"])
++def test_real_prefix_bypasses_changed_or_noncanonical_inputs(recorded, monkeypatch, changed):
++ @matching._cache_ridge_fits
++ def run():
++ call = prepared_calls(recorded)[0]
++ evaluate(call)
++ if changed == "bounds":
++ call["events"][0]["f0_range_hz"][0] = float("nan")
++ elif changed == "weight_shape":
++ call["measurements"][0][2].shape = (1, -1)
++ if changed == "clock_budget":
++ batches = acoustic(call, max_samples=1)
++ request = trial_prefix.request(**call, acoustic_batches=batches)
++ assert request is not None and request.lookup() is None
++ elif changed == "iterator":
++ assert trial_prefix.request(**call, acoustic_batches=iter(())) is None
++ batches = acoustic(call)
++ next(batches)
++ assert trial_prefix.request(**call, acoustic_batches=batches) is None
++ elif changed == "policy":
++ with np.errstate(under="raise"):
++ assert trial_prefix.request(**call, acoustic_batches=acoustic(call)) is None
++ else:
++ assert trial_prefix.request(**call, acoustic_batches=acoustic(call)) is None
++ run()
++
++
[email protected]("owner,name", [(matching, "_fit_ridge_uncached"), (matching, "_iter_acoustic_trials"),
++ (geometry.Track, "acoustic"), (batch_ridge, "first_event_fits"),
++ (acoustic_screen, "ridge_trial_rejections"), (search, "_association_event_ceiling")])
++def test_real_prefix_does_not_hide_custom_numerical_hooks(recorded, monkeypatch, owner, name):
++ @matching._cache_ridge_fits
++ def run():
++ call = prepared_calls(recorded)[0]
++ evaluate(call)
++ original = getattr(owner, name)
++ monkeypatch.setattr(owner, name, lambda *args, **kwargs: original(*args, **kwargs))
++ assert trial_prefix.request(**call, acoustic_batches=acoustic(call)) is None
++ run()
++
++
[email protected]("module,name", [("acoustic_screen", "_frequency_scale"),
++ ("batch_ridge", "_frequency_scale"), ("batch_ridge", "_ridge_fit_inputs"),
++ ("batch_ridge", "_fit_ridge_uncached"), ("batch_ridge", "_fit_argument_key"),
++ ("search", "_native_validate_ridge_events")])
++def test_real_prefix_rejects_custom_aliases_retained_by_module_import(recorded, module, name):
++ code = """
++import importlib
++import json
++from pathlib import Path
++import sys
++import numpy as np
++from dopplergeist import matching
++module_name, name, source, directory = sys.argv[1:]
++original = getattr(matching, name)
++calls = 0
++def hook(*args, **kwargs):
++ global calls
++ calls += 1
++ return False if original is None else original(*args, **kwargs)
++setattr(matching, name, hook)
++consumer = importlib.import_module('dopplergeist.' + module_name)
++setattr(matching, name, original)
++assert getattr(consumer, name) is hook
++from dopplergeist import search, trial_prefix
++from dopplergeist.data_access import FlightIndex
++fixture = json.loads(Path(source).read_text())
++witness = json.loads((Path(directory) / 'data/real-trial-prefix.json').read_text())['calls'][0]
++events, measurements = matching.prepare_events(fixture['events'])
++tracks = {track.id: track for track in FlightIndex(fixture['real_index']).lookup(
++ witness['track_ids'], max_tracks=2000, max_samples=2000000)}
++@matching._cache_ridge_fits
++def run():
++ frozen_events, frozen_measurements = matching._prepare_score_inputs(events, measurements)
++ by_id = {event['event_id']: (event, measured) for event, measured in zip(frozen_events, frozen_measurements)}
++ selected = [by_id[name] for name in witness['event_ids']]
++ options = dict(tracks=[tracks[name] for name in witness['track_ids']],
++ events=[pair[0] for pair in selected], measurements=[pair[1] for pair in selected],
++ observer=np.asarray(witness['observer']), starts=np.asarray(witness['starts']),
++ c=witness['c'], score_floor=witness['score_floor'], use_dual=witness['use_dual'],
++ legacy_first_batch=witness['legacy_first_batch'])
++ def batches():
++ return matching._iter_acoustic_trials(options['tracks'], options['measurements'],
++ options['observer'], options['starts'], options['c'])
++ assert matching._prepared_score_tokens(options['measurements'], options['events']) is not None
++ assert trial_prefix.request(**options, acoustic_batches=batches()) is None
++ first = search._batched_ridge_trials(**options, acoustic_batches=batches())
++ initial_calls = calls
++ second = search._batched_ridge_trials(**options, acoustic_batches=batches())
++ assert first == second
++ assert trial_prefix.cache_stats()['hits'] == 0
++ if name in ('_frequency_scale', '_native_validate_ridge_events'):
++ assert calls > initial_calls > 0
++run()
++"""
++ subprocess.run([sys.executable, "-c", code, module, name,
++ os.environ["DOPPLERGEIST_REAL_CPA_FIXTURE"], str(Path(__file__).parent)], check=True)
++
++
[email protected]("target", ["matching", "search", "native_export"])
++def test_real_prefix_rejects_changed_boolean_validator_aliases_and_export(recorded, monkeypatch, target):
++ if target == "native_export":
++ owner, name = matching._native_ridge_module, "valid_ridge_events"
++ if owner is None or not hasattr(owner, name):
++ pytest.skip("native boolean export is unavailable")
++ else:
++ owner = matching if target == "matching" else search
++ name = "_native_validate_ridge_events"
++
++ @matching._cache_ridge_fits
++ def run():
++ call = prepared_calls(recorded)[0]
++ evaluate(call)
++ original = getattr(owner, name)
++ def custom(*args):
++ return False if original is None else original(*args)
++ monkeypatch.setattr(owner, name, custom)
++ assert trial_prefix.request(**call, acoustic_batches=acoustic(call)) is None
++ run()
++
++
++def test_real_prefix_capacity_and_unsupported_trials(recorded, monkeypatch):
++ @matching._cache_ridge_fits
++ def run():
++ call = prepared_calls(recorded)[0]
++ original = identity(evaluate(call))
++ memo = matching._ridge_fit_cache.get().prefix_memo
++ monkeypatch.setattr(trial_prefix, "_MAX_ENTRIES", 1)
++ shifted = dict(call, starts=np.nextafter(call["starts"], np.inf))
++ evaluate(shifted)
++ assert len(memo.entries) <= 1
++ assert identity(evaluate(call)) == original
++ for limit in (1, 0):
++ monkeypatch.setattr(trial_prefix, "_MAX_BYTES", limit)
++ assert identity(evaluate(call)) == original
++ assert not memo.entries and memo.size == 0
++ monkeypatch.setattr(trial_prefix, "_MAX_BYTES", 8 * 1024 * 1024)
++ # The actual observed track has no support at this earlier clock. No
++ # fabricated motion or audio is used for the failure-envelope control.
++ unsupported = dict(call, starts=call["starts"] - 86400.)
++ result = evaluate(unsupported)
++ assert any(outcome[0] is not None or outcome[2] is not None for outcome in result[0])
++ assert not memo.entries
++ run()