Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List of symbols and tunable flag #466

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions docs/src/tutorials/spectralDCM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,15 @@ end
A_prior = 0.01*randn(nr, nr)
A_prior -= diagm(diag(A_prior)) # ensure diagonal dominance of matrix
# Since we want to optimize these weights we turn them into symbolic parameters:
# Add the symbolic weights to the edges and connect reegions.
@parameters A[1:nr^2] = vec(A_prior) [tunable = true]
# Add the symbolic weights to the edges and connect regions.
A = []
for (i, a) in enumerate(vec(A_prior))
symb = Symbol("A$(i)")
push!(A, only(@parameters $symb = a))
end
# With the function `untune!`` we can list indices of parameters whose tunable flag should be set to false.
# For instance the first element in the second row:
untune!(A, [4])
for (i, idx) in enumerate(CartesianIndices(A_prior))
if idx[1] == idx[2]
add_edge!(g, regions[idx[1]] => regions[idx[2]]; :weight => -exp(A[i])/2) # -exp(A[i])/2: treatement of diagonal elements in SPM12 to make diagonal dominance (see Gershgorin Theorem) more likely but it is not guaranteed
Expand Down
2 changes: 1 addition & 1 deletion src/Neuroblox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ export system_from_graph, graph_delays
export create_adjacency_edges!, adjmatrixfromdigraph
export get_namespaced_sys, nameof
export run_experiment!, run_trial!
export addnontunableparams
export addnontunableparams, untune!
export get_weights, get_dynamic_states, get_idx_tagged_vars, get_eqidx_tagged_vars
export BalloonModel,LeadField, boldsignal_endo_balloon
export PINGNeuronExci, PINGNeuronInhib
Expand Down
6 changes: 6 additions & 0 deletions src/blox/blox_utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ function paramscoping(;kwargs...)
return paramlist
end

function untune!(parlist, nontunable)
for i in nontunable
parlist[i] = setmetadata(parlist[i], ModelingToolkit.VariableTunable, false)
end
end

get_HH_exci_neurons(n::HHNeuronExciBlox) = [n]
get_HH_exci_neurons(n) = []

Expand Down
7 changes: 6 additions & 1 deletion test/datafitting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ using MAT
end

# add symbolic weights
@parameters A[1:length(vars["pE"]["A"])] = vec(vars["pE"]["A"]) [tunable = true]
A = []
for (i, a) in enumerate(vec(vars["pE"]["A"]))
symb = Symbol("A$(i)")
push!(A, only(@parameters $symb = a))
end
untune!(A, []) # list indices of parameters that should be set to tunable=false
for (i, idx) in enumerate(CartesianIndices(vars["pE"]["A"]))
if idx[1] == idx[2]
add_edge!(g, regions[idx[1]], regions[idx[2]], :weight, -exp(A[i])/2) # treatement of diagonal elements in SPM12, likely to avoid instabilities of the linear model
Expand Down
Loading