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.186692
1.0 0.0584898
2.0 0.953792
3.0 0.0206506
4.0 0.656827
5.0 0.541003
6.0 0.0712807
...
93.0 0.52275
94.0 0.259731
95.0 0.286987
96.0 0.170476
97.0 0.317588
98.0 0.458038
99.0 0.293482
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.735578 0.0923586 0.163201
1.0 0.956388 0.462336 0.875708
2.0 0.159868 0.89595 0.437328
3.0 0.869937 0.282365 0.431393
4.0 0.548772 0.00254393 0.328366
5.0 0.213888 0.483901 0.915165
6.0 0.740494 0.938885 0.793916
...
93.0 0.194238 0.591946 0.558185
94.0 0.990586 0.192972 0.186723
95.0 0.311333 0.751752 0.846317
96.0 0.873421 0.385836 0.198322
97.0 0.683563 0.274438 0.685149
98.0 0.385282 0.272793 0.536595
99.0 0.283649 0.367066 0.661982
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.435365 ... 0.261254] ...]
1.0 [[0.411434 ... 0.294697] ...]
2.0 [[0.527693 ... 0.359994] ...]
3.0 [[0.111626 ... 0.733191] ...]
4.0 [[0.794481 ... 0.622684] ...]
5.0 [[0.26038 ... 0.572627] ...]
6.0 [[0.27052 ... 0.865455] ...]
...
93.0 [[0.263 ... 0.244202] ...]
94.0 [[0.713006 ... 0.974396] ...]
95.0 [[0.597712 ... 0.104471] ...]
96.0 [[0.827283 ... 0.285449] ...]
97.0 [[0.186765 ... 0.208465] ...]
98.0 [[0.047052 ... 0.31883 ] ...]
99.0 [[0.681105 ... 0.426108] ...]
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)
9.257668639584027
22.070180400173356
29.426815914452952
35.83927800334432
37.581376140468016
62.4298296773688
70.5451884596394
83.7643489436675
90.54139263494937
93.91457824383393
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.0022
1 20.0043
2 30.0065
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 -1.8387
1 -0.698564
2 -1.10918
3 -1.26117
4 0.895353
5 -2.20269
6 0.252363
7 0.644598
8 0.895731
9 -0.10883
dtype: float64, shape: (10,)
to :
new_tsd = tsd.restrict(ep)
print(new_tsd)
Time (s)
---------- ---------
0 -1.8387
1 -0.698564
2 -1.10918
3 -1.26117
7 0.644598
8 0.895731
9 -0.10883
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.383892 0.40138 0.311536 0.558869
1.0 0.451837 0.902545 0.483746 0.71024
2.0 0.874661 0.565339 0.211734 0.556049
3.0 0.553197 0.446846 0.509472 0.651342
4.0 0.376945 0.570276 0.309544 0.364869
5.0 0.684604 0.619488 0.50153 0.484291
6.0 0.457847 0.487666 0.492055 0.143066
...
93.0 0.606988 0.508242 0.61306 0.564666
94.0 0.281482 0.595852 0.295384 0.343307
95.0 0.521206 0.307546 0.384952 0.63179
96.0 0.497075 0.337748 0.26567 0.317049
97.0 0.192799 0.502951 0.385146 0.378688
98.0 0.633869 0.348668 0.493177 0.398842
99.0 0.384479 0.797064 0.473748 0.789222
dtype: float64, shape: (100, 4)
Averaging along the time axis will return a numpy array object:
print(
np.mean(tsdtensor, 0)
)
[[0.52492872 0.49840182 0.49404674 0.51964588]
[0.48522054 0.52899371 0.48002122 0.45437012]
[0.52056134 0.53852703 0.46093151 0.49425633]]
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.436318 0.320296 0.100917 0.756777
0.1 -0.9427 -0.970657 -2.48148 -2.04484
0.2 0.688484 -0.130928 1.27715 -1.25756
0.3 0.301824 1.2182 0.454298 -0.79868
0.4 0.807748 0.474869 -0.514188 1.53822
0.5 -0.520685 -0.854424 1.51489 -0.39525
0.6 -0.623677 -1.0544 0.909653 -0.519583
0.7 1.36431 1.39069 -0.265951 0.852696
0.8 0.0135265 -0.252417 -1.04008 -0.342777
0.9 2.10238 1.68901 -0.108188 -3.49223
dtype: float64, shape: (10, 4)
print(tsdframe[4:7])
Time (s) 0 1 2 3
---------- --------- --------- --------- ---------
0.4 0.807748 0.474869 -0.514188 1.53822
0.5 -0.520685 -0.854424 1.51489 -0.39525
0.6 -0.623677 -1.0544 0.909653 -0.519583
dtype: float64, shape: (3, 4)
print(tsdframe[:,0])
Time (s)
---------- ----------
0 -0.436318
0.1 -0.9427
0.2 0.688484
0.3 0.301824
0.4 0.807748
0.5 -0.520685
0.6 -0.623677
0.7 1.36431
0.8 0.0135265
0.9 2.10238
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.0616972
1 0.728371
2 0.171383
3 -0.815099
4 0.857433
5 -0.667063
6 1.94087
7 0.482904
8 -1.17835
9 -1.11117
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 -2.18333 2.7491 1.24608
1 1.06522 -1.28359 0.684987
2 -1.9737 -0.483331 -0.163351
3 -0.201421 1.20217 -1.1366
dtype: float64, shape: (4, 3)
[-0.20142135 1.20217104 -1.13660168]
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.16554703681905814
0.35564800865611756
0.4598676887117237
0.4706333732023227
0.5128984308708895
0.5683612396526927
0.6268428879451782
...
99.17383417237915
99.21309653068946
99.35729827455219
99.3842954397683
99.39588755765763
99.43283922964505
99.9497841400977
shape: 1000
Slicing:
print(tsgroup[[0, 2]])
Index rate
------- -------
0 10.0022
2 30.0065
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.5162740124851402 12 26 37
1.516274012 10 14 33
2.516274012 12 16 24
3.516274012 10 24 31
4.516274012 18 26 28
5.516274012 9 17 30
6.516274012 9 22 31
...
93.516274012 6 17 34
94.516274012 9 17 29
95.516274012 10 23 37
96.516274012 8 19 26
97.516274012 13 25 26
98.516274012 8 16 21
99.516274012 7 15 27
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.276458
1 0.678111
2 0.624869
3 0.970133
4 0.341076
5 0.247336
6 0.259953
7 0.304425
8 0.131651
9 0.560809
dtype: float64, shape: (10,)
Time (s)
---------- --------
1 0.678111
2 0.624869
3 0.970133
9 0.560809
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 0.5
1 3.5 8.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 0.362692 -0.437984 -0.167359
0.1 -0.317843 -1.44436 1.15531
0.2 -0.327866 -1.76921 -0.61043
0.3 0.563524 -0.00522059 -1.18855
0.4 0.939607 -0.803718 0.833118
0.5 -0.093179 -2.18647 -1.36437
0.6 -0.641716 0.554087 -0.146128
...
9.3 -0.198127 1.38602 1.27076
9.4 -0.569093 2.82563 -0.817044
9.5 0.473236 -0.181735 0.163458
9.6 0.705792 -0.236879 0.0514112
9.7 0.579514 -0.782088 0.588866
9.8 -0.12694 0.630077 -0.0545509
9.9 1.28226 0.0660253 0.181876
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.033445 -0.699179 -0.0706888
1.5 -0.29536 0.114049 0.237915
2.5 0.205847 0.287525 0.0978722
3.5 0.00604706 -0.240211 0.263527
4.5 0.500979 -0.227536 0.439478
5.5 0.0110232 0.175718 0.123501
6.5 -0.126402 -0.262607 0.101979
7.5 -0.308386 0.182544 -0.302269
8.5 -0.63741 0.108053 0.166357
9.5 0.182534 0.367617 -0.191733
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.