Numpy tutorial#

This tutorial shows how pynapple interact with numpy.

Hide code cell content

import numpy as np
import pynapple as nap
import pandas as pd

Multiple time series object are avaible depending on the shape of the data.

  • TsdTensor : for data with of more than 2 dimensions, typically movies.

  • TsdFrame : for column-based data. It can be easily converted to a pandas.DataFrame. Columns can be labelled and selected similar to pandas.

  • Tsd : one-dimensional time series. It can be converted to a pandas.Series.

  • Ts : For timestamps data only.

Initialization#

tsdtensor = nap.TsdTensor(t=np.arange(100), d=np.random.rand(100, 5, 5), time_units="s")
tsdframe = nap.TsdFrame(t=np.arange(100), d=np.random.rand(100, 3), columns = ['a', 'b', 'c'])
tsd = nap.Tsd(t=np.arange(100), d=np.random.rand(100))
ts = nap.Ts(t=np.arange(100))

print(tsdtensor)
Time (s)
----------  -----------------------------
0.0         [[0.826081 ... 0.69573 ] ...]
1.0         [[0.105919 ... 0.454364] ...]
2.0         [[0.396669 ... 0.355507] ...]
3.0         [[0.761848 ... 0.365142] ...]
4.0         [[0.573859 ... 0.036939] ...]
5.0         [[0.885467 ... 0.301201] ...]
6.0         [[0.604733 ... 0.272205] ...]
...
93.0        [[0.363668 ... 0.223111] ...]
94.0        [[0.011137 ... 0.873867] ...]
95.0        [[0.116848 ... 0.432547] ...]
96.0        [[0.893478 ... 0.792906] ...]
97.0        [[0.735803 ... 0.887517] ...]
98.0        [[0.825824 ... 0.340064] ...]
99.0        [[0.208684 ... 0.239948] ...]
dtype: float64, shape: (100, 5, 5)

Tsd and Ts can be converted to a pandas.Series.

print(tsd.as_series())
0.0     0.064481
1.0     0.873385
2.0     0.728284
3.0     0.723826
4.0     0.680962
          ...   
95.0    0.478814
96.0    0.765455
97.0    0.861358
98.0    0.292530
99.0    0.843853
Length: 100, dtype: float64

TsdFrame to a pandas.DataFrame.

print(tsdframe.as_dataframe())
             a         b         c
0.0   0.784937  0.272200  0.465973
1.0   0.565852  0.348622  0.372769
2.0   0.713854  0.765989  0.495458
3.0   0.566916  0.622688  0.995151
4.0   0.482532  0.511517  0.024826
...        ...       ...       ...
95.0  0.266237  0.406266  0.994555
96.0  0.351837  0.185688  0.838639
97.0  0.139865  0.525252  0.727895
98.0  0.488593  0.099111  0.049178
99.0  0.422932  0.451065  0.132900

[100 rows x 3 columns]

Attributes#

The numpy array is accesible with the attributes .values, .d and functions as_array(), to_numpy(). The time index array is a TsIndex object accessible with .index or .t. .shape and .ndim are also accessible.

print(tsdtensor.ndim)
print(tsdframe.shape)
print(len(tsd))
3
(100, 3)
100

Slicing#

Slicing is very similar to numpy array. The first dimension is always time and time support is always passed on if a pynapple object is returned.

First 10 elements. Return a TsdTensor

print(tsdtensor[0:10])
Time (s)
----------  -----------------------------
0           [[0.826081 ... 0.69573 ] ...]
1           [[0.105919 ... 0.454364] ...]
2           [[0.396669 ... 0.355507] ...]
3           [[0.761848 ... 0.365142] ...]
4           [[0.573859 ... 0.036939] ...]
5           [[0.885467 ... 0.301201] ...]
6           [[0.604733 ... 0.272205] ...]
7           [[0.770671 ... 0.698015] ...]
8           [[0.481845 ... 0.674826] ...]
9           [[0.107305 ... 0.606899] ...]
dtype: float64, shape: (10, 5, 5)

First column. Return a Tsd

print(tsdframe[:,0])
Time (s)
----------  --------
0.0         0.784937
1.0         0.565852
2.0         0.713854
3.0         0.566916
4.0         0.482532
5.0         0.53956
6.0         0.014702
...
93.0        0.478886
94.0        0.682502
95.0        0.266237
96.0        0.351837
97.0        0.139865
98.0        0.488593
99.0        0.422932
dtype: float64, shape: (100,)

