# Online Computation of Interspike Interval

**URL:** <https://brian.discourse.group/t/online-computation-of-interspike-interval/834>\
**Category:** Support\
**Created:** [9 November 2022 13:34 UTC](https://brian.discourse.group/t/online-computation-of-interspike-interval/834 "2022-11-09T13:34:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haydar](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@Haydar](https://brian.discourse.group/u/Haydar)\
**Post date:** [9 November 2022 13:34 UTC](https://brian.discourse.group/t/online-computation-of-interspike-interval/834/1 "2022-11-09T13:34:39Z")

</div>

Hello Brain Community,  
I would like to ask if it is possible to compute the interspike interval in an online manner ? and not waiting the network to be simulated and after that compute the aforementioned parameter.  
Thank you so much.

---

<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:** [9 November 2022 14:07 UTC](https://brian.discourse.group/t/online-computation-of-interspike-interval/834/2 "2022-11-09T14:07:44Z")

</div>

Hi @Haydar, I can think of ways to do calculate ISIs online, but could you give a bit more detail about what you want to do? Do you want to use this information during the simulation somehow, or is it just for more convenient access after the simulation? And do you want to calculate _all_ interspike intervals, or are you only interested in e.g. the average interspike interval for each neuron.  
Just in case it is helpful, here’s a compact way to calculate a dictionary mapping neuron indices to their ISIs from a `SpikeMonitor`:

```python
ISIs = {index: np.diff(spikes)
        for index, spikes in spike_monitor.spike_trains().items()}

```

---

<div class="post-metadata">

**Author:** ![Haydar](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@Haydar](https://brian.discourse.group/u/Haydar)\
**Post date:** [10 November 2022 09:52 UTC](https://brian.discourse.group/t/online-computation-of-interspike-interval/834/3 "2022-11-10T09:52:26Z")

</div>

Hello,  
Thank you for your response.  
I would like to use this information during the simulation, more specifically, to activate specific synapses (out of group of synapses) upon the ISI of the pre synaptic neuron.

---

<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:** [10 November 2022 10:00 UTC](https://brian.discourse.group/t/online-computation-of-interspike-interval/834/4 "2022-11-10T10:00:29Z")

</div>

Thanks for the clarification. The easiest way to directly represent an ISI is to include a variable storing the time of the last spike. Actually, if your model uses refractoriness, then this variable already exists: it is called `lastspike` (see [Refractoriness — Brian 2 2.5.1 documentation](https://brian2.readthedocs.io/en/stable/user/refractoriness.html#defining-the-refractory-period)). The synaptic action in `on_pre` can then refer to something like `t - lastspike_pre`, which is the time since the last spike, i.e. the interspike interval.  
If your model does not use refractoriness, you’ll have to define the `lastspike` variable yourself by including `lastspike : second` in the equations, and adding `lastspike = t` to your `reset` statements.  
Hope that helps!

---

<div class="post-metadata">

**Author:** ![Haydar](https://avatars.discourse-cdn.com/v4/letter/h/d6d6ee/32.png) [@Haydar](https://brian.discourse.group/u/Haydar)\
**Post date:** [10 November 2022 10:02 UTC](https://brian.discourse.group/t/online-computation-of-interspike-interval/834/5 "2022-11-10T10:02:05Z")

</div>

Thank you very much !!
