SpikeMonitor class

(Shortest import: from brian2 import SpikeMonitor)

class brian2.monitors.spikemonitor.SpikeMonitor(source, variables=None, when=None, order=None, name='spikemonitor*', codeobj_class=None)[source]

Bases: brian2.monitors.spikemonitor.EventMonitor

Record spikes from a NeuronGroup or other spike source.

The recorded spikes can be accessed in various ways (see Examples below): the attributes i and t store all the indices and spike times, respectively. Alternatively, you can get a dictionary mapping neuron indices to spike trains, by calling the spike_trains method. If you record additional variables with the variables argument, these variables can be accessed by their name (see Examples).

Parameters:

source : (NeuronGroup, SpikeSource)

The source of spikes to record.

record : bool

Whether or not to record each spike in i and t (the count will always be recorded).

variables : str or sequence of str, optional

Which variables to record at the time of the spike (in addition to the index of the neuron). Can be the name of a variable or a list of names.

when : str, optional

When to record the events, by default records events in the same slot where the event is emitted.

order : int, optional

The priority of of this group for operations occurring at the same time step and in the same scheduling slot. Defaults to the order where the event is emitted + 1, i.e. it will be recorded directly afterwards.

name : str, optional

A unique name for the object, otherwise will use source.name+'_spikemonitor_0', etc.

codeobj_class : class, optional

The CodeObject class to run code with.

Examples

>>> from brian2 import *
>>> spikes = SpikeGeneratorGroup(3, [0, 1, 2], [0, 1, 2]*ms)
>>> spike_mon = SpikeMonitor(spikes)
>>> net = Network(spikes, spike_mon)
>>> net.run(3*ms)
>>> print(spike_mon.i[:])
[0 1 2]
>>> print(spike_mon.t[:])
[ 0.  1.  2.] ms
>>> print(spike_mon.t_[:])
[ 0.     0.001  0.002]
>>> G = NeuronGroup(1, """dv/dt = (1 - v)/(10*ms) : 1
...                       dv_th/dt = (0.5 - v_th)/(20*ms) : 1""",
...                 threshold='v>v_th',
...                 reset='v = 0; v_th += 0.1')
>>> crossings = SpikeMonitor(G, variables='v', name='crossings')
>>> net = Network(G, crossings)
>>> net.run(10*ms)
>>> crossings.t
<crossings.t: array([ 0. ,  1.4,  4.6,  9.7]) * msecond>
>>> crossings.v
<crossings.v: array([ 0.00995017,  0.13064176,  0.27385096,  0.39950442])>

Attributes

num_spikes Returns the total number of recorded spikes.

Methods

all_values() Return a dictionary mapping recorded variable names (including t) to a dictionary mapping neuron indices to arrays of variable values at the time of the spikes (sorted by time).
spike_trains() Return a dictionary mapping spike indices to arrays of spike times.
values(var) Return a dictionary mapping neuron indices to arrays of variable values at the time of the spikes (sorted by time).

Details

num_spikes

Returns the total number of recorded spikes.

all_values()[source]

Return a dictionary mapping recorded variable names (including t) to a dictionary mapping neuron indices to arrays of variable values at the time of the spikes (sorted by time). This is equivalent to (but more efficient than) calling values for each variable and storing the result in a dictionary.

Returns:

all_values : dict

Dictionary mapping variable names to dictionaries which themselves are mapping neuron indicies to arrays of variable values at the time of the spikes.

Examples

>>> from brian2 import *
>>> G = NeuronGroup(2, """dv/dt = 100*Hz : 1
...                       v_th : 1""", threshold='v>v_th', reset='v=0')
>>> G.v_th = [0.5, 1]
>>> mon = SpikeMonitor(G, variables='v')
>>> run(20*ms)
>>> all_values = mon.all_values()
>>> all_values['t'][0]
array([  4.9,   9.9,  14.9,  19.9]) * msecond
>>> all_values['v'][0]
array([ 0.5,  0.5,  0.5,  0.5])
spike_trains()[source]

Return a dictionary mapping spike indices to arrays of spike times.

Returns:

spike_trains : dict

Dictionary that stores an array with the spike times for each neuron index.

Examples

>>> from brian2 import *
>>> spikes = SpikeGeneratorGroup(3, [0, 1, 2], [0, 1, 2]*ms)
>>> spike_mon = SpikeMonitor(spikes)
>>> run(3*ms)
>>> spike_trains = spike_mon.spike_trains()
>>> spike_trains[1]
array([ 1.]) * msecond
values(var)[source]

Return a dictionary mapping neuron indices to arrays of variable values at the time of the spikes (sorted by time).

Parameters:

var : str

The name of the variable.

Returns:

values : dict

Dictionary mapping each neuron index to an array of variable values at the time of the spikes.

Examples

>>> from brian2 import *
>>> G = NeuronGroup(2, """dv/dt = 100*Hz : 1
...                       v_th : 1""", threshold='v>v_th', reset='v=0')
>>> G.v_th = [0.5, 1]
>>> mon = SpikeMonitor(G, variables='v')
>>> run(20*ms)
>>> v_values = mon.values('v')
>>> v_values[0]
array([ 0.5,  0.5,  0.5,  0.5])
>>> v_values[1]
array([ 1.,  1.])