Core methods#

Hide code cell content
import pynapple as nap
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
custom_params = {"axes.spines.right": False, "axes.spines.top": False}
sns.set_theme(style="ticks", palette="colorblind", font_scale=1.5, rc=custom_params)

Time series method#

Hide code cell content
tsdframe = nap.TsdFrame(t=np.arange(100), d=np.random.randn(100, 3), columns=['a', 'b', 'c'])
group = {
    0: nap.Ts(t=np.sort(np.random.uniform(0, 100, 10))),
    1: nap.Ts(t=np.sort(np.random.uniform(0, 100, 20))),
    2: nap.Ts(t=np.sort(np.random.uniform(0, 100, 30))),
}
tsgroup = nap.TsGroup(group)
epochs = nap.IntervalSet([10, 65], [25, 80])
tsd = nap.Tsd(t=np.arange(0, 100, 1), d=np.sin(np.arange(0, 10, 0.1)))

restrict#

restrict is used to get time points within an IntervalSet. This method is available for TsGroup, Tsd, TsdFrame, TsdTensor and Ts objects.

tsdframe.restrict(epochs) 
Time (s)    a         b         c
----------  --------  --------  --------
10.0        -0.11332  -0.13032  -2.39232
11.0        0.20949   -0.09281  -1.18848
12.0        -0.26742  -3.03556  -1.64385
13.0        0.72085   0.88107   -0.79903
14.0        -1.10181  0.53187   1.50105
15.0        -0.59425  -1.53259  0.48834
16.0        0.23247   0.03821   0.42385
...         ...       ...       ...
74.0        -0.39208  0.18018   0.02855
75.0        0.70748   -0.17915  0.64732
76.0        -0.3      -1.84877  -0.35967
77.0        -0.05634  1.26233   -1.12359
78.0        0.00217   -0.18673  1.6825
79.0        1.359     -0.50409  -0.71351
80.0        -0.1066   1.93288   -0.42313
dtype: float64, shape: (32, 3)
Hide code cell source
plt.figure()
plt.plot(tsdframe.restrict(epochs))
[plt.axvspan(s, e, alpha=0.2) for s, e in epochs.values]
plt.xlabel("Time (s)")
plt.title("tsdframe.restrict(epochs)")
plt.xlim(0, 100)
plt.show()
../_images/baf0becc1bdab85202d42ebde572a0d845b95828dd09be0fcfa3190df0839086.png

This operation update the time support attribute accordingly.

print(epochs)
print(tsdframe.restrict(epochs).time_support) 
  index    start    end
      0       10     25
      1       65     80
shape: (2, 2), time unit: sec.
  index    start    end
      0       10     25
      1       65     80
shape: (2, 2), time unit: sec.

count#

count the number of timestamps within bins or epochs of an IntervalSet object. This method is available for TsGroup, Tsd, TsdFrame, TsdTensor and Ts objects.

With a defined bin size:

count1 = tsgroup.count(bin_size=1.0, time_units='s')
print(count1) 
Time (s)      0    1    2
------------  ---  ---  ---
0.932888588   2.0  0.0  0.0
1.932888588   0.0  2.0  1.0
2.932888588   0.0  0.0  0.0
3.932888588   0.0  0.0  1.0
4.932888588   0.0  0.0  0.0
5.932888588   0.0  1.0  0.0
6.932888588   1.0  0.0  0.0
...           ...  ...  ...
91.932888588  0.0  0.0  0.0
92.932888588  0.0  0.0  0.0
93.932888588  0.0  0.0  0.0
94.932888588  0.0  1.0  0.0
95.932888588  0.0  0.0  0.0
96.932888588  0.0  0.0  0.0
97.932888588  0.0  1.0  1.0
dtype: float64, shape: (98, 3)
Hide code cell source
plt.figure()
plt.plot(count1[:,2], 'o-')
plt.title("tsgroup.count(bin_size=1.0)")
plt.plot(tsgroup[2].fillna(-1), '|', markeredgewidth=2)
[plt.axvline(t, linewidth=0.5, alpha=0.5) for t in np.arange(0, 21)]
plt.xlabel("Time (s)")
plt.xlim(0, 20)
plt.show()
../_images/e6e2070d846e7b8c9bad9675e5cf02b29c26f3fb85a263c2f8417dd16ad48bb6.png

With an IntervalSet:

count_ep = tsgroup.count(ep=epochs)

print(count_ep)
Time (s)      0    1    2
----------  ---  ---  ---
17.5          2    2    4
72.5          1    2   12
dtype: float64, shape: (2, 3)

bin_average#

bin_average downsample time series by averaging data point falling within a bin. This method is available for Tsd, TsdFrame and TsdTensor.

