Numpy tutorial#
This tutorial shows how pynapple interact with numpy.
Show 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.567582 ... 0.231626] ...]
1.0 [[0.008672 ... 0.458732] ...]
2.0 [[0.93822 ... 0.697647] ...]
3.0 [[0.614734 ... 0.801296] ...]
4.0 [[0.098947 ... 0.626836] ...]
5.0 [[0.154144 ... 0.800397] ...]
6.0 [[0.207274 ... 0.163244] ...]
...
93.0 [[0.191832 ... 0.26665 ] ...]
94.0 [[0.035967 ... 0.953479] ...]
95.0 [[0.527688 ... 0.738416] ...]
96.0 [[0.604269 ... 0.395832] ...]
97.0 [[0.741257 ... 0.926479] ...]
98.0 [[0.532823 ... 0.693157] ...]
99.0 [[0.877942 ... 0.037723] ...]
dtype: float64, shape: (100, 5, 5)
Tsd
and Ts
can be converted to a pandas.Series
.
print(tsd.as_series())
0.0 0.934240
1.0 0.216993
2.0 0.559173
3.0 0.717028
4.0 0.012468
...
95.0 0.070395
96.0 0.338375
97.0 0.862499
98.0 0.300487
99.0 0.635624
Length: 100, dtype: float64
TsdFrame
to a pandas.DataFrame
.
print(tsdframe.as_dataframe())
a b c
0.0 0.623532 0.904448 0.393566
1.0 0.460651 0.722814 0.222978
2.0 0.305574 0.636358 0.181248
3.0 0.107608 0.174524 0.964918
4.0 0.034340 0.209775 0.764003
... ... ... ...
95.0 0.456375 0.151015 0.247659
96.0 0.370254 0.000759 0.712458
97.0 0.710664 0.078416 0.872268
98.0 0.150682 0.569871 0.462309
99.0 0.135188 0.298950 0.056425
[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.567582 ... 0.231626] ...]
1 [[0.008672 ... 0.458732] ...]
2 [[0.93822 ... 0.697647] ...]
3 [[0.614734 ... 0.801296] ...]
4 [[0.098947 ... 0.626836] ...]
5 [[0.154144 ... 0.800397] ...]
6 [[0.207274 ... 0.163244] ...]
7 [[0.406894 ... 0.47527 ] ...]
8 [[0.454983 ... 0.793374] ...]
9 [[0.329952 ... 0.391992] ...]
dtype: float64, shape: (10, 5, 5)
First column. Return a Tsd
print(tsdframe[:,0])
Time (s)
---------- ---------
0.0 0.623532
1.0 0.460651
2.0 0.305574
3.0 0.107608
4.0 0.0343404
5.0 0.38002
6.0 0.720529
...
93.0 0.366401
94.0 0.821738
95.0 0.456375
96.0 0.370254
97.0 0.710664
98.0 0.150682
99.0 0.135188
dtype: float64, shape: (100,)
First element. Return a numpy ndarray
print(tsdtensor[0])
[[0.56758237 0.69202929 0.43082389 0.57956293 0.23162554]
[0.23137044 0.53051502 0.62154879 0.52971529 0.98636563]
[0.88924986 0.0166921 0.0156949 0.57231778 0.54686842]
[0.74087082 0.26798333 0.73020721 0.24284681 0.52099508]
[0.86248237 0.35875828 0.68044119 0.80269641 0.06174738]]
The time support is never changing when slicing time down.
print(tsd.time_support)
print(tsd[0:20].time_support)
start end
0 0 99
shape: (1, 2), time unit: sec.
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.623532
1.0 0.460651
2.0 0.305574
3.0 0.107608
4.0 0.0343404
5.0 0.38002
6.0 0.720529
...
93.0 0.366401
94.0 0.821738
95.0 0.456375
96.0 0.370254
97.0 0.710664
98.0 0.150682
99.0 0.135188
dtype: float64, shape: (100,)
Time (s) a c
---------- ------- -------
0.0 0.62353 0.39357
1.0 0.46065 0.22298
2.0 0.30557 0.18125
3.0 0.10761 0.96492
4.0 0.03434 0.764
5.0 0.38002 0.67741
6.0 0.72053 0.45802
...
93.0 0.3664 0.41666
94.0 0.82174 0.44914
95.0 0.45637 0.24766
96.0 0.37025 0.71246
97.0 0.71066 0.87227
98.0 0.15068 0.46231
99.0 0.13519 0.05643
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.4870256 0.53817969 0.50684871 0.45954803 0.56048794]
[0.45616429 0.47272719 0.50016884 0.49564113 0.50937036]
[0.50757345 0.43450331 0.3933726 0.47799882 0.49180239]
[0.49448203 0.55116732 0.50756579 0.51228409 0.49687732]
[0.53574462 0.51439324 0.47457151 0.56175038 0.50429517]]
Here averaging across the second dimension returns a TsdFrame
.
print(np.mean(tsdtensor, 1))
Time (s) 0 1 2 3 4
---------- ------- ------- ------- ------- -------
0.0 0.65831 0.3732 0.49574 0.54543 0.46952
1.0 0.30709 0.57056 0.81781 0.76162 0.51892
2.0 0.56266 0.25582 0.60978 0.68704 0.59802
3.0 0.57898 0.34281 0.72728 0.49233 0.58281
4.0 0.34596 0.59843 0.54688 0.53609 0.44819
5.0 0.35215 0.47915 0.5206 0.42647 0.45491
6.0 0.34647 0.60399 0.51646 0.56269 0.28897
...
93.0 0.49195 0.47864 0.36193 0.53469 0.46009
94.0 0.28274 0.5606 0.4882 0.52875 0.51679
95.0 0.37751 0.38506 0.48282 0.46273 0.61719
96.0 0.41973 0.63651 0.41802 0.49259 0.22991
97.0 0.42866 0.38589 0.4674 0.36569 0.54757
98.0 0.55714 0.54978 0.5384 0.39655 0.57852
99.0 0.58461 0.66218 0.41195 0.44326 0.55925
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.47172 1.75032 -0.30767 0.47172 1.75032 ...
1 -0.35394 -0.25148 2.01532 -0.35394 -0.25148 ...
2 -0.45957 0.77746 1.21838 -0.45957 0.77746 ...
3 -0.99652 0.5597 -0.47616 -0.99652 0.5597 ...
4 0.26612 0.84674 -1.11554 0.26612 0.84674 ...
dtype: float64, shape: (5, 6)
Spliting#
Array split functions are also implemented
print(np.array_split(tsdtensor[0:10], 2))
[Time (s)
---------- -----------------------------
0 [[0.567582 ... 0.231626] ...]
1 [[0.008672 ... 0.458732] ...]
2 [[0.93822 ... 0.697647] ...]
3 [[0.614734 ... 0.801296] ...]
4 [[0.098947 ... 0.626836] ...]
dtype: float64, shape: (5, 5, 5), Time (s)
---------- -----------------------------
5 [[0.154144 ... 0.800397] ...]
6 [[0.207274 ... 0.163244] ...]
7 [[0.406894 ... 0.47527 ] ...]
8 [[0.454983 ... 0.793374] ...]
9 [[0.329952 ... 0.391992] ...]
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'>]