First element. Return a numpy ndarray

print(tsdtensor[0])
[[0.8260812  0.51138552 0.07928032 0.95761493 0.69572981]
 [0.50344038 0.48714465 0.51864907 0.3959096  0.71989933]
 [0.46406014 0.07498307 0.06282477 0.74986918 0.18162249]
 [0.77076045 0.60133573 0.10525352 0.83225942 0.90841372]
 [0.95284192 0.36533762 0.1682528  0.10240889 0.24144857]]

The time support is never changing when slicing time down.

print(tsd.time_support)
print(tsd[0:20].time_support)
  index    start    end
      0        0     99
shape: (1, 2), time unit: sec.
  index    start    end
      0        0     99
shape: (1, 2), time unit: sec.

TsdFrame offers special slicing similar to pandas.DataFrame.

Only TsdFrame can have columns labelling and indexing.

print(tsdframe.loc['a'])
print(tsdframe.loc[['a', 'c']])
Time (s)
----------  --------
0.0         0.784937
1.0         0.565852
2.0         0.713854
3.0         0.566916
4.0         0.482532
5.0         0.53956
6.0         0.014702
...
93.0        0.478886
94.0        0.682502
95.0        0.266237
96.0        0.351837
97.0        0.139865
98.0        0.488593
99.0        0.422932
dtype: float64, shape: (100,)
Time (s)           a          c
----------  --------  ---------
0.0         0.784937  0.465973
1.0         0.565852  0.372769
2.0         0.713854  0.495458
3.0         0.566916  0.995151
4.0         0.482532  0.0248259
5.0         0.53956   0.219941
6.0         0.014702  0.304392
...
93.0        0.478886  0.39315
94.0        0.682502  0.985186
95.0        0.266237  0.994555
96.0        0.351837  0.838639
97.0        0.139865  0.727895
98.0        0.488593  0.0491781
99.0        0.422932  0.1329
dtype: float64, shape: (100, 2)

Arithmetic#

Arithmetical operations works similar to numpy

tsd = nap.Tsd(t=np.arange(5), d=np.ones(5))
print(tsd + 1)
Time (s)
----------  --
0            2
1            2
2            2
3            2
4            2
dtype: float64, shape: (5,)

It is possible to do array operations on the time series provided that the dimensions matches. The output will still be a time series object.

print(tsd - np.ones(5))
Time (s)
----------  --
0            0
1            0
2            0
3            0
4            0
dtype: float64, shape: (5,)

Nevertheless operations like this are not permitted :

try:
	tsd + tsd
except Exception as error:
	print(error)
operand type(s) all returned NotImplemented from __array_ufunc__(<ufunc 'add'>, '__call__', Time (s)
----------  --
0            1
1            1
2            1
3            1
4            1
dtype: float64, shape: (5,), Time (s)
----------  --
0            1
1            1
2            1
3            1
4            1
dtype: float64, shape: (5,)): 'Tsd', 'Tsd'

Array operations#

The most common numpy functions will return a time series if the output first dimension matches the shape of the time index.

Here the TsdTensor is averaged along the time axis. The output is a numpy array.

print(np.mean(tsdtensor, 0))
[[0.52754866 0.55706479 0.50010286 0.46161768 0.51324512]
 [0.48654168 0.46591461 0.45981728 0.46004165 0.5023898 ]
 [0.46985397 0.49771183 0.51045885 0.47897319 0.5199683 ]
 [0.50629383 0.45583042 0.52062778 0.48828955 0.49219437]
 [0.42495182 0.483135   0.50355118 0.4592238  0.51245397]]

Here averaging across the second dimension returns a TsdFrame.

