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.347298 ... 0.312675] ...]
1.0         [[0.726588 ... 0.09655 ] ...]
2.0         [[0.316917 ... 0.886029] ...]
3.0         [[0.659889 ... 0.453797] ...]
4.0         [[0.749661 ... 0.483082] ...]
5.0         [[0.103244 ... 0.558264] ...]
6.0         [[0.169825 ... 0.986949] ...]
...
93.0        [[0.950095 ... 0.644052] ...]
94.0        [[0.334862 ... 0.464067] ...]
95.0        [[0.914546 ... 0.910441] ...]
96.0        [[0.241861 ... 0.392164] ...]
97.0        [[0.214468 ... 0.505412] ...]
98.0        [[0.647248 ... 0.150906] ...]
99.0        [[0.201655 ... 0.648625] ...]
dtype: float64, shape: (100, 5, 5)

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

print(tsd.as_series())
0.0     0.828021
1.0     0.122005
2.0     0.491144
3.0     0.027348
4.0     0.169999
          ...   
95.0    0.132932
96.0    0.035508
97.0    0.986559
98.0    0.737438
99.0    0.583212
Length: 100, dtype: float64

TsdFrame to a pandas.DataFrame.

print(tsdframe.as_dataframe())
             a         b         c
0.0   0.051505  0.020901  0.980998
1.0   0.276424  0.970004  0.190043
2.0   0.490233  0.579364  0.224738
3.0   0.116960  0.440286  0.292008
4.0   0.092185  0.375822  0.911623
...        ...       ...       ...
95.0  0.779148  0.790131  0.844257
96.0  0.463862  0.269486  0.139613
97.0  0.317582  0.527982  0.807308
98.0  0.126130  0.363195  0.478708
99.0  0.041022  0.754480  0.176352

[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.347298 ... 0.312675] ...]
1           [[0.726588 ... 0.09655 ] ...]
2           [[0.316917 ... 0.886029] ...]
3           [[0.659889 ... 0.453797] ...]
4           [[0.749661 ... 0.483082] ...]
5           [[0.103244 ... 0.558264] ...]
6           [[0.169825 ... 0.986949] ...]
7           [[0.545584 ... 0.552871] ...]
8           [[0.243406 ... 0.996539] ...]
9           [[0.006885 ... 0.441234] ...]
dtype: float64, shape: (10, 5, 5)

First column. Return a Tsd

print(tsdframe[:,0])
Time (s)
----------  ---------
0.0         0.051505
1.0         0.276424
2.0         0.490233
3.0         0.11696
4.0         0.0921853
5.0         0.513659
6.0         0.907356
...
93.0        0.416159
94.0        0.968738
95.0        0.779148
96.0        0.463862
97.0        0.317582
98.0        0.12613
99.0        0.0410219
dtype: float64, shape: (100,)

First element. Return a numpy ndarray

print(tsdtensor[0])
[[0.34729811 0.24959377 0.70915297 0.53529747 0.31267481]
 [0.98558283 0.9735056  0.30139918 0.98662254 0.75423415]
 [0.18963471 0.24630938 0.10592641 0.43656669 0.31115624]
 [0.84162677 0.99204251 0.75773112 0.15683189 0.46960369]
 [0.77991682 0.51621841 0.70214206 0.47917812 0.89746178]]

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.051505
1.0         0.276424
2.0         0.490233
3.0         0.11696
4.0         0.0921853
5.0         0.513659
6.0         0.907356
...
93.0        0.416159
94.0        0.968738
95.0        0.779148
96.0        0.463862
97.0        0.317582
98.0        0.12613
99.0        0.0410219
dtype: float64, shape: (100,)
Time (s)            a          c
----------  ---------  ---------
0.0         0.051505   0.980998
1.0         0.276424   0.190043
2.0         0.490233   0.224738
3.0         0.11696    0.292008
4.0         0.0921853  0.911623
5.0         0.513659   0.0360951
6.0         0.907356   0.228232
...
93.0        0.416159   0.939112
94.0        0.968738   0.416719
95.0        0.779148   0.844257
96.0        0.463862   0.139613
97.0        0.317582   0.807308
98.0        0.12613    0.478708
99.0        0.0410219  0.176352
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.5004818  0.46931471 0.55636883 0.50296675 0.4895629 ]
 [0.57150321 0.51298529 0.53085253 0.49679499 0.51143415]
 [0.49901573 0.47240031 0.48436675 0.52416439 0.49554747]
 [0.45487373 0.49521917 0.57785814 0.46068238 0.52834642]
 [0.49179245 0.49991931 0.531605   0.47460642 0.53125933]]

Here averaging across the second dimension returns a TsdFrame.

print(np.mean(tsdtensor, 1))
Time (s)           0         1         2         3         4
----------  --------  --------  --------  --------  --------
0.0         0.628812  0.595534  0.51527   0.518899  0.549026
1.0         0.557174  0.466408  0.37262   0.455115  0.311361
2.0         0.296396  0.277973  0.50214   0.847393  0.352423
3.0         0.66032   0.500287  0.553058  0.312807  0.659834
4.0         0.557164  0.611641  0.547407  0.534694  0.569452
5.0         0.341193  0.452043  0.536632  0.71242   0.709087
6.0         0.374142  0.686656  0.636442  0.517546  0.590169
...
93.0        0.4967    0.652104  0.516409  0.68309   0.616414
94.0        0.548624  0.582247  0.471853  0.523211  0.710263
95.0        0.622695  0.422931  0.653703  0.607676  0.742235
96.0        0.407309  0.41702   0.413681  0.505474  0.613964
97.0        0.418569  0.596488  0.483567  0.59155   0.484316
98.0        0.576093  0.593694  0.632679  0.611374  0.289884
99.0        0.593382  0.361537  0.543778  0.318632  0.548477
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.0768139  -0.286404  -0.981265  -0.0768139  -0.286404  ...
1            0.90654    -0.804296  -0.613535   0.90654    -0.804296  ...
2           -0.764887   -1.86403   -0.430182  -0.764887   -1.86403   ...
3           -0.543025    1.88782    0.393478  -0.543025    1.88782   ...
4            0.764567    0.842751   0.606319   0.764567    0.842751  ...
dtype: float64, shape: (5, 6)

Spliting#

Array split functions are also implemented

print(np.array_split(tsdtensor[0:10], 2))
[Time (s)
----------  -----------------------------
0           [[0.347298 ... 0.312675] ...]
1           [[0.726588 ... 0.09655 ] ...]
2           [[0.316917 ... 0.886029] ...]
3           [[0.659889 ... 0.453797] ...]
4           [[0.749661 ... 0.483082] ...]
dtype: float64, shape: (5, 5, 5), Time (s)
----------  -----------------------------
5           [[0.103244 ... 0.558264] ...]
6           [[0.169825 ... 0.986949] ...]
7           [[0.545584 ... 0.552871] ...]
8           [[0.243406 ... 0.996539] ...]
9           [[0.006885 ... 0.441234] ...]
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'>]