pynapple.core.ts_group.TsGroup.save#

TsGroup.save(filename)[source]#

Save TsGroup object in npz format. The file will contain the timestamps, the data (if group of Tsd), group index, the time support and the metadata

The main purpose of this function is to save small/medium sized TsGroup objects.

The function will “flatten” the TsGroup by sorting all the timestamps and assigning to each the corresponding index. Typically, a TsGroup like this :

>>> TsGroup({
    0 : Tsd(t=[0, 2, 4], d=[1, 2, 3])
    1 : Tsd(t=[1, 5], d=[5, 6])})

will be saved as npz with the following keys:

>>> {
    't' : [0, 1, 2, 4, 5],
    'd' : [1, 5, 2, 3, 5],
    'index' : [0, 1, 0, 0, 1],
    'start' : [0],
    'end' : [5],
    'keys' : [0, 1],
    'type' : 'TsGroup'
}

Metadata are saved by columns with the column name as the npz key. To avoid potential conflicts, make sure the columns name of the metadata are different from [‘t’, ‘d’, ‘start’, ‘end’, ‘index’, ‘keys’]

You can load the object with nap.load_file. Default keys are ‘t’, ‘d’(optional), ‘start’, ‘end’, ‘index’, ‘keys’ and ‘type’. See the example below.

Parameters:

filename (str) – The filename

Examples

>>> import pynapple as nap
>>> import numpy as np
>>> tsgroup = nap.TsGroup({
    0 : nap.Ts(t=np.array([0.0, 2.0, 4.0])),
    6 : nap.Ts(t=np.array([1.0, 5.0]))
    },
    group = np.array([0, 1]),
    location = np.array(['right foot', 'left foot'])
    )
>>> tsgroup
  Index    rate    group  location
-------  ------  -------  ----------
      0     0.6        0  right foot
      6     0.4        1  left foot
>>> tsgroup.save("my_tsgroup.npz")

To get back to pynapple, you can use the nap.load_file function :

>>> tsgroup = nap.load_file("my_tsgroup.npz")
>>> tsgroup
  Index    rate    group  location
-------  ------  -------  ----------
      0     0.6        0  right foot
      6     0.4        1  left foot
Raises:

RuntimeError – If filename is not str, path does not exist or filename is a directory.