local_level#

pypillometry.signal.local_level(x, scales, usable=None, min_coverage=0.5, max_dev=0.2, details=False)[source]#

The local level of a signal: a robust, multi-scale moving median.

Overloads:

For each scale (window length, ascending), the median of the usable samples in a window around each sample is computed (on a grid of a quarter of the window, interpolated in between), and the share of usable samples in it (coverage). For each sample, the level is then chosen from coarse to fine: it starts with the coarsest scale whose coverage is at least min_coverage (the median of all usable samples if there is none); the next finer scale is taken if its coverage is at least min_coverage and its median deviates by at most max_dev (a fraction) from the level chosen so far; at the first scale that fails, the coarser level is kept. So the level follows the data closely where they are good, while gaps or long artifacts (which make a fine window deviate from the coarser ones) lead to a coarser scale. Finally, the level is smoothed with a moving average over the finest window (so that it does not jump where the scale changes).

Parameters:
  • x (np.ndarray) – the signal

  • scales (sequence of int) – window lengths in samples (e.g., 2, 4, …, 128 s); scales longer than the signal are left out

  • usable (np.ndarray of bool, optional) – the samples that count (default: the finite ones)

  • min_coverage (float) – minimum share (0-1) of usable samples in a window

  • max_dev (float) – maximum deviation of a scale from the next coarser chosen level, as a fraction of that level

  • details (bool) – also return the medians and coverage of each scale (for plots)

Returns:

  • level (np.ndarray) – the local level of each sample (NaN if no sample is usable)

  • scale (np.ndarray of int) – the index (into scales) of the scale chosen for each sample; -1: the median of all usable samples

  • details (dict, only with details) – "scales" (the scales used) and, per scale, "centers", "median" and "coverage" (on the grid)