excess_flank#

pypillometry.signal.excess_flank(sy, unusable, edge, direction, rate, noise, smooth=5, min_scale=10, max_scale=300, z=4.0, n_scales=16, sign=None, max_gap=0)[source]#

The flank of a dropout as a change faster than the pupil can manage, tested over many scales.

Over any interval of Δ samples, the pupil changes by at most rate * Δ plus noise. From the edge of the dropout, away from it, the excess change is

E(Δ) = ±(p(edge + Δ) - p(edge)) - rate * Δ

with p lightly smoothed (a running median of smooth samples) and p(edge) the median of the first smooth samples (the samples next to a dropout are often odd; Δ counts from the middle of these). The noise of the difference stays about constant over the scales, the allowed change grows with Δ, and an artifact grows quickly and then stops: at some scale it clearly exceeds both, also if it is too slow to stand out in a short window.

It is a flank if E(Δ) > z * sqrt(2) * noise at one of n_scales log-spaced scales between min_scale and max_scale (in either direction). It ends where E is largest: the excess grows only while the signal changes faster than rate, so its maximum is where the change has slowed to a pace the pupil could manage.

Δ runs only over usable samples, up to the next unusable one or max_scale; a usable stretch shorter than that is also tested over its full length (e.g., a short stretch between two dropouts). Runs of up to max_gap unusable samples do not stop it (e.g., a short dropout during the reopening): Δ goes on after them, with the same start value, and the allowed change grows over them too.

Parameters:
  • sy (np.ndarray) – pupil data

  • unusable (np.ndarray of bool) – samples without data (missing or masked)

  • edge (int) – the first sample next to the gap (the last one before it, or the first one after it)

  • direction (int) – -1: the flank before the gap (Δ runs to earlier samples); +1: after

  • rate (float or np.ndarray) – the largest plausible change of the pupil per sample (positive; one for all samples or one per sample: the allowed change over an interval is the sum over its samples)

  • noise (float) – standard deviation of the noise of a sample

  • smooth (int) – width (samples) of the running median and of the anchor

  • min_scale (int) – range of the scales (samples) of the test; max_scale also bounds the length of the flank

  • max_scale (int) – range of the scales (samples) of the test; max_scale also bounds the length of the flank

  • z (float) – threshold in standard deviations of the noise of a difference

  • n_scales (int) – number of scales of the test

  • sign (int, optional) – the direction of the change away from the gap: +1, the signal grows away from it (for a blink, on both sides: it falls before and rises after the gap); -1, it shrinks; None (default): either

  • max_gap (int) – Δ goes on across up to this many unusable samples in a row (see above)

Return type:

tuple[int, int, bool] | None

Returns:

the flank (both inclusive; (edge, edge, False) if there is none); None if there is no usable sample next to the gap