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.588313 ... 0.756126] ...]
1.0 [[0.666966 ... 0.642634] ...]
2.0 [[0.597288 ... 0.196689] ...]
3.0 [[0.370695 ... 0.270928] ...]
4.0 [[0.62771 ... 0.208911] ...]
5.0 [[0.136267 ... 0.67203 ] ...]
6.0 [[0.094411 ... 0.969207] ...]
...
93.0 [[0.790104 ... 0.352881] ...]
94.0 [[0.890359 ... 0.450594] ...]
95.0 [[0.859126 ... 0.180888] ...]
96.0 [[0.711572 ... 0.581762] ...]
97.0 [[0.465867 ... 0.432003] ...]
98.0 [[0.860015 ... 0.775891] ...]
99.0 [[0.565426 ... 0.082441] ...]
dtype: float64, shape: (100, 5, 5)
Tsd and Ts can be converted to a pandas.Series.
print(tsd.as_series())
0.0 0.735758
1.0 0.689587
2.0 0.026943
3.0 0.504070
4.0 0.219915
...
95.0 0.886554
96.0 0.320995
97.0 0.173624
98.0 0.950387
99.0 0.956821
Length: 100, dtype: float64
TsdFrame to a pandas.DataFrame.
print(tsdframe.as_dataframe())
a b c
0.0 0.229252 0.949727 0.023057
1.0 0.573386 0.044100 0.029912
2.0 0.181744 0.701858 0.259283
3.0 0.041049 0.465442 0.499930
4.0 0.795547 0.563218 0.025132
... ... ... ...
95.0 0.738515 0.844557 0.850147
96.0 0.215264 0.553732 0.760389
97.0 0.745316 0.184333 0.458243
98.0 0.644150 0.630361 0.239533
99.0 0.769969 0.742583 0.796704
[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.588313 ... 0.756126] ...]
1 [[0.666966 ... 0.642634] ...]
2 [[0.597288 ... 0.196689] ...]
3 [[0.370695 ... 0.270928] ...]
4 [[0.62771 ... 0.208911] ...]
5 [[0.136267 ... 0.67203 ] ...]
6 [[0.094411 ... 0.969207] ...]
7 [[0.827314 ... 0.218817] ...]
8 [[0.353052 ... 0.29571 ] ...]
9 [[0.50265 ... 0.50299] ...]
dtype: float64, shape: (10, 5, 5)
First column. Return a Tsd
print(tsdframe[:,0])
Time (s)
---------- ---------
0.0 0.229252
1.0 0.573386
2.0 0.181744
3.0 0.0410487
4.0 0.795547
5.0 0.0754028
6.0 0.327966
...
93.0 0.318202
94.0 0.722425
95.0 0.738515
96.0 0.215264
97.0 0.745316
98.0 0.64415
99.0 0.769969
dtype: float64, shape: (100,)
First element. Return a numpy ndarray
print(tsdtensor[0])
[[0.58831308 0.00892637 0.49734913 0.55805829 0.75612566]
[0.02643455 0.91520625 0.76334031 0.29816296 0.5122753 ]
[0.61067709 0.60153302 0.08234412 0.19173879 0.46933801]
[0.41022945 0.65740509 0.82871477 0.30069161 0.92624772]
[0.61579912 0.93404202 0.1287638 0.56424307 0.72540854]]
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.229252
1.0 0.573386
2.0 0.181744
3.0 0.0410487
4.0 0.795547
5.0 0.0754028
6.0 0.327966
...
93.0 0.318202
94.0 0.722425
95.0 0.738515
96.0 0.215264
97.0 0.745316
98.0 0.64415
99.0 0.769969
dtype: float64, shape: (100,)
Time (s) a c
---------- --------- ---------
0.0 0.229252 0.0230567
1.0 0.573386 0.0299122
2.0 0.181744 0.259283
3.0 0.0410487 0.49993
4.0 0.795547 0.025132
5.0 0.0754028 0.721368
6.0 0.327966 0.30689
...
93.0 0.318202 0.206265
94.0 0.722425 0.310507
95.0 0.738515 0.850147
96.0 0.215264 0.760389
97.0 0.745316 0.458243
98.0 0.64415 0.239533
99.0 0.769969 0.796704
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.49321699 0.46881613 0.51051588 0.4966401 0.48727488]
[0.52381963 0.4627103 0.48498841 0.52291871 0.52786919]
[0.54695508 0.49731186 0.49145481 0.47396427 0.49213069]
[0.48489025 0.48911274 0.51427638 0.51925029 0.50825803]
[0.51413237 0.52811764 0.50811282 0.49482385 0.48111582]]
Here averaging across the second dimension returns a TsdFrame.
print(np.mean(tsdtensor, 1))
Time (s) 0 1 2 3 4
---------- -------- -------- -------- -------- --------
0.0 0.450291 0.623423 0.460102 0.382579 0.677879
1.0 0.535409 0.746331 0.581938 0.308074 0.522654
2.0 0.731652 0.624255 0.541472 0.386838 0.447763
3.0 0.528747 0.486125 0.684582 0.452685 0.213022
4.0 0.481679 0.628598 0.488678 0.359029 0.376111
5.0 0.548776 0.551705 0.319101 0.284369 0.744559
6.0 0.348424 0.493404 0.491026 0.808649 0.613472
...
93.0 0.321392 0.464573 0.52932 0.72936 0.335908
94.0 0.65299 0.374519 0.454489 0.45175 0.50934
95.0 0.607102 0.470987 0.375311 0.487199 0.515984
96.0 0.580614 0.148714 0.463453 0.541988 0.334566
97.0 0.415767 0.465315 0.372025 0.205 0.23239
98.0 0.600372 0.472678 0.603061 0.224038 0.666758
99.0 0.576579 0.473041 0.53351 0.457928 0.513545
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.48072 1.01707 -0.343061 -1.48072 1.01707 ...
1 0.0721757 0.459401 1.38804 0.0721757 0.459401 ...
2 0.19681 -1.60633 1.81459 0.19681 -1.60633 ...
3 -0.979261 -1.64622 -0.0935726 -0.979261 -1.64622 ...
4 1.21375 0.890931 -0.299134 1.21375 0.890931 ...
dtype: float64, shape: (5, 6)
Spliting#
Array split functions are also implemented
print(np.array_split(tsdtensor[0:10], 2))
[Time (s)
---------- -----------------------------
0 [[0.588313 ... 0.756126] ...]
1 [[0.666966 ... 0.642634] ...]
2 [[0.597288 ... 0.196689] ...]
3 [[0.370695 ... 0.270928] ...]
4 [[0.62771 ... 0.208911] ...]
dtype: float64, shape: (5, 5, 5), Time (s)
---------- -----------------------------
5 [[0.136267 ... 0.67203 ] ...]
6 [[0.094411 ... 0.969207] ...]
7 [[0.827314 ... 0.218817] ...]
8 [[0.353052 ... 0.29571 ] ...]
9 [[0.50265 ... 0.50299] ...]
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'>]