[ESSREDUCE] fix: support rectilinear grids in the numba interpolator - #727
Draft
SimonHeybrock wants to merge 1 commit into
Draft
[ESSREDUCE] fix: support rectilinear grids in the numba interpolator#727SimonHeybrock wants to merge 1 commit into
SimonHeybrock wants to merge 1 commit into
Conversation
The numba interpolator located a cell by dividing by a single global step,
which assumes an equally spaced grid. On a grid that is not equally spaced it
therefore read the wrong cell -- and with boundscheck=False, often past the end
of the array:
numba: [ 2131.86 54175.32 -1.3e+254 ]
scipy: [ 2131.86 54175.32 265739.87 ]
The scipy implementation handles such a grid correctly, so which answer you got
depended on whether numba was installed. Nothing said the grid had to be
equally spaced except a line in the docstring.
Uniformity is now detected once per axis when the Interpolator is constructed
and passed to the kernel, which divides when the axis is equally spaced and
binary-searches when it is not. The uniform path keeps its hoisted cell-area
normalization and is unchanged in cost.
This matters for a lookup table that samples distance where components sit and
not at all in between -- dense across the detectors, a few rows at each monitor
-- which spans a beamline in a few tens of rows instead of thousands.
Also raises when an axis has fewer than two points. Such a grid cannot bracket
a value: it used to be read past the end of the array as well.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
interpolator_numbalocates a cell by dividing by a single global step, which assumes an equally spaced grid. On a grid that is not equally spaced it reads the wrong cell — and since the kernel runs withboundscheck=False, often past the end of the array. Reproduced onmain, interpolating a function sampled on a distance axis with two dense clusters and a gap between them:interpolator_scipyhandles such a grid correctly, so which answer you get depends on whether numba is installed. Nothing enforced the precondition; a line in the docstring stated it.How
Uniformity is detected once per axis in
Interpolator.__init__and passed to the kernel, which divides when the axis is equally spaced and binary-searches when it is not. The uniform path keeps its hoisted cell-area normalization, so it pays nothing for the new capability; the rectilinear path computes the cell area per point, since it is no longer the same everywhere.The constructor also raises when an axis has fewer than two points. Such a grid cannot bracket a value — it used to be read past the end of the array too. Happy to drop that part if you would rather keep the change to one thing.
Timings
Numba 0.67, 24 cores, grid of 101 × 201 nodes, each variant measured in a fresh process (best of 20 after warm-up):
No regression on the uniform path — measured slightly faster, which I would read as neutral rather than an improvement worth claiming. The rectilinear path costs roughly 1.8× the uniform one, paid only by grids that need it.
Tests
tests/unwrap/interpolator_test.pyis built around numba and scipy agreeing, which is exactly the invariant that was broken, so the new cases follow that pattern: agreement on a rectilinear grid, agreement across a gap in it, uniformity detection either way, and the too-short-axis error.tests/unwrappasses (379 tests).Why we want it
A wavelength lookup table for a whole beamline is mostly empty: monitors sit tens to hundreds of metres upstream of the detectors, so a uniform grid fine enough at the detectors is thousands of rows of nothing in between. Sampling distance only where components sit — dense across the detectors, a few rows at each monitor — spans the beamline in a few tens of rows. That table is rectilinear, not uniform, which is what led us here.
Draft because CONTRIBUTING asks for an issue first: opening this as the concrete thing to discuss rather than as a finished proposal.