Expand description
Standard-deviation contrast stretch, as a fused single-pass kernel.
Mirrors TerraTexture.stretch’s stretch_std(): values are mapped
linearly so that mean - n_std·std becomes 0 and mean + n_std·std
becomes 1, then clipped to [0, 1]. NaN in means NaN out, and NaNs
are excluded from the mean and standard deviation.
§Why this is faster than numpy
The numpy version calls np.nanmean() and then np.nanstd(), which
recomputes the mean internally, so the array is reduced several times
before the elementwise subtract/divide/clip. Here one reduction pass
collects the sum, sum of squares and count together. (Deduplicating
only the mean call in numpy gives about 1.1×; numpy’s C reductions are
already efficient, so the win is fewer passes, not language speed.)
§Precision
Statistics are accumulated in f64 for robustness on large arrays.
This deliberately does not bit-match numpy’s float32 pairwise
summation; it is verified against it within a statistical tolerance
in tests/test_stretch_rust.py, which is the right bar for a
mean/std computation.
Functions§
- stretch_
std_ core - Contrast-stretch an array to [0, 1] using mean ±
n_stdstandard deviations, ignoring NaNs.