SpikeMonitor class

(Shortest import: from brian2 import SpikeMonitor)

class brian2.monitors.spikemonitor.SpikeMonitor(*args, **kw)[source]

Bases: 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.

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.

record : bool, optional

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

when : str, optional

When to record the events, by default records events in the same slot where the event is emitted. See Scheduling for possible values.

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]
>>> from brian2 import *
>>> G = NeuronGroup(2, '''counter1 : integer
...                       counter2 : integer
...                       max_value : integer''',
...                    threshold='counter1 >= max_value',
...                    reset='counter1 = 0')
>>> G.run_regularly('counter1 += 1; counter2 += 1')  
CodeRunner(...)
>>> G.max_value = [50, 100]
>>> mon = SpikeMonitor(G, variables='counter2')
>>> net = Network(G, mon)
>>> net.run(10*ms)
>>> print(mon.i[:])
[0 0 1]
>>> print(mon.counter2[:])
[ 50 100 100]

Attributes

count

The array of spike counts (length = size of target group)

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 neuron 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

count

The array of spike counts (length = size of target group)

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, '''counter1 : integer
...                       counter2 : integer
...                       max_value : integer''',
...                    threshold='counter1 >= max_value',
...                    reset='counter1 = 0')
>>> G.run_regularly('counter1 += 1; counter2 += 1')  
CodeRunner(...)
>>> G.max_value = [50, 100]
>>> mon = SpikeMonitor(G, variables='counter2')
>>> run(10*ms)
>>> all_values = mon.all_values()
>>> print(all_values['counter2'][0])
[ 50 100]
>>> print(all_values['t'][1])
[ 9.9] ms
spike_trains()[source]

Return a dictionary mapping neuron 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, '''counter1 : integer
...                       counter2 : integer
...                       max_value : integer''',
...                    threshold='counter1 >= max_value',
...                    reset='counter1 = 0')
>>> G.run_regularly('counter1 += 1; counter2 += 1')  
CodeRunner(...)
>>> G.max_value = [50, 100]
>>> mon = SpikeMonitor(G, variables='counter2')
>>> run(10*ms)
>>> counter2_values = mon.values('counter2')
>>> print(counter2_values[0])
[ 50 100]
>>> print(counter2_values[1])
[100]

Tutorials and examples using this