2023-08-02__Julia_speedtest_AdEx_Nto1

(We’re reusing code from 2023-02-24__multisim-winline.jl and 2023-03-14__[setup]_Nto1_sim_AdEx.jl).

cd(joinpath(homedir(), "phd", "pkg" , "SpikeWorks"))
run(`git switch metdeklak`);
Your branch is up to date with 'origin/metdeklak'.
Already on 'metdeklak'
cd(joinpath(homedir(), "phd"))
Pkg.activate(".")
  Activating project at `C:\Users\tfiers\phd`
]st
Status `C:\Users\tfiers\phd\Project.toml`
 [1520ce14] AbstractTrees v0.4.3
 [336ed68f] CSV v0.10.9
  [8be319e6] Chain v0.5.0
 [da1fd8a2] CodeTracking v1.2.0
 [b0b7db55] ComponentArrays v0.13.6
 [8f4d0f93] Conda v1.7.0
  [03fa6f02] ConnTestEval v0.1.0 `pkg\ConnTestEval`
  [4ec9db02] ConnectionTests v0.1.0 `pkg\ConnectionTests.jl`
 [f68482b8] Cthulhu v2.7.6
 [a93c6f00] DataFrames v1.4.4
 [1313f7d8] DataFramesMeta v0.12.0
 [864edb3b] DataStructures v0.18.13
  [31a5f54b] Debugger v0.7.8
  [019de50b] DistributedLoopMonitor v0.1.0 `pkg\DistributedLoopMonitor`
 [31c24e10] Distributions v0.25.80
  [48062228] FilePathsBase v0.9.20
 [f6369f11] ForwardDiff v0.10.34
  [4356e881] GlobalMacros v0.1.0 `pkg\GlobalMacros`
  [4c0ca9eb] Gtk v1.3.0
 [7073ff75] IJulia v1.24.0
 [033835bb] JLD2 v0.4.29
  [984bce1d] LambertW v0.4.6
 [23fbe1c1] Latexify v0.15.18
  [2fda8390] LsqFit v0.13.0 `https://github.com/JuliaNLSolvers/LsqFit.jl#e9b9e8732`
  [1914dd2f] MacroTools v0.5.10
  [40c6d27e] MemDiskCache v0.1.0 `pkg\MemDiskCache`
 [6fafb56a] Memoization v0.2.0
  [54cd1024] MyToolbox v0.1.0 `pkg\MyToolbox`
  [d96e819e] Parameters v0.12.3
  [570af359] PartialFunctions v1.1.1
  [3ec2fb83] PhDPlots v0.1.0 `pkg\PhDPlots`
 [08abe8d2] PrettyTables v2.2.3
 [c46f51b8] ProfileView v1.5.2
  [92933f4c] ProgressMeter v1.7.2
 [438e738f] PyCall v1.95.1
 [d330b81b] PyPlot v2.11.0
 [295af30f] Revise v3.5.0
  [61be95e5] Sciplotlib v0.1.0 `pkg\Sciplotlib`
 [aa65fe97] SnoopCompile v2.9.7
 [e2b509da] SnoopCompileCore v2.9.0
 [66db9d55] SnoopPrecompile v1.0.1
  [fe4ab31d] SpikeWorks v0.1.0 `pkg\SpikeWorks`
 [2913bbd2] StatsBase v0.33.21
 [4c63d2b9] StatsFuns v1.1.1
 [09ab397b] StructArrays v0.6.14
 [fd094767] Suppressor v0.2.1
 [bd369af6] Tables v1.10.0
 [5d786b92] TerminalLoggers v0.1.6
 [4239201d] ThreadSafeDicts v0.1.0
 [b8865327] UnicodePlots v3.4.1
 [1986cc42] Unitful v1.12.2
  [5f9f03f5] Units v0.1.0 `pkg\Units`
  [5e413c25] WithFeedback v0.1.0 `pkg\WithFeedback`
