Preprocessing

class blockinvgpfa.EventTimesToCounts(bin_size=0.02, t_stop=None, extrapolate_last_bin=False)[source]

Bins sequence of event times into event counts within evenly spaced time bins.

This class supports binning sequences of event times (e.g., spike trains) into a matrix that contain event counts within evenly spaced time bins (first bin starts at time 0s). It supports specifying the bin size, and multiple options for how the last event time is determined.

Parameters:
bin_sizefloat, default0.02 [s]

The width of each time bin in seconds.

t_stopfloat, optional, defaultNone

The largest time considered for binning. This time is assumed to be the same across all event time sequences. If t_stop doesn’t match a bin boundary, extrapolate_last_bin determins whether or not the last bin includes t_stop or not.

If not given, t_stop is set to the largest event time across all sequences in the provided data. If the data is a neo.SpikeTrain object, t_stop is set to X[0].t_stop.magnitude.

extrapolate_last_binboolean, optional, defaultFalse

In cases where t_stop does not match a bin boundary, this option determines whether the last bin includes t_stop. If False, the last bin ends before t_stop, and all events after this final bin are ignored. If True, the last bin includes t_stop, and event counts are up-scaled to account for the fact that t_stop happens before the end of the last time bin. For example, if t_stop falls into the middle of the last bin, all event counts that fall into the last bin are doubled. In this case, X_out of transform() returns is of type float as it might contain non-integer event counts for the last bin.

Methods

transform:

Transforms data from event times to binned event counts

Examples

>>> import numpy as np
>>> from gpfa import EventTimesToCounts
>>> bin_size = 0.1  # [s]
>>> t_stop = 0.8  # [s]
>>> X = [
...     [0, 0.1, 0.15, 0.4, 0.5, 0.6, 0.8],
...     [0.05, 0.3, 0.4, 0.55, 0.7]
...     ]
>>> ettc = EventTimesToCounts(
...                           bin_size=bin_size,
...                           t_stop=t_stop,
...                           extrapolate_last_bin=False
...                          )
>>> ettc.transform(X)
array([[1, 2, 0, 0, 1, 2, 0, 1],
       [1, 0, 1, 0, 1, 1, 1, 0]])
>>> ettc_extrapolate_last_bin = EventTimesToCounts(
...                             bin_size=bin_size,
...                             t_stop=t_stop,
...                             extrapolate_last_bin=True
...                          )
>>> ettc.transform(X)
array([[1, 2, 0, 0, 1, 2, 0, 1],
       [1, 0, 1, 0, 1, 1, 1, 0]])
>>> # Using defaults t_stop=None and extrapolate_last_bin=False
>>> ettc = EventTimesToCounts(bin_size)
>>> ettc.transform(X)
array([[1, 2, 0, 0, 1, 2, 0, 1],
       [1, 0, 1, 0, 1, 1, 1, 0]])
>>> t_stop2 = 0.88  # [s]
>>> ettc = EventTimesToCounts(
...                           bin_size=bin_size,
...                           t_stop=t_stop2,
...                           extrapolate_last_bin=False
...                          )
>>> ettc.transform(X)
array([[1, 2, 0, 0, 1, 2, 0, 1],
       [1, 0, 1, 0, 1, 1, 1, 0]])
>>> t_stop2 = 0.88  # [s]
>>> ettc_extrapolate_last_bin = EventTimesToCounts(
...                             bin_size=bin_size,
...                             t_stop=t_stop2,
...                             extrapolate_last_bin=True
...                          )
>>> ettc.transform(X)
array([[1.  , 2.  , 0.  , 0.  , 1.  , 2.  , 0.  , 0.  , 1.25],
       [1.  , 0.  , 1.  , 0.  , 1.  , 1.  , 1.  , 0.  , 0.  ]])

The following example only works if the Neo package is installed.

>>> import neo
>>> t_stop = 0.8  # [s]
>>> neoSpikeTrain = [
...     neo.SpikeTrain(X[0],units='sec', t_stop=t_stop),
...     neo.SpikeTrain(X[1], units='sec', t_stop=t_stop)
...     ]
>>> ettc = EventTimesToCounts(
                    bin_size=bin_size,
                    t_stop=None,
                    extrapolate_last_bin=False
                    )
>>> ettc.transform(neoSpikeTrain)
array([[1, 2, 0, 0, 1, 2, 0, 1],
       [1, 0, 1, 0, 1, 1, 1, 0]])
transform(X)[source]

Transforms data from event times to binned event counts

Parameters:
Xnumpy.array or neo.SpikeTrain

An array-like containing #sequences of event time sequences (usually sequences of float’s). Each element in X can contain a different number of event times. The are all assumed to share the same final time (i.e., t_stop).

Returns:
X_outnumpy.array

A numpy matrix of size #sequences x #bins, containing the binned event counts.