Spiking Neural Model

Hi,

I am new to Julia and my aim is to create a network of spiking neurons. I use simple leaky integrate and fire (LIF) model for now with ODEProblem solver. Whenever the neuron (u) reaches a threshold value, it should enter a refractory period of some fixed ms, where du would be 0 and u would be kept in a reset value. Also the time points at which u reaches the threshold must be recorded as a vector for each neuron. However I couldn`t really understand how I can implement this, I use callbacks for thresholds passing condition, I am not sure how to use callbacks for setting refractory time. In python I would use a counter inside the function but since ODE handles the time itself I am confused how to intervene with it.

What did you try? What not just add a counter inside the function as you describe?

using DifferentialEquations

τ_m = 20.0
V_rest = -70.0
V_thresh = -55.0
V_reset = -75.0
τ_ref = 5.0
I_ext = 20.0

u0 = [V_rest]
spike_times = Float64[]
in_refractory = Ref(false)
refractory_end = Ref(0.0)

function lif!(du, u, p, t)
    V = u[1]
    if in_refractory[]
        du[1] = 0.0
    else
      du[1] = (V_rest - V + I_ext) / τ_m
    end
end

condition(u, t, integrator) = in_refractory[] ? -1.0 : u[1] - V_thresh

function affect!(integrator)
    push!(spike_times, integrator.t)
    integrator.u[1] = V_reset
    in_refractory[] = true
    refractory_end[] = integrator.t + τ_ref
    add_tstop!(integrator, refractory_end[])
end

spike_cb = ContinuousCallback(condition, affect!, nothing)

function refractory_condition(u, t, integrator)
    in_refractory[] && t >= refractory_end[]
end

function refractory_affect!(integrator)
    in_refractory[] = false
end

refractory_cb = DiscreteCallback(refractory_condition, refractory_affect!)

cb = CallbackSet(spike_cb, refractory_cb)

prob = ODEProblem(lif!, u0, (0.0, 200.0))
sol = solve(prob, Tsit5(), callback=cb)

println("Spike times: ", spike_times)