| 1 |
Custom events |
| 2 |
============= |
| 3 |
|
| 4 |
Overview |
| 5 |
-------- |
| 6 |
|
| 7 |
In most simulations, a `NeuronGroup` defines a threshold on its membrane |
| 8 |
potential that triggers a spike event. This event can be monitored by a |
| 9 |
`SpikeMonitor`, it is used in synaptic interactions, and in integrate-and-fire |
| 10 |
models it also leads to the execution of one or more reset statements. |
| 11 |
|
| 12 |
Sometimes, it can be useful to define additional events, e.g. when an ion |
| 13 |
concentration in the cell crosses a certain threshold. This can be done |
| 14 |
with the custom events system in Brian, which is illustrated in this diagram. |
| 15 |
|
| 16 |
.. image:: custom_events.* |
| 17 |
|
| 18 |
You can see in this diagram that the source `NeuronGroup` has four types |
| 19 |
of events, called ``spike``, ``evt_other``, ``evt_mon`` and ``evt_run``. |
| 20 |
The event ``spike`` is the default event. It is triggered when you |
| 21 |
you include ``threshold='...'`` in a `NeuronGroup`, and has two |
| 22 |
potential effects. Firstly, when the event is triggered it causes the |
| 23 |
reset code to run, specified by ``reset='...'``. Secondly, if there |
| 24 |
are `Synapses` connected, it causes the ``on_pre`` on ``on_post`` |
| 25 |
code to run (depending if the `NeuronGroup` is presynaptic or |
| 26 |
postsynaptic for those `Synapses`). |
| 27 |
|
| 28 |
In the diagram though, we have three additional event types. We've |
| 29 |
included several event types here to make it clearer, but you could |
| 30 |
use the same event for different purposes. Let's start |
| 31 |
with the first one, ``evt_other``. To understand this, we need to look at |
| 32 |
the `Synapses` object in a bit more detail. A `Synapses` object has |
| 33 |
multiple *pathways* associated to it. By default, there are just two, |
| 34 |
called ``pre`` and ``post``. The ``pre`` pathway is activated by |
| 35 |
presynaptic spikes, and the ``post`` pathway by postsynaptic spikes. |
| 36 |
Specifically, the ``spike`` event on the presynaptic `NeuronGroup` triggers |
| 37 |
the ``pre`` pathway, and the ``spike`` event on the postsynaptic |
| 38 |
`NeuronGroup` triggers the ``post`` pathway. In the example in the diagram, |
| 39 |
we have created a new pathway called ``other``, and the ``evt_other`` |
| 40 |
event in the presynaptic `NeuronGroup` triggers this pathway. Note that |
| 41 |
we can arrange this however we want. We could have ``spike`` trigger the |
| 42 |
``other`` pathway if we wanted to, or allow it to trigger both the |
| 43 |
``pre`` and ``other`` pathways. We could also allow ``evt_other`` to |
| 44 |
trigger the ``pre`` pathway. See below for details on the syntax for this. |
| 45 |
|
| 46 |
The third type of event in the example is named ``evt_mon`` and this |
| 47 |
is connected to an `EventMonitor` which works exactly the same way |
| 48 |
as `SpikeMonitor` (which is just an `EventMonitor` attached by default |
| 49 |
to the event ``spike``). |
| 50 |
|
| 51 |
Finally, the fourth type of event in the example is named ``evt_run``, |
| 52 |
and this causes some code to be run in the `NeuronGroup` triggered by |
| 53 |
the event. To add this code, we call `NeuronGroup.run_on_event`. So, |
| 54 |
when you set ``reset='...'``, this is equivalent to calling |
| 55 |
`NeuronGroup.run_on_event` with the ``spike`` event. |
| 56 |
|
| 57 |
Details |
| 58 |
------- |
| 59 |
|
| 60 |
Defining an event |
| 61 |
~~~~~~~~~~~~~~~~~ |
| 62 |
|
| 63 |
This can be done with |
| 64 |
the ``events`` keyword in the `NeuronGroup` initializer:: |
| 65 |
|
| 66 |
group = NeuronGroup(N, '...', threshold='...', reset='...', |
| 67 |
events={'custom_event': 'x > x_th'}) |
| 68 |
|
| 69 |
In this example, we define an event with the name ``custom_event`` that is |
| 70 |
triggered when the ``x`` variable crosses the threshold ``x_th``. Note |
| 71 |
that you can define any number of custom events. Each event is defined |
| 72 |
by its name as the key, and its condition as the value of the |
| 73 |
dictionary. |
| 74 |
|
| 75 |
Recording events |
| 76 |
~~~~~~~~~~~~~~~~ |
| 77 |
|
| 78 |
Custom events can be recorded with an `EventMonitor`:: |
| 79 |
|
| 80 |
event_mon = EventMonitor(group, 'custom_event') |
| 81 |
|
| 82 |
Such an `EventMonitor` can be used in the same way as a `SpikeMonitor` -- in |
| 83 |
fact, creating the `SpikeMonitor` is basically identical to recording the |
| 84 |
``spike`` event with an `EventMonitor`. An `EventMonitor` is not limited to |
| 85 |
record the event time/neuron index, it can also record other variables of the |
| 86 |
model at the time of the event:: |
| 87 |
|
| 88 |
event_mon = EventMonitor(group, 'custom_event', variables['var1', 'var2']) |
| 89 |
|
| 90 |
Triggering `NeuronGroup` code |
| 91 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 92 |
|
| 93 |
If the event should trigger a series of statements (i.e. the equivalent of |
| 94 |
``reset`` statements), this can be added by calling `~NeuronGroup.run_on_event`:: |
| 95 |
|
| 96 |
group.run_on_event('custom_event', 'x=0') |
| 97 |
|
| 98 |
Triggering synaptic pathways |
| 99 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 100 |
|
| 101 |
When neurons are connected by `Synapses`, the ``pre`` and ``post`` pathways |
| 102 |
are triggered by ``spike`` events on the presynaptic and postsynaptic `NeuronGroup` |
| 103 |
by default. It is possible to change which pathway is triggered by which event by |
| 104 |
providing an ``on_event`` keyword that either specifies which event to use for all |
| 105 |
pathways, or a specific event for each pathway (where non-specified pathways use |
| 106 |
the default ``spike`` event):: |
| 107 |
|
| 108 |
synapse_1 = Synapses(group, another_group, '...', on_pre='...', on_event='custom_event') |
| 109 |
|
| 110 |
The code above causes all pathways to be triggered by an event named ``custom_event`` |
| 111 |
instead of the default ``spike``. |
| 112 |
|
| 113 |
:: |
| 114 |
|
| 115 |
synapse_2 = Synapses(group, another_group, '...', on_pre='...', on_post='...', |
| 116 |
on_event={'pre': 'custom_event'}) |
| 117 |
|
| 118 |
In the code above, only the ``pre`` pathway is triggered by the ``custom_event`` |
| 119 |
event. |
| 120 |
|
| 121 |
We can also create new pathways and have them be triggered by custom events. |
| 122 |
For example:: |
| 123 |
|
| 124 |
synapse_3 = Synapses(group, another_group, '...', |
| 125 |
on_pre={'pre': '....', |
| 126 |
'custom_pathway': '...'}, |
| 127 |
on_event={'pre': 'spike', |
| 128 |
'custom_pathway': 'custom_event'}) |
| 129 |
|
| 130 |
In this code, the default ``pre`` pathway is still triggered by the ``spike`` |
| 131 |
event, but there is a new pathway called ``custom_pathway`` that is triggered |
| 132 |
by the ``custom_event`` event. |
| 133 |
|
| 134 |
Scheduling |
| 135 |
~~~~~~~~~~ |
| 136 |
By default, custom events are checked after the spiking threshold (in the |
| 137 |
``after_thresholds`` slots) and statements are executed after the reset (in |
| 138 |
the ``after_resets`` slots). The slot for the execution of custom |
| 139 |
event-triggered statements can be changed when it is added with the usual |
| 140 |
``when`` and ``order`` keyword arguments (see :ref:`scheduling` for details). |
| 141 |
To change the time when the condition is checked, use |
| 142 |
`NeuronGroup.set_event_schedule`. |