elephant.conversion.BinnedSpikeTrainView

class elephant.conversion.BinnedSpikeTrainView(t_start, t_stop, bin_size, units, sparse_matrix, tolerance=1e-08)[source]

A view of BinnedSpikeTrain.

This class is used to avoid deep copies in several functions of a binned spike train object like BinnedSpikeTrain.binarize(), BinnedSpikeTrain.time_slice(), etc.

Parameters:
t_start, t_stopfloat

Unit-less start and stop times that share the same units.

bin_sizefloat

Unit-less bin size that was used used in binning the sparse_matrix.

unitspq.Quantity

The units of input spike trains.

sparse_matrixscipy.sparse.csr_matrix

Binned sparse matrix.

tolerancefloat or None, optional

The tolerance property of the original BinnedSpikeTrain. Default: 1e-8

Warning

This class is an experimental feature.

Methods

__init__(t_start, t_stop, bin_size, units, …) Initialize self.
binarize([copy]) Clip the internal array (no.
copy() Copies the binned sparse matrix and returns a view.
get_num_of_spikes([axis]) Compute the number of binned spikes.
rescale(units) Inplace rescaling to the new quantity units.
time_slice([t_start, t_stop, copy]) Returns a view or a copied view of currently binned spike trains with (t_start, t_stop) time slice.
to_array([dtype]) Returns a dense matrix, calculated from the sparse matrix, with counted time points of spikes.
to_bool_array() Returns a matrix, in which the rows correspond to the spike trains and the columns correspond to the bins in the BinnedSpikeTrain.
to_sparse_array() Getter for sparse matrix with time points.
to_sparse_bool_array() Getter for boolean version of the sparse matrix, calculated from sparse matrix with counted time points.
to_spike_trains([spikes, as_array, …]) Generate spike trains from the binned spike train object.

Attributes

bin_centers Returns each center time point of all bins between t_start and t_stop points.
bin_edges Returns all time edges as a quantity array with n_bins bins.
bin_size Bin size quantity.
binsize Deprecated in favor of bin_size.
is_binary Returns True if the sparse matrix contains binary values only.
num_bins Deprecated in favor of n_bins.
shape The shape of the sparse matrix.
sparsity The sparsity of the sparse matrix computed as the no.
spike_indices A list of lists for each spike train (i.e., rows of the binned matrix), that in turn contains for each spike the index into the binned matrix where this spike enters.
t_start t_start quantity; spike times below this value have been ignored.
t_stop t_stop quantity; spike times above this value have been ignored.
__getitem__(item)

Returns a binned slice view of itself; t_start and t_stop will be set accordingly to the second slicing argument, if any.

Parameters:
itemint or slice or tuple

Spike train and bin index slicing, passed to self.sparse_matrix.

Returns:
BinnedSpikeTrainView

A slice of itself that carry the original data. Any changes to the returned binned sparse matrix will affect the original data.

__setitem__(key, value)

Changes the values of self.sparse_matrix according to key and value. A shortcut to self.sparse_matrix[key] = value.

Parameters:
keyint or list or tuple or slice

The binned sparse matrix keys (axes slice) to change.

valueint or list or tuple or slice

New values of the sparse matrix selection.

property bin_centers

Returns each center time point of all bins between t_start and t_stop points.

The center of each bin of all time steps between start and stop.

Returns:
bin_edgespq.Quantity

All center edges in interval (start, stop).

property bin_edges

Returns all time edges as a quantity array with n_bins bins.

The borders of all time steps between t_start and t_stop with a step bin_size. It is crucial for many analyses that all bins have the same size, so if t_stop - t_start is not divisible by bin_size, there will be some leftover time at the end (see https://github.com/NeuralEnsemble/elephant/issues/255). The length of the returned array should match n_bins.

Returns:
bin_edgespq.Quantity

All edges in interval [t_start, t_stop] with n_bins bins are returned as a quantity array.

property bin_size

Bin size quantity.

binarize(copy=True)

Clip the internal array (no. of spikes in a bin) to 0 (no spikes) or 1 (at least one spike) values only.

Parameters:
copybool, optional

If True, a shallow copy - a view of BinnedSpikeTrain - is returned with the data array filled with zeros and ones. Otherwise, the binarization (clipping) is done in-place. A shallow copy means that indices and indptr of a sparse matrix is shared with the original sparse matrix. Only the data is copied. If you want to perform a deep copy, call BinnedSpikeTrain.copy() prior to binarizing. Default: True

Returns:
bstBinnedSpikeTrain or BinnedSpikeTrainView

A (view of) BinnedSpikeTrain with the sparse matrix data clipped to zeros and ones.

property binsize

Deprecated in favor of bin_size.

copy()

Copies the binned sparse matrix and returns a view. Any changes to the copied object won’t affect the original object.

Returns:
BinnedSpikeTrainView

A copied view of itself.

get_num_of_spikes(axis=None)

Compute the number of binned spikes.

Parameters:
axisint, optional

If None, compute the total num. of spikes. Otherwise, compute num. of spikes along axis. If axis is 1, compute num. of spikes per spike train (row). Default is None.

Returns:
n_spikes_per_rowint or np.ndarray

The number of binned spikes.

property is_binary

Returns True if the sparse matrix contains binary values only. Beware, that the function does not know if the input was binary because e.g to_bool_array() was used before or if the input is just sparse (i.e. only one spike per bin at maximum).

Returns:
bool

True for binary input, False otherwise.

property num_bins

Deprecated in favor of n_bins.

rescale(units)

Inplace rescaling to the new quantity units.

Parameters:
unitspq.Quantity or str

New quantity units.

Raises:
TypeError

If the input units are not quantities.

property shape

The shape of the sparse matrix.

property sparsity

The sparsity of the sparse matrix computed as the no. of nonzero elements divided by the matrix size.

Returns:
float
property spike_indices

A list of lists for each spike train (i.e., rows of the binned matrix), that in turn contains for each spike the index into the binned matrix where this spike enters.

In contrast to self.sparse_matrix.nonzero(), this function will report two spikes falling in the same bin as two entries.

Examples

>>> import elephant.conversion as conv
>>> import neo as n
>>> import quantities as pq
>>> st = n.SpikeTrain([0.5, 0.7, 1.2, 3.1, 4.3, 5.5, 6.7] * pq.s,
...                   t_stop=10.0 * pq.s)
>>> x = conv.BinnedSpikeTrain(st, n_bins=10, bin_size=1 * pq.s,
...                           t_start=0 * pq.s)
>>> print(x.spike_indices)
[[0, 0, 1, 3, 4, 5, 6]]
>>> print(x.sparse_matrix.nonzero()[1])
[0 1 3 4 5 6]
>>> print(x.to_array())
[[2, 1, 0, 1, 1, 1, 1, 0, 0, 0]]
property t_start

t_start quantity; spike times below this value have been ignored.

property t_stop

t_stop quantity; spike times above this value have been ignored.

time_slice(t_start=None, t_stop=None, copy=False)

Returns a view or a copied view of currently binned spike trains with (t_start, t_stop) time slice. Only valid (fully overlapping) bins are sliced.

Parameters:
t_start, t_stoppq.Quantity or None, optional

Start and stop times or Nones. Default: None

copybool, optional

Copy the sparse matrix or not. Default: False

Returns:
BinnedSpikeTrainView

A time slice of itself.

to_array(dtype=None)

Returns a dense matrix, calculated from the sparse matrix, with counted time points of spikes. The rows correspond to spike trains and the columns correspond to bins in a BinnedSpikeTrain. Entries contain the count of spikes that occurred in the given bin of the given spike train.

Returns:
matrixnp.ndarray

Matrix with spike counts. Columns represent the index positions of the binned spikes and rows represent the spike trains.

See also

scipy.sparse.csr_matrix
scipy.sparse.csr_matrix.toarray

Examples

>>> import elephant.conversion as conv
>>> import neo as n
>>> import quantities as pq
>>> a = n.SpikeTrain([0.5, 0.7, 1.2, 3.1, 4.3, 5.5, 6.7] * pq.s,
...                  t_stop=10.0 * pq.s)
>>> x = conv.BinnedSpikeTrain(a, n_bins=10, bin_size=1 * pq.s,
...                           t_start=0 * pq.s)
>>> print(x.to_array())
[[2 1 0 1 1 1 1 0 0 0]]
to_bool_array()

Returns a matrix, in which the rows correspond to the spike trains and the columns correspond to the bins in the BinnedSpikeTrain. True indicates a spike in given bin of given spike train and False indicates lack of spikes.

Returns:
numpy.ndarray

Returns a dense matrix representation of the sparse matrix, with True indicating a spike and False indicating a no-spike. The columns represent the index position of the bins and rows represent the number of spike trains.

See also

scipy.sparse.csr_matrix
scipy.sparse.csr_matrix.toarray

Examples

>>> import elephant.conversion as conv
>>> import neo as n
>>> import quantities as pq
>>> a = n.SpikeTrain([0.5, 0.7, 1.2, 3.1, 4.3, 5.5, 6.7] * pq.s,
...                  t_stop=10.0 * pq.s)
>>> x = conv.BinnedSpikeTrain(a, n_bins=10, bin_size=1 * pq.s,
...                           t_start=0 * pq.s)
>>> print(x.to_bool_array())
[[ True  True False  True  True  True  True False False False]]
to_sparse_array()

Getter for sparse matrix with time points. Deprecated in favor of sparse_matrix.

Returns:
scipy.sparse.csr_matrix or scipy.sparse.csc_matrix

Sparse matrix, version with spike counts.

See also

scipy.sparse.csr_matrix
to_array
to_sparse_bool_array()

Getter for boolean version of the sparse matrix, calculated from sparse matrix with counted time points.

Returns:
scipy.sparse.csr_matrix or scipy.sparse.csc_matrix

Sparse matrix, binary, boolean version.

See also

scipy.sparse.csr_matrix
to_bool_array
to_spike_trains(spikes='random', as_array=False, annotate_bins=False)

Generate spike trains from the binned spike train object. This function is inverse to binning such that

BinnedSpikeTrain(binned_st.to_spike_trains()) == binned_st

The object bin size is stored in resulting spiketrain.annotations['bin_size'].

Parameters:
spikes{“left”, “center”, “random”}, optional

Specifies how to generate spikes inside bins.

  • “left”: align spikes from left to right to have equal inter-

spike interval;

  • “center”: align spikes around center to have equal inter-spike

interval;

  • “random”: generate spikes from a homogenous Poisson process;

it’s the fastest mode.

Default: “random”

as_arraybool, optional

If True, numpy arrays are returned; otherwise, wrap the arrays in neo.SpikeTrain. Default: False

annotate_binsbool, optional

If as_array is False, this flag allows to include the bin index in resulting spiketrain.array_annotations['bins']. Default: False

Returns:
spiketrainslist of neo.SpikeTrain

A list of spike trains - one possible realisation of spiketrains that could have been used as the input to BinnedSpikeTrain.