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.729327 ... 0.585016] ...]
1.0         [[0.377929 ... 0.576972] ...]
2.0         [[0.484227 ... 0.700254] ...]
3.0         [[0.189554 ... 0.213155] ...]
4.0         [[0.202537 ... 0.573791] ...]
5.0         [[0.575364 ... 0.027699] ...]
6.0         [[0.180485 ... 0.451549] ...]
...
93.0        [[0.776905 ... 0.488398] ...]
94.0        [[0.10257  ... 0.922165] ...]
95.0        [[0.473575 ... 0.838738] ...]
96.0        [[0.550563 ... 0.907056] ...]
97.0        [[0.588213 ... 0.568854] ...]
98.0        [[0.287782 ... 0.499943] ...]
99.0        [[0.412394 ... 0.421457] ...]
dtype: float64, shape: (100, 5, 5)

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

print(tsd.as_series())
0.0     0.359062
1.0     0.121045
2.0     0.847740
3.0     0.989190
4.0     0.384765
          ...   
95.0    0.446567
96.0    0.683297
97.0    0.398030
98.0    0.843351
99.0    0.194079
Length: 100, dtype: float64

TsdFrame to a pandas.DataFrame.

print(tsdframe.as_dataframe())
             a         b         c
0.0   0.517128  0.100095  0.619255
1.0   0.424178  0.590804  0.426257
2.0   0.059132  0.132273  0.779476
3.0   0.894297  0.355206  0.248692
4.0   0.471893  0.750695  0.960159
...        ...       ...       ...
95.0  0.105909  0.467195  0.445812
96.0  0.381994  0.717516  0.766497
97.0  0.450097  0.006290  0.809114
98.0  0.998942  0.542131  0.843285
99.0  0.788869  0.907800  0.640897

[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.729327 ... 0.585016] ...]
1           [[0.377929 ... 0.576972] ...]
2           [[0.484227 ... 0.700254] ...]
3           [[0.189554 ... 0.213155] ...]
4           [[0.202537 ... 0.573791] ...]
5           [[0.575364 ... 0.027699] ...]
6           [[0.180485 ... 0.451549] ...]
7           [[0.767762 ... 0.363819] ...]
8           [[0.659385 ... 0.772454] ...]
9           [[0.496862 ... 0.944736] ...]
dtype: float64, shape: (10, 5, 5)

First column. Return a Tsd

print(tsdframe[:,0])
Time (s)
----------  ---------
0.0         0.517128
1.0         0.424178
2.0         0.0591324
3.0         0.894297
4.0         0.471893
5.0         0.319778
6.0         0.575554
...
93.0        0.951391
94.0        0.370632
95.0        0.105909
96.0        0.381994
97.0        0.450097
98.0        0.998942
99.0        0.788869
dtype: float64, shape: (100,)

First element. Return a numpy ndarray

print(tsdtensor[0])
[[0.72932678 0.58686495 0.87334816 0.65111006 0.5850158 ]
 [0.68320957 0.82686616 0.22890101 0.81993414 0.83816458]
 [0.18663623 0.18209467 0.25205317 0.87193515 0.16337421]
 [0.13861072 0.29624649 0.68050017 0.24814238 0.9351152 ]
 [0.42802733 0.7427821  0.01321533 0.94901792 0.1557731 ]]

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.517128
1.0         0.424178
2.0         0.0591324
3.0         0.894297
4.0         0.471893
5.0         0.319778
6.0         0.575554
...
93.0        0.951391
94.0        0.370632
95.0        0.105909
96.0        0.381994
97.0        0.450097
98.0        0.998942
99.0        0.788869
dtype: float64, shape: (100,)
Time (s)    a        c
----------  -------  -------
0.0         0.51713  0.61925
1.0         0.42418  0.42626
2.0         0.05913  0.77948
3.0         0.8943   0.24869
4.0         0.47189  0.96016
5.0         0.31978  0.02826
6.0         0.57555  0.23265
...         ...      ...
93.0        0.95139  0.65862
94.0        0.37063  0.302
95.0        0.10591  0.44581
96.0        0.38199  0.7665
97.0        0.4501   0.80911
98.0        0.99894  0.84328
99.0        0.78887  0.6409
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.5009519  0.46425839 0.51412889 0.5394045  0.52000177]
 [0.53669132 0.51204054 0.48060033 0.4897499  0.48541563]
 [0.46387578 0.48631504 0.51463951 0.55301385 0.45385942]
 [0.44350465 0.49338194 0.55649783 0.50176174 0.50986324]
 [0.51704784 0.48877805 0.48709892 0.51960523 0.49409385]]

Here averaging across the second dimension returns a TsdFrame.

print(np.mean(tsdtensor, 1))
Time (s)    0        1        2        3        4
----------  -------  -------  -------  -------  -------
0.0         0.43316  0.52697  0.4096   0.70803  0.53549
1.0         0.37384  0.59984  0.28743  0.55565  0.57226
2.0         0.52381  0.32313  0.55131  0.54961  0.52434
3.0         0.50093  0.39157  0.60452  0.60731  0.6757
4.0         0.57837  0.40263  0.62303  0.644    0.57313
5.0         0.42726  0.49544  0.49756  0.57408  0.41742
6.0         0.27842  0.18712  0.4564   0.6677   0.54179
...         ...      ...      ...      ...      ...
93.0        0.61081  0.5339   0.50829  0.78366  0.2518
94.0        0.46849  0.47138  0.59188  0.42413  0.60632
95.0        0.55303  0.31661  0.7227   0.2723   0.6118
96.0        0.61851  0.30595  0.42899  0.52705  0.65926
97.0        0.70503  0.61686  0.29164  0.23807  0.41798
98.0        0.37216  0.39554  0.42066  0.49147  0.20253
99.0        0.54908  0.47844  0.57612  0.37068  0.46699
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.58403  0.92563  -1.51881  -0.58403  0.92563  ...
1           -0.13608  1.14921  -1.4005   -0.13608  1.14921  ...
2           -0.02758  0.20881  -0.02139  -0.02758  0.20881  ...
3            0.62014  1.12809   0.37834   0.62014  1.12809  ...
4           -0.18813  1.28844   0.36229  -0.18813  1.28844  ...
dtype: float64, shape: (5, 6)

Spliting#

Array split functions are also implemented

print(np.array_split(tsdtensor[0:10], 2))
[Time (s)
----------  -----------------------------
0           [[0.729327 ... 0.585016] ...]
1           [[0.377929 ... 0.576972] ...]
2           [[0.484227 ... 0.700254] ...]
3           [[0.189554 ... 0.213155] ...]
4           [[0.202537 ... 0.573791] ...]
dtype: float64, shape: (5, 5, 5), Time (s)
----------  -----------------------------
5           [[0.575364 ... 0.027699] ...]
6           [[0.180485 ... 0.451549] ...]
7           [[0.767762 ... 0.363819] ...]
8           [[0.659385 ... 0.772454] ...]
9           [[0.496862 ... 0.944736] ...]
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'>]