StateMonitor class

(Shortest import: from brian2 import StateMonitor)

class brian2.monitors.statemonitor.StateMonitor(source, variables, record=None, dt=None, clock=None, when='start', order=0, name='statemonitor*', codeobj_class=None)[source]


Record values of state variables during a run

To extract recorded values after a run, use the t attribute for the array of times at which values were recorded, and variable name attribute for the values. The values will have shape (len(indices), len(t)), where indices are the array indices which were recorded. When indexing the StateMonitor directly, the returned object can be used to get the recorded values for the specified indices, i.e. the indexing semantic refers to the indices in source, not to the relative indices of the recorded values. For example, when recording only neurons with even numbers, mon[[0, 2]].v will return the values for neurons 0 and 2, whereas mon.v[[0, 2]] will return the values for the first and third recorded neurons, i.e. for neurons 0 and 4.


source : Group

Which object to record values from.

variables : str, sequence of str, True

Which variables to record, or True to record all variables (note that this may use a great deal of memory).

record : None, False, True, sequence of ints, optional

Which indices to record, nothing is recorded for None or False, everything is recorded for True (warning: may use a great deal of memory), or a specified subset of indices. Defaults to None.

dt : Quantity, optional

The time step to be used for the monitor. Cannot be combined with the clock argument.

clock : Clock, optional

The update clock to be used. If neither a clock, nor the dt argument is specified, the clock of the source() will be used.

when : str, optional

At which point during a time step the values should be recorded. Defaults to 'start'.

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 0.

name : str, optional

A unique name for the object, otherwise will use'statemonitor_0', etc.

codeobj_class : CodeObject, optional

The CodeObject class to create.


Since this monitor by default records in the 'start' time slot, recordings of the membrane potential in integrate-and-fire models may look unexpected: the recorded membrane potential trace will never be above threshold in an integrate-and-fire model, because the reset statement will have been applied already. Set the when keyword to a different value if this is not what you want.


Record all variables, first 5 indices:

eqs = """
dV/dt = (2-V)/(10*ms) : 1
threshold = 'V>1'
reset = 'V = 0'
G = NeuronGroup(100, eqs, threshold=threshold, reset=reset)
G.V = rand(len(G))
M = StateMonitor(G, True, record=range(5))
plot(M.t, M.V.T)


record The array of recorded indices
record_variables The variables to record


record_single_timestep() Records a single time step.



The array of recorded indices


The variables to record


Records a single time step. Useful for recording the values at the end of the simulation – otherwise a StateMonitor will not record the last simulated values since its when attribute defaults to 'start', i.e. the last recording is at the beginning of the last time step.


This function will only work if the StateMonitor has been already run, but a run with a length of 0*ms does suffice.


>>> from brian2 import *
>>> G = NeuronGroup(1, 'dv/dt = -v/(5*ms) : 1')
>>> G.v = 1
>>> mon = StateMonitor(G, 'v', record=True)
>>> run(0.5*ms)
>>> mon.v
array([[ 1.        ,  0.98019867,  0.96078944,  0.94176453,  0.92311635]])
>>> mon.t[:]
array([   0.,  100.,  200.,  300.,  400.]) * usecond
>>> G.v[:]  # last value had not been recorded
array([ 0.90483742])
>>> mon.record_single_timestep()
>>> mon.t[:]
array([   0.,  100.,  200.,  300.,  400.,  500.]) * usecond
>>> mon.v[:]
array([[ 1.        ,  0.98019867,  0.96078944,  0.94176453,  0.92311635,