nirspipe.pipeline.glm_whitening#

What the noise model left in a GLM’s residual: its spectrum against white noise, and its lag-1 autocorrelation.

Module Contents#

Functions#

residual_spectrum

Power of a residual relative to white noise of the same variance, per frequency.

lag1_autocorrelation

Sample autocorrelation at lag 1, about the series mean.

band_mean

Mean of ratio over band[0] <= f < band[1], NaN when no bin falls inside.

whitening_tables

Per channel, the residual and whitened residual side by side; then their mean spectra.

Data#

API#

nirspipe.pipeline.glm_whitening.WHITENING_PSD_SEGMENT_S = 200.0#
nirspipe.pipeline.glm_whitening.WHITENING_LOWBAND = (0.01, 0.1)#
nirspipe.pipeline.glm_whitening.residual_spectrum(x: numpy.ndarray, sfreq: float, segment_s: float = WHITENING_PSD_SEGMENT_S) → tuple[numpy.ndarray, numpy.ndarray]#

Power of a residual relative to white noise of the same variance, per frequency.

The series is standardised, its one-sided Welch density taken, and the density multiplied by sfreq / 2, which is what white noise of variance 1 has at every frequency. So the ratio is 1 where the residual is white, above 1 where correlation is left over, below 1 where the whitening overshot.

Parameters:
  • x (ndarray, shape (n,)) -- One channel’s residual.

  • sfreq (float) -- Sampling rate in Hz.

  • segment_s (float) -- Welch segment length in seconds, capped at the series length.

Returns:

  • freqs (ndarray) -- Frequencies in Hz, the zero and Nyquist bins left out: a one-sided density holds half the power there, so white noise would read 0.5 on them.

  • ratio (ndarray) -- Power relative to white, same length as freqs.

Examples

White noise reads about 1 at every frequency; an AR(1) residual with coefficient 0.9 reads about 19 near zero frequency, (1 + 0.9) / (1 - 0.9).

nirspipe.pipeline.glm_whitening.lag1_autocorrelation(x: numpy.ndarray) → float#

Sample autocorrelation at lag 1, about the series mean.

[1, 2, 3, 4] -> deviations [-1.5, -0.5, 0.5, 1.5], lagged products 0.75 - 0.25 + 0.75 = 1.25 over the sum of squares 5, so 0.25.

nirspipe.pipeline.glm_whitening.band_mean(freqs: numpy.ndarray, ratio: numpy.ndarray, band: tuple[float, float] = WHITENING_LOWBAND) → float#

Mean of ratio over band[0] <= f < band[1], NaN when no bin falls inside.

nirspipe.pipeline.glm_whitening.whitening_tables(glm_est: Any, residual: numpy.ndarray, keep: numpy.ndarray, sfreq: float, chroma: list[str], long_channels: set[str], bads: set[str], self_fitted: set[str]) → tuple[pandas.DataFrame, pandas.DataFrame]#

Per channel, the residual and whitened residual side by side; then their mean spectra.

The whitened residual is the fit’s own, the series its standard errors assume white: for AR-IRLS it carries the robust weights, for a censored fit only the kept frames. residual is the data-space residual, cut to the same kept frames for the comparison. The spectra average the long, good channels of each chromophore; a channel fitted against a copy of itself has a zero residual and is left out of everything.

Returns:

  • channels (DataFrame) -- One row per channel: ch_name, Chroma, long, bad, n_samples, acf1_residual, acf1_whitened, lowband_residual, lowband_whitened.

  • spectrum (DataFrame) -- freq and, per chromophore present, <chroma>_whitened and <chroma>_residual.