tsdframe.bin_average(3.5)
Time (s)    a         b         c
----------  --------  --------  --------
1.75        -0.52761  -0.36568  -0.55015
5.25        0.35983   -0.94439  0.64676
8.75        0.52389   0.41946   -1.08715
12.25       0.22098   -0.7491   -1.21045
15.75       -0.30719  0.19163   0.69525
19.25       -0.60948  0.82747   -0.46772
22.75       0.57833   -0.50003  0.3608
...         ...       ...       ...
75.25       0.00513   -0.61591  0.1054
78.75       0.29956   0.6261    -0.14443
82.25       1.02615   0.11774   -0.17694
85.75       -0.84745  0.75071   -0.384
89.25       -0.50002  0.57645   -1.18509
92.75       -0.29161  0.30926   -0.9551
96.25       0.17966   -1.14467  0.96564
dtype: float64, shape: (28, 3)
Hide code cell source
bin_size = 3.5
plt.figure()
plt.plot(tsdframe[:,0], '.--', label="tsdframe[:,0]")
plt.plot(tsdframe[:,0].bin_average(bin_size), 'o-', label="new_tsdframe[:,0]")
plt.title(f"tsdframe.bin_average(bin_size={bin_size})")
[plt.axvline(t, linewidth=0.5, alpha=0.5) for t in np.arange(0, 21,bin_size)]
plt.xlabel("Time (s)")
plt.xlim(0, 20)
plt.legend(bbox_to_anchor=(1.0, 0.5, 0.5, 0.5))
plt.show()
../_images/c9d9e6ed119fe31a3c8b8010ae8f9d44604e2d6ed8d6dcef5ef03258c807cf7f.png

interpolate#

Theinterpolate method of Tsd, TsdFrame and TsdTensor can be used to fill gaps in a time series. It is a wrapper of numpy.interp.

Hide code cell content
tsd = nap.Tsd(t=np.arange(0, 25, 5), d=np.random.randn(5))
ts = nap.Ts(t=np.arange(0, 21, 1))
new_tsd = tsd.interpolate(ts)
Hide code cell source
plt.figure()
plt.plot(new_tsd, '.-', label="new_tsd")
plt.plot(tsd, 'o', label="tsd")
plt.plot(ts.fillna(0), '+', label="ts")
plt.title("tsd.interpolate(ts)")
plt.xlabel("Time (s)")
plt.legend(bbox_to_anchor=(1.0, 0.5, 0.5, 0.5))
plt.show()
../_images/df5e0a91bf4b4573b153581f92dfc6dd7a9a53cb681a50eaed51495cc429d8d9.png

value_from#

value_from assign to every timestamps the closed value in time from another time series. Let’s define the time series we want to assign values from.

For every timestamps in tsgroup, we want to assign the closest value in time from tsd.

Hide code cell content
tsd = nap.Tsd(t=np.arange(0, 100, 1), d=np.sin(np.arange(0, 10, 0.1)))
tsgroup_from_tsd = tsgroup.value_from(tsd)

We can display the first element of tsgroup and tsgroup_sin.

Hide code cell source
plt.figure()
plt.plot(tsgroup[0].fillna(0), "|", label="tsgroup[0]", markersize=20, mew=3)
plt.plot(tsd, linewidth=2, label="tsd")
plt.plot(tsgroup_from_tsd[0], "o", label = "tsgroup_from_tsd[0]", markersize=20)
plt.title("tsgroup.value_from(tsd)")
plt.xlabel("Time (s)")
plt.yticks([-1, 0, 1])
plt.legend(bbox_to_anchor=(1.0, 0.5, 0.5, 0.5))
plt.show()
../_images/894b708960bf8df469693a104290d9797535625a3b508123464083e646f757a2.png

threshold#

The method threshold of Tsd returns a new Tsd with all the data above or below a certain threshold. Default is above. The time support of the new Tsd object get updated accordingly.

Hide code cell content
tsd = nap.Tsd(t=np.arange(0, 100, 1), d=np.sin(np.arange(0, 10, 0.1)))
tsd_above = tsd.threshold(0.5, method='above')

This method can be used to isolate epochs for which a signal is above/below a certain threshold.

epoch_above = tsd_above.time_support
Hide code cell source
plt.figure()
plt.plot(tsd, label="tsd")
plt.plot(tsd_above, 'o-', label="tsd_above")
[plt.axvspan(s, e, alpha=0.2) for s, e in epoch_above.values]
plt.axhline(0.5, linewidth=0.5, color='grey')
plt.legend()
plt.xlabel("Time (s)")
plt.title("tsd.threshold(0.5)")
plt.show()
../_images/80a39ba5f7ade7f37015176686a51a30a9f57efe6d420cd4d1c133c6bbe7c452.png

Mapping between TsGroup and Tsd#

