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.865315 ... 0.885427] ...]
1.0         [[0.502683 ... 0.047289] ...]
2.0         [[0.985251 ... 0.579143] ...]
3.0         [[0.269853 ... 0.020564] ...]
4.0         [[0.574916 ... 0.692886] ...]
5.0         [[0.034614 ... 0.835352] ...]
6.0         [[0.421093 ... 0.840131] ...]
...
93.0        [[0.959672 ... 0.08211 ] ...]
94.0        [[0.112176 ... 0.369518] ...]
95.0        [[0.986694 ... 0.274608] ...]
96.0        [[0.063516 ... 0.239918] ...]
97.0        [[0.710077 ... 0.742115] ...]
98.0        [[0.383314 ... 0.394812] ...]
99.0        [[0.046751 ... 0.988182] ...]
dtype: float64, shape: (100, 5, 5)

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

print(tsd.as_series())
0.0     0.962121
1.0     0.176320
2.0     0.171856
3.0     0.482809
4.0     0.276715
          ...   
95.0    0.031823
96.0    0.991307
97.0    0.329573
98.0    0.804941
99.0    0.063024
Length: 100, dtype: float64

TsdFrame to a pandas.DataFrame.

print(tsdframe.as_dataframe())
             a         b         c
0.0   0.010192  0.645313  0.273866
1.0   0.579827  0.449624  0.029319
2.0   0.413265  0.426770  0.500965
3.0   0.923472  0.068590  0.434644
4.0   0.987043  0.219949  0.475483
...        ...       ...       ...
95.0  0.816280  0.758135  0.577824
96.0  0.525335  0.940479  0.344898
97.0  0.198864  0.162525  0.818122
98.0  0.986754  0.797658  0.956435
99.0  0.492367  0.280486  0.867149

[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.865315 ... 0.885427] ...]
1           [[0.502683 ... 0.047289] ...]
2           [[0.985251 ... 0.579143] ...]
3           [[0.269853 ... 0.020564] ...]
4           [[0.574916 ... 0.692886] ...]
5           [[0.034614 ... 0.835352] ...]
6           [[0.421093 ... 0.840131] ...]
7           [[0.506914 ... 0.15189 ] ...]
8           [[0.300724 ... 0.368884] ...]
9           [[0.148727 ... 0.183799] ...]
dtype: float64, shape: (10, 5, 5)

First column. Return a Tsd

print(tsdframe[:,0])
Time (s)
----------  ---------
0.0         0.0101925
1.0         0.579827
2.0         0.413265
3.0         0.923472
4.0         0.987043
5.0         0.610378
6.0         0.824378
...
93.0        0.638483
94.0        0.668723
95.0        0.81628
96.0        0.525335
97.0        0.198864
98.0        0.986754
99.0        0.492367
dtype: float64, shape: (100,)

First element. Return a numpy ndarray

print(tsdtensor[0])
[[0.86531526 0.46473617 0.71005987 0.48231798 0.88542672]
 [0.89041978 0.39759494 0.26546629 0.78616237 0.28749397]
 [0.2367623  0.36641841 0.3160939  0.64064268 0.72018418]
 [0.87454635 0.36470434 0.25101526 0.34179335 0.33111326]
 [0.76123749 0.18678075 0.47688906 0.56249264 0.54403063]]

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.0101925
1.0         0.579827
2.0         0.413265
3.0         0.923472
4.0         0.987043
5.0         0.610378
6.0         0.824378
...
93.0        0.638483
94.0        0.668723
95.0        0.81628
96.0        0.525335
97.0        0.198864
98.0        0.986754
99.0        0.492367
dtype: float64, shape: (100,)
Time (s)    a        c
----------  -------  -------
0.0         0.01019  0.27387
1.0         0.57983  0.02932
2.0         0.41327  0.50097
3.0         0.92347  0.43464
4.0         0.98704  0.47548
5.0         0.61038  0.14691
6.0         0.82438  0.7211
...         ...      ...
93.0        0.63848  0.04236
94.0        0.66872  0.69125
95.0        0.81628  0.57782
96.0        0.52534  0.3449
97.0        0.19886  0.81812
98.0        0.98675  0.95643
99.0        0.49237  0.86715
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.48778861 0.49902743 0.43840317 0.53795293 0.49054817]
 [0.45738823 0.48915845 0.48566518 0.49534076 0.50200346]
 [0.49674916 0.50785187 0.53191238 0.51466288 0.47875229]
 [0.48401488 0.51721702 0.49416187 0.46865212 0.53247089]
 [0.44369284 0.43944345 0.47981102 0.46817722 0.55748802]]

Here averaging across the second dimension returns a TsdFrame.

print(np.mean(tsdtensor, 1))
Time (s)    0        1        2        3        4
----------  -------  -------  -------  -------  -------
0.0         0.72566  0.35605  0.4039   0.56268  0.55365
1.0         0.67913  0.4776   0.67839  0.50673  0.37023
2.0         0.74895  0.47596  0.55332  0.4821   0.48462
3.0         0.18572  0.30522  0.22269  0.71296  0.46775
4.0         0.60269  0.59468  0.50726  0.49397  0.60029
5.0         0.35445  0.44486  0.52379  0.23911  0.611
6.0         0.26754  0.61253  0.49309  0.52294  0.56914
...         ...      ...      ...      ...      ...
93.0        0.56679  0.53593  0.46028  0.57612  0.25619
94.0        0.36578  0.45176  0.43069  0.73228  0.59086
95.0        0.56693  0.39205  0.65631  0.43483  0.54716
96.0        0.40495  0.34198  0.47287  0.41689  0.50551
97.0        0.61524  0.63895  0.43733  0.57852  0.61493
98.0        0.34848  0.26434  0.62224  0.572    0.3631
99.0        0.38166  0.59707  0.47011  0.47569  0.66853
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.16634  -0.79319  -0.32055  -0.16634  -0.79319  ...
1           -0.5388   -0.78645  -0.14719  -0.5388   -0.78645  ...
2           -0.34891   0.22815  -0.16539  -0.34891   0.22815  ...
3           -0.76513   2.85594   1.81752  -0.76513   2.85594  ...
4           -0.19759  -1.26216  -0.6322   -0.19759  -1.26216  ...
dtype: float64, shape: (5, 6)

Spliting#

Array split functions are also implemented

print(np.array_split(tsdtensor[0:10], 2))
[Time (s)
----------  -----------------------------
0           [[0.865315 ... 0.885427] ...]
1           [[0.502683 ... 0.047289] ...]
2           [[0.985251 ... 0.579143] ...]
3           [[0.269853 ... 0.020564] ...]
4           [[0.574916 ... 0.692886] ...]
dtype: float64, shape: (5, 5, 5), Time (s)
----------  -----------------------------
5           [[0.034614 ... 0.835352] ...]
6           [[0.421093 ... 0.840131] ...]
7           [[0.506914 ... 0.15189 ] ...]
8           [[0.300724 ... 0.368884] ...]
9           [[0.148727 ... 0.183799] ...]
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'>]