Skip to content

Commit

Permalink
Merge pull request AStupidBear#15 from JuliaSNN/main
Browse files Browse the repository at this point in the history
get upstream changes
  • Loading branch information
russelljjarvis authored Jan 13, 2025
2 parents fa770c1 + 7854fb9 commit 28541fc
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 28 deletions.
17 changes: 9 additions & 8 deletions src/extra_plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ function dendrite_gplot(population, target; neuron=1, r, param=:dend_syn, nmda=t
end
end
curr .= curr ./1000
@info size(curr), size(r)

ylims =abs.(maximum(abs.(curr[neuron,:,:]))) |> x->(-x, x)
plot(r, curr[neuron,1,:].+curr[neuron,2,:], label="Glu")
Expand Down Expand Up @@ -132,9 +131,9 @@ Returns:
Example:
"""
function plot_activity(network, Trange; conductance=false)
frE, interval = SNN.firing_rate(network.pop.E, interval = Trange, τ=10ms)
frI1, interval = SNN.firing_rate(network.pop.I1, interval = Trange, τ=10ms)
frI2, interval = SNN.firing_rate(network.pop.I2, interval = Trange, τ=10ms)
frE, interval = SNN.firing_rate(network.pop.E, interval = Trange, τ=10ms, interpolate=true)
frI1, interval = SNN.firing_rate(network.pop.I1, interval = Trange, τ=10ms, interpolate=true)
frI2, interval = SNN.firing_rate(network.pop.I2, interval = Trange, τ=10ms, interpolate=true)
pr = plot(xlabel = "Time (ms)", ylabel = "Firing rate (Hz)")
plot!(Trange, mean(frE[:,Trange], dims=1)[1,:], label = "E", c = :black)
plot!(Trange, mean(frI1[:,Trange], dims=1)[1,:], label = "I1", c = :red)
Expand All @@ -150,7 +149,8 @@ function plot_activity(network, Trange; conductance=false)
plot!(ylims=:auto, margin = 5Plots.mm, ylabel = "Membrane potential (mV)", legend=true, xlabel="")
rplot = SNN.raster(network.pop, Trange, size=(900,500), margin=5Plots.mm, xlabel="")

p5 = histogram(average_firing_rate(network.pop.E), c=:black, lc=:black, label="Excitatory", normalize=true)
p5 = plot()
p5 = histogram!(average_firing_rate(network.pop.E), c=:black, lc=:black, label="Excitatory", normalize=true)
p5 = histogram!(average_firing_rate(network.pop.I1), c=:red, lc=:red, alpha=0.5, label="Inhibitory 1", normalize=true)
p5 = histogram!(average_firing_rate(network.pop.I2), c=:green, lc=:green, alpha=0.5, label="Inhibitory 2", normalize=true)
## Conductance
Expand Down Expand Up @@ -356,15 +356,15 @@ end
- `Plots.Plot`: Plot of the STDP kernel
"""
function stdp_kernel(stdp_param; ΔTs= -97.5:2.5:100ms, fill=true)
function stdp_kernel(stdp_param; ΔTs= -200.5:5:200ms, fill=true)
ΔWs = zeros(Float32, length(ΔTs))
Threads.@threads for i in eachindex(ΔTs)
ΔT = ΔTs[i]
spiketime = [2000ms, 2000ms+ΔT]
neurons = [[1], [2]]
inputs = SpikeTime(spiketime, neurons)
w = zeros(Float32, 2,2)
w[1, 2] = 1f0
w[2, 1] = 1f0
st = Identity(N=max_neurons(inputs))
stim = SpikeTimeStimulusIdentity(st, :g, param=inputs)
syn = SpikingSynapse(st, st, :h, w = w, param = stdp_param)
Expand All @@ -379,7 +379,8 @@ function stdp_kernel(stdp_param; ΔTs= -97.5:2.5:100ms, fill=true)
# R(X; f=maximum) = [f([x,0]) for x in X]
plot(ΔTs[n_minus],ΔWs[n_minus] , legend=false, fill=fill, xlabel="ΔT", ylabel="ΔW", title="STDP", size=(500, 300), alphafill=0.5, lw=4)
plot!(ΔTs[n_plus],ΔWs[n_plus] , legend=false, fill=fill,xlabel="T_post - T_pre ", ylabel="ΔW", title="STDP", size=(500, 300), alphafill=0.5, lw=4)
plot!(ylims=:auto)
plot!(ylims=extrema(ΔWs).*1.4, xlims=extrema(ΔTs), framestyle=:zerolines)
# plot!(ylims=(-10,10), xlims=extrema(ΔTs))
end

## Measure the weight change for decorrelated spike trains
Expand Down
2 changes: 1 addition & 1 deletion src/graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function add_connection!(graph, pre_id, post_id, k, syn, type)
pre_name = get_prop(graph, pre_node, :name)
post_name = get_prop(graph, post_node, :name)
sym = haskey(syn.targets, :sym) ? syn.targets[:sym] : :soma
syn_name = "$(pre_name) -> $(post_name).$sym"
syn_name = "$(syn.name): $(pre_name) -> $(post_name).$sym"
id = syn.id
if !has_edge(graph, pre_node, post_node)
add_edge!(graph, pre_node, post_node,
Expand Down
4 changes: 2 additions & 2 deletions src/neuron/multicompartment/tripod_params.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ AdExSoma
# After spike timescales and membrane
AP_membrane::FT = 10.0f0mV
BAP::FT = 1.0f0mV
up::IT = 1ms
τabs::IT = 2ms
up::FT = 1ms
τabs::FT = 2ms
end

## Synapses Tripod neuron
Expand Down
2 changes: 0 additions & 2 deletions src/record.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ end
end



function record!(obj::C, T::Time) where {C<:AbstractConnection}
@unpack records = obj
# timestamp::Vector{Float32} = records[:timestamp]
Expand Down Expand Up @@ -326,7 +325,6 @@ function interpolated_record(p, sym)

# ! adjust the end time to account for the added first element
_end = (size(v_dt,)[end]-1)/sr
@show sr, _end
# ! this is the recorded time (in ms), it assumes all recordings are contained in v_dt
r_v = 0:1/sr:_end

Expand Down
1 change: 0 additions & 1 deletion src/spikes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,6 @@ function firing_rate(
# rates = vcat(rates'...)

if interpolate
@info typeof(rates), typeof(interval), size(rates), size(interval)
rates = Interpolations.scale(Interpolations.interpolate(copy(hcat(rates...)'), BSpline(Interpolations.Linear)), 1:length(spiketimes), interval)
end
return rates, interval
Expand Down
5 changes: 3 additions & 2 deletions src/stimulus/balanced_stim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ function BalancedStimulus(post::T, sym_e::Symbol, sym_i::Symbol, target = nothin
rowptr, colptr, I, J, index, W = dsparse(w)

if isnothing(target)
g = getfield(post, sym)
ge = getfield(post, sym_e)
gi = getfield(post, sym_i)
targets = Dict(:pre => :Balanced, :g => post.id, :sym=>:soma)
elseif typeof(target) == Symbol
ge = getfield(post, Symbol("$(sym_e)_$target"))
Expand All @@ -74,7 +75,7 @@ function BalancedStimulus(post::T, sym_e::Symbol, sym_i::Symbol, target = nothin
sym_e= Symbol("$(sym_e)_d")
ge = getfield(post, sym_e)[target]
gi = getfield(post, sym_i)[target]
targets = Dict(:pre => :Balanced, :g => post.id, :sym=>Symbol(target))
targets = Dict(:pre => :Balanced, :g => post.id, :sym=>Symbol(string(sym_e, target)))
end

# if isnothing(target)
Expand Down
16 changes: 16 additions & 0 deletions src/stimulus/poisson_stim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,22 @@ function OrnsteinUhlenbeckProcess(x::Float32, param::PSParam)
return X
end

function SinWaveNoise(x::Float32, param::PSParam)
X::Float32 = param.variables[:X]
θ::Float32 = param.variables[]
μ::Float32 = param.variables[]
σ::Float32 = param.variables[]
dt::Float32 = param.variables[:dt]
ν::Float32 = param.variables[]

W = σ * rand(Normal()) * sqrt(dt)
X = X + θ *-X)*dt + W
param.variables[:X] = X

Y = sin(x * 2π * ν)
return X*0.1 + Y*μ
end




Expand Down
2 changes: 1 addition & 1 deletion src/synapse/sparse_plasticity/iSTDP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function plasticity!(
tpost[i] += 1
@turbo for st = rowptr[i]:(rowptr[i+1]-1) ##
st = index[st]
W[st] = clamp(W[st] + η * (tpre[J[st]] - 1/τy), Wmin, Wmax)
W[st] = clamp(W[st] + η * (tpre[J[st]]), Wmin, Wmax)
end
end
end
Expand Down
39 changes: 28 additions & 11 deletions src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,16 @@ Raises an assertion error if any component in the populations is not a subtype o
function print_model(model, get_keys=false)
model_graph = graph(model)
@unpack pop, syn, stim = model
@info "================"
@info "Model: $(model.name)"
@info "----------------"
@info "Populations:"
populations = Vector{String}()
for k in keys(pop)
v = filter_first_vertex(model_graph, (g, v) -> get_prop(model_graph, v, :key) == k)
name = props(model_graph, v)[:name]
_k = get_keys ? "($k)" : ""
@info "$name $(_k): $(nameof(typeof(getfield(pop,k)))): $(nameof(typeof(getfield(pop,k).param)))"
@assert typeof(getfield(pop, k)) <: SNN.AbstractPopulation "Expected neuron, got $(typeof(getfield(network.pop,k)))"
push!(populations, "$name $(_k): $(nameof(typeof(getfield(pop,k)))): $(nameof(typeof(getfield(pop,k).param)))")

end
@info "----------------"
@info "Synapses:"
synapses = Vector{String}()
for k in keys(syn)
isa(syn[k], SNN.SynapseNormalization) && continue
_edges, _ids = filter_edge_props(model_graph, :key, k)
Expand All @@ -238,22 +235,42 @@ function print_model(model, get_keys=false)
_k = get_keys ? "($k)" : ""
norm = props(model_graph, e)[:norm][i] !== :none ? "<-> norm: $(props(model_graph, e)[:norm][i])" : ""
# @info "$name $(_k) $norm: \n $(nameof(typeof(getfield(syn,k)))): $(nameof(typeof(getfield(syn,k).param)))"
@info "$name $(_k) $norm: $(nameof(typeof(getfield(syn,k).param)))"
@assert typeof(getfield(syn, k)) <: SNN.AbstractConnection "Expected synapse, got $(typeof(getfield(network.syn,k)))"
push!(synapses,"$name $(_k) $norm: $(nameof(typeof(getfield(syn,k).param)))")
end
end
@info "----------------"
@info "Stimuli:"
stimuli = Vector{String}()
for k in keys(stim)
_edges, _ids = filter_edge_props(model_graph, :key, k)
for (e, i) in zip(_edges, _ids)
name = props(model_graph, e)[:name][i]
_k = get_keys ? "($k)" : ""
@info "$name $(_k): $(nameof(typeof(getfield(stim,k))))"
# @info "$name $(_k): $(nameof(typeof(getfield(stim,k)))): $(nameof(typeof(getfield(stim,k).param)))"
@assert typeof(getfield(stim, k)) <: SNN.AbstractStimulus "Expected stimulus, got $(typeof(getfield(network.stim,k)))"
push!(stimuli, "$name $(_k): $(nameof(typeof(getfield(stim,k))))")
end
end
sort!(stimuli)
sort!(synapses)
sort!(populations)

@info "================"
@info "Model: $(model.name)"
@info "----------------"
@info "Populations ($(length(populations))):"
for p in populations
@info p
end
@info "----------------"
@info "Synapses ($(length(synapses))): "
for s in synapses
@info s
end
@info "----------------"
@info "Stimuli ($(length(stimuli))):"
for s in stimuli
@info s
end
@info "================"
end

Expand Down

0 comments on commit 28541fc

Please sign in to comment.