Introduction to pynapple#
The goal of this tutorial is to quickly learn enough about pynapple to get started with data analysis. This tutorial assumes familiarity with the basics functionalities of numpy.
You can check how to install pynapple here.
Important
By default, pynapple will assume a time units in seconds when passing timestamps array or time parameters such as bin size (unless specified with the time_units argument)
Importing pynapple#
The convention is to import pynapple with a namespace:
import pynapple as nap
Instantiating pynapple objects#
nap.Tsd: 1-dimensional time series#
If you have a 1-dimensional time series, you use the nap.Tsd object. The arguments t and d are the arguments for timestamps and data.
tsd = nap.Tsd(t=np.arange(100), d=np.random.rand(100))
print(tsd)
Time (s)
---------- --------
0.0 0.597667
1.0 0.489525
2.0 0.861813
3.0 0.276282
4.0 0.464154
5.0 0.661476
6.0 0.21541
...
93.0 0.768823
94.0 0.657939
95.0 0.771283
96.0 0.948592
97.0 0.849173
98.0 0.280543
99.0 0.889425
dtype: float64, shape: (100,)
nap.TsdFrame: 2-dimensional time series#
If you have a 2-dimensional time series, you use the nap.TsdFrame object. The arguments t and d are the arguments for timestamps and data. You can add the argument columns to label each columns.
tsdframe = nap.TsdFrame(
t=np.arange(100), d=np.random.rand(100, 3), columns=["a", "b", "c"]
)
print(tsdframe)
Time (s) a b c
---------- ---------- --------- ---------
0.0 0.00138164 0.750142 0.299939
1.0 0.678971 0.659385 0.555152
2.0 0.762483 0.680242 0.0163202
3.0 0.627755 0.899426 0.48724
4.0 0.206533 0.599799 0.710378
5.0 0.403997 0.927562 0.974724
6.0 0.0775724 0.516798 0.0662966
...
93.0 0.17492 0.408394 0.695218
94.0 0.252086 0.297545 0.660354
95.0 0.939776 0.0922707 0.58197
96.0 0.827601 0.0379682 0.474202
97.0 0.416393 0.258462 0.43035
98.0 0.090671 0.58314 0.956237
99.0 0.886312 0.794339 0.13085
dtype: float64, shape: (100, 3)
nap.TsdTensor: n-dimensional time series#
If you have larger than 2 dimensions time series (typically movies), you use the nap.TsdTensor object . The arguments t and d are the arguments for timestamps and data.
tsdtensor = nap.TsdTensor(
t=np.arange(100), d=np.random.rand(100, 3, 4)
)
print(tsdtensor)
Time (s)
---------- -----------------------------
0.0 [[0.43971 ... 0.042699] ...]
1.0 [[0.750158 ... 0.168475] ...]
2.0 [[0.231678 ... 0.314521] ...]
3.0 [[0.623206 ... 0.797968] ...]
4.0 [[0.199641 ... 0.639024] ...]
5.0 [[0.543836 ... 0.314921] ...]
6.0 [[0.986646 ... 0.038855] ...]
...
93.0 [[0.544424 ... 0.368637] ...]
94.0 [[0.515253 ... 0.728628] ...]
95.0 [[0.597089 ... 0.453622] ...]
96.0 [[0.924483 ... 0.461194] ...]
97.0 [[0.108808 ... 0.789486] ...]
98.0 [[0.666149 ... 0.151143] ...]
99.0 [[0.559414 ... 0.261665] ...]
dtype: float64, shape: (100, 3, 4)
nap.IntervalSet: intervals#
The IntervalSet object stores multiple epochs with a common time unit in a table format. The epochs are strictly non-overlapping. Both start and end arguments are necessary.
epochs = nap.IntervalSet(start=[0, 10], end=[5, 15])
print(epochs)
index start end
0 0 5
1 10 15
shape: (2, 2), time unit: sec.
nap.Ts: timestamps#
The Ts object stores timestamps data (typically spike times or reward times). The argument t for timestamps is necessary.
ts = nap.Ts(t=np.sort(np.random.uniform(0, 100, 10)))
print(ts)
Time (s)
13.014411588
20.595781591
35.291251378
35.341336918
36.121811027
36.653790191
69.93563026
76.709427462
98.302353652
99.108048716
shape: 10
nap.TsGroup: group of timestamps#
TsGroup is a dictionnary that stores multiple time series with different time stamps (.i.e. a group of neurons with different spike times from one session). The first argument data can be a dictionnary of Ts, Tsd or numpy 1d array.
data = {
0: nap.Ts(t=np.sort(np.random.uniform(0, 100, 1000))),
1: nap.Ts(t=np.sort(np.random.uniform(0, 100, 2000))),
2: nap.Ts(t=np.sort(np.random.uniform(0, 100, 3000))),
}
tsgroup = nap.TsGroup(data)
print(tsgroup, "\n")
Index rate
------- -------
0 10.0067
1 20.0133
2 30.02
Interaction between pynapple objects#
Time support : attribute of time series#
A key feature of how pynapple manipulates time series is an inherent time support object defined for Ts, Tsd, TsdFrame and TsGroup objects. The time support object is defined as an IntervalSet that provides the time serie with a context. For example, the restrict operation will automatically update the time support object for the new time series. Ideally, the time support object should be defined for all time series when instantiating them. If no time series is given, the time support is inferred from the start and end of the time series.
In this example, a Tsd is instantiated with and without a time support of intervals 0 to 5 seconds. Notice how the shape of the Tsd varies.
time_support = nap.IntervalSet(start=0, end=2)
print(time_support)
index start end
0 0 2
shape: (1, 2), time unit: sec.
Without time support :
print(nap.Tsd(t=[0, 1, 2, 3, 4], d=[0, 1, 2, 3, 4]))
Time (s)
---------- --
0 0
1 1
2 2
3 3
4 4
dtype: int64, shape: (5,)
With time support :
print(
nap.Tsd(
t=[0, 1, 2, 3, 4], d=[0, 1, 2, 3, 4],
time_support = time_support
)
)
Time (s)
---------- --
0 0
1 1
2 2
dtype: int64, shape: (3,)
The time support object has become an attribute of the time series. Depending on the operation applied to the time series, it will be updated.
tsd = nap.Tsd(
t=np.arange(10), d=np.random.randn(10),
time_support = time_support
)
print(tsd.time_support)
index start end
0 0 2
shape: (1, 2), time unit: sec.
Restricting time series to epochs#
The central function of pynapple is the restrict method of Ts, Tsd, TsdFrame and TsGroup. The argument is an IntervalSet object. Only time points within the intervals of the IntervalSet object are returned. The time support of the time series is updated accordingly.
tsd = nap.Tsd(t=np.arange(10), d=np.random.randn(10))
ep = nap.IntervalSet(start=[0, 7], end=[3.5, 12.4])
print(ep)
index start end
0 0 3.5
1 7 12.4
shape: (2, 2), time unit: sec.
From :
print(tsd)
Time (s)
---------- ---------
0 -0.912362
1 0.698239
2 0.998824
3 -0.362134
4 -0.457662
5 0.377119
6 0.607243
7 1.02573
8 2.01605
9 -0.465354
dtype: float64, shape: (10,)
to :
new_tsd = tsd.restrict(ep)
print(new_tsd)
Time (s)
---------- ---------
0 -0.912362
1 0.698239
2 0.998824
3 -0.362134
7 1.02573
8 2.01605
9 -0.465354
dtype: float64, shape: (7,)
Numpy & pynapple#
Pynapple relies on numpy to store the data. Pynapple objects behave very similarly to numpy and numpy functions can be applied directly
tsdtensor = nap.TsdTensor(t=np.arange(100), d=np.random.rand(100, 3, 4))
If a numpy function preserves the time axis, a pynapple object is returned.
In this example, averaging a TsdTensor along the second dimension returns a TsdFrame:
print(
np.mean(tsdtensor, 1)
)
Time (s) 0 1 2 3
---------- -------- -------- -------- ---------
0.0 0.610782 0.633606 0.482939 0.574788
1.0 0.686812 0.601134 0.447847 0.556066
2.0 0.151856 0.440784 0.633003 0.0480801
3.0 0.700996 0.15243 0.45885 0.186125
4.0 0.561318 0.507466 0.535982 0.57637
5.0 0.445979 0.688867 0.553652 0.299613
6.0 0.561736 0.492146 0.223787 0.602279
...
93.0 0.704863 0.141359 0.454289 0.419582
94.0 0.728147 0.799612 0.480831 0.682091
95.0 0.388418 0.449321 0.561023 0.751467
96.0 0.406836 0.103961 0.823678 0.689502
97.0 0.633218 0.660349 0.406719 0.686683
98.0 0.503224 0.527032 0.300725 0.468475
99.0 0.432973 0.319131 0.553021 0.349762
dtype: float64, shape: (100, 4)
Averaging along the time axis will return a numpy array object:
print(
np.mean(tsdtensor, 0)
)
[[0.52199622 0.53388434 0.47056107 0.50053853]
[0.48861133 0.53382909 0.47701037 0.50410815]
[0.51252822 0.49344197 0.49018271 0.50771092]]
Slicing objects#
Slicing time series and intervals#
Like numpy array#
Ts, Tsd, TsdFrame, TsdTensor and IntervalSet can be sliced similar to numpy array:
tsdframe = nap.TsdFrame(t=np.arange(10)/10, d=np.random.randn(10,4))
print(tsdframe)
Time (s) 0 1 2 3
---------- --------- --------- --------- ----------
0 -1.05979 1.22797 -1.27134 -0.539009
0.1 0.195938 -0.195769 1.88263 0.225658
0.2 0.444729 -1.75045 -0.609599 -0.435669
0.3 -1.75764 0.417326 0.254205 0.178866
0.4 0.444505 0.270077 1.11477 -1.45038
0.5 -0.399102 -0.569199 0.222696 2.06912
0.6 -0.697306 -0.199581 -0.931161 -0.0778431
0.7 1.13052 0.222108 1.96693 0.939448
0.8 0.735338 -0.354337 -0.282143 -0.999641
0.9 -0.112738 1.08938 -0.146924 0.214077
dtype: float64, shape: (10, 4)
print(tsdframe[4:7])
Time (s) 0 1 2 3
---------- --------- --------- --------- ----------
0.4 0.444505 0.270077 1.11477 -1.45038
0.5 -0.399102 -0.569199 0.222696 2.06912
0.6 -0.697306 -0.199581 -0.931161 -0.0778431
dtype: float64, shape: (3, 4)
print(tsdframe[:,0])
Time (s)
---------- ---------
0 -1.05979
0.1 0.195938
0.2 0.444729
0.3 -1.75764
0.4 0.444505
0.5 -0.399102
0.6 -0.697306
0.7 1.13052
0.8 0.735338
0.9 -0.112738
dtype: float64, shape: (10,)
ep = nap.IntervalSet(start=[0, 10, 20], end=[4, 15, 25])
print(ep)
index start end
0 0 4
1 10 15
2 20 25
shape: (3, 2), time unit: sec.
print(ep[0:2])
index start end
0 0 4
1 10 15
shape: (2, 2), time unit: sec.
print(ep[1])
index start end
0 10 15
shape: (1, 2), time unit: sec.
Like pandas DataFrame#
Important
This page references all the way to slice TsdFrame
TsdFrame can be sliced like pandas DataFrame when the columns have been labelled with strings :
tsdframe = nap.TsdFrame(t=np.arange(10), d=np.random.randn(10,3), columns=['a', 'b', 'c'])
print(tsdframe['a'])
Time (s)
---------- ----------
0 -0.709744
1 -0.297871
2 -0.207522
3 -0.924687
4 -0.981593
5 -0.935689
6 0.103536
7 0.0848542
8 0.489256
9 0.619162
dtype: float64, shape: (10,)
but integer-indexing only works like numpy if a list of integers is used to label columns :
tsdframe = nap.TsdFrame(t=np.arange(4), d=np.random.randn(4,3), columns=[3, 2, 1])
print(tsdframe, "\n")
print(tsdframe[3])
Time (s) 3 2 1
---------- --------- ----------- ---------
0 -1.4776 -0.00562107 -0.221774
1 1.53177 -2.06289 -0.985396
2 -0.574217 0.0828647 1.46104
3 -1.04457 -2.72851 0.279296
dtype: float64, shape: (4, 3)
[-1.04457339 -2.72850949 0.27929551]
The loc method can be used to slice column-based only:
print(tsdframe.loc[3])
Slicing TsGroup#
TsGroup object can be indexed to return directly the timestamp object or sliced to return a new TsGroup.
Indexing:
print(tsgroup[0], "\n")
Time (s)
0.165657857
0.349486161
0.439008151
0.55497871
0.668679108
0.691229571
0.746379992
...
99.013586315
99.070236793
99.08840871
99.269782914
99.464877869
99.500537477
99.97363709
shape: 1000
Slicing:
print(tsgroup[[0, 2]])
Index rate
------- -------
0 10.0067
2 30.02
Core functions#
Objects have methods that can help transform and refine time series. This is a non exhaustive list.
Binning: counting events#
Time series objects have the count method that count the number of timestamps. This is typically used when counting number of spikes within a particular bin over multiple intervals. The returned object is a Tsd or TsdFrame with the timestamps being the center of the bins.
count = tsgroup.count(1)
print(count)
Time (s) 0 1 2
----------- --- --- ---
0.54777327 13 25 32
1.54777327 13 26 29
2.54777327 9 21 28
3.54777327 5 13 31
4.54777327 10 13 29
5.54777327 14 16 31
6.54777327 12 14 29
...
93.54777327 8 21 39
94.54777327 11 25 25
95.54777327 15 22 30
96.54777327 18 25 29
97.54777327 13 19 29
98.54777327 14 26 26
99.54777327 6 17 28
dtype: int64, shape: (100, 3)
Thresholding#
Some time series have specific methods. The threshold method of Tsd returns a new Tsd with all the data above or below a given value.
tsd = nap.Tsd(t=np.arange(10), d=np.random.rand(10))
print(tsd)
print(tsd.threshold(0.5))
Time (s)
---------- ---------
0 0.433538
1 0.79228
2 0.105801
3 0.816293
4 0.82377
5 0.500204
6 0.608591
7 0.539089
8 0.0928263
9 0.410181
dtype: float64, shape: (10,)
Time (s)
---------- --------
1 0.79228
3 0.816293
4 0.82377
5 0.500204
6 0.608591
7 0.539089
dtype: float64, shape: (6,)
An important aspect of the tresholding is that the time support get updated based on the time points remaining. To get the epochs above/below a certain threshold, you can access the time support of the returned object.
print(tsd.time_support)
print(tsd.threshold(0.5, "below").time_support)
index start end
0 0 9
shape: (1, 2), time unit: sec.
index start end
0 0 0.5
1 1.5 2.5
2 7.5 9
shape: (3, 2), time unit: sec.
Time-bin averaging of data#
Many analyses requires to bring time series to the same rates and same dimensions. A quick way to downsample a time series to match in size for example a count array is to bin average. The bin_average method takes a bin size in unit of time.
tsdframe = nap.TsdFrame(t=np.arange(0, 100)/10, d=np.random.randn(100,3))
print(tsdframe)
Time (s) 0 1 2
---------- --------- ---------- ----------
0.0 1.7539 -0.859829 1.3034
0.1 0.177225 0.780792 -2.11161
0.2 -1.06363 1.39132 0.518663
0.3 0.987645 -1.50892 2.0864
0.4 0.299551 -0.382373 -0.0412513
0.5 -0.24715 0.8963 -1.83804
0.6 1.05237 1.72895 -0.379201
...
9.3 -0.620279 0.197302 0.0768078
9.4 -0.453773 0.0934914 1.5342
9.5 -0.246108 0.276373 -0.16601
9.6 -1.41285 -0.76853 -0.726569
9.7 -0.885964 0.327409 -0.387105
9.8 0.925209 -0.24695 -1.82291
9.9 0.958679 -0.454163 0.570685
dtype: float64, shape: (100, 3)
Here we go from a timepoint every 100ms to a timepoint every second.
print(tsdframe.bin_average(1))
Time (s) 0 1 2
---------- ------------ ----------- ----------
0.5 0.373079 0.00766696 0.0595008
1.5 -0.393871 -0.252231 0.0234697
2.5 0.159134 0.333353 0.137074
3.5 0.456615 -0.00300065 0.187397
4.5 0.33761 0.169651 -0.0456744
5.5 0.000567709 -0.0998906 0.817627
6.5 0.416608 -0.872527 0.128612
7.5 -0.280223 -0.50657 0.369353
8.5 0.306362 -0.132841 -0.502574
9.5 -0.42186 0.0689126 0.0207782
dtype: float64, shape: (10, 3)
Loading data#
See here for more details about loading data.
Loading NWB#
Pynapple supports by default the NWB standard.
NWB files can be loaded with :
nwb = nap.load_file("path/to/my.nwb")
or directly with the NWBFile class:
nwb = nap.NWBFile("path/to/my.nwb")
print(nwb)
my.nwb
┍━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┑
│ Keys │ Type │
┝━━━━━━━━━━━━━━━━━┿━━━━━━━━━━━━━┥
│ units │ TsGroup │
┕━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┙
The returned object behaves like a dictionnary. The first column indicates the keys. The second column indicate the object type.
print(nwb['units'])
Index rate location group
------- ------ ---------- -------
0 1.0 brain 0
1 1.0 brain 0
2 1.0 brain 0
Overview of advanced analysis#
The process module of pynapple contains submodules that group methods that can be applied for high level analysis. All of the method are directly available from the nap namespace.
This module analyses discrete events, specifically correlograms (for example by computing the cross-correlograms of a population of neurons) and interspike interval (ISI) distributions.
The decoding module perfoms bayesian decoding given a set of tuning curves and a TsGroup.
Bandpass, lowpass, highpass or bandstop filtering can be done to any time series using either Butterworth filter or windowed-sinc convolution.
The perievent module has a set of functions to center time series and timestamps data around a particular events.
The randomize module holds multiple technique to shuffle timestamps in order to create surrogate datasets.
The spectrum module contains the methods to return the (mean) power spectral density of a time series.
Tuning curves of neurons based on spiking or calcium activity can be computed.
The wavelets module performs Morlet wavelets decomposition of a time series.
This module provides methods for building trial-based tensors and time-warped trial-based tensors.