pynapple.TsGroup.subsample#

TsGroup.subsample(fraction, seed=None)[source]#

Randomly subsample timestamps in each element of the TsGroup.

This function randomly selects a fraction of the timestamps from each Ts/Tsd object in the TsGroup using a fast mask-based approach.

Parameters:
  • fraction (float) – The fraction of timestamps to keep. Must be between 0 and 1.

  • seed (int, optional) – Random seed for reproducibility. If None, no seed is set.

Returns:

A new TsGroup with subsampled timestamps.

Return type:

TsGroup

Raises:
  • ValueError – If fraction is not between 0 and 1.

  • TypeError – If fraction is not a number.

Examples

>>> import pynapple as nap
>>> import numpy as np
>>> tmp = {
...     0: nap.Ts(t=np.arange(0, 100)),
...     1: nap.Ts(t=np.arange(0, 100, 0.5)),
...     2: nap.Ts(t=np.arange(0, 100, 0.25)),
... }
>>> tsgroup = nap.TsGroup(tmp)
>>> tsgroup
  Index     rate
-------  -------
      0  1.0101
      1  2.0202
      2  4.0404

Subsample to keep exactly 50% of the timestamps:

>>> subsampled = tsgroup.subsample(0.5, seed=42)