mask_signal#

pypillometry.mask_signal(signal, lower=None, upper=None, method='value', percentile=False, eyes=[], variables=[], layer=None, margin=0, ignore_existing_mask=True, store_as=None)#

Pipeline step for mask_signal() (d | pp.mask_signal(...)).

Mask the data where a quality signal is out of range.

The quality signal is a channel in d.aux (e.g., the distance of the eye from the camera), a variable of the eyes or a signal you compute. Samples where it (or a statistic of it, see method) is below lower or above upper are masked in layer layer of the selected eyes and variables. The layer replaces the one from an earlier call with the same layer (so running the method again with other thresholds gives the same result as running it once; clear_layer(layer) removes it). Like all layers except "interpolated", it makes the samples unusable under the "default" policy. Samples where the signal is NaN are not masked (there is no information about them).

The thresholds are found for each eye separately; those that were used (e.g., the value of the 99th percentile) are noted in the history (see print_history()).

Parameters:
  • signal (str, callable, array or dict) –

    the quality signal:

    • a name, looked up like the variables of the plots (see plot_timeseries()): a variable of the eyes (e.g., "pupil") or a channel in d.aux (e.g., "gaze_luminance"), or the last or first part of the names of channels, e.g. "distance" for left_distance and right_distance. A channel of an eye (<eye>_...) is used for this eye, other channels for all eyes.

    • a function f(d, eye) that returns the signal of eye (one value per sample) or None (the eye is not checked), e.g. lambda d, eye: d.data[f"{eye}_pupil"] / d.aux[f"{eye}_pupilpx"]

    • an array (one value per sample) used for all eyes, or a dict {eye: array}

  • lower (float, optional) – thresholds: samples where the statistic of the signal is below lower or above upper are masked; at least one is needed. With percentile, they are fractions (0-1) of the distribution of the statistic (e.g., upper=0.99).

  • upper (float, optional) – thresholds: samples where the statistic of the signal is below lower or above upper are masked; at least one is needed. With percentile, they are fractions (0-1) of the distribution of the statistic (e.g., upper=0.99).

  • method (str) –

    the statistic that the thresholds apply to:

    • "value" (default): the signal itself

    • "mad": the robust z-score, i.e. the distance from the median in units of the median absolute deviation (scaled to the SD for normal data), e.g. lower=-5, upper=5

    • "jump": the absolute change per second to the previous or next sample (the larger one), so that both samples of a jump are masked, e.g. upper=2000 for a distance that changes by more than 2000 mm/s

  • percentile (bool) – lower and upper are percentiles (0-1) of the statistic

  • eyes (str or list) – eyes to check (default: all)

  • variables (str or list) – variables of the eyes to mask (default: all)

  • layer (Optional[str]) – name of the layer; default: signal (needed if signal is not a name). Not "interpolated" or "excluded".

  • margin (float, str, Quantity or tuple) – time added before and after each masked stretch (plain numbers in ms; a tuple (before, after) for different margins); default 0

  • ignore_existing_mask (bool) – if True (default), check all samples, also those that are masked already (e.g., blinks); if False, only those that are usable under the default policy for all masked variables of the eye (the layer itself does not count). The thresholds of percentile and "mad" come from the checked samples.

  • store_as (Optional[str]) – store the signal in d.aux: as <eye>_<store_as> if it belongs to an eye, otherwise as store_as (which then needs the form <group>_<name>)

  • inplace (bool or None) – if True, make change in-place and return the object; if False, make and return copy before making changes; if None, use the object-level setting

Return type:

Self

Returns:

the object with the samples masked (self or a copy, depending on inplace)

Raises:

ValueError – if signal is not found, a name stands for several channels of the same eye, no threshold is given, method is unknown, a percentile is not between 0 and 1, layer is missing or not allowed, or a signal does not have one value per sample

Examples

>>> import numpy as np
>>> import pypillometry as pp
>>> d = pp.get_example_data("rlmw_002_short")
>>> dist = np.full(len(d), 600.)         # e.g., the eye-camera distance in mm
>>> dist[1000:1100] = 900.
>>> d = d.mask_signal(dist, upper=750, layer="distance", margin=50)
>>> blocks = d.get_layer_intervals("distance", "left", "pupil", units="samples")
>>> blocks["left_pupil"].intervals    # 50 ms (25 samples) margin
[(975, 1125)]

With a channel in d.aux, e.g. of a GazePoint recording (left_distance, right_distance, in mm), the percentiles of the change of the distance, or a computed signal:

>>> d = pp.get_example_data("robison_pvt")
>>> d = d.mask_signal("distance", lower=450, upper=750)
>>> d = d.mask_signal("distance", method="jump", upper=0.999, percentile=True,
...                   layer="distance_jump")
>>> d = d.mask_signal(lambda d, eye: d.data[f"{eye}_pupil"] / d.aux[f"{eye}_pupilpx"],
...                   method="mad", lower=-5, upper=5, layer="mmpx")
>>> d.print_history()  # the thresholds that were used