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.561154 ... 0.895041] ...]
1.0         [[0.884298 ... 0.66718 ] ...]
2.0         [[0.334973 ... 0.911362] ...]
3.0         [[0.32953  ... 0.432645] ...]
4.0         [[0.905593 ... 0.902499] ...]
5.0         [[0.564456 ... 0.934602] ...]
6.0         [[0.754851 ... 0.175522] ...]
...
93.0        [[0.862707 ... 0.178347] ...]
94.0        [[0.523921 ... 0.577821] ...]
95.0        [[0.907974 ... 0.041482] ...]
96.0        [[0.684531 ... 0.268876] ...]
97.0        [[0.924976 ... 0.377012] ...]
98.0        [[0.016758 ... 0.73863 ] ...]
99.0        [[0.969527 ... 0.228625] ...]
dtype: float64, shape: (100, 5, 5)

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

print(tsd.as_series())
0.0     0.612596
1.0     0.437217
2.0     0.944202
3.0     0.008920
4.0     0.351977
          ...   
95.0    0.956224
96.0    0.517004
97.0    0.873391
98.0    0.213389
99.0    0.969339
Length: 100, dtype: float64

TsdFrame to a pandas.DataFrame.

print(tsdframe.as_dataframe())
             a         b         c
0.0   0.667479  0.663499  0.091797
1.0   0.201342  0.367431  0.512777
2.0   0.679658  0.647030  0.280831
3.0   0.662804  0.713921  0.646882
4.0   0.947452  0.180630  0.598913
...        ...       ...       ...
95.0  0.122278  0.596896  0.576976
96.0  0.549110  0.563996  0.179331
97.0  0.033316  0.670082  0.291832
98.0  0.451147  0.934460  0.496162
99.0  0.879373  0.298011  0.090483

[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.561154 ... 0.895041] ...]
1           [[0.884298 ... 0.66718 ] ...]
2           [[0.334973 ... 0.911362] ...]
3           [[0.32953  ... 0.432645] ...]
4           [[0.905593 ... 0.902499] ...]
5           [[0.564456 ... 0.934602] ...]
6           [[0.754851 ... 0.175522] ...]
7           [[0.606965 ... 0.6836  ] ...]
8           [[0.941496 ... 0.773701] ...]
9           [[0.039689 ... 0.75706 ] ...]
dtype: float64, shape: (10, 5, 5)

First column. Return a Tsd

print(tsdframe[:,0])
Time (s)
----------  ---------
0.0         0.667479
1.0         0.201342
2.0         0.679658
3.0         0.662804
4.0         0.947452
5.0         0.924051
6.0         0.057548
...
93.0        0.133795
94.0        0.837081
95.0        0.122278
96.0        0.54911
97.0        0.0333159
98.0        0.451147
99.0        0.879373
dtype: float64, shape: (100,)

First element. Return a numpy ndarray

print(tsdtensor[0])
[[0.56115407 0.11527369 0.11864276 0.87989742 0.89504126]
 [0.6265364  0.93123282 0.71557534 0.28474318 0.97698095]
 [0.20564516 0.44540874 0.359418   0.31877399 0.94995318]
 [0.85468629 0.25672726 0.6325242  0.45570799 0.30904881]
 [0.41266347 0.08552807 0.47948732 0.6858727  0.25198175]]

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.667479
1.0         0.201342
2.0         0.679658
3.0         0.662804
4.0         0.947452
5.0         0.924051
6.0         0.057548
...
93.0        0.133795
94.0        0.837081
95.0        0.122278
96.0        0.54911
97.0        0.0333159
98.0        0.451147
99.0        0.879373
dtype: float64, shape: (100,)
Time (s)    a        c
----------  -------  -------
0.0         0.66748  0.0918
1.0         0.20134  0.51278
2.0         0.67966  0.28083
3.0         0.6628   0.64688
4.0         0.94745  0.59891
5.0         0.92405  0.03744
6.0         0.05755  0.82376
...         ...      ...
93.0        0.1338   0.90373
94.0        0.83708  0.32367
95.0        0.12228  0.57698
96.0        0.54911  0.17933
97.0        0.03332  0.29183
98.0        0.45115  0.49616
99.0        0.87937  0.09048
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.487716   0.56767253 0.46216802 0.43381307 0.52798965]
 [0.46606408 0.53188655 0.48596139 0.50086788 0.49355964]
 [0.51847996 0.52070502 0.49121291 0.49881271 0.51712942]
 [0.49590283 0.47823845 0.51448852 0.45531357 0.45868846]
 [0.48716393 0.48165895 0.54285924 0.53170031 0.43176585]]

Here averaging across the second dimension returns a TsdFrame.

print(np.mean(tsdtensor, 1))
Time (s)    0        1        2        3        4
----------  -------  -------  -------  -------  -------
0.0         0.53214  0.36683  0.46113  0.525    0.6766
1.0         0.47511  0.49505  0.27797  0.55905  0.58741
2.0         0.2815   0.39594  0.47925  0.56088  0.45158
3.0         0.3733   0.31826  0.37389  0.37153  0.39225
4.0         0.56553  0.47883  0.71378  0.46844  0.79439
5.0         0.26651  0.36146  0.65118  0.53855  0.67098
6.0         0.74411  0.6026   0.55707  0.4732   0.46393
...         ...      ...      ...      ...      ...
93.0        0.51093  0.67221  0.62574  0.34241  0.59652
94.0        0.40106  0.72024  0.46456  0.23958  0.36248
95.0        0.5281   0.41325  0.72618  0.7249   0.47859
96.0        0.37945  0.47278  0.55108  0.17068  0.35449
97.0        0.58928  0.66017  0.62599  0.41221  0.325
98.0        0.39506  0.43554  0.37788  0.44452  0.71511
99.0        0.5624   0.66788  0.51594  0.40834  0.39895
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         3         4  ...
----------  --------  --------  --------  --------  --------  -----
0            1.59044  -1.38487  -1.19261   1.59044  -1.38487  ...
1           -0.80443  -0.00069  -0.2002   -0.80443  -0.00069  ...
2           -0.30212   0.70747  -1.45473  -0.30212   0.70747  ...
3            0.96237   1.77466   0.19455   0.96237   1.77466  ...
4           -0.90007  -0.32105  -0.4195   -0.90007  -0.32105  ...
dtype: float64, shape: (5, 6)

Spliting#

Array split functions are also implemented

print(np.array_split(tsdtensor[0:10], 2))
[Time (s)
----------  -----------------------------
0           [[0.561154 ... 0.895041] ...]
1           [[0.884298 ... 0.66718 ] ...]
2           [[0.334973 ... 0.911362] ...]
3           [[0.32953  ... 0.432645] ...]
4           [[0.905593 ... 0.902499] ...]
dtype: float64, shape: (5, 5, 5), Time (s)
----------  -----------------------------
5           [[0.564456 ... 0.934602] ...]
6           [[0.754851 ... 0.175522] ...]
7           [[0.606965 ... 0.6836  ] ...]
8           [[0.941496 ... 0.773701] ...]
9           [[0.039689 ... 0.75706 ] ...]
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'>]