pynapple.process.correlograms#

Functions to compute correlograms of timestamps data.

Functions

compute_autocorrelogram(group, binsize, ...)

Computes the autocorrelogram of a group of Ts/Tsd objects.

compute_crosscorrelogram(group, binsize, ...)

Computes all the pairwise cross-correlograms for TsGroup or list/tuple of two TsGroup.

compute_eventcorrelogram(group, event, ...)

Computes the correlograms of a group of Ts/Tsd objects with another single Ts/Tsd object The time of reference is the event times.

compute_isi_distribution(data[, bins, ...])

Computes the interspike interval distribution.

pynapple.process.correlograms.compute_autocorrelogram(group: TsGroup, binsize: float, windowsize: float, ep: IntervalSet | None = None, norm: bool = True, time_units: str = 's') DataFrame[source]#

Computes the autocorrelogram of a group of Ts/Tsd objects. The group can be passed directly as a TsGroup object.

Parameters:
  • group (TsGroup) – The group of Ts/Tsd objects to auto-correlate

  • binsize (float) – The bin size. Default is second. If different, specify with the parameter time_units (‘s’ [default], ‘ms’, ‘us’).

  • windowsize (float) – The window size. Default is second. If different, specify with the parameter time_units (‘s’ [default], ‘ms’, ‘us’).

  • ep (IntervalSet) – The epoch on which auto-corrs are computed. If None, the epoch is the time support of the group.

  • norm (bool, optional) – If True, autocorrelograms are normalized to baseline (i.e. divided by the average rate) If False, autocorrelograms are returned as the rate (Hz) of the time series (relative to itself)

  • time_units (str, optional) – The time units of the parameters. They have to be consistent for binsize and windowsize. (‘s’ [default], ‘ms’, ‘us’).

Returns:

DataFrame with time lags as index and unit IDs as columns. Values represent the firing rate (or normalized rate if norm=True).

Return type:

pandas.DataFrame

Raises:

TypeError – If group is not a TsGroup, or if binsize, windowsize, ep, norm, or time_units have invalid types.

Examples

>>> import pynapple as nap
>>> import numpy as np
>>> ts_group = nap.TsGroup({0: nap.Ts(t=np.sort(np.random.uniform(0, 10, 100)))})
>>> autocorr = nap.compute_autocorrelogram(ts_group, binsize=0.01, windowsize=0.1)
pynapple.process.correlograms.compute_crosscorrelogram(group: TsGroup | tuple[TsGroup, TsGroup] | list[TsGroup], binsize: float, windowsize: float, ep: IntervalSet | None = None, norm: bool = True, time_units: str = 's', reverse: bool = False) DataFrame[source]#

Computes all the pairwise cross-correlograms for TsGroup or list/tuple of two TsGroup.

If input is TsGroup only, the reference Ts/Tsd and target are chosen based on the builtin itertools.combinations function. For example if indexes are [0,1,2], the function computes cross-correlograms for the pairs (0,1), (0, 2), and (1, 2). The left index gives the reference time series. To reverse the order, set reverse=True.

If input is tuple/list of TsGroup, for example group=(group1, group2), the reference for each pairs comes from group1.

Parameters:
  • group (TsGroup or tuple/list of two TsGroups) – The group(s) of Ts/Tsd objects to cross-correlate. If a single TsGroup, computes pairwise cross-correlograms within the group. If a tuple/list of two TsGroups, computes cross-correlograms between all pairs from group1 (reference) and group2 (target).

  • binsize (float) – The bin size. Default is second. If different, specify with the parameter time_units (‘s’ [default], ‘ms’, ‘us’).

  • windowsize (float) – The window size. Default is second. If different, specify with the parameter time_units (‘s’ [default], ‘ms’, ‘us’).

  • ep (IntervalSet) – The epoch on which cross-corrs are computed. If None, the epoch is the time support of the group.

  • norm (bool, optional) – If True (default), cross-correlograms are normalized to baseline (i.e. divided by the average rate of the target time series) If False, cross-correlograms are returned as the rate (Hz) of the target time series (relative to the reference time series)

  • time_units (str, optional) – The time units of the parameters. They have to be consistent for binsize and windowsize. (‘s’ [default], ‘ms’, ‘us’).

  • reverse (bool, optional) – To reverse the pair order if input is TsGroup

Returns:

DataFrame with time lags as index and pair tuples (i, j) as columns. Values represent the firing rate of unit j relative to unit i (or normalized rate if norm=True).

Return type:

pandas.DataFrame

Raises:

TypeError – If group is not a TsGroup or tuple/list of two TsGroups, or if binsize, windowsize, ep, norm, time_units, or reverse have invalid types.

Examples

