Skip to content

[ESSREDUCE] fix: support rectilinear grids in the numba interpolator - #727

Draft
SimonHeybrock wants to merge 1 commit into
mainfrom
interpolator-rectilinear-grid
Draft

[ESSREDUCE] fix: support rectilinear grids in the numba interpolator#727
SimonHeybrock wants to merge 1 commit into
mainfrom
interpolator-rectilinear-grid

Conversation

@SimonHeybrock

Copy link
Copy Markdown
Member

What

interpolator_numba locates 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 with boundscheck=False, often past the end of the array. Reproduced on main, interpolating a function sampled on a distance axis with two dense clusters and a gap between them:

numba: [ 2131.86  54175.32  -1.31886686e+254 ]
scipy: [ 2131.86  54175.32       265739.87   ]
exact: [ 2126.75  54134.67       265638.52   ]

interpolator_scipy handles 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):

points uniform, before uniform, after rectilinear, after
1 M 0.92 ms 0.72 ms 1.35 ms
10 M 8.75 ms 6.81 ms 13.28 ms

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.py is 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/unwrap passes (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.

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.
@github-actions github-actions Bot added the essreduce Issues for essreduce. label Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

essreduce Issues for essreduce.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant