interpolate_blinks_kalman#
- pypillometry.signal.interpolate_blinks_kalman(sy, blinks, fs, window, missing=None, optimize=False, max_fit_windows=100)[source]#
Interpolate blinks with a Gauss-Markov model (Kalman smoother).
Around each blink, a local window of window samples on each side is taken (blinks with overlapping windows share one window) and a linear trend fitted to its valid samples is removed. The residuals are modelled as the sum of a fast and a slow Matérn process (smoothness 3/2) plus white measurement noise. The blink samples are replaced by the smoothed mean (Kalman filter + RTS smoother) plus the trend.
By default, the time scales of the components are fixed (5 and 300 ms), their standard deviations are 0.1 and 1 times the SD of the residuals, and the SD of the noise is estimated from the second differences of the residuals. The smoothed mean depends only on the ratios of these SDs, so the defaults do not depend on the units of the pupil data. With optimize=True, all parameters are fitted instead (maximum likelihood over the windows, exact for the missing samples), which is much slower and changes the result little.
- Parameters:
sy (np.ndarray) – pupil data (NaN or 0 for missing values)
blinks (iterable of (start, end)) – blinks in samples (end-exclusive); these samples are replaced
fs (float) – sampling rate in Hz
window (int) – number of samples before and after each blink to use
missing (np.ndarray of bool, optional) – further samples that are not used as observations (e.g., excluded periods or other blinks); the blinks, NaN and 0 are always missing
optimize (bool) – fit all parameters of the model instead of using the defaults
max_fit_windows (int) – with optimize=True, at most this many (evenly spaced) windows are used to fit the parameters
- Return type:
- Returns:
filled (np.ndarray) – copy of sy with the blinks replaced
interpolated (np.ndarray of bool) – the replaced samples
model (GaussMarkovModel or None) – the model used (None if there was nothing to interpolate)