Info Packages marked with  and  have new versions available, but those with  are restricted by compatibility constraints from upgrading. To see why use `status --outdated`
using WithFeedback
@withfb using SpikeWorks
@withfb using SpikeWorks.Units
using SpikeWorks … 
[ Info: Precompiling SpikeWorks [fe4ab31d-2284-4e18-9761-4109e720cf88]
✔ (5.3 s)
using SpikeWorks.Units … ✔ (1.5 s)
@typed begin
    # AdEx LIF neuron params (cortical RS)
    C  = 104  * pF
    gₗ = 4.3  * nS
    Eₗ = -65  * mV
    Vₜ = -52  * mV
    Δₜ = 0.8  * mV
    Vₛ =   0  * mV
    Vᵣ = -53  * mV
    a  = -0.8 * nS
    b  =  65  * pA
    τw =  88  * ms
    # Conductance-based synapses
    Eₑ =   0 * mV
    Eᵢ = -80 * mV
    τ  =   7 * ms
end;

Simulated variables and their initial values

const x₀ = (
    # AdEx variables
    v   = Eₗ,      # Membrane potential
    w   = 0 * pA,  # Adaptation current
    # Synaptic conductances g
    gₑ  = 0 * nS,  # = Sum over all exc. synapses
    gᵢ  = 0 * nS,  # = Sum over all inh. synapses
)
(v = -0.065, w = 0.0, gₑ = 0.0, gᵢ = 0.0)

Differential equations:

calculate time derivatives of simulated vars
(and store them “in-place”, in Dₜ).

function f!(Dₜ, vars)
    v, w, gₑ, gᵢ = vars

    # Conductance-based synaptic current
    Iₛ = gₑ*(v-Eₑ) + gᵢ*(v-Eᵢ)

    # AdEx 2D system
    Dₜ.v = (-gₗ*(v-Eₗ) + gₗ*Δₜ*exp((v-Vₜ)/Δₜ) - Iₛ - w) / C
    Dₜ.w = (a*(v-Eₗ) - w) / τw

    # Synaptic conductance decay
    Dₜ.gₑ = -gₑ / τ
    Dₜ.gᵢ = -gᵢ / τ
end;

Spike discontinuity

has_spiked(vars) = (vars.v > Vₛ)

function on_self_spike!(vars)
    vars.v = Vᵣ
    vars.w += b
end;

Conductance-based AdEx neuron

const coba_adex_neuron = NeuronModel(x₀, f!; has_spiked, on_self_spike!);

More parameters, and input spikers

using SpikeWorks: LogNormal

# Firing rates λ for the Poisson inputs
const fr_distr = LogNormal(median = 4Hz, g = 2)

@typed begin
    Δt = 0.1ms
    EIratio = 4//1
end;
N = 6500
δ_nS = 0.02
(; Nₑ, Nᵢ) = EIMix(N, EIratio)
EIMix of 6500 neurons
- 5200 excitatory (80%)
- 1300 inhibitory (20%)
- 4:1 EI-ratio
duration = 10*seconds
10.0
firing_rates = rand(fr_distr, N)
input_IDs = 1:N
inputs = [
    Nto1Input(ID, poisson_SpikeTrain(λ, duration))
    for (ID, λ) in zip(input_IDs, firing_rates)
];
neuron_type(ID) = (ID  Nₑ) ? :exc : :inh
Δgₑ = δ_nS * nS
Δgᵢ = δ_nS * nS * EIratio
8.000000000000001e-11
using Random
Random.seed!(1);
on_spike_arrival!(vars, spike) =
    if neuron_type(source(spike)) == :exc
        vars.gₑ += Δgₑ
    else
        vars.gᵢ += Δgᵢ
    end;
using SpikeWorks: newsim, run!

sim = newsim(coba_adex_neuron, inputs, on_spike_arrival!, Δt)

SpikeWorks.Simulation{Nto1System{NeuronModel{NamedTuple{(:v, :w, :gₑ, :gᵢ), NTuple{4, Float64}}, typeof(f!), typeof(has_spiked), typeof(on_self_spike!)}, typeof(on_spike_arrival!)}, CVec{(:v, :w, :gₑ, :gᵢ)}}
Summary: not started
Properties: 
       system: Nto1System, x₀: (v = -0.065, w = 0.0, gₑ = 0.0, gᵢ = 0.0), input feed: 0/330644 spikes processed
           Δt: 0.0001
     duration: 10.0
  stepcounter: 0/100000
        state: t = 0 seconds, neuron = vars: (v: -0.065, w: 0.0, gₑ: 0.0, gᵢ: 0.0), Dₜvars: (v: 0.0, w: 0.0, gₑ: 0.0, gᵢ: 0.0)
          rec: v: [303.958, 2.984e-320, 304.353, 2.984e-320, 304.391, 2.984e-320, 304.497, 2.984e-320, 304.632, 2.984e-320  …  0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], spiketimes: Float64[]
@time run!(sim)
  0.740851 seconds (1.62 M allocations: 50.463 MiB)

SpikeWorks.Simulation{Nto1System{NeuronModel{NamedTuple{(:v, :w, :gₑ, :gᵢ), NTuple{4, Float64}}, typeof(f!), typeof(has_spiked), typeof(on_self_spike!)}, typeof(on_spike_arrival!)}, CVec{(:v, :w, :gₑ, :gᵢ)}}
Summary: completed. 10 spikes/s
Properties: 
       system: Nto1System, x₀: (v = -0.065, w = 0.0, gₑ = 0.0, gᵢ = 0.0), input feed: all 330644 spikes processed
           Δt: 0.0001
     duration: 10.0
  stepcounter: 100000 (complete)
        state: t = 10 seconds, neuron = vars: (v: -0.053115, w: 4.80322e-11, gₑ: 3.79993e-9, gᵢ: 3.59675e-9), Dₜvars: (v: 0.0807286, w: -6.54536e-10, gₑ: -5.50715e-7, gᵢ: -5.21268e-7)
          rec: v: [-0.064995, -0.0649912, -0.0649873, -0.0649834, -0.0649757, -0.0649631, -0.0649507, -0.0649359, -0.0649202, -0.0648984  …  -0.0531876, -0.0531746, -0.0531684, -0.0531616, -0.0531552, -0.0531493, -0.0531418, -0.0531306, -0.0531231, -0.053115], spiketimes: [0.0325, 0.127, 0.2158, 0.3237, 0.4107, 0.5056, 0.5998, 0.7161, 0.8009, 0.9133  …  9.0339, 9.1291, 9.2522, 9.3429, 9.4633, 9.5588, 9.6279, 9.7407, 9.864, 9.9548]
_spiketimes(input::Nto1Input) = input.train.spiketimes

simdata = (;
    spiketrains   = _spiketimes.(inputs),
    voltsig       = sim.rec.v,
    spikerate     = SpikeWorks.spikerate(sim),
    input_types   = neuron_type.(input_IDs),
    sim_duration  = duration,
    firing_rates, input_IDs, N, δ_nS
);