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.0053 ... 0.690074] ...]
1.0 [[0.566912 ... 0.583545] ...]
2.0 [[0.99813 ... 0.301213] ...]
3.0 [[0.849902 ... 0.659236] ...]
4.0 [[0.992987 ... 0.631527] ...]
5.0 [[0.578445 ... 0.041483] ...]
6.0 [[0.818012 ... 0.447257] ...]
...
93.0 [[0.26513 ... 0.74487] ...]
94.0 [[0.895007 ... 0.106769] ...]
95.0 [[0.516122 ... 0.727734] ...]
96.0 [[0.982979 ... 0.614222] ...]
97.0 [[0.965147 ... 0.049682] ...]
98.0 [[0.890305 ... 0.373121] ...]
99.0 [[0.304139 ... 0.958852] ...]
dtype: float64, shape: (100, 5, 5)
Tsd and Ts can be converted to a pandas.Series.
print(tsd.as_series())
0.0 0.377692
1.0 0.152566
2.0 0.781145
3.0 0.820107
4.0 0.150298
...
95.0 0.858521
96.0 0.174959
97.0 0.039717
98.0 0.158901
99.0 0.795611
Length: 100, dtype: float64
TsdFrame to a pandas.DataFrame.
print(tsdframe.as_dataframe())
a b c
0.0 0.851044 0.246953 0.740215
1.0 0.462154 0.911999 0.990912
2.0 0.551696 0.757215 0.604653
3.0 0.104066 0.237738 0.653355
4.0 0.062665 0.780707 0.599648
... ... ... ...
95.0 0.906628 0.028055 0.759401
96.0 0.083635 0.046119 0.024004
97.0 0.826357 0.144247 0.343547
98.0 0.105027 0.899808 0.728835
99.0 0.370736 0.341477 0.710382
[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.0053 ... 0.690074] ...]
1 [[0.566912 ... 0.583545] ...]
2 [[0.99813 ... 0.301213] ...]
3 [[0.849902 ... 0.659236] ...]
4 [[0.992987 ... 0.631527] ...]
5 [[0.578445 ... 0.041483] ...]
6 [[0.818012 ... 0.447257] ...]
7 [[0.508829 ... 0.036815] ...]
8 [[0.891461 ... 0.801579] ...]
9 [[0.116013 ... 0.117882] ...]
dtype: float64, shape: (10, 5, 5)
First column. Return a Tsd
print(tsdframe[:,0])
Time (s)
---------- ---------
0.0 0.851044
1.0 0.462154
2.0 0.551696
3.0 0.104066
4.0 0.0626653
5.0 0.490087
6.0 0.800344
...
93.0 0.967049
94.0 0.209197
95.0 0.906628
96.0 0.083635
97.0 0.826357
98.0 0.105027
99.0 0.370736
dtype: float64, shape: (100,)
First element. Return a numpy ndarray
print(tsdtensor[0])
[[0.00529959 0.25402616 0.7595449 0.67178121 0.69007378]
[0.25036514 0.19887175 0.41382322 0.39692537 0.11118493]
[0.59406673 0.93731646 0.06573988 0.74397691 0.48385997]
[0.87671273 0.82645245 0.38979377 0.69330831 0.75904535]
[0.11507897 0.32986752 0.13574736 0.64782605 0.32492065]]
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.851044
1.0 0.462154
2.0 0.551696
3.0 0.104066
4.0 0.0626653
5.0 0.490087
6.0 0.800344
...
93.0 0.967049
94.0 0.209197
95.0 0.906628
96.0 0.083635
97.0 0.826357
98.0 0.105027
99.0 0.370736
dtype: float64, shape: (100,)
Time (s) a c
---------- --------- ---------
0.0 0.851044 0.740215
1.0 0.462154 0.990912
2.0 0.551696 0.604653
3.0 0.104066 0.653355
4.0 0.0626653 0.599648
5.0 0.490087 0.147332
6.0 0.800344 0.666815
...
93.0 0.967049 0.377941
94.0 0.209197 0.467226
95.0 0.906628 0.759401
96.0 0.083635 0.0240041
97.0 0.826357 0.343547
98.0 0.105027 0.728835
99.0 0.370736 0.710382
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.55527026 0.50469969 0.52405246 0.53461611 0.51772953]
[0.49120906 0.50376376 0.49854863 0.53282388 0.47881911]
[0.54085199 0.51445333 0.49201724 0.48562389 0.44557534]
[0.47930113 0.51139173 0.50428308 0.50473882 0.45598157]
[0.46590468 0.45300251 0.520082 0.48975519 0.51793053]]
Here averaging across the second dimension returns a TsdFrame.
print(np.mean(tsdtensor, 1))
Time (s) 0 1 2 3 4
---------- -------- -------- -------- -------- --------
0.0 0.368305 0.509307 0.35293 0.630764 0.473817
1.0 0.416354 0.697578 0.812398 0.421519 0.621102
2.0 0.448139 0.521064 0.610085 0.563153 0.467878
3.0 0.693095 0.623839 0.428307 0.391297 0.614345
4.0 0.345923 0.687282 0.287865 0.596288 0.415201
5.0 0.663976 0.52228 0.513091 0.371468 0.299023
6.0 0.58227 0.350942 0.527032 0.652877 0.570303
...
93.0 0.361657 0.358836 0.388443 0.494632 0.487227
94.0 0.409797 0.358406 0.3547 0.585395 0.433619
95.0 0.590525 0.592197 0.585913 0.515342 0.422272
96.0 0.503343 0.45304 0.511832 0.379933 0.56885
97.0 0.595753 0.460688 0.473409 0.363679 0.22786
98.0 0.539916 0.369353 0.472358 0.574382 0.388359
99.0 0.538147 0.486853 0.198854 0.68646 0.32698
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.08847 1.98467 0.196059 -1.08847 1.98467 ...
1 0.625375 -1.21658 -0.983034 0.625375 -1.21658 ...
2 0.375473 -0.520732 -1.18419 0.375473 -0.520732 ...
3 -0.856703 0.458259 -0.849551 -0.856703 0.458259 ...
4 -0.249614 0.478548 0.5414 -0.249614 0.478548 ...
dtype: float64, shape: (5, 6)
Spliting#
Array split functions are also implemented
print(np.array_split(tsdtensor[0:10], 2))
[Time (s)
---------- -----------------------------
0 [[0.0053 ... 0.690074] ...]
1 [[0.566912 ... 0.583545] ...]
2 [[0.99813 ... 0.301213] ...]
3 [[0.849902 ... 0.659236] ...]
4 [[0.992987 ... 0.631527] ...]
dtype: float64, shape: (5, 5, 5), Time (s)
---------- -----------------------------
5 [[0.578445 ... 0.041483] ...]
6 [[0.818012 ... 0.447257] ...]
7 [[0.508829 ... 0.036815] ...]
8 [[0.891461 ... 0.801579] ...]
9 [[0.116013 ... 0.117882] ...]
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'>]