StateMonitor class

(Shortest import: from brian2 import StateMonitor)

class brian2.monitors.statemonitor.StateMonitor(*args, **kw)[source]

Bases: Group, CodeRunner

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.

Parameters:

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 : bool, sequence of ints

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

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

name : str, optional

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

codeobj_class : CodeObject, optional

The CodeObject class to create.

Notes

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.

Note that record=True only works in runtime mode for synaptic variables. This is because the actual array of indices has to be calculated and this is not possible in standalone mode, where the synapses have not been created yet at this stage. Consider using an explicit array of indices instead, i.e. something like record=np.arange(n_synapses).

Examples

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))
run(100*ms)
plot(M.t, M.V.T)
show()

Attributes

record

The array of recorded indices

record_variables

The variables to record

Methods

record_single_timestep()

Records a single time step.

reinit()

resize(new_size)

Details

record

The array of recorded indices

record_variables

The variables to record

record_single_timestep()[source]

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.

Notes

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

Examples

>>> 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)
>>> print(np.array_str(mon.v[:], precision=3))
[[ 1.     0.98   0.961  0.942  0.923]]
>>> print(mon.t[:])
[   0.  100.  200.  300.  400.] us
>>> print(np.array_str(G.v[:], precision=3))  # last value had not been recorded
[ 0.905]
>>> mon.record_single_timestep()
>>> print(mon.t[:])
[   0.  100.  200.  300.  400.  500.] us
>>> print(np.array_str(mon.v[:], precision=3))
[[ 1.     0.98   0.961  0.942  0.923  0.905]]
reinit()[source]
resize(new_size)[source]

Tutorials and examples using this