pynapple.TsdFrame.value_from#

TsdFrame.value_from(data, ep=None, mode='closest')[source]#

Replace the value with the closest value from Tsd/TsdFrame/TsdTensor argument

Parameters:
  • data (Tsd, TsdFrame or TsdTensor) – The object holding the values to replace.

  • ep (IntervalSet (optional)) – The IntervalSet object to restrict the operation. If None, the time support of the tsd input object is used.

  • mode (literal, either 'closest', 'before', 'after') – If closest, replace value with value from Tsd/TsdFrame/TsdTensor, if before gets the first value before, if after the first value after.

Returns:

out – Object with the new values

Return type:

Tsd, TsdFrame or TsdTensor

Examples

In this example, the tsdframe object will receive the closest values in time from a tsd.

>>> import pynapple as nap
>>> import numpy as np; np.random.seed(42)
>>> t = np.unique(np.sort(np.random.randint(0, 1000, 100))) # random times
>>> tsdframe = nap.TsdFrame(t=t, d=np.random.randn(len(t), 3), time_units='s')
>>> tsdframe
Time (s)    0         1         2
----------  --------  --------  --------
1.0         -2.17833  -1.0439   0.17269
13.0        0.3242    0.74586   -1.83658
20.0        0.56446   0.0255    0.47319
21.0        0.65919   2.34075   1.07099
34.0        0.09642   0.4191    -0.95303
58.0        -1.04787  -1.87568  -1.36678
...         ...       ...       ...
897.0       1.95128   -0.04009  0.52944
931.0       -0.18389  -0.09008  -0.50588
942.0       0.05074   0.4946    1.67831
955.0       -1.87447  1.61082   0.52796
957.0       -0.36776  -0.54772  1.04368
975.0       0.22898   -1.53407  0.36307
dtype: float64, shape: (94, 3)

tsdframe is a timestamp table with values.

>>> tsd_from = nap.Tsd(t=np.arange(0,1000), d=np.random.choice([1,2,3], 1000), time_units='s')
>>> tsd_from
Time (s)
----------  --
0.0          3
1.0          1
2.0          3
3.0          1
4.0          3
5.0          1
6.0          2
...
993.0        1
994.0        2
995.0        3
996.0        1
997.0        3
998.0        2
999.0        3
dtype: int64, shape: (1000,)

tsd_from contains values, for example the tracking data.

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

An epoch can be passed to restrict the operation.

>>> tsd_after = tsdframe.value_from(tsd_from, ep, mode='closest')
>>> tsd_after
Time (s)
----------  --
1.0          1
13.0         3
20.0         3
21.0         2
34.0         1
58.0         3
71.0         3
...
459.0        2
466.0        2
474.0        3
475.0        1
476.0        2
484.0        2
491.0        3
dtype: int64, shape: (53,)

tsd_after is the same length as ts when restricted to ep.

>>> print(len(tsdframe.restrict(ep)), len(tsd_after))
53 53