| 1 |
Monitors (Brian 1 --> 2 conversion) |
| 2 |
=================================== |
| 3 |
.. sidebar:: Brian 2 documentation |
| 4 |
|
| 5 |
For the main documentation about recording network activity, see the |
| 6 |
document :doc:`../../user/recording`. |
| 7 |
|
| 8 |
.. contents:: |
| 9 |
:local: |
| 10 |
:depth: 1 |
| 11 |
|
| 12 |
Monitoring spiking activity |
| 13 |
--------------------------- |
| 14 |
The main class to record spiking activity is `SpikeMonitor` which is created in |
| 15 |
the same way as in Brian 1. However, the internal storage and retrieval of |
| 16 |
spikes is different. In Brian 1, spikes were stored as a list of pairs |
| 17 |
``(i, t)``, the index and time of each spike. In Brian 2, spikes are stored as |
| 18 |
two arrays ``i`` and ``t``, storing the indices and times. You can access these |
| 19 |
arrays as attributes of the monitor, there's also a convenience attribute ``it`` |
| 20 |
that returns both at the same time. The following table shows how the spike |
| 21 |
indices and times can be retrieved in various forms in Brian 1 and Brian 2: |
| 22 |
|
| 23 |
+-----------------------------------------------+-------------------------------------------+ |
| 24 |
| Brian 1 | Brian 2 | |
| 25 |
+===============================================+===========================================+ |
| 26 |
+ .. code:: | .. code:: | |
| 27 |
+ | | |
| 28 |
+ mon = SpikeMonitor(group) | mon = SpikeMonitor(group) | |
| 29 |
+ #... do the run | #... do the run | |
| 30 |
+ list_of_pairs = mon.spikes | list_of_pairs = zip(*mon.it) | |
| 31 |
+ index_list, time_list = zip(*list_of_pairs) | index_list = list(mon.i) | |
| 32 |
+ index_array = array(index_list) | time_list = list(mon.t) | |
| 33 |
+ time_array = array(time_list) | index_array, time_array = mon.i, mon.t | |
| 34 |
+ # time_array is unitless in Brian 1 | # time_array has units in Brian 2 | |
| 35 |
+-----------------------------------------------+-------------------------------------------+ |
| 36 |
|
| 37 |
You can also access the spike times for individual neurons. In Brian 1, you |
| 38 |
could directly index the monitor which is no longer allowed in Brian 2. |
| 39 |
Instead, ask for a dictionary of spike times and index the returned dictionary: |
| 40 |
|
| 41 |
+-----------------------------------------------+-----------------------------------------------+ |
| 42 |
| Brian 1 | Brian 2 | |
| 43 |
+===============================================+===============================================+ |
| 44 |
+ .. code:: | .. code:: | |
| 45 |
+ | | |
| 46 |
+ # dictionary of spike times for each neuron:| # dictionary of spike times for each neuron:| |
| 47 |
+ spike_dict = mon.spiketimes | spike_dict = mon.spike_trains() | |
| 48 |
+ # all spikes for neuron 3: | # all spikes for neuron 3: | |
| 49 |
+ spikes_3 = spike_dict[3] # (no units) | spikes_3 = spike_dict[3] # with units | |
| 50 |
+ spikes_3 = mon[3] # alternative (no units) | | |
| 51 |
+ | | |
| 52 |
+-----------------------------------------------+-----------------------------------------------+ |
| 53 |
|
| 54 |
In Brian 2, `SpikeMonitor` also provides the functionality of the Brian 1 |
| 55 |
classes ``SpikeCounter`` and ``PopulationSpikeCounter``. If you are only |
| 56 |
interested in the counts and not in the individual spike events, use |
| 57 |
``record=False`` to save the memory of storing them: |
| 58 |
|
| 59 |
+-----------------------------------------------+-----------------------------------------------+ |
| 60 |
| Brian 1 | Brian 2 | |
| 61 |
+===============================================+===============================================+ |
| 62 |
+ .. code:: | .. code:: | |
| 63 |
+ | | |
| 64 |
+ counter = SpikeCounter(group) | counter = SpikeMonitor(group, record=False) | |
| 65 |
+ pop_counter = PopulationSpikeCounter(group) | | |
| 66 |
+ #... do the run | #... do the run | |
| 67 |
+ # Number of spikes for neuron 3: | # Number of spikes for neuron 3 | |
| 68 |
+ count_3 = counter[3] | count_3 = counter.count[3] | |
| 69 |
+ # Total number of spikes: | # Total number of spikes: | |
| 70 |
+ total_spikes = pop_counter.nspikes | total_spikes = counter.num_spikes | |
| 71 |
+ | | |
| 72 |
+-----------------------------------------------+-----------------------------------------------+ |
| 73 |
|
| 74 |
|
| 75 |
Currently Brian 2 provides no functionality to calculate statistics such as |
| 76 |
correlations or histograms online, there is no equivalent to the following |
| 77 |
classes that existed in Brian 1: ``AutoCorrelogram``, ``CoincidenceCounter``, |
| 78 |
``CoincidenceMatrixCounter``, ``ISIHistogramMonitor``, ``VanRossumMetric``. |
| 79 |
You will therefore have to be calculate the corresponding statistiacs manually |
| 80 |
after the simulation based on the information stored in the `SpikeMonitor`. If |
| 81 |
you use the default :ref:`runtime`, you can also create a new Python class that |
| 82 |
calculates the statistic online |
| 83 |
(see this `example from a Brian 2 tutorial <https://github.com/brian-team/brian-material/blob/master/2015-CNS-tutorial/04-advanced-brian2/coincidence_counter.ipynb>`_). |
| 84 |
|
| 85 |
|
| 86 |
Monitoring variables |
| 87 |
-------------------- |
| 88 |
Single variables are recorded with a `StateMonitor` in the same way as in |
| 89 |
Brian 1, but the times and variable values are accessed differently: |
| 90 |
|
| 91 |
+---------------------------------------+--------------------------------------+ |
| 92 |
| Brian 1 | Brian 2 | |
| 93 |
+=======================================+======================================+ |
| 94 |
+ .. code:: | .. code:: | |
| 95 |
+ | | |
| 96 |
+ mon = StateMonitor(group, 'v', | mon = StateMonitor(group, 'v', | |
| 97 |
+ record=True) | record=True) | |
| 98 |
+ # ... do the run | # ... do the run | |
| 99 |
+ # plot the trace of neuron 3: | # plot the trace of neuron 3: | |
| 100 |
+ plot(mon.times/ms, mon[3]/mV) | plot(mon.t/ms, mon[3].v/mV) | |
| 101 |
+ # plot the traces of all neurons: | # plot the traces of all neurons: | |
| 102 |
+ plot(mon.times/ms, mon.values.T/mV) | plot(mon.t/ms, mon.v.T/mV) | |
| 103 |
+ | | |
| 104 |
+---------------------------------------+--------------------------------------+ |
| 105 |
|
| 106 |
Further differences: |
| 107 |
|
| 108 |
* `StateMonitor` now records in the ``'start'`` scheduling slot by default. This |
| 109 |
leads to a more intuitive correspondence between the recorded times and the |
| 110 |
values: in Brian 1 (where `StateMonitor` recorded in the ``'end'`` slot) the |
| 111 |
recorded value at 0ms was not the initial value of the variable but the value |
| 112 |
after integrating it for a single time step. The disadvantage of this new |
| 113 |
default is that the very last value at the end of the last time step of a |
| 114 |
simulation is not recorded anymore. However, this value can be manually added |
| 115 |
to the monitor by calling `StateMonitor.record_single_timestep`. |
| 116 |
* To not record every time step, use the ``dt`` argument (as for all other |
| 117 |
classes) instead of specifying a number of ``timesteps``. |
| 118 |
* Using ``record=False`` does no longer provide mean and variance of the |
| 119 |
recorded variable. |
| 120 |
|
| 121 |
In contrast to Brian 1, `StateMonitor` can now record multiple variables and |
| 122 |
therefore replaces Brian 1's ``MultiStateMonitor``: |
| 123 |
|
| 124 |
+-----------------------------------------------------------+------------------------------------------------------+ |
| 125 |
| Brian 1 | Brian 2 | |
| 126 |
+===========================================================+======================================================+ |
| 127 |
+ .. code:: | .. code:: | |
| 128 |
+ | | |
| 129 |
+ mon = MultiStateMonitor(group, ['v', 'w'], | mon = StateMonitor(group, ['v', 'w'], | |
| 130 |
+ record=True) | record=True) | |
| 131 |
+ # ... do the run | # ... do the run | |
| 132 |
+ # plot the traces of v and w for neuron 3: | # plot the traces of v and w for neuron 3: | |
| 133 |
+ plot(mon['v'].times/ms, mon['v'][3]/mV) | plot(mon.t/ms, mon[3].v/mV) | |
| 134 |
+ plot(mon['w'].times/ms, mon['w'][3]/mV) | plot(mon.t/ms, mon[3].w/mV) | |
| 135 |
+ | | |
| 136 |
+-----------------------------------------------------------+------------------------------------------------------+ |
| 137 |
|
| 138 |
To record variable values at the times of spikes, Brian 2 no longer provides a |
| 139 |
separate class as Brian 1 did (``StateSpikeMonitor``). Instead, you can use |
| 140 |
`SpikeMonitor` to record additional variables (in addition to the neuron index |
| 141 |
and the spike time): |
| 142 |
|
| 143 |
+-----------------------------------------------------------+------------------------------------------------------+ |
| 144 |
| Brian 1 | Brian 2 | |
| 145 |
+===========================================================+======================================================+ |
| 146 |
+ .. code:: | .. code:: | |
| 147 |
+ | | |
| 148 |
+ # We assume that "group" has a varying threshold | # We assume that "group" has a varying threshold | |
| 149 |
+ mon = StateSpikeMonitor(group, 'v') | mon = SpikeMonitor(group, variables='v') | |
| 150 |
+ # ... do the run | # ... do the run | |
| 151 |
+ # plot the mean v at spike time for each neuron | # plot the mean v at spike time for each neuron | |
| 152 |
+ mean_values = [mean(mon.values('v', idx)) | values = mon.values('v') | |
| 153 |
+ for idx in range(len(group))] | mean_values = [mean(values[idx]) | |
| 154 |
+ | for idx in range(len(group))] | |
| 155 |
+ plot(mean_values/mV, 'o') | plot(mean_values/mV, 'o') | |
| 156 |
+ | | |
| 157 |
+-----------------------------------------------------------+------------------------------------------------------+ |
| 158 |
|
| 159 |
Note that there is no equivalent to ``StateHistogramMonitor``, you will have to |
| 160 |
calculate the histogram from the recorded values or write your own custom |
| 161 |
monitor class. |