Skip to content

Commit

Permalink
Merge pull request #272 from Neuroblox/parameter_scope
Browse files Browse the repository at this point in the history
PR to enable shared parameters
  • Loading branch information
hstrey authored Jul 16, 2023
2 parents d2cffa4 + 053142d commit d7de86a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/Neuroblox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ include("utilities/learning_tools.jl")
include("control/controlerror.jl")
include("measurementmodels/fmri.jl")
include("datafitting/spectralDCM.jl")
include("blox/blox_utilities.jl")
include("blox/neural_mass.jl")
include("blox/cortical_blox.jl")
include("blox/canonicalmicrocircuit.jl")
Expand Down
11 changes: 11 additions & 0 deletions src/blox/blox_utilities.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function scope_dict(para_dict::Dict{Symbol, Union{Real,Num}})
para_dict_copy = copy(para_dict)
for (n,v) in para_dict_copy
if typeof(v) == Num
para_dict_copy[n] = ParentScope(v)
else
para_dict_copy[n] = (@parameters $n=v)[1]
end
end
return para_dict_copy
end
16 changes: 12 additions & 4 deletions src/blox/neural_mass.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ mutable struct JansenRitCBlox <: NeuralMassBlox
initial::Dict{Num, Tuple{Float64, Float64}}
odesystem::ODESystem
function JansenRitCBlox(;name, τ=0.001, H=20.0, λ=5.0, r=0.15)
params = @parameters τ=τ H=H λ=λ r=r
para_dict = scope_dict(Dict{Symbol,Union{Real,Num}}( => τ,:H => H, => λ,:r => r))
τ=para_dict[]
H=para_dict[:H]
λ=para_dict[]
r=para_dict[:r]
sts = @variables x(t)=1.0 y(t)=1.0 jcn(t)=0.0
eqs = [D(x) ~ y - ((2/τ)*x),
D(y) ~ -x/*τ) + (H/τ)*((2*λ)/(1 + exp(-r*(jcn))) - λ)]
odesys = ODESystem(eqs, t, sts, params; name=name)
odesys = ODESystem(eqs, t, sts, values(para_dict); name=name)
new(τ, H, λ, r, odesys.x,[odesys.x],[odesys.x,odesys.y],
Dict(odesys.x => (-1.0,1.0), odesys.y => (-1.0,1.0)),
odesys)
Expand All @@ -78,11 +82,15 @@ mutable struct JansenRitSCBlox <: NeuralMassBlox
initial::Dict{Num, Tuple{Float64, Float64}}
odesystem::ODESystem
function JansenRitSCBlox(;name, τ=0.014, H=20.0, λ=400.0, r=0.1)
params = @parameters τ=τ H=H λ=λ r=r
para_dict = scope_dict(Dict{Symbol,Union{Real,Num}}( => τ,:H => H, => λ,:r => r))
τ=para_dict[]
H=para_dict[:H]
λ=para_dict[]
r=para_dict[:r]
sts = @variables x(t)=1.0 y(t)=1.0 jcn(t)=0.0
eqs = [D(x) ~ y - ((2/τ)*x),
D(y) ~ -x/*τ) + (H/τ)*((2*λ)/(1 + exp(-r*(jcn))) - λ)]
odesys = ODESystem(eqs, t, sts, params; name=name)
odesys = ODESystem(eqs, t, sts, values(para_dict); name=name)
new(τ, H, λ, r, odesys.x,[odesys.x],[odesys.x,odesys.y],
Dict(odesys.x => (-1.0,1.0), odesys.y => (-1.0,1.0)),
odesys)
Expand Down
10 changes: 5 additions & 5 deletions test/components.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ Components Test for Cortical-Subcortical Jansen-Rit blox
"""

# Create Regions
@named GPe = jansen_ritC=0.04, H=20, λ=400, r=0.1)
@named STN = jansen_ritC=0.01, H=20, λ=500, r=0.1)
@named GPi = jansen_ritC=0.014, H=20, λ=400, r=0.1)
@named Thalamus = jansen_ritSC=0.002, H=10, λ=20, r=5)
@named PFC = jansen_ritSC=0.001, H=20, λ=5, r=0.15)
@named GPe = JansenRitCBlox=0.04, H=20, λ=400, r=0.1)
@named STN = JansenRitCBlox=0.01, H=20, λ=500, r=0.1)
@named GPi = JansenRitCBlox=0.014, H=20, λ=400, r=0.1)
@named Thalamus = JansenRitSCBlox=0.002, H=10, λ=20, r=5)
@named PFC = JansenRitSCBlox=0.001, H=20, λ=5, r=0.15)

# Connect Regions through Adjacency Matrix
blox = [GPe, STN, GPi, Thalamus, PFC]
Expand Down
21 changes: 18 additions & 3 deletions test/ode_from_graph.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Neuroblox, Test, SparseArrays, Graphs, MetaGraphs

@named GPe = harmonic_oscillator=4*2*π, ζ=1, k=(4*2*π)^2, h=0.1)
@named STN = harmonic_oscillator=4*2*π, ζ=1, k=(4*2*π)^2, h=1)
@named GPe = HarmonicOscillatorBlox()
@named STN = HarmonicOscillatorBlox()
@named BP = BandPassFilterBlox()

# Connect Regions through Adjacency Matrix
Expand Down Expand Up @@ -77,4 +77,19 @@ add_edge!(gg,3,1,:weight,.2)
# add your test to see whether you get the same ODESystem
@test typeof(neuron_net) == ODESystem
@test typeof(neuron_net_graph) == ODESystem
@test equations(neuron_net) == equations(neuron_net_graph)
@test equations(neuron_net) == equations(neuron_net_graph)

# parameter sharing test
@parameters λ=400.0
@named jr1 = JansenRitSCBlox=0.014, H=20.0, λ=λ, r=0.1)
@named jr2 = JansenRitSCBlox=0.014, H=20.0, λ=λ, r=0.1)
jg = MetaDiGraph()
add_blox!(jg,jr1)
add_blox!(jg,jr2)
add_edge!(jg,1,2,:weight,1.0)
add_edge!(jg,2,1,:weight,2.0)
add_edge!(jg,1,1,:weight,-1.0)
add_edge!(jg,2,2,:weight,-2.0)
@named jg_graph = ODEfromGraph(jg)
jg_graph_s = structural_simplify(jg_graph)
@test length(parameters(jg_graph_s))==7

0 comments on commit d7de86a

Please sign in to comment.