Monitors

Monitors are used to record properties of your network. The two most important are SpikeMonitor which records spikes, and StateMonitor which records values of state variables. These objects are just added to the network like a NeuronGroup or Connection.

Implementation note: monitors that record spikes are classes derived from Connection, and overwrite the propagate method to store spikes. If you want to write your own custom spike monitors, you can do the same (or just use SpikeMonitor with a custom function). Monitors that record values are classes derived from NetworkOperation and implement the __call__ method to store values each time the network updates. Custom state monitors are most easily written by just writing your own network operation using the network_operation decorator.

class brian.SpikeMonitor(source, record=True, delay=0, function=None)

Counts or records spikes from a NeuronGroup

Initialised as one of:

SpikeMonitor(source(,record=True))
SpikeMonitor(source,function=function)

Where:

source
A NeuronGroup to record from
record
True or False to record all the spikes or just summary statistics.
function
A function f(spikes) which is passed the array of neuron numbers that have fired called each step, to define custom spike monitoring.

Has attributes:

nspikes
The number of recorded spikes
spikes
A time ordered list of pairs (i,t) where neuron i fired at time t.
spiketimes
A dictionary with keys the indices of the neurons, and values an array of the spike times of that neuron. For example, t=M.spiketimes[3] gives the spike times for neuron 3.
it
Return a tuple (i, t) where i and t are the arrays of spike indices and corresponding spike times (int and float).

For M a SpikeMonitor, you can also write:

M[i]
An array of the spike times of neuron i.

Notes:

SpikeMonitor is subclassed from Connection. To define a custom monitor, either define a subclass and rewrite the propagate method, or pass the monitoring function as an argument (function=myfunction, with def myfunction(spikes):...)

class brian.SpikeCounter(source)

Counts spikes from a NeuronGroup

Initialised as:

SpikeCounter(source)

With argument:

source
A NeuronGroup to record from

Has two attributes:

nspikes
The number of recorded spikes
count
An array of spike counts for each neuron

For a SpikeCounter M you can also write M[i] for the number of spikes counted for neuron i.

class brian.PopulationSpikeCounter(source, delay=0)

Counts spikes from a NeuronGroup

Initialised as:

PopulationSpikeCounter(source)

With argument:

source
A NeuronGroup to record from

Has one attribute:

nspikes
The number of recorded spikes
class brian.StateSpikeMonitor(source, var)

Counts or records spikes and state variables at spike times from a NeuronGroup

Initialised as:

StateSpikeMonitor(source, var)

Where:

source
A NeuronGroup to record from
var
The variable name or number to record from, or a tuple of variable names or numbers if you want to record multiple variables for each spike.

Has two attributes:

nspikes

The number of recorded spikes

spikes

A time ordered list of tuples (i,t,v) where neuron i fired at time t and the specified variable had value v. If you specify multiple variables, each tuple will be of the form (i,t,v0,v1,v2,...) where the vi are the values corresponding in order to the variables you specified in the var keyword.

And two methods:

times(i=None)

Returns an array of the spike times for the whole monitored group, or just for neuron i if specified.

values(var, i=None)

Returns an array of the values of variable var for the whole monitored group, or just for neuron i if specified.

class brian.StateMonitor(P, varname, clock=None, record=False, timestep=1, when='end')

Records the values of a state variable from a NeuronGroup.

Initialise as:

