pynapple.TsdTensor.value_from#

TsdTensor.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 tsdtensor 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
>>> tsdtensor = nap.TsdTensor(t=t, d=np.random.randn(len(t), 3, 3), time_units='s')
>>> tsdtensor
Time (s)
----------  -------------------------------
1.0         [[-2.178334 ...  0.172694] ...]
13.0        [[0.659191 ... 1.070985] ...]
20.0        [[0.636305 ... 0.476043] ...]
21.0        [[-1.123898 ...  1.726516] ...]
34.0        [[-0.779702 ... -0.150534] ...]
58.0        [[0.423943 ... 2.287226] ...]
...
897.0       [[1.444173 ... 0.175421] ...]
931.0       [[ 0.995858 ... -0.019882] ...]
942.0       [[-0.80603  ...  0.286974] ...]
955.0       [[ 0.636631 ... -0.883157] ...]
957.0       [[-0.0971   ...  1.138171] ...]
975.0       [[ 1.194182 ... -1.499837] ...]
dtype: float64, shape: (94, 3, 3)

tsdtensor is a timestamp tensor 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          3
2.0          2
3.0          1
4.0          1
5.0          3
6.0          1
...
993.0        1
994.0        1
995.0        1
996.0        1
997.0        2
998.0        1
999.0        1
dtype: int64, shape: (1000,)

tsd_from contains other values.

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

tsd_after is the same length as tsdtensor restricted to ep.

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