tune_post_margins#

pypillometry.signal.tune_post_margins(sy, blinks, fs, grid, window, kernel=None, centres=None, sites=50, pad=0, clearance=0, kalman_window=None, mode='multiplicative', missing=None, max_core=None, min_eff=10, se=1.0, coarse=4, blink_val=0, seed=None)[source]#

Tune the margin after each blink (e.g., against slow openings of the lid) on transplanted blinks.

After some blinks the lid opens slowly: the first samples after the reopening are too low and climb back to the pupil over a few hundred ms. A margin after the blink masks them, but a longer margin also makes the interpolated stretch longer. Which margin is best is found on transplanted blinks (see transplant): the signal around each blink with a window around it is cut out and a slow baseline is removed (see extract_blink_artifacts()). A single one of these artifacts also contains the fluctuations of the pupil around that blink, which cannot be told apart from the lid; they would make the margin longer the more the pupil moves after a blink. So the artifacts of the neighbouring blinks are pooled first: their weighted median (aligned at the start and the end of the blinks, with a Gaussian kernel over time as weights) keeps what is the same after all of them (the lid) and removes the fluctuations, which differ from blink to blink. Only the part where the pooled artifact is below the baseline (a ratio below 1, or a negative difference) counts (the lid lowers the pupil; a rise of the pupil after blinks is a response of the pupil and not masked).

The pooled artifact (with a core of the typical length of the blinks) is put into sites places of clean signal, which are interpolated (interpolate_blinks_kalman()) with each margin of the grid after the core. The error is the root mean squared difference to the clean signal over the whole window (with pad), the same samples for all margins, averaged over the places (the same places at all centres). The margin is the shortest one whose error is at most se standard errors (of the difference to the best margin, paired over the places) above that of the best one, so that margins that hardly help are not used. Not all margins of the grid are tried: first every coarse-th one, then the steps around the best and the chosen margin are halved until their neighbours on the grid are tried. Between the centres, the margins of the blinks are interpolated linearly.

Parameters:
  • sy (np.ndarray) – pupil data (NaN or blink_val for missing values)

  • blinks (np.ndarray) – the blinks (n x 2, end-exclusive, sorted), e.g. from the blink detection; the samples in them are missing in the artifacts

  • fs (float) – sampling rate (Hz)

  • grid (sequence of int) – margins after the blinks to try (samples)

  • window (int or tuple of int) – samples (before, after) the blinks that are cut out; after the blink, it should be longer than the largest margin plus the context of the interpolation

  • kernel (float, optional) – standard deviation (samples) of the Gaussian weights of the blinks around each centre; None: all blinks have the same weight (one margin for all blinks)

  • centres (sequence of int, optional) – samples at which the margin is tuned; default: one in the middle without kernel, otherwise every half kernel from the first to the last blink

  • sites (int) – number of places the pooled artifact is put into

  • pad (int) – clean samples on each side of the window of a transplanted blink that count for the error

  • clearance (int) – minimum distance of the places (with pad) from missing samples, blinks and missing

  • kalman_window (int, optional) – samples before and after each interpolated stretch that the Kalman smoother uses; default: half a second

  • mode (str) – "multiplicative" (default; the lid covers a share of the pupil, for a positive signal) or "additive" (e.g., log-transformed data); see extract_blink_artifacts()

  • missing (np.ndarray of bool, optional) – further samples without usable signal (e.g., other masks); blinks with such samples in their window are not used, and no blinks are put near them

  • max_core (int, optional) – blinks longer than this are not used for the artifacts

  • min_eff (float) – a warning is given for centres with fewer blinks (effective number, with the weights of the kernel)

  • se (float) – number of standard errors (see above); 0: the margin with the smallest error

  • coarse (int) – step (in margins of the grid) of the first, coarse search; 1: all margins of the grid are tried

  • blink_val (float) – “missing value” code in addition to NaN (default 0)

  • seed (int or np.random.Generator, optional) – seed of the random numbers (the places)

Return type:

PostMarginResult

Returns:

the margins of the blinks and the errors