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.533151 ... 0.885646] ...]
1.0         [[0.620299 ... 0.757264] ...]
2.0         [[0.672924 ... 0.413569] ...]
3.0         [[0.414281 ... 0.146035] ...]
4.0         [[0.620709 ... 0.652171] ...]
5.0         [[0.434099 ... 0.345883] ...]
6.0         [[0.411678 ... 0.356491] ...]
...
93.0        [[0.48804  ... 0.149203] ...]
94.0        [[0.229974 ... 0.889445] ...]
95.0        [[0.78224 ... 0.46421] ...]
96.0        [[0.907607 ... 0.441686] ...]
97.0        [[0.537749 ... 0.66584 ] ...]
98.0        [[0.294713 ... 0.441581] ...]
99.0        [[0.288545 ... 0.815727] ...]
dtype: float64, shape: (100, 5, 5)

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

print(tsd.as_series())
0.0     0.285720
1.0     0.080603
2.0     0.408775
3.0     0.694150
4.0     0.101843
          ...   
95.0    0.677120
96.0    0.948513
97.0    0.173626
98.0    0.942590
99.0    0.315350
Length: 100, dtype: float64

TsdFrame to a pandas.DataFrame.

print(tsdframe.as_dataframe())
             a         b         c
0.0   0.848074  0.913270  0.109936
1.0   0.139460  0.372159  0.802731
2.0   0.002257  0.517419  0.635126
3.0   0.291946  0.406501  0.282708
4.0   0.780871  0.317024  0.424559
...        ...       ...       ...
95.0  0.701713  0.472060  0.549747
96.0  0.868375  0.490047  0.900148
97.0  0.155355  0.391352  0.458521
98.0  0.722098  0.455217  0.101092
99.0  0.143975  0.708316  0.458449

[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.533151 ... 0.885646] ...]
1           [[0.620299 ... 0.757264] ...]
2           [[0.672924 ... 0.413569] ...]
3           [[0.414281 ... 0.146035] ...]
4           [[0.620709 ... 0.652171] ...]
5           [[0.434099 ... 0.345883] ...]
6           [[0.411678 ... 0.356491] ...]
7           [[0.865662 ... 0.515725] ...]
8           [[0.658476 ... 0.574419] ...]
9           [[0.223396 ... 0.383349] ...]
dtype: float64, shape: (10, 5, 5)

First column. Return a Tsd

print(tsdframe[:,0])
Time (s)
----------  ----------
0.0         0.848074
1.0         0.13946
2.0         0.00225732
3.0         0.291946
4.0         0.780871
5.0         0.362422
6.0         0.392401
...
93.0        0.741434
94.0        0.0116412
95.0        0.701713
96.0        0.868375
97.0        0.155355
98.0        0.722098
99.0        0.143975
dtype: float64, shape: (100,)

First element. Return a numpy ndarray

print(tsdtensor[0])
[[0.53315098 0.60169001 0.73523611 0.41386671 0.88564558]
 [0.74351286 0.47718942 0.11191842 0.91852617 0.75746448]
 [0.19514828 0.54351617 0.9568866  0.84104195 0.80516067]
 [0.74087291 0.79571384 0.76581522 0.81457971 0.93137851]
 [0.12948372 0.74136304 0.77621764 0.43680746 0.61592745]]

The time support is never changing when slicing time down.

print(tsd.time_support)
print(tsd[0:20].time_support)
            start    end
       0        0     99
shape: (1, 2), time unit: sec.
            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.848074
1.0         0.13946
2.0         0.00225732
3.0         0.291946
4.0         0.780871
5.0         0.362422
6.0         0.392401
...
93.0        0.741434
94.0        0.0116412
95.0        0.701713
96.0        0.868375
97.0        0.155355
98.0        0.722098
99.0        0.143975
dtype: float64, shape: (100,)
Time (s)          a        c
----------  -------  -------
0.0         0.84807  0.10994
1.0         0.13946  0.80273
2.0         0.00226  0.63513
3.0         0.29195  0.28271
4.0         0.78087  0.42456
5.0         0.36242  0.0278
6.0         0.3924   0.70229
...
93.0        0.74143  0.06472
94.0        0.01164  0.55764
95.0        0.70171  0.54975
96.0        0.86838  0.90015
97.0        0.15535  0.45852
98.0        0.7221   0.10109
99.0        0.14398  0.45845
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.52035591 0.52267668 0.50363455 0.49592419 0.42239605]
 [0.46806391 0.52216438 0.49800572 0.53901574 0.48897815]
 [0.53611619 0.51073155 0.48254718 0.51585477 0.51659674]
 [0.56494284 0.51420808 0.51682941 0.46872673 0.47015837]
 [0.49411079 0.51872856 0.48225632 0.46536491 0.49106298]]

Here averaging across the second dimension returns a TsdFrame.

print(np.mean(tsdtensor, 1))
Time (s)          0        1        2        3        4
----------  -------  -------  -------  -------  -------
0.0         0.46843  0.63189  0.66921  0.68496  0.79912
1.0         0.50476  0.61039  0.41112  0.29512  0.43724
2.0         0.55917  0.21768  0.61746  0.49611  0.56199
3.0         0.53231  0.54565  0.17468  0.65317  0.34064
4.0         0.71631  0.47427  0.54791  0.39543  0.626
5.0         0.43907  0.32373  0.58829  0.37783  0.54083
6.0         0.41296  0.4774   0.46118  0.66019  0.3284
...
93.0        0.66044  0.42032  0.76378  0.41767  0.30931
94.0        0.68995  0.59632  0.51552  0.50133  0.50625
95.0        0.66852  0.66112  0.42286  0.33033  0.40462
96.0        0.69749  0.41652  0.44195  0.37756  0.56315
97.0        0.48551  0.63088  0.4823   0.31186  0.48278
98.0        0.5207   0.86618  0.76817  0.54372  0.73368
99.0        0.43696  0.55619  0.69225  0.5552   0.6076
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.66317  -0.03139   1.05607  0.66317  -0.03139  ...
1           0.70816  -1.44875  -0.66517  0.70816  -1.44875  ...
2           0.35552   0.39029  -1.37863  0.35552   0.39029  ...
3           0.29735  -0.89868  -1.89576  0.29735  -0.89868  ...
4           2.23261  -1.52399   0.17421  2.23261  -1.52399  ...
dtype: float64, shape: (5, 6)

Spliting#

Array split functions are also implemented

print(np.array_split(tsdtensor[0:10], 2))
[Time (s)
----------  -----------------------------
0           [[0.533151 ... 0.885646] ...]
1           [[0.620299 ... 0.757264] ...]
2           [[0.672924 ... 0.413569] ...]
3           [[0.414281 ... 0.146035] ...]
4           [[0.620709 ... 0.652171] ...]
dtype: float64, shape: (5, 5, 5), Time (s)
----------  -----------------------------
5           [[0.434099 ... 0.345883] ...]
6           [[0.411678 ... 0.356491] ...]
7           [[0.865662 ... 0.515725] ...]
8           [[0.658476 ... 0.574419] ...]
9           [[0.223396 ... 0.383349] ...]
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'>]