semivariogram#

pypillometry.signal.semivariogram(signal, fs, mask=None, max_lag=2000, method='fft', segments=None)[source]#

Empirical semivariogram of a signal with missing samples.

Only pairs of samples that are both valid are used (see pypillometry.signal.variogram for the formula). Invalid samples are the masked ones and NaN/inf values; they are neither interpolated nor dropped, so gaps do not shift the time axis.

Parameters:
  • signal (np.ndarray or np.ma.MaskedArray) – the signal, sampled at fs; the mask of a masked array is used

  • fs (float) – sampling rate in Hz

  • mask (np.ndarray, optional) – boolean array, True for invalid samples (combined with the mask of a masked array and with NaN values)

  • max_lag (float, str or Quantity, optional) – largest lag; plain numbers are ms (default 2000 ms). Limited to the length of the signal.

  • method ({"fft", "direct"}, optional) – “fft” (default) computes all lags at once in O(n log n); “direct” loops over the lags in O(n * lags) and is exact (used to check the FFT version).

  • segments (list of (start, stop), optional) – sample indices of segments (e.g., blocks of an experiment): only pairs of samples within the same segment are used, each segment around its own mean, and the sums are pooled over the segments. This keeps changes of the level between the segments out of the variogram. Samples outside the segments are not used.

Return type:

Variogram

Returns:

named tuple (lags, gamma, npairs, variance), lags in ms

Examples

White noise has a flat variogram at its variance:

>>> rng = np.random.default_rng(0)
>>> x = rng.normal(0, 2, 10000)
>>> x[1000:1200] = np.nan   # a gap
>>> vg = semivariogram(x, fs=1000, max_lag=50)
>>> vg.lags[:3]
array([1., 2., 3.])
>>> bool(np.allclose(vg.gamma, 4, rtol=0.1))
True