pynapple.TsGroup.set_info#

TsGroup.set_info(metadata=None, **kwargs)[source]#

Add metadata information about the object. Metadata are saved as a pandas.DataFrame.

If the metadata name does not contain special nor overlaps with class attributes, it can also be set using attribute assignment.

If the metadata name does not overlap with class-reserved keys, it can also be set using key assignment.

Metadata entries (excluding “rate” for TsGroup) are mutable and can be overwritten.

Parameters:
  • metadata (pandas.DataFrame or dict or pandas.Series, optional) –

    Object containing metadata information, where metadata names are extracted from column names (pandas.DataFrame), key names (dict), or index (pandas.DataFrame).

    • If a pandas.DataFrame is passed, the index must match the metadata index.

    • If a dictionary is passed, the length of each value must match the metadata index length.

    • A pandas.Series can only be passed if the object has a single interval.

  • **kwargs (optional) – Key-word arguments for setting metadata. Values can be either pandas.Series, numpy.ndarray, list or tuple, and must have the same length as the metadata index. If pandas.Series, the index must match the metadata index. If the object only has one index, non-iterable values are also accepted.

Raises:
  • ValueError

    • If metadata index does not match input index (pandas.DataFrame, pandas.Series) - If input array length does not match metadata length (numpy.ndarray, list, tuple)

  • RuntimeError – If the metadata argument is passed as a pandas.Series (for more than one metadata index), numpy.ndarray, list or tuple.

  • TypeError – If key-word arguments are not of type pandas.Series, tuple, list, or numpy.ndarray and cannot be set.

Examples

>>> import pynapple as nap
>>> import numpy as np
>>> tmp = {0:nap.Ts(t=np.arange(0,200), time_units='s'),
... 1:nap.Ts(t=np.arange(0,200,0.5), time_units='s'),
... 2:nap.Ts(t=np.arange(0,300,0.25), time_units='s'),
... }
>>> tsgroup = nap.TsGroup(tmp)

To add metadata with a pandas.DataFrame:

>>> import pandas as pd
>>> structs = pd.DataFrame(index = [0,1,2], data=['pfc','pfc','ca1'], columns=['struct'])
>>> tsgroup.set_info(structs)
>>> tsgroup
  Index     rate  struct
-------  -------  --------
      0  0.66722  pfc
      1  1.33445  pfc
      2  4.00334  ca1

To add metadata with a dictionary:

>>> coords = {"coords": [[0,0],[0,1],[1,0]]}
>>> tsgroup.set_info(coords)
>>> tsgroup
  Index     rate  struct    coords
-------  -------  --------  --------
      0  0.66722  pfc       [0, 0]
      1  1.33445  pfc       [0, 1]
      2  4.00334  ca1       [1, 0]

To add metadata with a keyword argument (pd.Series, numpy.ndarray, list or tuple):

>>> hd = pd.Series(index = [0,1,2], data = [0,1,1])
>>> tsgroup.set_info(hd=hd)
>>> tsgroup
  Index     rate  struct    coords      hd
-------  -------  --------  --------  ----
      0  0.66722  pfc       [0, 0]       0
      1  1.33445  pfc       [0, 1]       1
      2  4.00334  ca1       [1, 0]       1

To add metadata as an attribute:

>>> tsgroup.label = ["a", "b", "c"]
>>> tsgroup
  Index     rate  struct    coords      hd  label
-------  -------  --------  --------  ----  -------
      0  0.66722  pfc       [0, 0]       0  a
      1  1.33445  pfc       [0, 1]       1  b
      2  4.00334  ca1       [1, 0]       1  c

To add metadata as a key:

>>> tsgroup["type"] = ["multi", "multi", "single"]
>>> tsgroup
  Index     rate  struct    coords      hd  label    type
-------  -------  --------  --------  ----  -------  ------
      0  0.66722  pfc       [0, 0]       0  a        multi
      1  1.33445  pfc       [0, 1]       1  b        multi
      2  4.00334  ca1       [1, 0]       1  c        single

Metadata can be overwritten:

>>> tsgroup.set_info(label=["x", "y", "z"])
>>> tsgroup
  Index     rate  struct    coords      hd  label    type
-------  -------  --------  --------  ----  -------  ------
      0  0.66722  pfc       [0, 0]       0  x        multi
      1  1.33445  pfc       [0, 1]       1  y        multi
      2  4.00334  ca1       [1, 0]       1  z        single