pynapple.Tsd.value_from#

Tsd.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, a tsd object will receive the closest values in time from another 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
>>> tsd_before = nap.Tsd(t=t, d=np.random.rand(len(t)), time_units='s')
>>> tsd_before
Time (s)
----------  ---------
1.0         0.449754
13.0        0.39515
20.0        0.926659
21.0        0.727272
34.0        0.326541
58.0        0.570444
71.0        0.520834
...
875.0       0.0648922
897.0       0.253915
931.0       0.246876
942.0       0.696304
955.0       0.712271
957.0       0.148087
975.0       0.99774
dtype: float64, shape: (94,)

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

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

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