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.135672 ... 0.318081] ...]
1.0         [[0.112587 ... 0.771509] ...]
2.0         [[0.663385 ... 0.151462] ...]
3.0         [[0.165028 ... 0.987412] ...]
4.0         [[0.945754 ... 0.498591] ...]
5.0         [[0.018744 ... 0.905327] ...]
6.0         [[0.667127 ... 0.347725] ...]
...
93.0        [[0.783547 ... 0.326195] ...]
94.0        [[0.573633 ... 0.37208 ] ...]
95.0        [[0.591055 ... 0.897778] ...]
96.0        [[0.948339 ... 0.251466] ...]
97.0        [[0.38238 ... 0.62254] ...]
98.0        [[0.187683 ... 0.273602] ...]
99.0        [[0.809483 ... 0.722638] ...]
dtype: float64, shape: (100, 5, 5)

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

print(tsd.as_series())
0.0     0.443263
1.0     0.915594
2.0     0.559576
3.0     0.936161
4.0     0.852189
          ...   
95.0    0.185061
96.0    0.131702
97.0    0.967871
98.0    0.432674
99.0    0.640323
Length: 100, dtype: float64

TsdFrame to a pandas.DataFrame.

print(tsdframe.as_dataframe())
             a         b         c
0.0   0.502644  0.984508  0.536133
1.0   0.018498  0.662098  0.022568
2.0   0.676550  0.315668  0.779384
3.0   0.604352  0.880397  0.790823
4.0   0.744657  0.238869  0.544313
...        ...       ...       ...
95.0  0.040300  0.009400  0.914616
96.0  0.719177  0.760830  0.315832
97.0  0.169190  0.939619  0.753422
98.0  0.536203  0.603321  0.519928
99.0  0.193433  0.035690  0.444365

[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.135672 ... 0.318081] ...]
1           [[0.112587 ... 0.771509] ...]
2           [[0.663385 ... 0.151462] ...]
3           [[0.165028 ... 0.987412] ...]
4           [[0.945754 ... 0.498591] ...]
5           [[0.018744 ... 0.905327] ...]
6           [[0.667127 ... 0.347725] ...]
7           [[0.335488 ... 0.800883] ...]
8           [[0.945731 ... 0.156305] ...]
9           [[0.712048 ... 0.823691] ...]
dtype: float64, shape: (10, 5, 5)

First column. Return a Tsd

print(tsdframe[:,0])
Time (s)
----------  ---------
0.0         0.502644
1.0         0.0184976
2.0         0.67655
3.0         0.604352
4.0         0.744657
5.0         0.0489298
6.0         0.946419
...
93.0        0.240835
94.0        0.411795
95.0        0.0402999
96.0        0.719177
97.0        0.16919
98.0        0.536203
99.0        0.193433
dtype: float64, shape: (100,)

First element. Return a numpy ndarray

print(tsdtensor[0])
[[0.13567171 0.92805366 0.32240345 0.47066131 0.31808066]
 [0.15285272 0.01115451 0.69700402 0.14311207 0.06386968]
 [0.9589296  0.54561013 0.35295964 0.54775811 0.84044841]
 [0.32135583 0.79766636 0.14352354 0.0637754  0.42104881]
 [0.31648923 0.91262862 0.71728579 0.45467257 0.53234806]]

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.502644
1.0         0.0184976
2.0         0.67655
3.0         0.604352
4.0         0.744657
5.0         0.0489298
6.0         0.946419
...
93.0        0.240835
94.0        0.411795
95.0        0.0402999
96.0        0.719177
97.0        0.16919
98.0        0.536203
99.0        0.193433
dtype: float64, shape: (100,)
Time (s)    a        c
----------  -------  -------
0.0         0.50264  0.53613
1.0         0.0185   0.02257
2.0         0.67655  0.77938
3.0         0.60435  0.79082
4.0         0.74466  0.54431
5.0         0.04893  0.66224
6.0         0.94642  0.08396
...         ...      ...
93.0        0.24083  0.33564
94.0        0.4118   0.02378
95.0        0.0403   0.91462
96.0        0.71918  0.31583
97.0        0.16919  0.75342
98.0        0.5362   0.51993
99.0        0.19343  0.44436
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.50327024 0.52192687 0.5003526  0.54635673 0.48464622]
 [0.48281641 0.5123021  0.50348182 0.48929226 0.49584379]
 [0.45233979 0.46186221 0.53137673 0.4187626  0.5138332 ]
 [0.43845436 0.48312688 0.48752111 0.4580286  0.49904036]
 [0.50592838 0.47853257 0.50589697 0.53918757 0.50321629]]

Here averaging across the second dimension returns a TsdFrame.

print(np.mean(tsdtensor, 1))
Time (s)    0        1        2        3        4
----------  -------  -------  -------  -------  -------
0.0         0.37706  0.63902  0.44664  0.336    0.43516
1.0         0.40475  0.51272  0.56428  0.77618  0.30166
2.0         0.59579  0.51698  0.37402  0.59932  0.44602
3.0         0.57567  0.62622  0.24276  0.54516  0.80899
4.0         0.40822  0.72875  0.58912  0.43528  0.51378
5.0         0.53575  0.65836  0.53378  0.74049  0.71071
6.0         0.56037  0.4632   0.37149  0.30696  0.49292
...         ...      ...      ...      ...      ...
93.0        0.75979  0.37807  0.52483  0.37963  0.32042
94.0        0.6056   0.26695  0.40026  0.62061  0.4462
95.0        0.4741   0.54471  0.59702  0.40932  0.4747
96.0        0.5735   0.67363  0.36496  0.47432  0.54993
97.0        0.59961  0.55297  0.6465   0.5776   0.47675
98.0        0.52962  0.55381  0.5664   0.55331  0.39345
99.0        0.65355  0.47845  0.5827   0.41292  0.57843
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            0.27316   1.36206   0.10877   0.27316   1.36206  ...
1           -0.62702   0.40721   0.14628  -0.62702   0.40721  ...
2            0.68602   0.30801  -0.66651   0.68602   0.30801  ...
3            1.01268  -1.23154  -1.31669   1.01268  -1.23154  ...
4           -2.0977    2.55836   0.65491  -2.0977    2.55836  ...
dtype: float64, shape: (5, 6)

Spliting#

Array split functions are also implemented

print(np.array_split(tsdtensor[0:10], 2))
[Time (s)
----------  -----------------------------
0           [[0.135672 ... 0.318081] ...]
1           [[0.112587 ... 0.771509] ...]
2           [[0.663385 ... 0.151462] ...]
3           [[0.165028 ... 0.987412] ...]
4           [[0.945754 ... 0.498591] ...]
dtype: float64, shape: (5, 5, 5), Time (s)
----------  -----------------------------
5           [[0.018744 ... 0.905327] ...]
6           [[0.667127 ... 0.347725] ...]
7           [[0.335488 ... 0.800883] ...]
8           [[0.945731 ... 0.156305] ...]
9           [[0.712048 ... 0.823691] ...]
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'>]