Numpy tutorial#
This tutorial shows how pynapple interact with numpy.
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.10147 ... 0.351432] ...]
1.0 [[0.71328 ... 0.866112] ...]
2.0 [[0.09222 ... 0.198203] ...]
3.0 [[0.577468 ... 0.54496 ] ...]
4.0 [[0.542191 ... 0.61854 ] ...]
5.0 [[0.23052 ... 0.140044] ...]
6.0 [[0.730668 ... 0.234288] ...]
...
93.0 [[0.710661 ... 0.754791] ...]
94.0 [[0.825005 ... 0.927469] ...]
95.0 [[0.068184 ... 0.717843] ...]
96.0 [[0.58731 ... 0.29638] ...]
97.0 [[0.617418 ... 0.860026] ...]
98.0 [[0.133894 ... 0.074162] ...]
99.0 [[0.975336 ... 0.381678] ...]
dtype: float64, shape: (100, 5, 5)
Tsd and Ts can be converted to a pandas.Series.
print(tsd.as_series())
0.0 0.986795
1.0 0.586881
2.0 0.977664
3.0 0.942753
4.0 0.654458
...
95.0 0.971091
96.0 0.107823
97.0 0.390494
98.0 0.647533
99.0 0.325704
Length: 100, dtype: float64
TsdFrame to a pandas.DataFrame.
print(tsdframe.as_dataframe())
a b c
0.0 0.656296 0.842150 0.339901
1.0 0.056682 0.589484 0.383657
2.0 0.548477 0.055075 0.404893
3.0 0.366125 0.922974 0.760525
4.0 0.779885 0.973269 0.799260
... ... ... ...
95.0 0.774242 0.059777 0.398422
96.0 0.824227 0.266269 0.996695
97.0 0.871338 0.093348 0.426366
98.0 0.641514 0.716468 0.814937
99.0 0.465818 0.125435 0.060104
[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.10147 ... 0.351432] ...]
1 [[0.71328 ... 0.866112] ...]
2 [[0.09222 ... 0.198203] ...]
3 [[0.577468 ... 0.54496 ] ...]
4 [[0.542191 ... 0.61854 ] ...]
5 [[0.23052 ... 0.140044] ...]
6 [[0.730668 ... 0.234288] ...]
7 [[0.757589 ... 0.580473] ...]
8 [[0.669051 ... 0.541018] ...]
9 [[0.944677 ... 0.776209] ...]
dtype: float64, shape: (10, 5, 5)
First column. Return a Tsd
print(tsdframe[:,0])
Time (s)
---------- ---------
0.0 0.656296
1.0 0.0566817
2.0 0.548477
3.0 0.366125
4.0 0.779885
5.0 0.614083
6.0 0.726917
...
93.0 0.107212
94.0 0.569952
95.0 0.774242
96.0 0.824227
97.0 0.871338
98.0 0.641514
99.0 0.465818
dtype: float64, shape: (100,)
First element. Return a numpy ndarray
print(tsdtensor[0])
[[0.1014704 0.02501788 0.08180354 0.30554124 0.35143155]
[0.09147851 0.11351497 0.57608798 0.48050935 0.83818487]
[0.34415671 0.23237787 0.12635145 0.64181337 0.80128188]
[0.31645433 0.1482377 0.86240884 0.54360661 0.63447157]
[0.39396922 0.12285448 0.17914732 0.49852656 0.86682747]]
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.656296
1.0 0.0566817
2.0 0.548477
3.0 0.366125
4.0 0.779885
5.0 0.614083
6.0 0.726917
...
93.0 0.107212
94.0 0.569952
95.0 0.774242
96.0 0.824227
97.0 0.871338
98.0 0.641514
99.0 0.465818
dtype: float64, shape: (100,)
Time (s) a c
---------- --------- ---------
0.0 0.656296 0.339901
1.0 0.0566817 0.383657
2.0 0.548477 0.404893
3.0 0.366125 0.760525
4.0 0.779885 0.79926
5.0 0.614083 0.0124286
6.0 0.726917 0.947067
...
93.0 0.107212 0.473956
94.0 0.569952 0.983481
95.0 0.774242 0.398422
96.0 0.824227 0.996695
97.0 0.871338 0.426366
98.0 0.641514 0.814937
99.0 0.465818 0.0601041
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.50461292 0.49160757 0.44441726 0.53414305 0.531463 ]
[0.48896618 0.50528351 0.47678746 0.51424141 0.49167822]
[0.51677283 0.53125792 0.51488947 0.51207277 0.51307667]
[0.5308515 0.50443617 0.50086259 0.47533561 0.53531277]
[0.51710726 0.48617311 0.47039073 0.4843791 0.51134247]]
Here averaging across the second dimension returns a TsdFrame.
print(np.mean(tsdtensor, 1))
Time (s) 0 1 2 3 4
---------- -------- -------- -------- -------- --------
0.0 0.249506 0.128401 0.36516 0.493999 0.698439
1.0 0.292488 0.323618 0.359753 0.502399 0.504539
2.0 0.590748 0.490263 0.30334 0.395045 0.559251
3.0 0.486868 0.388154 0.389775 0.409261 0.472102
4.0 0.452117 0.313017 0.364207 0.414162 0.376803
5.0 0.470897 0.414719 0.458141 0.542288 0.421601
6.0 0.596822 0.542488 0.436315 0.468789 0.460987
...
93.0 0.632547 0.617943 0.448664 0.664903 0.636979
94.0 0.553513 0.63294 0.537793 0.502047 0.578782
95.0 0.429531 0.627856 0.480308 0.531321 0.640594
96.0 0.395609 0.250323 0.534421 0.623423 0.367298
97.0 0.618171 0.573396 0.35846 0.429355 0.572974
98.0 0.387207 0.454136 0.715395 0.524881 0.279109
99.0 0.610816 0.516841 0.509889 0.45703 0.393693
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 -1.71381 1.22047 -0.155778 -1.71381 1.22047 ...
1 -1.42937 1.15973 0.474329 -1.42937 1.15973 ...
2 -0.695747 -0.803579 -1.18788 -0.695747 -0.803579 ...
3 0.358923 -0.876245 -0.860615 0.358923 -0.876245 ...
4 0.514457 -0.0594709 0.188502 0.514457 -0.0594709 ...
dtype: float64, shape: (5, 6)
Spliting#
Array split functions are also implemented
print(np.array_split(tsdtensor[0:10], 2))
[Time (s)
---------- -----------------------------
0 [[0.10147 ... 0.351432] ...]
1 [[0.71328 ... 0.866112] ...]
2 [[0.09222 ... 0.198203] ...]
3 [[0.577468 ... 0.54496 ] ...]
4 [[0.542191 ... 0.61854 ] ...]
dtype: float64, shape: (5, 5, 5), Time (s)
---------- -----------------------------
5 [[0.23052 ... 0.140044] ...]
6 [[0.730668 ... 0.234288] ...]
7 [[0.757589 ... 0.580473] ...]
8 [[0.669051 ... 0.541018] ...]
9 [[0.944677 ... 0.776209] ...]
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'>]