It’s is possible to transform a TsGroup to Tsd with the method to_tsd and a Tsd to TsGroup with the method to_tsgroup.

This is useful to flatten the activity of a population in a single array.

tsd = tsgroup.to_tsd()

print(tsd)
Time (s)
------------  --
0.432888588    0
1.344059152    0
1.949615176    1
2.003917529    2
2.047915159    1
4.263436221    2
5.584064128    1
...
80.203842743   1
85.051360084   2
85.912322461   2
86.208192629   2
95.26550339    1
98.051674256   1
98.427014938   2
dtype: float64, shape: (60,)

The object tsd contains all the timestamps of the tsgroup with the associated value being the index of the unit in the TsGroup.

The method to_tsgroup converts the Tsd object back to the original TsGroup.

back_to_tsgroup = tsd.to_tsgroup()

print(back_to_tsgroup)
  Index     rate
-------  -------
      0  0.10205
      1  0.20409
      2  0.30614

Parameterizing a raster#

The method to_tsd makes it easier to display a raster plot. TsGroup object can be plotted with plt.plot(tsgroup.to_tsd(), 'o'). Timestamps can be mapped to any values passed directly to the method or by giving the name of a specific metadata name of the TsGroup.

tsgroup['label'] = np.arange(3)*np.pi

print(tsgroup)
  Index     rate    label
-------  -------  -------
      0  0.10205  0
      1  0.20409  3.14159
      2  0.30614  6.28319
Hide code cell source
plt.figure()
plt.subplot(2,2,1)
plt.plot(tsgroup.to_tsd(), '|')
plt.title("tsgroup.to_tsd()")
plt.xlabel("Time (s)")

plt.subplot(2,2,2)
plt.plot(tsgroup.to_tsd([10,20,30]), '|')
plt.title("togroup.to_tsd([10,20,30])")
plt.xlabel("Time (s)")

plt.subplot(2,2,3)
plt.plot(tsgroup.to_tsd("label"), '|')
plt.title("togroup.to_tsd('label')")
plt.xlabel("Time (s)")
plt.tight_layout()
plt.show()
../_images/97d4a958915b1144095a44de008ab1a8205a1ab6d74ca8266a500d5566c4e2c2.png

Special slicing : TsdFrame#

For users that are familiar with pandas, TsdFrame is the closest object to a DataFrame. but there are distinctive behavior when slicing the object. TsdFrame behaves primarily like a numpy array. This section lists all the possible ways of slicing TsdFrame.

1. If not column labels are passed#

tsdframe = nap.TsdFrame(t=np.arange(4), d=np.random.randn(4,3))
print(tsdframe)
Time (s)           0         1         2
----------  --------  --------  --------
0           -0.57924  -1.17769  -0.88778
1           -1.27598  -0.05642  -1.23292
2            0.23143  -0.32768   1.70402
3           -0.85883   0.03501  -0.54912
dtype: float64, shape: (4, 3)

Slicing should be done like numpy array :

tsdframe[0]
array([-0.57924126, -1.17769304, -0.88777596])
tsdframe[:, 1]
Time (s)
----------  ----------
0           -1.17769
1           -0.056425
2           -0.327682
3            0.0350052
dtype: float64, shape: (4,)
tsdframe[:, [0, 2]]
Time (s)           0         2
----------  --------  --------
0           -0.57924  -0.88778
1           -1.27598  -1.23292
2            0.23143   1.70402
3           -0.85883  -0.54912
dtype: float64, shape: (4, 2)

2. If column labels are passed as integers#

The typical case is channel mapping. The order of the columns on disk are different from the order of the columns on the recording device it corresponds to.

tsdframe = nap.TsdFrame(t=np.arange(4), d=np.random.randn(4,4), columns = [3, 2, 0, 1])
print(tsdframe)
Time (s)           3         2         0         1
----------  --------  --------  --------  --------
0            0.09738   0.35388   0.09691   0.70291
1            0.85299  -0.10331  -0.06359   0.78312
2           -1.2344    0.2721    0.00093  -0.87456
3            0.19506   0.32317   0.17271   0.73063
dtype: float64, shape: (4, 4)

In this case, indexing like numpy still has priority which can led to confusing behavior :

tsdframe[:, [0, 2]]
Time (s)           3         0
----------  --------  --------
0            0.09738   0.09691
1            0.85299  -0.06359
2           -1.2344    0.00093
3            0.19506   0.17271
dtype: float64, shape: (4, 2)

Note how this corresponds to column labels 3 and 0.

To slice using column labels only, the TsdFrame object has the loc method similar to Pandas :

tsdframe.loc[[0, 2]]
Time (s)           0         2
----------  --------  --------
0            0.09691   0.35388
1           -0.06359  -0.10331
2            0.00093   0.2721
3            0.17271   0.32317
dtype: float64, shape: (4, 2)

