pynapple.IntervalSet.set_info#

IntervalSet.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
>>> times = np.array([[0, 5], [10, 12], [20, 33]])
>>> ep = nap.IntervalSet(times)

To add metadata with a pandas.DataFrame:

>>> import pandas as pd
>>> metadata = pd.DataFrame(data=['left','right','left'], columns=['choice'])
>>> ep.set_info(metadata)
>>> ep
  index    start    end     choice
      0        0      5     left
      1       10     12     right
      2       20     33     left
shape: (3, 2), time unit: sec.

To add metadata with a dictionary:

>>> metadata = {"reward": [1, 0, 1]}
>>> ep.set_info(metadata)
>>> ep
  index    start    end     choice      reward
      0        0      5     left             1
      1       10     12     right            0
      2       20     33     left             1
shape: (3, 2), time unit: sec.

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

>>> stim = pd.Series(data = [10, -23, 12])
>>> ep.set_info(stim=stim)
>>> ep
  index    start    end     choice      reward    stim
      0        0      5     left             1      10
      1       10     12     right            0     -23
      2       20     33     left             1      12
shape: (3, 2), time unit: sec.

To add metadata as an attribute:

>>> ep.label = ["a", "b", "c"]
>>> ep
  index    start    end     choice      reward  label
      0        0      5     left             1  a
      1       10     12     right            0  b
      2       20     33     left             1  c
shape: (3, 2), time unit: sec.

To add metadata as a key:

>>> ep["error"] = [0, 0, 0]
>>> ep
  index    start    end     choice      reward  label      error
      0        0      5     left             1  a             0
      1       10     12     right            0  b             0
      2       20     33     left             1  c             0
shape: (3, 2), time unit: sec.

Metadata can be overwritten:

>>> ep.set_info(label=["x", "y", "z"])
>>> ep
  index    start    end     choice      reward  label      error
      0        0      5     left             1  x             0
      1       10     12     right            0  y             0
      2       20     33     left             1  z             0
shape: (3, 2), time unit: sec.