pynapple.Ts.value_from#

Ts.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 ts object will receive the closest values in time from 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
>>> ts = nap.Ts(t=t, time_units='s')
>>> ts
Time (s)
1.0
13.0
20.0
21.0
34.0
58.0
71.0
...
897.0
931.0
942.0
955.0
957.0
975.0
shape: 94

ts is a timestamp object.

>>> 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          2
1.0          2
2.0          2
3.0          2
4.0          3
5.0          3
6.0          2
...
993.0        1
994.0        1
995.0        2
996.0        2
997.0        2
998.0        1
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 = ts.value_from(tsd_from, ep, mode='closest')
>>> tsd_after
Time (s)
----------  --
1.0          2
13.0         3
20.0         3
21.0         1
34.0         3
58.0         1
71.0         1
...
459.0        3
466.0        2
474.0        2
475.0        1
476.0        3
484.0        1
491.0        3
dtype: int64, shape: (53,)

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

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