Skip to content

Commit

Permalink
symbols as paramkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
montyvesselinov committed Aug 1, 2024
1 parent 0313783 commit 17cd62a
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src-old/MadsCalibrateNLopt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function calibratenlopt(madsdata::AbstractDict; algorithm=:LD_LBFGS) # TODO swit
targets = Mads.getobstarget(madsdata, obskeys)
fg = makemadscommandfunctionandgradient(madsdata)
function fg_nlopt(arrayparameters::AbstractVector, grad::AbstractVector)
parameters = OrderedCollections.OrderedDict{String,Float64}(zip(paramkeys, arrayparameters))
parameters = OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(zip(paramkeys, arrayparameters))
resultdict, gradientdict = fg(parameters)
residuals = Array{Float64}(undef, length(madsdata["Observations"]))
ssr = 0
Expand Down
2 changes: 1 addition & 1 deletion src/MadsAnasol.jl
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ function makecomputeconcentrations(madsdata::AbstractDict; calczeroweightobs::Bo
end
end
global modelruns += 1
return OrderedCollections.OrderedDict{String,Float64}(zip(wellkeys, vcat(wellc...)))
return OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(zip(wellkeys, vcat(wellc...)))
end
return computeconcentrations
end
Expand Down
4 changes: 2 additions & 2 deletions src/MadsBayesInfoGap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function makebigdt!(madsdata::AbstractDict, choice::AbstractDict)
end
function performancegoalsatisfied(arrayparams::AbstractVector, horizon::Real)
paramdict = Mads.getparamdict(madsdata)
optparams = OrderedCollections.OrderedDict{String,Float64}(zip(getoptparamkeys(madsdata), arrayparams))
optparams = OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(zip(getoptparamkeys(madsdata), arrayparams))
merge!(paramdict, optparams)
predictions = f(paramdict)
paramsandpredictionsdict = merge(paramdict, predictions)
Expand All @@ -77,7 +77,7 @@ function makebigdt!(madsdata::AbstractDict, choice::AbstractDict)
end
function gethorizonoffailure(arrayparams::AbstractVector)
paramdict = Mads.getparamdict(madsdata)
optparams = OrderedCollections.OrderedDict{String,Float64}(zip(getoptparamkeys(madsdata), arrayparams))
optparams = OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(zip(getoptparamkeys(madsdata), arrayparams))
merge!(paramdict, optparams)
predictions = f(paramdict)
paramsandpredictionsdict = merge(paramdict, predictions)
Expand Down
2 changes: 1 addition & 1 deletion src/MadsCMads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Returns:
"""
function readobservations_cmads(madsdata::AbstractDict)
obsids = getobskeys(madsdata)
observations = OrderedCollections.OrderedDict{String,Float64}(zip(obsids, zeros(length(obsids))))
observations = OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(zip(obsids, zeros(length(obsids))))
for instruction in madsdata["Instructions"]
obs = cmadsins_obs(obsids, instruction["ins"], instruction["read"])
# this loop assumes that cmadsins_obs gives a zero value if the obs is not found, and that each obs will appear only once
Expand Down
8 changes: 4 additions & 4 deletions src/MadsCalibrate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,15 @@ function calibrate(madsdata::AbstractDict; tolX::Number=1e-4, tolG::Number=1e-6,
outfile = open("$rootname.initialresults", "a+")
write(outfile, string("OF: ", of, "\n"))
write(outfile, string("lambda: ", lambda, "\n"))
write(outfile, string(OrderedCollections.OrderedDict{String,Float64}(zip(optparamkeys, x_init_real)), "\n"))
write(outfile, string(OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(zip(optparamkeys, x_init_real)), "\n"))
close(outfile)
end
function interationcallback(x_best::AbstractVector, of::Number, lambda::Number)
x_best_real = sinetransform(x_best, lowerbounds, upperbounds, indexlogtransformed)
outfile = open("$rootname.iterationresults", "a+")
write(outfile, string("OF: ", of, "\n"))
write(outfile, string("lambda: ", lambda, "\n"))
write(outfile, string(OrderedCollections.OrderedDict{String,Float64}(zip(optparamkeys, x_best_real)), "\n"))
write(outfile, string(OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(zip(optparamkeys, x_best_real)), "\n"))
close(outfile)
end
function jacobiancallback(x::AbstractVector, J::AbstractMatrix)
Expand All @@ -263,7 +263,7 @@ function calibrate(madsdata::AbstractDict; tolX::Number=1e-4, tolG::Number=1e-6,
outfile = open("$rootname.finalresults", "a+")
write(outfile, string("OF: ", of, "\n"))
write(outfile, string("lambda: ", lambda, "\n"))
write(outfile, string(OrderedCollections.OrderedDict{String,Float64}(zip(optparamkeys, x_best_real)), "\n"))
write(outfile, string(OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(zip(optparamkeys, x_best_real)), "\n"))
close(outfile)
end
else
Expand All @@ -281,7 +281,7 @@ function calibrate(madsdata::AbstractDict; tolX::Number=1e-4, tolG::Number=1e-6,
end
global modelruns += results.f_calls
minimizer = Mads.sinetransform(results.minimizer, lowerbounds, upperbounds, indexlogtransformed)
minimumdict = OrderedCollections.OrderedDict{String,Float64}(zip(getparamkeys(madsdata), Mads.getparamsinit(madsdata)))
minimumdict = OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(zip(getparamkeys(madsdata), getparamsinit(madsdata)))
for i = eachindex(optparamkeys)
minimumdict[optparamkeys[i]] = minimizer[i]
end
Expand Down
8 changes: 4 additions & 4 deletions src/MadsCreate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ function createobservations!(md::AbstractDict, obs::Union{AbstractVector,Abstrac
end

function createparameters(param::AbstractVector; key::AbstractVector=["p$i" for i=1:length(param)], name::AbstractVector=[], plotname::AbstractVector=[], type::AbstractVector=["opt" for i=1:length(param)], min::AbstractVector=[], max::AbstractVector=[], minorig::AbstractVector=min, maxorig::AbstractVector=max, dist::AbstractVector=[], expressions::AbstractVector=["" for i=1:length(param)], log::AbstractVector=falses(length(param)), distribution::Bool=false)
mdp = OrderedCollections.OrderedDict{String,OrderedCollections.OrderedDict}()
mde = OrderedCollections.OrderedDict{String,OrderedCollections.OrderedDict}()
mdp = OrderedCollections.OrderedDict{Union{String,Symbol},OrderedCollections.OrderedDict}()
mde = OrderedCollections.OrderedDict{Union{String,Symbol},OrderedCollections.OrderedDict}()
@assert length(param) == length(key)
@assert length(param) == length(type)
@assert length(param) == length(log)
Expand Down Expand Up @@ -236,11 +236,11 @@ function createproblem(in::Integer, out::Integer, f::Union{Function,AbstractStri
createproblem(rand(Mads.rng, in), rand(Mads.rng, out), f; kw...)
return nothing
end
function createproblem(param::AbstractVector, obs::Union{AbstractVector,AbstractMatrix}, f::Union{Symbol,Function,AbstractString}; problemname::AbstractString="", paramkey::AbstractVector=["p$i" for i=1:length(param)], paramname::AbstractVector=paramkey, paramplotname::AbstractVector=paramname, paramtype::AbstractVector=["opt" for i=1:length(param)], parammin::AbstractVector=[], parammax::AbstractVector=[], paramlog::AbstractVector=falses(length(param)), paramminorig::AbstractVector=parammin, parammaxorig::AbstractVector=parammax, paramdist::AbstractVector=[], distribution::Bool=false, expressions::AbstractVector=["" for i=1:length(param)], obskey::AbstractVector=["o$i" for i=1:length(obs)], obsweight::AbstractVector=ones(length(obs)), obstime::AbstractVector=[], obsmin::Union{Number,AbstractVector}=[], obsmax::Union{Number,AbstractVector}=[], obsminorig::Union{Number,AbstractVector}=obsmin, obsmaxorig::Union{Number,AbstractVector}=obsmax, obsdist::AbstractVector=[])
function createproblem(param::AbstractVector, obs::Union{AbstractVector,AbstractMatrix}, f::Union{Symbol,Function,AbstractString}; modeltype::AbstractString="Julia function", problemname::AbstractString="", paramkey::AbstractVector=["p$i" for i=1:length(param)], paramname::AbstractVector=paramkey, paramplotname::AbstractVector=paramname, paramtype::AbstractVector=["opt" for i=1:length(param)], parammin::AbstractVector=[], parammax::AbstractVector=[], paramlog::AbstractVector=falses(length(param)), paramminorig::AbstractVector=parammin, parammaxorig::AbstractVector=parammax, paramdist::AbstractVector=[], distribution::Bool=false, expressions::AbstractVector=["" for i=1:length(param)], obskey::AbstractVector=["o$i" for i=1:length(obs)], obsweight::AbstractVector=ones(length(obs)), obstime::AbstractVector=[], obsmin::Union{Number,AbstractVector}=[], obsmax::Union{Number,AbstractVector}=[], obsminorig::Union{Number,AbstractVector}=obsmin, obsmaxorig::Union{Number,AbstractVector}=obsmax, obsdist::AbstractVector=[])
md = Dict{String,Any}()
createparameters!(md, param; key=paramkey, name=paramname, plotname=paramplotname, type=paramtype, min=parammin, max=parammax, minorig=paramminorig, maxorig=parammaxorig, dist=paramdist, distribution=distribution, expressions=expressions, log=paramlog)
createobservations!(md, obs; key=obskey, weight=obsweight, time=obstime, min=obsmin, max=obsmax, minorig=obsminorig, maxorig=obsmaxorig, dist=obsdist, distribution=distribution)
setmodel!(md, f)
setmodel!(md, f, modeltype)
if problemname != ""
md["Filename"] = problemname .* ".mads"
end
Expand Down
2 changes: 1 addition & 1 deletion src/MadsForward.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function forward(madsdata::AbstractDict, paramdict::AbstractDict; all::Bool=fals
paraminitdict = Mads.getparamdict(madsdata)
if l == 1
p = merge(paraminitdict, paramdict)
r = convert(OrderedCollections.OrderedDict{String,Float64}, f(p))
r = convert(OrderedCollections.OrderedDict{Union{String,Symbol},Float64}, f(p))
return r
else
optkeys = Mads.getoptparamkeys(madsdata_c)
Expand Down
6 changes: 3 additions & 3 deletions src/MadsFunc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function makemadscommandfunction(madsdata_in::AbstractDict; obskeys::AbstractVec
"MADS command function based on a Julia function '$(madsdata["Julia function"])'"
function madscommandfunctionvector(parameters::AbstractDict)
o = jf(collect(values(parameters)))
return OrderedCollections.OrderedDict{String,Float64}(zip(Mads.getobskeys(madsdata), o))
return OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(zip(Mads.getobskeys(madsdata), o))
end
madscommandfunction = madscommandfunctionvector
elseif haskey(madsdata, "Julia model")
Expand Down Expand Up @@ -163,7 +163,7 @@ function makemadscommandfunction(madsdata_in::AbstractDict; obskeys::AbstractVec
"MADS command function"
function madscommandfunctionexternal(parameters::AbstractVector)
o = madsdata["Julia function"](parameters)
return OrderedCollections.OrderedDict{String,Float64}(zip(Mads.getobskeys(madsdata), o))
return OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(zip(Mads.getobskeys(madsdata), o))
end
madsdatacommandfunction = madscommandfunctionexternal
else
Expand Down Expand Up @@ -230,7 +230,7 @@ function makemadscommandfunction(madsdata_in::AbstractDict; obskeys::AbstractVec
else
out = madsdatacommandfunction(md)
end
results = convert(OrderedCollections.OrderedDict{String,Float64}, out)
results = convert(OrderedCollections.OrderedDict{Union{String,Symbol},Float64}, out)
trying = false
catch errmsg
if !latest
Expand Down
8 changes: 4 additions & 4 deletions src/MadsIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ argtext=Dict("madsdata"=>"MADS problem dictionary"),
keytext=Dict("obskeys"=>"observation keys [default=getobskeys(madsdata)]")))
"""
function readmodeloutput(madsdata::AbstractDict; obskeys::AbstractVector=getobskeys(madsdata))
results = OrderedCollections.OrderedDict{String,Float64}()
results = OrderedCollections.OrderedDict{Union{String,Symbol},Float64}()
if haskey(madsdata, "Instructions")
results = readobservations(madsdata, obskeys)
end
Expand Down Expand Up @@ -1062,7 +1062,7 @@ function readmodeloutput(madsdata::AbstractDict; obskeys::AbstractVector=getobsk
predictions = loadasciifile(madsdata["ASCIIPredictions"])
obsid=[convert(String,k) for k in obskeys]
@assert length(obskeys) == length(predictions)
results = merge(results, OrderedCollections.OrderedDict{String,Float64}(zip(obsid, predictions)))
results = merge(results, OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(zip(obsid, predictions)))
end
missingkeys = Array{String}(undef, 0)
validtargets = (Mads.getobsweight(madsdata) .> 0) .& .!isnan.(Mads.getobstarget(madsdata))
Expand All @@ -1075,7 +1075,7 @@ function readmodeloutput(madsdata::AbstractDict; obskeys::AbstractVector=getobsk
madswarn("Observations are missing (total count = $(length(missingkeys)))!")
madscritical("Missing observation keys: $(missingkeys)")
end
return convert(OrderedCollections.OrderedDict{String,Float64}, results)
return convert(OrderedCollections.OrderedDict{Union{String,Symbol},Float64}, results)
end

searchdir(key::Regex; path::AbstractString = ".") = filter(x->occursin(key, x), readdir(path))
Expand Down Expand Up @@ -1402,7 +1402,7 @@ Returns:
"""
function readobservations(madsdata::AbstractDict, obskeys::AbstractVector=getobskeys(madsdata))
dictelements = zip(obskeys, zeros(Int, length(obskeys)))
observations = OrderedCollections.OrderedDict{String,Float64}(dictelements)
observations = OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(dictelements)
obscount = OrderedCollections.OrderedDict{String,Int}()
for instruction in madsdata["Instructions"]
obs = ins_obs(instruction["ins"], instruction["read"])
Expand Down
10 changes: 5 additions & 5 deletions src/MadsMisc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ function makearrayfunction_dictionary(madsdata::AbstractDict, f::Function=makema
initparams = Mads.getparamdict(madsdata)
function arrayfunction_merge(arrayparameters::AbstractVector)
@assert length(arrayparameters) == length(optparamkeys)
return f(merge(initparams, OrderedCollections.OrderedDict{String,Float64}(zip(optparamkeys, arrayparameters))))
return f(merge(initparams, OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(zip(optparamkeys, arrayparameters))))
end
function arrayfunction(arrayparameters::AbstractVector)
@assert length(arrayparameters) == length(optparamkeys)
return f(OrderedCollections.OrderedDict{String,Float64}(zip(optparamkeys, arrayparameters)))
return f(OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(zip(optparamkeys, arrayparameters)))
end
if length(initparams) == length(optparamkeys)
return arrayfunction
Expand Down Expand Up @@ -109,7 +109,7 @@ function makearrayconditionalloglikelihood(madsdata::AbstractDict, conditionallo
initparams = Mads.getparamdict(madsdata)
function arrayconditionalloglikelihood(arrayparameters::AbstractVector)
@assert length(arrayparameters) == length(optparamkeys)
predictions = f(merge(initparams, OrderedCollections.OrderedDict{String,Float64}(zip(optparamkeys, arrayparameters))))
predictions = f(merge(initparams, OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(zip(optparamkeys, arrayparameters))))
cll = conditionalloglikelihood(predictions, madsdata["Observations"])
return cll
end
Expand All @@ -134,7 +134,7 @@ function makearrayloglikelihood(madsdata::AbstractDict, loglikelihood)
function arrayloglikelihood(arrayparameters::AbstractVector)
predictions = OrderedCollections.OrderedDict()
@assert length(arrayparameters) == length(optparamkeys)
d = OrderedCollections.OrderedDict{String,Float64}(zip(optparamkeys, arrayparameters))
d = OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(zip(optparamkeys, arrayparameters))
try
predictions = f(merge(initparams, d))
catch
Expand Down Expand Up @@ -184,7 +184,7 @@ function evaluatemadsexpressions(madsdata::AbstractDict, parameters::AbstractDic
end
end
if haskey(madsdata, "Order")
parameters_ordered = OrderedCollections.OrderedDict{String,Float64}()
parameters_ordered = OrderedCollections.OrderedDict{Union{String,Symbol},Float64}()
for k in madsdata["Order"]
parameters_ordered[k] = parameters[k]
end
Expand Down
4 changes: 2 additions & 2 deletions src/MadsMonteCarlo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,14 @@ function montecarlo(madsdata::AbstractDict; compute::Bool=true, N::Integer=100,
end
end
end
paramdicts[i] = OrderedCollections.OrderedDict{String,Float64}(zip(paramkeys, params))
paramdicts[i] = OrderedCollections.OrderedDict{Union{String,Symbol},Float64}(zip(paramkeys, params))
end
if compute
f = makemadscommandfunction(madsdata)
results = RobustPmap.rpmap(f, paramdicts)
outputdicts = Array{OrderedCollections.OrderedDict}(undef, N)
for i = 1:N
outputdicts[i] = OrderedCollections.OrderedDict()
outputdicts[i] = OrderedCollections.OrderedDict{String,OrderedCollections.OrderedDict}()
outputdicts[i]["Parameters"] = paramdicts[i]
outputdicts[i]["Results"] = results[i]
end
Expand Down
Loading

0 comments on commit 17cd62a

Please sign in to comment.