Numpy tutorial#

This tutorial shows how pynapple interact with numpy.

Hide 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.969231 ... 0.517821] ...]
1.0         [[0.691207 ... 0.174477] ...]
2.0         [[0.447301 ... 0.287317] ...]
3.0         [[0.945993 ... 0.127147] ...]
4.0         [[0.461237 ... 0.495863] ...]
5.0         [[0.953146 ... 0.887053] ...]
6.0         [[0.428793 ... 0.97389 ] ...]
...
93.0        [[0.694064 ... 0.966087] ...]
94.0        [[0.8782   ... 0.192555] ...]
95.0        [[0.552521 ... 0.449839] ...]
96.0        [[0.161994 ... 0.11756 ] ...]
97.0        [[0.485467 ... 0.742935] ...]
98.0        [[0.105507 ... 0.986018] ...]
99.0        [[0.701217 ... 0.819254] ...]
dtype: float64, shape: (100, 5, 5)

Tsd and Ts can be converted to a pandas.Series.

print(tsd.as_series())
0.0     0.409778
1.0     0.051986
2.0     0.832351
3.0     0.545054
4.0     0.062835
          ...   
95.0    0.238727
96.0    0.298128
97.0    0.031043
98.0    0.289414
99.0    0.578259
Length: 100, dtype: float64

TsdFrame to a pandas.DataFrame.

print(tsdframe.as_dataframe())
             a         b         c
0.0   0.950872  0.589027  0.689778
1.0   0.690975  0.407228  0.836317
2.0   0.883386  0.998743  0.957273
3.0   0.907098  0.146465  0.805547
4.0   0.686170  0.646375  0.276124
...        ...       ...       ...
95.0  0.647187  0.487727  0.836093
96.0  0.164545  0.234918  0.440819
97.0  0.302891  0.400374  0.059482
98.0  0.030110  0.744224  0.752495
99.0  0.132986  0.415695  0.487976

[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.969231 ... 0.517821] ...]
1           [[0.691207 ... 0.174477] ...]
2           [[0.447301 ... 0.287317] ...]
3           [[0.945993 ... 0.127147] ...]
4           [[0.461237 ... 0.495863] ...]
5           [[0.953146 ... 0.887053] ...]
6           [[0.428793 ... 0.97389 ] ...]
7           [[0.107697 ... 0.078799] ...]
8           [[0.865244 ... 0.015253] ...]
9           [[0.815602 ... 0.054545] ...]
dtype: float64, shape: (10, 5, 5)

First column. Return a Tsd

print(tsdframe[:,0])
Time (s)
----------  ---------
0.0         0.950872
1.0         0.690975
2.0         0.883386
3.0         0.907098
4.0         0.68617
5.0         0.256267
6.0         0.125698
...
93.0        0.917418
94.0        0.440351
95.0        0.647187
96.0        0.164545
97.0        0.302891
98.0        0.0301104
99.0        0.132986
dtype: float64, shape: (100,)

First element. Return a numpy ndarray

print(tsdtensor[0])
[[0.96923052 0.29716793 0.28053347 0.23311659 0.51782061]
 [0.18948741 0.37641178 0.05478512 0.18885286 0.96578868]
 [0.43616492 0.68838407 0.39177279 0.64047378 0.74620485]
 [0.25896089 0.22673972 0.70592331 0.37012677 0.82706204]
 [0.62946791 0.21266931 0.58501882 0.79387502 0.62235394]]

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.950872
1.0         0.690975
2.0         0.883386
3.0         0.907098
4.0         0.68617
5.0         0.256267
6.0         0.125698
...
93.0        0.917418
94.0        0.440351
95.0        0.647187
96.0        0.164545
97.0        0.302891
98.0        0.0301104
99.0        0.132986
dtype: float64, shape: (100,)
Time (s)    a        c
----------  -------  -------
0.0         0.95087  0.68978
1.0         0.69098  0.83632
2.0         0.88339  0.95727
3.0         0.9071   0.80555
4.0         0.68617  0.27612
5.0         0.25627  0.58136
6.0         0.1257   0.49086
...         ...      ...
93.0        0.91742  0.27926
94.0        0.44035  0.08459
95.0        0.64719  0.83609
96.0        0.16454  0.44082
97.0        0.30289  0.05948
98.0        0.03011  0.7525
99.0        0.13299  0.48798
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.4912208  0.52118981 0.51299375 0.54433891 0.46700554]
 [0.50002635 0.49612976 0.49567259 0.43874878 0.535856  ]
 [0.49439129 0.5560834  0.47787811 0.53516709 0.54213129]
 [0.50919779 0.50851925 0.51695101 0.54014674 0.46360068]
 [0.52859017 0.49363115 0.48953923 0.52800662 0.55151736]]

Here averaging across the second dimension returns a TsdFrame.

print(np.mean(tsdtensor, 1))
Time (s)    0        1        2        3        4
----------  -------  -------  -------  -------  -------
0.0         0.49666  0.36027  0.40361  0.44529  0.73585
1.0         0.5938   0.50294  0.65511  0.55576  0.52336
2.0         0.58887  0.59605  0.32596  0.55319  0.60827
3.0         0.51384  0.70762  0.40186  0.44996  0.50415
4.0         0.36421  0.30984  0.48995  0.45105  0.70789
5.0         0.62262  0.57042  0.58546  0.66305  0.58939
6.0         0.5402   0.57581  0.46359  0.52209  0.64601
...         ...      ...      ...      ...      ...
93.0        0.56003  0.38595  0.37121  0.72855  0.80834
94.0        0.68186  0.50616  0.60018  0.58373  0.48185
95.0        0.66497  0.61605  0.42642  0.3569   0.69373
96.0        0.30394  0.46671  0.30208  0.65073  0.35402
97.0        0.72519  0.55796  0.35402  0.53107  0.58912
98.0        0.50175  0.44738  0.49279  0.56009  0.64064
99.0        0.58836  0.44205  0.33121  0.43354  0.70882
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            1.52172  -0.00463  -0.68949   1.52172  -0.00463  ...
1           -1.02246  -0.46308   0.3791   -1.02246  -0.46308  ...
2           -0.18191  -0.61945   0.46189  -0.18191  -0.61945  ...
3           -1.54641  -0.90172  -1.55428  -1.54641  -0.90172  ...
4           -0.47934   1.83717   2.25127  -0.47934   1.83717  ...
dtype: float64, shape: (5, 6)

Spliting#

Array split functions are also implemented

print(np.array_split(tsdtensor[0:10], 2))
[Time (s)
----------  -----------------------------
0           [[0.969231 ... 0.517821] ...]
1           [[0.691207 ... 0.174477] ...]
2           [[0.447301 ... 0.287317] ...]
3           [[0.945993 ... 0.127147] ...]
4           [[0.461237 ... 0.495863] ...]
dtype: float64, shape: (5, 5, 5), Time (s)
----------  -----------------------------
5           [[0.953146 ... 0.887053] ...]
6           [[0.428793 ... 0.97389 ] ...]
7           [[0.107697 ... 0.078799] ...]
8           [[0.865244 ... 0.015253] ...]
9           [[0.815602 ... 0.054545] ...]
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'>]