-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameters.jl
More file actions
137 lines (119 loc) · 3.61 KB
/
parameters.jl
File metadata and controls
137 lines (119 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# fill A, S, and M matrices with the parameter values according to the parameters map
function fill_A_S_M!(
A::AbstractMatrix,
S::AbstractMatrix,
M::Union{AbstractVector, Nothing},
A_indices::AbstractArrayParamsMap,
S_indices::AbstractArrayParamsMap,
M_indices::Union{AbstractArrayParamsMap, Nothing},
parameters::AbstractVector,
)
@inbounds for (iA, iS, par) in zip(A_indices, S_indices, parameters)
for index_A in iA
A[index_A] = par
end
for index_S in iS
S[index_S] = par
end
end
if !isnothing(M)
@inbounds for (iM, par) in zip(M_indices, parameters)
for index_M in iM
M[index_M] = par
end
end
end
end
# build the map from the index of the parameter to the linear indices
# of this parameter occurences in M
# returns ArrayParamsMap object
function array_parameters_map(parameters::AbstractVector, M::AbstractArray)
params_index = Dict(param => i for (i, param) in enumerate(parameters))
T = Base.eltype(eachindex(M))
res = [Vector{T}() for _ in eachindex(parameters)]
for (i, val) in enumerate(M)
par_ind = get(params_index, val, nothing)
if !isnothing(par_ind)
push!(res[par_ind], i)
end
end
return res
end
function eachindex_lower(M; linear_indices = false, kwargs...)
indices = CartesianIndices(M)
indices = filter(x -> (x[1] >= x[2]), indices)
if linear_indices
indices = cartesian2linear(indices, M)
end
return indices
end
function cartesian2linear(ind_cart, dims)
ind_lin = LinearIndices(dims)[ind_cart]
return ind_lin
end
function linear2cartesian(ind_lin, dims)
ind_cart = CartesianIndices(dims)[ind_lin]
return ind_cart
end
function set_constants!(M, M_pre)
for index in eachindex(M)
δ = tryparse(Float64, string(M[index]))
if !iszero(M[index]) & (δ !== nothing)
M_pre[index] = δ
end
end
end
function check_constants(M)
for index in eachindex(M)
δ = tryparse(Float64, string(M[index]))
if !iszero(M[index]) & (δ !== nothing)
return true
end
end
return false
end
# construct length(M)×length(parameters) sparse matrix of 1s at the positions,
# where the corresponding parameter occurs in the M matrix
function matrix_gradient(M_indices::ArrayParamsMap, M_length::Integer)
rowval = reduce(vcat, M_indices)
colptr =
pushfirst!(accumulate((ptr, M_ind) -> ptr + length(M_ind), M_indices, init = 1), 1)
return SparseMatrixCSC(
M_length,
length(M_indices),
colptr,
rowval,
ones(length(rowval)),
)
end
# fill M with parameters
function fill_matrix!(
M::AbstractMatrix,
M_indices::AbstractArrayParamsMap,
parameters::AbstractVector,
)
for (iM, par) in zip(M_indices, parameters)
for index_M in iM
M[index_M] = par
end
end
return M
end
# range of parameters that are referenced in the matrix
function param_range(mtx_indices::AbstractArrayParamsMap)
first_i = findfirst(!isempty, mtx_indices)
last_i = findlast(!isempty, mtx_indices)
if !isnothing(first_i) && !isnothing(last_i)
for i in first_i:last_i
if isempty(mtx_indices[i])
# TODO show which parameter is missing in which matrix
throw(
ErrorException(
"Your parameter vector is not partitioned into directed and undirected effects",
),
)
end
end
end
return first_i:last_i
end