In this case, this corresponds to columns labelled 0 and 2.

3. If column labels are passed as strings#

Similar to Pandas, it is possible to label columns using strings.

tsdframe = nap.TsdFrame(t=np.arange(4), d=np.random.randn(4,3), columns = ["kiwi", "banana", "tomato"])
print(tsdframe)
Time (s)        kiwi    banana    tomato
----------  --------  --------  --------
0            2.06108   0.68375  -0.47273
1           -0.4386    0.43254   0.69227
2            0.27001   0.41652   2.1873
3            1.20322   1.98281   1.17817
dtype: float64, shape: (4, 3)

When the column labels are all strings, it is possible to use either direct bracket indexing or using the loc method:

print(tsdframe['kiwi'])
print(tsdframe.loc['kiwi']) 
Time (s)
----------  ---------
0            2.06108
1           -0.438597
2            0.270013
3            1.20322
dtype: float64, shape: (4,)
Time (s)
----------  ---------
0            2.06108
1           -0.438597
2            0.270013
3            1.20322
dtype: float64, shape: (4,)

4. If column labels are mixed type#

It is possible to mix types in column names.

tsdframe = nap.TsdFrame(t=np.arange(4), d=np.random.randn(4,3), columns = ["kiwi", 0, np.pi])
print(tsdframe)
Time (s)        kiwi         0    3.141592653589793
----------  --------  --------  -------------------
0           -1.37821   0.03486              0.69055
1            1.47178   0.41471              1.05142
2            3.15982  -1.86143             -0.64798
3           -0.09222   0.02498             -0.10355
dtype: float64, shape: (4, 3)

Direct bracket indexing only works if the column label is a string.

print(tsdframe['kiwi'])
Time (s)
----------  ----------
0           -1.37821
1            1.47178
2            3.15982
3           -0.0922219
dtype: float64, shape: (4,)

To slice with mixed types, it is best to use the loc method :

print(tsdframe.loc[['kiwi', np.pi]])
Time (s)        kiwi    3.141592653589793
----------  --------  -------------------
0           -1.37821              0.69055
1            1.47178              1.05142
2            3.15982             -0.64798
3           -0.09222             -0.10355
dtype: float64, shape: (4, 2)

In general, it is probably a bad idea to mix types when labelling columns.

Interval sets methods#

Interaction between epochs#

epoch1 = nap.IntervalSet(start=0, end=10)  # no time units passed. Default is us.
epoch2 = nap.IntervalSet(start=[5, 30], end=[20, 45])
print(epoch1, "\n")
print(epoch2, "\n")
  index    start    end
      0        0     10
shape: (1, 2), time unit: sec. 

  index    start    end
      0        5     20
      1       30     45
shape: (2, 2), time unit: sec. 

union#

epoch = epoch1.union(epoch2)
print(epoch)
  index    start    end
      0        0     20
      1       30     45
shape: (2, 2), time unit: sec.

intersect#

epoch = epoch1.intersect(epoch2)
print(epoch)
  index    start    end
      0        5     10
shape: (1, 2), time unit: sec.

set_diff#

epoch = epoch1.set_diff(epoch2)
print(epoch)
  index    start    end
      0        0      5
shape: (1, 2), time unit: sec.

split#

Useful for chunking time series, the split method splits an IntervalSet in a new IntervalSet based on the interval_size argument.

epoch = nap.IntervalSet(start=0, end=100)

print(epoch.split(10, time_units="s"))
  index    start    end
      0        0     10
      1       10     20
      2       20     30
      3       30     40
      4       40     50
      5       50     60
      6       60     70
      7       70     80
      8       80     90
      9       90    100
shape: (10, 2), time unit: sec.

Drop intervals#

epoch = nap.IntervalSet(start=[5, 30], end=[6, 45])
print(epoch)
  index    start    end
      0        5      6
      1       30     45
shape: (2, 2), time unit: sec.

drop_short_intervals#

print(
    epoch.drop_short_intervals(threshold=5)
    )
  index    start    end
      0       30     45
shape: (1, 2), time unit: sec.

drop_long_intervals#

print(
    epoch.drop_long_intervals(threshold=5)
    )
  index    start    end
      0        5      6
shape: (1, 2), time unit: sec.

merge_close_intervals#

Hide code cell source
epoch = nap.IntervalSet(start=[1, 7], end=[6, 45])
print(epoch)
  index    start    end
      0        1      6
      1        7     45
shape: (2, 2), time unit: sec.

If two intervals are closer than the threshold argument, they are merged.

print(
    epoch.merge_close_intervals(threshold=2.0)
    )
  index    start    end
      0        1     45
shape: (1, 2), time unit: sec.