Skip to main content

stretch_std_core

Function stretch_std_core 

Source
pub fn stretch_std_core(
    arr: ArrayView2<'_, f32>,
    n_std: f32,
    out: &mut Array2<f32>,
)
Expand description

Contrast-stretch an array to [0, 1] using mean ± n_std standard deviations, ignoring NaNs.

§Arguments

  • arr - ArrayView2<f32>, shape (H, W): input values, any range. NaN is allowed and marks missing data. Any memory layout, but the parallel reduction is only used on C-contiguous input.
  • n_std - f32: half-width of the stretch window in standard deviations (e.g. 2.0 maps mean − 2σ → 0 and mean + 2σ → 1).
  • out - &mut Array2<f32>, shape (H, W): overwritten with the stretched values in [0, 1], and NaN wherever arr is NaN.

Uses the population standard deviation (numpy’s default ddof=0). If every element is NaN, or the array is empty, mean and std are taken as 0.

A constant array has std 0, so the window collapses to a point and only the 1e-12 guard keeps the division finite. Every value then equals the mean, so every output is 0, as in the numpy version.

Runs serially below PARALLEL_THRESHOLD elements and in parallel at or above it.

§Panics

If out does not have the same shape as arr.

§Example

use ndarray::{array, Array2};
use terra_texture_rs::stretch_std_core;

let arr = array![[1.0_f32, 2.0], [3.0, f32::NAN]];
let mut out = Array2::<f32>::zeros(arr.raw_dim());
stretch_std_core(arr.view(), 1.0, &mut out);

assert!((out[[0, 1]] - 0.5).abs() < 1e-6); // the mean maps to 0.5
assert!(out[[1, 1]].is_nan());             // NaN passes through