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.290179
1.0 0.607549
2.0 0.0112764
3.0 0.500046
4.0 0.0336119
5.0 0.0500035
6.0 0.926091
...
93.0 0.674053
94.0 0.262536
95.0 0.792257
96.0 0.908178
97.0 0.523571
98.0 0.446802
99.0 0.32906
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.718049 0.741366 0.0308653
1.0 0.912269 0.166679 0.133716
2.0 0.299747 0.911584 0.31652
3.0 0.369065 0.62699 0.384415
4.0 0.532212 0.301696 0.354819
5.0 0.0853615 0.610561 0.827288
6.0 0.69824 0.931255 0.747439
...
93.0 0.404128 0.602311 0.621358
94.0 0.717372 0.834498 0.393338
95.0 0.885184 0.860446 0.585467
96.0 0.843816 0.229394 0.197271
97.0 0.53939 0.0713994 0.582686
98.0 0.103533 0.0943459 0.508583
99.0 0.420821 0.365143 0.639024
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.81098 ... 0.990353] ...]
1.0 [[0.673624 ... 0.405846] ...]
2.0 [[0.503924 ... 0.078329] ...]
3.0 [[0.279835 ... 0.716857] ...]
4.0 [[0.787282 ... 0.107152] ...]
5.0 [[0.028305 ... 0.124568] ...]
6.0 [[0.521427 ... 0.515634] ...]
...
93.0 [[0.460627 ... 0.008133] ...]
94.0 [[0.462699 ... 0.826132] ...]
95.0 [[0.390058 ... 0.896685] ...]
96.0 [[0.961737 ... 0.829967] ...]
97.0 [[0.159861 ... 0.195906] ...]
98.0 [[0.075542 ... 0.592684] ...]
99.0 [[0.060396 ... 0.679566] ...]
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)
0.080405519
2.764453629
7.653873798
13.58417204
36.003652011
45.427553225
67.851833407
71.800845599
81.156204828
87.437401709
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.0041
1 20.0083
2 30.0125
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 -1.08007
1 1.1124
2 -1.87303
3 -0.595168
4 -0.068596
5 0.724248
6 0.474248
7 -0.279912
8 -0.133509
9 1.57398
dtype: float64, shape: (10,)
to :
new_tsd = tsd.restrict(ep)
print(new_tsd)
Time (s)
---------- ---------
0 -1.08007
1 1.1124
2 -1.87303
3 -0.595168
7 -0.279912
8 -0.133509
9 1.57398
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.582967 0.671526 0.720581 0.522852
1.0 0.106441 0.617627 0.610933 0.623864
2.0 0.450063 0.598902 0.460234 0.626798
3.0 0.730849 0.557076 0.431721 0.10807
4.0 0.396138 0.356121 0.518555 0.48898
5.0 0.669234 0.275283 0.379349 0.272046
6.0 0.671848 0.14631 0.452384 0.534817
...
93.0 0.500151 0.876448 0.74318 0.803716
94.0 0.372174 0.326717 0.526241 0.637089
95.0 0.658243 0.608586 0.581749 0.608421
96.0 0.232309 0.619391 0.617049 0.409568
97.0 0.680957 0.525701 0.773036 0.633158
98.0 0.531129 0.319292 0.562378 0.242882
99.0 0.510857 0.416415 0.54438 0.605407
dtype: float64, shape: (100, 4)
Averaging along the time axis will return a numpy array object:
print(
np.mean(tsdtensor, 0)
)
[[0.46881742 0.532337 0.50633207 0.48277466]
[0.48193884 0.47302201 0.4938226 0.55581783]
[0.4966171 0.54089598 0.47960547 0.46137622]]
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 0.0506634 0.248633 0.457537 -0.175388
0.1 0.715696 -1.82192 0.841611 -0.558922
0.2 -0.751384 0.616098 0.674911 2.61405
0.3 1.33992 0.115942 1.34855 -0.373186
0.4 0.0426354 1.47576 -1.03831 -0.723806
0.5 -0.915272 0.0341948 1.91842 -0.728856
0.6 -0.253131 -0.00758213 -0.355352 1.34646
0.7 0.13206 1.22236 0.719314 -1.02035
0.8 -0.627509 0.32673 -0.532987 -1.49962
0.9 -1.01603 -1.49943 0.837423 -1.48422
dtype: float64, shape: (10, 4)
print(tsdframe[4:7])
Time (s) 0 1 2 3
---------- ---------- ----------- --------- ---------
0.4 0.0426354 1.47576 -1.03831 -0.723806
0.5 -0.915272 0.0341948 1.91842 -0.728856
0.6 -0.253131 -0.00758213 -0.355352 1.34646
dtype: float64, shape: (3, 4)
print(tsdframe[:,0])
Time (s)
---------- ----------
0 0.0506634
0.1 0.715696
0.2 -0.751384
0.3 1.33992
0.4 0.0426354
0.5 -0.915272
0.6 -0.253131
0.7 0.13206
0.8 -0.627509
0.9 -1.01603
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 -1.06632
1 -0.802131
2 -1.04822
3 -1.79697
4 -1.48628
5 -0.926501
6 -0.589084
7 1.50523
8 -2.04795
9 -0.908785
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.28556 1.19638 1.23626
1 0.191676 -1.36894 1.12791
2 0.66728 -1.05385 -0.509776
3 2.04104 -1.71291 -1.77704
dtype: float64, shape: (4, 3)
[ 2.04103869 -1.71291214 -1.77703863]
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.013654971
0.118785267
0.280120626
0.298965234
0.558926235
0.871965738
0.89266946
...
99.633270846
99.658924848
99.846691722
99.869495478
99.869665061
99.894979808
99.953274544
shape: 1000
Slicing:
print(tsgroup[[0, 2]])
Index rate
------- -------
0 10.0041
2 30.0125
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.513654971 8 19 26
1.513654971 16 11 25
2.513654971 6 18 24
3.513654971 9 24 22
4.513654971 13 20 29
5.513654971 6 12 33
6.513654971 12 26 28
...
93.513654971 6 16 25
94.513654971 9 20 29
95.513654971 9 25 29
96.513654971 10 31 34
97.513654971 8 20 30
98.513654971 9 18 32
99.513654971 11 18 38
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.493021
1 0.013094
2 0.674886
3 0.164758
4 0.0166289
5 0.416454
6 0.645169
7 0.994349
8 0.940563
9 0.831535
dtype: float64, shape: (10,)
Time (s)
---------- --------
2 0.674886
6 0.645169
7 0.994349
8 0.940563
9 0.831535
dtype: float64, shape: (5,)
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 1.5
1 2.5 5.5
shape: (2, 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.43889 0.885849 0.0695796
0.1 -0.785995 0.0380492 -0.757078
0.2 -1.33988 -0.901356 -1.584
0.3 0.489894 -0.418404 0.504686
0.4 -0.649127 -1.1127 0.320679
0.5 2.09366 0.689575 -1.4898
0.6 0.390467 -1.63197 2.2689
...
9.3 -2.42333 1.19812 0.864943
9.4 0.514316 -0.111172 -0.721793
9.5 -0.885745 -0.394747 1.67353
9.6 0.608712 -1.61442 0.344974
9.7 0.00686128 -2.10256 -0.0393451
9.8 0.261917 -1.29562 0.137775
9.9 -1.02212 0.960664 -1.44472
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.19974 -0.223409 -0.302242
1.5 0.305996 0.0195996 -0.850827
2.5 0.260453 0.355405 -0.165736
3.5 0.259453 -0.592384 0.239479
4.5 -0.236748 -0.235955 0.253997
5.5 0.344163 0.198811 0.122942
6.5 0.122952 0.359794 -0.258158
7.5 0.181488 0.37725 0.57136
8.5 -0.457635 0.193112 0.344298
9.5 -0.165631 -0.539564 0.366281
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 modules allows for computing analytic signals and extracting the phase and envelope.
This module provides methods for building trial-based tensors and time-warped trial-based tensors.