Numpy tutorial#
This tutorial shows how pynapple interact with numpy.
Multiple time series object are available 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.186218 ... 0.649924] ...]
1.0 [[0.010692 ... 0.386449] ...]
2.0 [[0.551449 ... 0.009941] ...]
3.0 [[0.454963 ... 0.316769] ...]
4.0 [[0.823954 ... 0.154197] ...]
5.0 [[0.26538 ... 0.936211] ...]
6.0 [[0.601181 ... 0.477291] ...]
...
93.0 [[0.849395 ... 0.535854] ...]
94.0 [[0.846186 ... 0.828174] ...]
95.0 [[0.044014 ... 0.906635] ...]
96.0 [[0.418819 ... 0.579245] ...]
97.0 [[0.666689 ... 0.060465] ...]
98.0 [[0.595505 ... 0.726916] ...]
99.0 [[0.361369 ... 0.434447] ...]
dtype: float64, shape: (100, 5, 5)
Tsd and Ts can be converted to a pandas.Series.
print(tsd.as_series())
0.0 0.104090
1.0 0.013070
2.0 0.894791
3.0 0.326995
4.0 0.581012
...
95.0 0.033371
96.0 0.424782
97.0 0.371060
98.0 0.764232
99.0 0.634433
Length: 100, dtype: float64
TsdFrame to a pandas.DataFrame.
print(tsdframe.as_dataframe())
a b c
0.0 0.901380 0.548672 0.361905
1.0 0.326765 0.399546 0.336547
2.0 0.244275 0.914635 0.939945
3.0 0.064310 0.539144 0.598494
4.0 0.576453 0.578163 0.729988
... ... ... ...
95.0 0.690978 0.042501 0.454237
96.0 0.998974 0.271932 0.398942
97.0 0.014881 0.527541 0.485064
98.0 0.474094 0.190988 0.784191
99.0 0.145249 0.622297 0.152958
[100 rows x 3 columns]
Attributes#
The numpy array is accessible 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.186218 ... 0.649924] ...]
1 [[0.010692 ... 0.386449] ...]
2 [[0.551449 ... 0.009941] ...]
3 [[0.454963 ... 0.316769] ...]
4 [[0.823954 ... 0.154197] ...]
5 [[0.26538 ... 0.936211] ...]
6 [[0.601181 ... 0.477291] ...]
7 [[0.524454 ... 0.930873] ...]
8 [[0.841627 ... 0.288315] ...]
9 [[0.268393 ... 0.791978] ...]
dtype: float64, shape: (10, 5, 5)
First column. Return a Tsd
print(tsdframe[:,0])
Time (s)
---------- ---------
0.0 0.90138
1.0 0.326765
2.0 0.244275
3.0 0.0643104
4.0 0.576453
5.0 0.778786
6.0 0.299152
...
93.0 0.845809
94.0 0.437888
95.0 0.690978
96.0 0.998974
97.0 0.0148805
98.0 0.474094
99.0 0.145249
dtype: float64, shape: (100,)
First element. Return a numpy ndarray
print(tsdtensor[0])
[[0.18621847 0.4392246 0.03558727 0.37065168 0.64992362]
[0.15160054 0.66593636 0.05974801 0.2413929 0.50950747]
[0.88845965 0.23201463 0.71961044 0.47581099 0.46804304]
[0.90575732 0.39900676 0.05760168 0.19401337 0.11843012]
[0.81998374 0.82418848 0.32339473 0.92085162 0.68187099]]
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.90138
1.0 0.326765
2.0 0.244275
3.0 0.0643104
4.0 0.576453
5.0 0.778786
6.0 0.299152
...
93.0 0.845809
94.0 0.437888
95.0 0.690978
96.0 0.998974
97.0 0.0148805
98.0 0.474094
99.0 0.145249
dtype: float64, shape: (100,)
Time (s) a c
---------- --------- --------
0.0 0.90138 0.361905
1.0 0.326765 0.336547
2.0 0.244275 0.939945
3.0 0.0643104 0.598494
4.0 0.576453 0.729988
5.0 0.778786 0.616034
6.0 0.299152 0.723579
...
93.0 0.845809 0.601996
94.0 0.437888 0.175343
95.0 0.690978 0.454237
96.0 0.998974 0.398942
97.0 0.0148805 0.485064
98.0 0.474094 0.784191
99.0 0.145249 0.152958
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.46577185 0.50249559 0.51439386 0.50966914 0.52794379]
[0.47393711 0.44744094 0.47558983 0.51083697 0.47865356]
[0.50726594 0.50508188 0.49751277 0.4837151 0.50233476]
[0.47313938 0.54095941 0.51776162 0.48280531 0.52168938]
[0.50384281 0.51196003 0.45198354 0.55756216 0.53856606]]
Here averaging across the second dimension returns a TsdFrame.
print(np.mean(tsdtensor, 1))
Time (s) 0 1 2 3 4
---------- -------- -------- -------- -------- --------
0.0 0.590404 0.512074 0.239188 0.440544 0.485555
1.0 0.203335 0.40684 0.579154 0.517641 0.421785
2.0 0.456278 0.772993 0.254397 0.55405 0.34594
3.0 0.346668 0.675031 0.392541 0.493975 0.456972
4.0 0.593507 0.557132 0.650352 0.526933 0.507928
5.0 0.236533 0.50131 0.539544 0.497785 0.684107
6.0 0.618806 0.27593 0.614013 0.378515 0.302574
...
93.0 0.524728 0.494344 0.61534 0.623092 0.443853
94.0 0.608187 0.612722 0.481144 0.594864 0.684706
95.0 0.253986 0.505269 0.508121 0.793701 0.723098
96.0 0.596535 0.531348 0.572538 0.745428 0.421335
97.0 0.575271 0.383958 0.575645 0.66048 0.491524
98.0 0.437346 0.690722 0.357353 0.635281 0.661525
99.0 0.533603 0.462558 0.270837 0.724482 0.479423
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 0.115344 -0.269149 -2.2381 0.115344 -0.269149 ...
1 0.563317 0.611774 -0.382257 0.563317 0.611774 ...
2 1.0169 0.274684 -0.631586 1.0169 0.274684 ...
3 -1.10021 0.553952 -0.0999241 -1.10021 0.553952 ...
4 0.541204 -0.79925 -0.379317 0.541204 -0.79925 ...
dtype: float64, shape: (5, 6)
Splitting#
Array split functions are also implemented
print(np.array_split(tsdtensor[0:10], 2))
[Time (s)
---------- -----------------------------
0 [[0.186218 ... 0.649924] ...]
1 [[0.010692 ... 0.386449] ...]
2 [[0.551449 ... 0.009941] ...]
3 [[0.454963 ... 0.316769] ...]
4 [[0.823954 ... 0.154197] ...]
dtype: float64, shape: (5, 5, 5), Time (s)
---------- -----------------------------
5 [[0.26538 ... 0.936211] ...]
6 [[0.601181 ... 0.477291] ...]
7 [[0.524454 ... 0.930873] ...]
8 [[0.841627 ... 0.288315] ...]
9 [[0.268393 ... 0.791978] ...]
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'>]