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.308698
1.0 0.587513
2.0 0.372941
3.0 0.158773
4.0 0.108912
5.0 0.0764819
6.0 0.586141
...
93.0 0.035947
94.0 0.23642
95.0 0.41208
96.0 0.105979
97.0 0.668912
98.0 0.296068
99.0 0.268531
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.675187 0.872647 0.631611
1.0 0.623938 0.110166 0.856101
2.0 0.698888 0.156884 0.844417
3.0 0.649824 0.813926 0.504728
4.0 0.617084 0.0587874 0.832097
5.0 0.312579 0.935966 0.380647
6.0 0.842666 0.709563 0.170968
...
93.0 0.451256 0.692907 0.0726903
94.0 0.856778 0.503539 0.0139698
95.0 0.269917 0.574443 0.278924
96.0 0.0809903 0.449075 0.324868
97.0 0.260211 0.417874 0.0862989
98.0 0.0304906 0.305511 0.956257
99.0 0.401766 0.907656 0.642704
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.198746 ... 0.98063 ] ...]
1.0 [[0.583752 ... 0.224794] ...]
2.0 [[0.948067 ... 0.3988 ] ...]
3.0 [[0.037974 ... 0.264452] ...]
4.0 [[0.092863 ... 0.140244] ...]
5.0 [[0.746604 ... 0.994514] ...]
6.0 [[0.98391 ... 0.404406] ...]
...
93.0 [[0.9543 ... 0.261599] ...]
94.0 [[0.83026 ... 0.533003] ...]
95.0 [[0.724039 ... 0.478322] ...]
96.0 [[0.404132 ... 0.221766] ...]
97.0 [[0.855837 ... 0.471349] ...]
98.0 [[0.850392 ... 0.217584] ...]
99.0 [[0.296275 ... 0.973186] ...]
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)
3.414161190191367
5.109839291607865
6.537323160766972
26.04702860795136
35.16713999237739
45.85486446039827
50.23303187910084
54.73073581459333
74.41496236699221
97.28561846865324
shape: 10
nap.TsGroup: group of timestamps#
TsGroup is a dictionary 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 dictionary 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.001
1 20.002
2 30.0029
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 series 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 2.14622
1 -0.308947
2 -0.798797
3 2.01238
4 0.639
5 -0.0497394
6 -0.25516
7 -0.187741
8 0.57031
9 -0.510826
dtype: float64, shape: (10,)
to :
new_tsd = tsd.restrict(ep)
print(new_tsd)
Time (s)
---------- ---------
0 2.14622
1 -0.308947
2 -0.798797
3 2.01238
7 -0.187741
8 0.57031
9 -0.510826
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.702534 0.649093 0.510931 0.548917
1.0 0.249373 0.574856 0.20841 0.534496
2.0 0.577751 0.584377 0.62091 0.469231
3.0 0.565983 0.596248 0.665243 0.488063
4.0 0.532631 0.72012 0.337078 0.361317
5.0 0.395779 0.528819 0.127124 0.268511
6.0 0.710122 0.354425 0.667753 0.32072
...
93.0 0.442732 0.410363 0.493302 0.634534
94.0 0.803859 0.521149 0.209687 0.607486
95.0 0.558542 0.709475 0.369575 0.550107
96.0 0.584983 0.419825 0.634166 0.358697
97.0 0.464961 0.303095 0.613836 0.528852
98.0 0.618078 0.340468 0.220791 0.251834
99.0 0.592781 0.0978181 0.435772 0.316296
dtype: float64, shape: (100, 4)
Averaging along the time axis will return a numpy array object:
print(
np.mean(tsdtensor, 0)
)
[[0.49150597 0.5028356 0.52272272 0.55045308]
[0.50758661 0.51792557 0.49772078 0.48660326]
[0.51272514 0.48670116 0.52843382 0.50733302]]
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.20781 -1.14077 -0.339755 -2.09405
0.1 0.184465 1.43097 0.176146 -0.299472
0.2 -0.171414 -0.715949 -0.390677 -2.04217
0.3 1.26424 0.873652 -0.646128 0.0746554
0.4 -0.109359 -1.019 -1.2376 -0.58311
0.5 -1.02436 -0.404247 0.3705 -0.798952
0.6 -0.467853 -0.56084 -0.201984 -0.270484
0.7 -1.85861 0.0551563 -0.701135 1.20273
0.8 0.242562 0.19074 -0.218811 -0.555226
0.9 -1.36066 1.70634 -0.0261355 -0.949094
dtype: float64, shape: (10, 4)
print(tsdframe[4:7])
Time (s) 0 1 2 3
---------- --------- --------- --------- ---------
0.4 -0.109359 -1.019 -1.2376 -0.58311
0.5 -1.02436 -0.404247 0.3705 -0.798952
0.6 -0.467853 -0.56084 -0.201984 -0.270484
dtype: float64, shape: (3, 4)
print(tsdframe[:,0])
Time (s)
---------- ---------
0 1.20781
0.1 0.184465
0.2 -0.171414
0.3 1.26424
0.4 -0.109359
0.5 -1.02436
0.6 -0.467853
0.7 -1.85861
0.8 0.242562
0.9 -1.36066
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.497214
1 0.240368
2 -0.10222
3 0.255051
4 0.275343
5 -1.62376
6 -0.370136
7 1.34925
8 -0.923809
9 0.638779
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 -0.60393 -0.0988635 -0.171888
1 -0.958575 0.219098 -0.628451
2 0.739682 1.36657 -0.0496038
3 0.180665 0.0136706 0.802375
dtype: float64, shape: (4, 3)
[0.1806648 0.01367058 0.80237492]
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.004240165398738949
0.029571991996724112
0.03239899510092403
0.0579813884254321
0.2695243169638539
0.37560169089903006
0.4507221747771961
...
99.08540593423892
99.13694603700782
99.56546215148892
99.65747757228563
99.8611892342488
99.92271632214683
99.92704704963309
shape: 1000
Slicing:
print(tsgroup[[0, 2]])
Index rate
------- -------
0 10.001
2 30.0029
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.504240165398739 10 23 26
1.504240165 3 19 29
2.504240165 9 25 31
3.504240165 15 14 23
4.504240165 11 12 18
5.504240165 12 16 17
6.504240165 15 16 39
...
93.504240165 15 19 25
94.504240165 9 26 36
95.504240165 13 25 34
96.504240165 12 14 23
97.504240165 12 22 25
98.504240165 9 14 36
99.504240165 9 13 35
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.967802
1 0.369982
2 0.394933
3 0.490548
4 0.231541
5 0.509573
6 0.586346
7 0.155225
8 0.651159
9 0.104685
dtype: float64, shape: (10,)
Time (s)
---------- --------
0 0.967802
5 0.509573
6 0.586346
8 0.651159
dtype: float64, shape: (4,)
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.5 4.5
1 6.5 7.5
2 8.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 0.875135 -0.240311 0.0201283
0.1 0.359046 -0.392172 0.115866
0.2 -1.10547 -0.21771 0.81467
0.3 -0.0331567 -0.74342 -0.907198
0.4 -0.119785 -2.08538 -0.0604287
0.5 -1.18245 -0.206555 -0.316438
0.6 0.430663 2.64401 -1.22293
...
9.3 0.149189 0.0987444 -1.21914
9.4 0.780407 0.76772 -0.987706
9.5 0.802167 -0.573378 0.448505
9.6 -0.895569 0.00274954 2.12248
9.7 -1.03264 1.07514 -0.768415
9.8 -1.90204 0.129377 -0.00881831
9.9 -0.364964 -1.06008 1.06055
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.104746 -0.315851 -0.0607514
1.5 -0.245972 0.184584 -0.389784
2.5 -0.422793 -0.398152 0.147198
3.5 0.0464979 0.686433 -0.240018
4.5 0.0376215 -0.0388518 0.0943311
5.5 0.366837 0.0065325 -0.582155
6.5 0.486888 -0.215951 0.0903164
7.5 0.100763 -0.0466201 0.5933
8.5 0.344254 -0.262746 0.460844
9.5 -0.197622 -0.0395864 -0.0307243
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 dictionary. 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 performs 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.