# How to convert a sequence of spike train containing spike times to a poisson-distributed spike train

**URL:** <https://brian.discourse.group/t/how-to-convert-a-sequence-of-spike-train-containing-spike-times-to-a-poisson-distributed-spike-train/556>\
**Category:** Support\
**Created:** [14 December 2021 17:14 UTC](https://brian.discourse.group/t/how-to-convert-a-sequence-of-spike-train-containing-spike-times-to-a-poisson-distributed-spike-train/556 "2021-12-14T17:14:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rihana](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/rihana/32/369_2.png) [@Rihana](https://brian.discourse.group/u/Rihana)\
**Post date:** [14 December 2021 17:14 UTC](https://brian.discourse.group/t/how-to-convert-a-sequence-of-spike-train-containing-spike-times-to-a-poisson-distributed-spike-train/556/1 "2021-12-14T17:14:08Z")

</div>

# Description of problem

I have an Izhikevich neuron which produces a voltage-based output with a number of spikes during a 600-ms time.  
I want to fed my SSN based on LIF neurons using this output.  
how can I do it ?

If I give it to my network as an input current : I , I don’t take any spikes in my network neurons while I will take activity with only a pulse input as "I " for an interval of 2 second ( 0-2 second : 1 and 2-4 second : 2 and  
4-6 second 1 )

1. If I want to convert my output of Izhikevich neuron to a poison-distributed spike train, then give it to my network as I, is it possible using Brian ? and how ?

---

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/adam/32/197_2.png) [@adam](https://brian.discourse.group/u/adam)\
**Post date:** [15 December 2021 15:16 UTC](https://brian.discourse.group/t/how-to-convert-a-sequence-of-spike-train-containing-spike-times-to-a-poisson-distributed-spike-train/556/2 "2021-12-15T15:16:38Z")

</div>

Hi Rihana,  
When you say a “Poisson-distributed spike train,” would a homogenous-rate Poisson work?  
If so, this should be pretty straghtforward:

- measure the overall rate of your source neuron (the izhikevich neuron)
- create a [PoissonGroup](https://brian2.readthedocs.io/en/stable/user/input.html#poisson-inputs) and set its rate to that of the izhikevich neuron
- connect the PoissonGroup to your LIF neuron with a Synapse

This approach will give you a different Poisson spike train every time your run your simulation, but all the input trains will be generated with the target rate.

I’m not sure I understood the part of your question which discusses pulse inputs and 2-second intervals, so let me know if the approach above works, or whether there’s something I’ve misunderstood.  
Thanks,  
Adam

---

<div class="post-metadata">

**Author:** ![Rihana](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/rihana/32/369_2.png) [@Rihana](https://brian.discourse.group/u/Rihana)\
**Post date:** [19 December 2021 11:36 UTC](https://brian.discourse.group/t/how-to-convert-a-sequence-of-spike-train-containing-spike-times-to-a-poisson-distributed-spike-train/556/3 "2021-12-19T11:36:58Z")

</div>

Hi Adam,

First, thank you very much for your time.  
In fact, my scenario is to give several consecutive sinusoidal signals to my network as current input with 100 ms interval. First of all , I should convert them to spike trains, so I used an Izhikevich neuron.  
Now my problem is that how to use Brian class or functions to do this ? How can I use Brian classes such as PoissonGroup or another ones to produce spike train from my primary signal to apply for the network as input current?

Definitely, in this condition, I have not homogeneous rate for my inputs.

Thanks in advance,  
Rihana

---

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/adam/32/197_2.png) [@adam](https://brian.discourse.group/u/adam)\
**Post date:** [20 December 2021 21:27 UTC](https://brian.discourse.group/t/how-to-convert-a-sequence-of-spike-train-containing-spike-times-to-a-poisson-distributed-spike-train/556/4 "2021-12-20T21:27:07Z")

</div>

I think the answer to this and [your other question](https://brian.discourse.group/t/how-to-convert-a-quantized-non-periodic-signal-to-poisson-spike-trains-for-applying-for-a-network/562) is to specify the time-varying rate for your [PoissonGroup](https://brian2.readthedocs.io/en/stable/reference/brian2.input.poissongroup.PoissonGroup.html) as a [TimedArray](https://brian2.readthedocs.io/en/2.0rc/reference/brian2.input.timedarray.TimedArray.html),  
[https://brian2.readthedocs.io/en/stable/reference/brian2.input.poissongroup.PoissonGroup.html](https://brian2.readthedocs.io/en/stable/reference/brian2.input.poissongroup.PoissonGroup.html)

```auto
dt_drive = 100*ms
time_vec = np.arange(0*ms, duration, dt_drive)
sine_freq = 2*np.pi # modulation frequency of sinusoidal input
sine_amp = 5*Hz
example_sinusoid = sine_amp*(np.sin( time_vec * sine_freq) +1)
time_var_rate = TimedArray(example_sinusoid, dt=dt_drive)

poisson_drive = PoissonGroup(num_drive, rates=time_var_rate)
# ... connect poisson_drive to the rest of network

```

be sure to check the documentation for [`is_locally_constant`](https://brian2.readthedocs.io/en/2.5.0.1/reference/brian2.input.timedarray.TimedArray.html#brian2.input.timedarray.TimedArray.is_locally_constant) to decide whether you want values to be interpolated or updated in a step-like way

---

<div class="post-metadata">

**Author:** ![Rihana](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/rihana/32/369_2.png) [@Rihana](https://brian.discourse.group/u/Rihana)\
**Post date:** [23 December 2021 12:23 UTC](https://brian.discourse.group/t/how-to-convert-a-sequence-of-spike-train-containing-spike-times-to-a-poisson-distributed-spike-train/556/5 "2021-12-23T12:23:04Z")

</div>

Thank you very much Adam

Actually, I’m not sure this is what I meant.  
because it just changes the input signal to an quantized one with negative values not based on spike times.

Suppose, I have a spike train containing for example [12, 16, 20.2, 26, 50.5] second.  
I want to give it to my network in the form of 0, 1 depending on when spike time occurs.I can’t give negative value as external input.

How can I do it ?

\*\*\* Of course, I have 100 different spike trains which the mentioned is one of them.

- my another question is that it means for dt\_drive=1 ms ?

To clarify my meaning, I give an example.  
I have a non-priodic signal. I want to give it to my network. As far as I know I can do it in 2 different forms :

1. As external current (I)
2. over a single neuron connected to my network  
Is it true?

Now my question is how to do it using these 2 forms ?  
If I connect a PoissonGroup via my signal, it will be considered a single neuron in Brian or external input ?  
And so I should set " I " in the neuron model equation to “0” or remove it ?

---

<div class="post-metadata">

**Author:** ![adam](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/adam/32/197_2.png) [@adam](https://brian.discourse.group/u/adam)\
**Post date:** [2 January 2022 19:24 UTC](https://brian.discourse.group/t/how-to-convert-a-sequence-of-spike-train-containing-spike-times-to-a-poisson-distributed-spike-train/556/6 "2022-01-02T19:24:23Z")

</div>

If you simply want to “replay” the exact spike times from one simulation as though it were an input neuron in a new simulation (maybe “option 2” in your most recent message) you can do that with [`SpikeGeneratorGroup`](https://brian2.readthedocs.io/en/stable/user/input.html#spike-generation) and you can specify whether that input acts as though from a single neuron, or multiple:

```python
from brain2 import * 
import numpy as np

G_output = NeuronGroup(N_output, eqs)

# set up replay input group
input_spike_times_A = [12, 16, 20.2, 26, 50.5]*second
input_spike_times_B = [10, 20, 30]*second
N_input = 2

def spike_times_to_index_array(times, idx):
    return np.ones(size(times), dtype=int) * idx

# this concatenation is only necessary if you want to have multiple pre-synaptic neurons replay their spikes in parallel 
all_input_spike_times = np.concatenate((input_spike_times_A, input_spike_times_B))

input_neuron_indices = np.concatenate(
    (spike_times_to_index_array(input_spike_times_A, 0), 
    spike_times_to_index_array(input_spike_times_B, 1) ) )

G_replay = SpikeGeneratorGroup(N_input, input_neuron_indices, all_input_spike_times)

# set up output group
N_output = 10
eqs = '''
dv/dt = (I-v)/tau : 1
I : 1
tau : second
'''

#connect
S = Synapses(G_replay, G_output, 'w:1', on_pre='v_post += w')
S.connect() # connect all-to-all
S.w = 1.0 #set synaptic weight

```

> **(here's the same code as above, but without the extra complication of multiple neurons being replayed, click to expand)**
>
> ```python
> from brain2 import * 
> import numpy as np
> 
> G_output = NeuronGroup(N_output, eqs)
> 
> # set up replay input group
> input_spike_times = [12, 16, 20.2, 26, 50.5]*second
> N_input = 1
> 
> def spike_times_to_index_array(times, idx):
> return np.ones(size(times), dtype=int) * idx
> 
> input_neuron_indices = spike_times_to_index_array(input_spike_times, 0)
> 
> G_replay = SpikeGeneratorGroup(1, input_neuron_indices, input_spike_times)
> 
> # set up output group
> N_output = 10
> eqs = '''
> dv/dt = (I-v)/tau : 1
> I : 1
> tau : second
> '''
> 
> #connect
> S = Synapses(G_replay, G_output, 'w:1', on_pre='v_post += w')
> S.connect() # connect all-to-all
> S.w = 1.0 #set synaptic weight
> 
> ```

note that a big difference from using a `PoissonGroup` is that a `PoissonGroup` will generate new random spikes every time you call the script, while this `SpikeGeneratorGroup` will deliver spikes at exactly the times you specify.

[The documentation](https://brian2.readthedocs.io/en/stable/user/input.html#spike-generation) also gives an example of `set_spikes` which can update the spike times for a generator group within a run in case that’s something you need
