Skip to content

Commit

Permalink
List of symbols and tunable flag (#466)
Browse files Browse the repository at this point in the history
* switched from symbolic arrays to list of symbolic parameters. Needed to be able to set tunable flag for individual parameters of the array. Added untune! utility function to set tunable flag to false on list elements.

* added new approach to set tunable flag for the connection matrix also to the tutorial
  • Loading branch information
david-hofmann authored Oct 22, 2024
1 parent d956d33 commit b7eaf5d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
11 changes: 9 additions & 2 deletions docs/src/tutorials/spectralDCM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,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

0 comments on commit b7eaf5d

Please sign in to comment.