StateMonitor(P,varname(,record=False)
    (,when='end)(,timestep=1)(,clock=clock))

Where:

P
The group to be recorded from
varname
The state variable name or number to be recorded
record
What to record. The default value is False and the monitor will only record summary statistics for the variable. You can choose record=integer to record every value of the neuron with that number, record=``list of integers to record every value of each of those neurons, or ``record=True to record every value of every neuron (although beware that this may use a lot of memory).
when
When the recording should be made in the network update, possible values are any of the strings: 'start', 'before_groups', 'after_groups', 'before_connections', 'after_connections', 'before_resets', 'after_resets', 'end' (in order of when they are run).
timestep
A recording will be made each timestep clock updates (so timestep should be an integer).
clock
A clock for the update schedule, use this if you have specified a clock other than the default one in your network, or to update at a lower frequency than the update cycle. Note though that if the clock here is different from the main clock, the when parameter will not be taken into account, as network updates are done clock by clock. Use the timestep parameter if you need recordings to be made at a precise point in the network update step.

The StateMonitor object has the following properties:

times
The times at which recordings were made
mean
The mean value of the state variable for every neuron in the group (not just the ones specified in the record keyword)
var
The unbiased estimate of the variances, as in mean
std
The square root of var, as in mean
values
A 2D array of the values of all the recorded neurons, each row is a single neuron’s values.

In addition, if M` is a StateMonitor object, you write:

M[i]

for the recorded values of neuron i (if it was specified with the record keyword). It returns a numpy array.

Methods:

plot([indices=None[, cmap=None[, refresh=None[, showlast=None[, redraw=True]]]]])

Plots the recorded values using pylab. You can specify an index or list of indices, otherwise all the recorded values will be plotted. The graph plotted will have legends of the form name[i] for name the variable name, and i the neuron index. If cmap is specified then the colours will be set according to the matplotlib colormap cmap. refresh specifies how often (in simulation time) you would like the plot to refresh. Note that this will only work if pylab is in interactive mode, to ensure this call the pylab ion() command. If you are using the refresh option, showlast specifies a fixed time window to display (e.g. the last 100ms). If you are using more than one realtime monitor, only one of them needs to issue a redraw command, therefore set redraw=False for all but one of them.

Note that with some IDEs, interactive plotting will not work with the default matplotlib backend, try doing something like this at the beginning of your script (before importing brian):

import matplotlib
matplotlib.use('WXAgg')

You may need to experiment, try WXAgg, GTKAgg, QTAgg, TkAgg.

insert_spikes(spikemonitor[, value=0])

Inserts spikes into recorded traces (for plotting). State values at spike times are replaced with the given value (peak value of spike).

class brian.MultiStateMonitor(G, vars=None, clock=None, **kwds)

Monitors multiple state variables of a group

This class is a container for multiple StateMonitor objects, one for each variable in the group. You can retrieve individual StateMonitor objects using M[name] or retrieve the recorded values using M[name, i] for neuron i.

Initialised with a group G and a list of variables vars. If vars is omitted then all the variables of G will be recorded. Any additional keyword argument used to initialise the object will be passed to the individual StateMonitor objects (e.g. the when keyword).

Methods:

items(), iteritems()
Returns the pairs (var, mon)
plot([indices[, cmap]])
Plots all the monitors (note that real-time plotting is not supported for this class).

Attributes:

vars
The list of variables recorded.
times
The times at which recordings were made.
monitors
The dictionary of monitors indexed by variable name.

Usage:

G = NeuronGroup(N, eqs, ...)
M = MultiStateMonitor(G, record=True)
...
run(...)
...
plot(M['V'].times, M['V'][0])
figure()
for name, m in M.iteritems():
    plot(m.times, m[0], label=name)
legend()
show()
class brian.RecentStateMonitor(P, varname, duration=5.0 * msecond, clock=None, record=True, timestep=1, when='end')

StateMonitor that records only the most recent fixed amount of time.

Works in the same way as a StateMonitor except that it has one additional initialiser keyword duration which gives the length of time to record values for, the record keyword defaults to True instead of False, and there are some different or additional attributes:

values, values_, times, times_
These will now return at most the most recent values over an interval of maximum time duration. These arrays are copies, so for faster access use unsorted_values, etc.
unsorted_values, unsorted_values_, unsorted_times, unsorted_times_
The raw versions of the data, the associated times may not be in sorted order and if duration hasn’t passed, not all the values will be meaningful.
current_time_index
Says which time index the next values to be recorded will be stored in, varies from 0 to M-1.
has_looped
Whether or not the current_time_index has looped from M back to 0 - can be used to tell whether or not every value in the unsorted_values array is meaningful or not (they will only all be meaningful when has_looped==True, i.e. after time duration).

The __getitem__ method also returns values in sorted order.

To plot, do something like:

plot(M.times, M.values[:, i])
class brian.AERSpikeMonitor(source, filename, record=False, delay=0)

Records spikes to an AER file

Initialised as:

FileSpikeMonitor(source, filename[, record=False])

Does everything that a SpikeMonitor does except ONLY records the spikes to the named file in AER format, during the simulation. These spikes can then be reloaded via the io.load_aer function and used later into a SpikeGeneratorGroup.

It is about three times faster than the FileSpikeMonitor and creates smaller files. On the other hand those files are not human-readable because they are in binary format.

Has one additional method:

close()
Closes the file manually (will happen automatically when the program ends).
class brian.FileSpikeMonitor(source, filename, record=False, delay=0)

Records spikes to a file

Initialised as:

FileSpikeMonitor(source, filename[, record=False])

Does everything that a SpikeMonitor does except also records the spikes to the named file. note that spikes are recorded as an ASCII file of lines each of the form:

i, t

Where i is the neuron that fired, and t is the time in seconds.

Has one additional method:

close()
Closes the file manually (will happen automatically when the program ends).
class brian.ISIHistogramMonitor(source, bins, delay=0)

Records the interspike interval histograms of a group.

Initialised as:

ISIHistogramMonitor(source, bins)
source
The source group to record from.
bins
The lower bounds for each bin, so that e.g. bins = [0*ms, 10*ms, 20*ms] would correspond to bins with intervals 0-10ms, 10-20ms and 20+ms.

Has properties:

bins
The bins array passed at initialisation.
count
An array of length len(bins) counting how many ISIs were in each bin.

This object can be passed directly to the plotting function hist_plot().

class brian.PopulationRateMonitor(source, bin=None)

Monitors and stores the (time-varying) population rate

Initialised as:

PopulationRateMonitor(source,bin)

Records the average activity of the group for every bin.

Properties:

rate, rate_
An array of the rates in Hz.
times, times_
The times of the bins.
bin
The duration of a bin (in second).
class brian.VanRossumMetric(source, tau=2.0 * msecond)

van Rossum spike train metric. From M. van Rossum (2001): A novel spike distance (Neural Computation).

Compute the van Rossum distance between every spike train from the source population.

Arguments:

source
The group to compute the distances for.
tau
Time constant of the kernel (low pass filter).

Has one attribute:

distance
A square symmetric matrix containing the distances.
class brian.CoincidenceCounter(source, data, spiketimes_offset=None, spikedelays=None, coincidence_count_algorithm='exclusive', onset=None, delta=4.0 * msecond)

Coincidence counter class.

Counts the number of coincidences between the spikes of the neurons in the network (model spikes), and some user-specified data spike trains (target spikes). This number is defined as the number of target spikes such that there is at least one model spike within +- delta, where delta is the half-width of the time window.

Initialised as:

cc = CoincidenceCounter(source, data, delta = 4*ms)

with the following arguments:

source
A NeuronGroup object which neurons are being monitored.
data

The list of spike times. Several spike trains can be passed in the following way. Define a single 1D array data which contains all the target spike times one after the other. Now define an array spiketimes_offset of integers so that neuron i should be linked to target train: data[spiketimes_offset[i]], data[spiketimes_offset[i]+1], etc.

It is essential that each spike train with the spiketimes array should begin with a spike at a large negative time (e.g. -1*second) and end with a spike that is a long time after the duration of the run (e.g. duration+1*second).

delta=4*ms
The half-width of the time window for the coincidence counting algorithm.
spiketimes_offset
A 1D array, spiketimes_offset[i] is the index of the first spike of the target train associated to neuron i.
spikedelays
A 1D array with spike delays for each neuron. All spikes from the target train associated to neuron i are shifted by spikedelays[i].
coincidence_count_algorithm
If set to exclusive, the algorithm cannot count more than one coincidence for each model spike. If set to inclusive, the algorithm can count several coincidences for a single model spike.
onset
A scalar value in seconds giving the start of the counting: no coincidences are counted before onset.

Has three attributes:

coincidences
The number of coincidences for each neuron of the NeuronGroup. coincidences[i] is the number of coincidences for neuron i.
model_length
The number of spikes for each neuron. model_length[i] is the spike count for neuron i.
target_length
The number of spikes in the target spike train associated to each neuron.
class brian.StateHistogramMonitor(group, varname, range, period=1.0 * msecond, nbins=20)

Records the histogram of a state variable from a NeuronGroup.

Initialise as:

StateHistogramMonitor(P,varname,range(,period=1*ms)(,nbins=20))

Where:

P
The group to be recorded from
varname
The state variable name or number to be recorded
range
The minimum and maximum values for the state variable. A 2-tuple of floats.
period
When to record.
nbins
Number of bins for the histogram.

The StateHistogramMonitor object has the following properties:

mean
The mean value of the state variable for every neuron in the group
var
The unbiased estimate of the variances, as in mean
std
The square root of var, as in mean
hist
A 2D array of the histogram values of all the neurons, each row is a single neuron’s histogram.
bins
A 1D array of the bin centers used to compute the histogram
bin_edges
A 1D array of the bin edges used to compute the histogram

In addition, if M` is a StateHistogramMonitor object, you write:

M[i]

for the histogram of neuron i.

Project Versions

Previous topic

Network

Next topic

Plotting

This Page