# KeyError: 'spike' in SpikeMonitor

**URL:** <https://brian.discourse.group/t/keyerror-spike-in-spikemonitor/277>\
**Category:** Support\
**Created:** [31 January 2021 21:49 UTC](https://brian.discourse.group/t/keyerror-spike-in-spikemonitor/277 "2021-01-31T21:49:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![atefeasd](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/atefeasd/32/136_2.png) [@atefeasd](https://brian.discourse.group/u/atefeasd)\
**Post date:** [31 January 2021 21:49 UTC](https://brian.discourse.group/t/keyerror-spike-in-spikemonitor/277/1 "2021-01-31T21:49:11Z")

</div>

Hi. I hope you are well.  
I have a problem with “Spike Monitor”. I get this result from my simulation  
 ![Stn](https://global.discourse-cdn.com/free1/uploads/brian/original/1X/6ef6c98efe24bcf641fab43e8cfa5a739b5275d5.png)  
but when I use “SpikeMonitir” order, I recieve this error:

```auto
Traceback (most recent call last):
  File "STN.py", line 181, in <module>
    sp=SpikeMonitor(GSTN)
  File "/home/atefeh/.local/lib/python3.8/site-packages/brian2/monitors/spikemonitor.py", line 420, in __init__
    super(SpikeMonitor, self). __init__ (source, event='spike',
  File "/home/atefeh/.local/lib/python3.8/site-packages/brian2/monitors/spikemonitor.py", line 77, in __init__
    parent_obj = source.thresholder[event]
KeyError: 'spike'

```

I will be very happy if you help me another time!  
with respect

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [1 February 2021 10:18 UTC](https://brian.discourse.group/t/keyerror-spike-in-spikemonitor/277/2 "2021-02-01T10:18:21Z")

</div>

Admittedly, this error message is not very good… You get this error message because your model did not specify any `threshold`, so Brian does not know what you consider a spike. Make sure to also set a `refractory` condition to avoid that the `SpikeMonitor` records multiple spikes during a single spike. I’m pasting the explanation from the [documentation](https://brian2.readthedocs.io/en/stable/user/refractoriness.html?highlight=refractory%3D%27v%3E#defining-the-refractory-period) here:

> In some cases, the condition for leaving the refractory period is not easily expressed as a certain time span. For example, in a Hodgkin-Huxley type model the threshold is only used for counting spikes and the refractoriness is used to prevent to count multiple spikes for a single threshold crossing (the threshold condition would evaluate to True for several time points). When a neuron should leave the refractory period is not easily expressed as a time span but more naturally as a condition that the neuron should remain refractory for as long as it stays above the threshold. This can be achieved by using a string expression for the refractory keyword that evaluates to a boolean condition:

```Python
G = NeuronGroup(N, model='...', threshold='v > -20*mV',
                refractory='v >= -20*mV')

```

PS: I moved your question to its own topic. Please open new topics instead of replying to existing ones for independent questions.

---

<div class="post-metadata">

**Author:** ![atefeasd](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/atefeasd/32/136_2.png) [@atefeasd](https://brian.discourse.group/u/atefeasd)\
**Post date:** [1 February 2021 15:35 UTC](https://brian.discourse.group/t/keyerror-spike-in-spikemonitor/277/3 "2021-02-01T15:35:30Z")

</div>

Thank you very much .Do you mean that my code is no longer capable of forming synapses because it cannot detect spikes?

---

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [1 February 2021 15:57 UTC](https://brian.discourse.group/t/keyerror-spike-in-spikemonitor/277/4 "2021-02-01T15:57:38Z")

</div>

If your synapses are based on spike-based interactions, i.e. with `on_pre` statements that trigger post-synaptic changes, then indeed they won’t work without a `threshold` definition in the pre-synaptic group. But in that case you’d also get an error message similar to the one that you got previously. If your synapses are modelled as continuous interaction, i.e. as some function of the pre-synaptic membrane potential (see e.g. [this example](https://brian2.readthedocs.io/en/stable/examples/synapses.gapjunctions.html)) then they will work without any `threshold` definition.

---

<div class="post-metadata">

**Author:** ![atefeasd](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/atefeasd/32/136_2.png) [@atefeasd](https://brian.discourse.group/u/atefeasd)\
**Post date:** [1 February 2021 16:19 UTC](https://brian.discourse.group/t/keyerror-spike-in-spikemonitor/277/5 "2021-02-01T16:19:08Z")

</div>

Thank you very much for your patience and accuracy in answering. Thank you again!
