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.172694
13.0         0.324199    0.74586    -1.83658
20.0         0.564464    0.0255007   0.473193
21.0         0.659191    2.34075     1.07099
34.0         0.0964165   0.419102   -0.953028
58.0        -1.04787    -1.87568    -1.36678
71.0         0.636305   -0.906721    0.476043
...
875.0       -0.9812     -1.23823    -0.824651
897.0        1.95128    -0.0400851   0.529436
931.0       -0.183892   -0.0900786  -0.50588
942.0        0.050744    0.4946      1.67831
955.0       -1.87447     1.61082     0.52796
957.0       -0.367763   -0.547723    1.04368
975.0        0.228979   -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