print(np.mean(tsdtensor, 1))
Time (s)           0         1         2         3         4
----------  --------  --------  --------  --------  --------
0.0         0.703437  0.408037  0.186852  0.607612  0.549423
1.0         0.441122  0.485956  0.426342  0.536343  0.471654
2.0         0.626144  0.557025  0.593753  0.468361  0.381245
3.0         0.578502  0.792432  0.657809  0.458799  0.514091
4.0         0.371146  0.445376  0.44434   0.240249  0.371467
5.0         0.445753  0.209757  0.384497  0.58244   0.443491
6.0         0.471155  0.614124  0.666983  0.438567  0.667188
...
93.0        0.424599  0.430675  0.239213  0.251426  0.678532
94.0        0.394938  0.568533  0.440301  0.345355  0.459065
95.0        0.121888  0.255144  0.530623  0.488413  0.500386
96.0        0.707203  0.498573  0.470572  0.513813  0.658417
97.0        0.579147  0.466921  0.571409  0.589302  0.672442
98.0        0.475746  0.299869  0.408295  0.51882   0.548037
99.0        0.469509  0.710794  0.423009  0.630124  0.49482
dtype: float64, shape: (100, 5)

This is not true for FFT functions though.

try:
	np.fft.fft(tsd)
except Exception as error:
	print(error)
no implementation found for 'numpy.fft.fft' on types that implement __array_function__: [<class 'pynapple.core.time_series.Tsd'>]

Concatenating#

It is possible to concatenate time series providing than they don’t overlap meaning time indexe should be already sorted through all time series to concatenate

tsd1 = nap.Tsd(t=np.arange(5), d=np.ones(5))
tsd2 = nap.Tsd(t=np.arange(5)+10, d=np.ones(5)*2)
tsd3 = nap.Tsd(t=np.arange(5)+20, d=np.ones(5)*3)

print(np.concatenate((tsd1, tsd2, tsd3)))
Time (s)
----------  --
0.0          1
1.0          1
2.0          1
3.0          1
4.0          1
10.0         2
11.0         2
...
13.0         2
14.0         2
20.0         3
21.0         3
22.0         3
23.0         3
24.0         3
dtype: float64, shape: (15,)

It’s also possible to concatenate vertically if time indexes matches up to pynapple float precision

tsdframe = nap.TsdFrame(t=np.arange(5), d=np.random.randn(5, 3))

print(np.concatenate((tsdframe, tsdframe), 1))
Time (s)            0          1          2          0          1  ...
----------  ---------  ---------  ---------  ---------  ---------  -----
0            0.456088   1.33932   -1.08675    0.456088   1.33932   ...
1            0.880725  -0.619875  -0.563258   0.880725  -0.619875  ...
2            0.275141  -0.661413   0.81539    0.275141  -0.661413  ...
3            0.148617   0.880903   0.088155   0.148617   0.880903  ...
4           -0.345122   0.295537   0.301177  -0.345122   0.295537  ...
dtype: float64, shape: (5, 6)

Spliting#

Array split functions are also implemented

print(np.array_split(tsdtensor[0:10], 2))
[Time (s)
----------  -----------------------------
0           [[0.826081 ... 0.69573 ] ...]
1           [[0.105919 ... 0.454364] ...]
2           [[0.396669 ... 0.355507] ...]
3           [[0.761848 ... 0.365142] ...]
4           [[0.573859 ... 0.036939] ...]
dtype: float64, shape: (5, 5, 5), Time (s)
----------  -----------------------------
5           [[0.885467 ... 0.301201] ...]
6           [[0.604733 ... 0.272205] ...]
7           [[0.770671 ... 0.698015] ...]
8           [[0.481845 ... 0.674826] ...]
9           [[0.107305 ... 0.606899] ...]
dtype: float64, shape: (5, 5, 5)]

Modifying#

It is possible to modify a time series element wise

print(tsd1)

tsd1[0] = np.pi

print(tsd1)
Time (s)
----------  --
0            1
1            1
2            1
3            1
4            1
dtype: float64, shape: (5,)
Time (s)
----------  -------
0           3.14159
1           1
2           1
3           1
4           1
dtype: float64, shape: (5,)

It is also possible to modify a time series with logical operations

tsd[tsd.values>0.5] = 0.0

print(tsd)
Time (s)
----------  --
0            0
1            0
2            0
3            0
4            0
dtype: float64, shape: (5,)

Sorting#

It is not possible to sort along the first dimension as it would break the sorting of the time index

tsd = nap.Tsd(t=np.arange(100), d=np.random.rand(100))

try:
	np.sort(tsd)
except Exception as error:
	print(error)
no implementation found for 'numpy.sort' on types that implement __array_function__: [<class 'pynapple.core.time_series.Tsd'>]