pynapple.TsdFrame.count#

TsdFrame.count(bin_size=None, ep=None, time_units='s', dtype=None)[source]#

Count occurences of events within bin_size or within a set of bins defined as an IntervalSet. You can call this function in multiple ways :

1. tsd.count(bin_size=1, time_units = ‘ms’) -> Count occurence of events within a 1 ms bin defined on the time support of the object.

2. tsd.count(1, ep=my_epochs) -> Count occurent of events within a 1 second bin defined on the IntervalSet my_epochs.

3. tsd.count(ep=my_bins) -> Count occurent of events within each epoch of the intervalSet object my_bins

4. tsd.count() -> Count occurent of events within each epoch of the time support.

bin_size should be seconds unless specified. If bin_size is used and no epochs is passed, the data will be binned based on the time support of the object.

Parameters:
  • bin_size (None or float, optional) – The bin size (default is second)

  • ep (None or IntervalSet, optional) – IntervalSet to restrict the operation

  • time_units (str, optional) – Time units of bin size (‘us’, ‘ms’, ‘s’ [default])

  • dtype (type, optional) – Data type for the count. Default is np.int64.

Returns:

out – A Tsd object indexed by the center of the bins.

Return type:

Tsd

Examples

This example shows how to count timestamps within bins of 0.1 second.

>>> import pynapple as nap
>>> import numpy as np; np.random.seed(42)
>>> t = np.unique(np.sort(np.random.randint(0, 1000, 100)))
>>> tsdframe = nap.TsdFrame(t=t, d=np.random.randn(len(t), 4), time_units='s')
>>> tsdframe
Time (s)    0         1         2         3
----------  --------  --------  --------  --------
1.0         -2.17833  -1.0439   0.17269   0.3242
13.0        0.74586   -1.83658  0.56446   0.0255
20.0        0.47319   0.65919   2.34075   1.07099
21.0        0.09642   0.4191    -0.95303  -1.04787
34.0        -1.87568  -1.36678  0.63631   -0.90672
58.0        0.47604   1.30366   0.21159   0.59704
...         ...       ...       ...       ...
897.0       -0.98723  -0.49116  -1.20912  1.58914
931.0       -0.75691  -0.87508  -1.32561  -0.77121
942.0       -0.49489  -0.04948  -0.64532  -1.60061
955.0       -1.51457  0.67966   -0.12279  0.64889
957.0       0.78028   0.15108   -1.23173  0.18958
975.0       1.3996    -0.44743  0.34062   -0.01378
dtype: float64, shape: (94, 4)

tsdframe_before is a timestamp table with data.

>>> ep = nap.IntervalSet(start = 0, end = 500, time_units = 's')
>>> ep
  index    start    end
      0        0    500
shape: (1, 2), time unit: sec.

ep is an IntervalSet object defining the epochs.

>>> bincount = tsdframe.count(10.0, ep=ep)
>>> bincount
Time (s)
----------  --
5.0          1
15.0         1
25.0         2
35.0         1
45.0         0
55.0         1
...
445.0        0
455.0        3
465.0        1
475.0        3
485.0        1
495.0        1
dtype: int64, shape: (50,)
>>> bincount.time_support
  index    start    end
      0        0    500
shape: (1, 2), time unit: sec.

bincount automatically inherits ep as time support.