>>> import pynapple as nap
>>> import numpy as np
>>> ts_group = nap.TsGroup({
...     0: nap.Ts(t=np.sort(np.random.uniform(0, 10, 100))),
...     1: nap.Ts(t=np.sort(np.random.uniform(0, 10, 100)))
... })
>>> crosscorr = nap.compute_crosscorrelogram(ts_group, binsize=0.01, windowsize=0.1)
pynapple.process.correlograms.compute_eventcorrelogram(group: TsGroup, event: Ts | Tsd, binsize: float, windowsize: float, ep: IntervalSet | None = None, norm: bool = True, time_units: str = 's') DataFrame[source]#

Computes the correlograms of a group of Ts/Tsd objects with another single Ts/Tsd object The time of reference is the event times.

Parameters:
  • group (TsGroup) – The group of Ts/Tsd objects to correlate with the event

  • event (Ts/Tsd) – The event to correlate the each of the time series in the group with.

  • binsize (float) – The bin size. Default is second. If different, specify with the parameter time_units (‘s’ [default], ‘ms’, ‘us’).

  • windowsize (float) – The window size. Default is second. If different, specify with the parameter time_units (‘s’ [default], ‘ms’, ‘us’).

  • ep (IntervalSet) – The epoch on which cross-corrs are computed. If None, the epoch is the time support of the event.

  • norm (bool, optional) – If True (default), cross-correlograms are normalized to baseline (i.e. divided by the average rate of the target time series) If False, cross-correlograms are returned as the rate (Hz) of the target time series (relative to the event time series)

  • time_units (str, optional) – The time units of the parameters. They have to be consistent for binsize and windowsize. (‘s’ [default], ‘ms’, ‘us’).

Returns:

DataFrame with time lags as index and unit IDs as columns. Values represent the firing rate of each unit relative to the event times (or normalized rate if norm=True).

Return type:

pandas.DataFrame

Raises:

TypeError – If group is not a TsGroup, if event is not a Ts or Tsd, or if binsize, windowsize, ep, norm, or time_units have invalid types.

Examples

>>> import pynapple as nap
>>> import numpy as np
>>> ts_group = nap.TsGroup({0: nap.Ts(t=np.sort(np.random.uniform(0, 10, 100)))})
>>> event = nap.Ts(t=np.array([1.0, 3.0, 5.0, 7.0, 9.0]))
>>> eventcorr = nap.compute_eventcorrelogram(ts_group, event, binsize=0.01, windowsize=0.1)
pynapple.process.correlograms.compute_isi_distribution(data: Ts | Tsd | TsdFrame | TsdTensor | TsGroup, bins: int | list | ndarray[tuple[Any, ...], dtype[_ScalarT]] = 10, log_scale: bool = False, epochs: IntervalSet | None = None) DataFrame[source]#

Computes the interspike interval distribution.

Parameters:
  • data (Ts, TsGroup, Tsd, TsdFrame or TsdTensor) – The Ts, TsGroup, Tsd, TsdFrame or TsdTensor to compute the interspike interval distribution for.

  • bins (int or sequence of scalars) – If bins is an int, it defines the number of equal-width bins in the given range (10, by default). If bins is a sequence, it defines a monotonically increasing array of bin edges, including the rightmost edge, allowing for non-uniform bin widths.

  • log_scale (bool, optional) – If True, the computed ISI’s are log-transformed. Default is False.

  • epochs (IntervalSet, optional) – The epochs on which interspike intervals are computed. If None, the time support of the input is used.

Returns:

DataFrame to hold the distribution data.

Return type:

pandas.DataFrame

Raises:
  • TypeError – If data is not a Ts, TsGroup, Tsd, TsdFrame, or TsdTensor.

  • TypeError – If bins is not an int, list, or np.ndarray.

  • TypeError – If log_scale is not a bool.

  • ValueError – If bins is less than 1 (when int) or not monotonically increasing (when array).

Examples

>>> import numpy as np; np.random.seed(42)
>>> import pynapple as nap
>>> ts1 = nap.Ts(t=np.sort(np.random.uniform(0, 1000, 2000)), time_units="s")
>>> ts2 = nap.Ts(t=np.sort(np.random.uniform(0, 1000, 1000)), time_units="s")
>>> epochs = nap.IntervalSet(start=0, end=1000, time_units="s")
>>> ts_group = nap.TsGroup({0: ts1, 1: ts2}, time_support=epochs)
>>> isi_distribution = nap.compute_isi_distribution(data=ts_group, bins=10, epochs=epochs)
>>> isi_distribution
             0    1
0.322415  1474  477
0.966402   378  237
1.610388   100  140
2.254375    34   67
2.898362    12   39
3.542349     1   17
4.186335     0   12
4.830322     0    6
5.474309     0    2
6.118296     0    2