Skip to content

Commit

Permalink
emcee fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
montyvesselinov committed Nov 13, 2024
1 parent 4b360b6 commit a069b30
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
8 changes: 7 additions & 1 deletion src/MadsForward.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,13 @@ function forward(madsdata::AbstractDict, paramarray::AbstractArray; parallel::Bo
else
r = values(func_forward(paramarray))
end
return collect(r)
if isnothing(r)
@error("Model run failed! Check for errors in the model function and in the output above!")
throw(ErrorException("Model run failed!"))
return nothing
else
return collect(r)
end
end

@doc """
Expand Down
12 changes: 5 additions & 7 deletions src/MadsFunc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,13 @@ function makemadscommandfunction(madsdata_in::AbstractDict; obskeys::AbstractVec
parameterswithexpressions = evaluatemadsexpressions(madsdata, paramsnoexpressions)
local out = nothing
try
out = madscommandfunction(parameterswithexpressions)
out = Base.invokelatest(madscommandfunction, parameterswithexpressions)
catch errmsg
try
out = Base.invokelatest(madscommandfunction, parameterswithexpressions)
catch errmsg
printerrormsg(errmsg)
Mads.madswarn("Failed Dir: $(pwd())")
Mads.madserror("madscommandfunction in madscommandfunctionwithexpressions cannot be executed!")
printerrormsg(errmsg)
if pwd() != Mads.dir
Mads.madswarn("Execution failed in directory: $(pwd())")
end
Mads.madserror("madscommandfunction cannot be executed!")
end
return out
end
Expand Down
8 changes: 3 additions & 5 deletions src/MadsMonteCarlo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ function emceesampling(madsdata::AbstractDict; filename::AbstractString="", load
chain, llhoods, observations = emceesampling(madsdata, p0; filename=filename, load=load, save=save, execute=execute, numwalkers=numwalkers, nexecutions=nexecutions, burnin=burnin, thinning=thinning, seed=seed, rng=rng, kw...)
return chain, llhoods, observations
end
function emceesampling(madsdata::AbstractDict, p0::AbstractMatrix; filename::AbstractString="", load::Bool=false, save::Bool=false, execute::Bool=true, numwalkers::Integer=10, nexecutions::Integer=100, burnin::Integer=numwalkers, thinning::Integer=10, seed::Integer=-1, weightfactor::Number=1.0, rng::Union{Nothing, Random.AbstractRNG, DataType}=nothing, distributed_function::Bool=false)
function emceesampling(madsdata::AbstractDict, p0::AbstractMatrix; filename::AbstractString="", load::Bool=false, save::Bool=false, execute::Bool=true, numwalkers::Integer=10, nexecutions::Integer=100, burnin::Integer=numwalkers, thinning::Integer=10, seed::Integer=-1, weightfactor::Number=1.0, rng::Union{Nothing, Random.AbstractRNG, DataType}=nothing, distributed_function::Bool=false, type::DataType=Float64, checkoutputs::Bool=true)
if filename != ""
save = true
end
Expand Down Expand Up @@ -178,7 +178,6 @@ function emceesampling(madsdata::AbstractDict, p0::AbstractMatrix; filename::Abs
Mads.setseed(seed; rng=rng)
madsloglikelihood = makemadsloglikelihood(madsdata; weightfactor=weightfactor)
arrayloglikelihood = Mads.makearrayloglikelihood(madsdata, madsloglikelihood)
sleep(1) # rest to avoid a "method too new" error
if distributed_function
Distributed.@everywhere arrayloglikelihood_distributed = Mads.makearrayloglikelihood($madsdata, $madsloglikelihood)
arrayloglikelihood = (x) -> Core.eval(Main, :arrayloglikelihood_distributed)(x)
Expand All @@ -189,11 +188,10 @@ function emceesampling(madsdata::AbstractDict, p0::AbstractMatrix; filename::Abs
end
numsamples = numsamples_perwalker_burnin * numwalkers
@info("AffineInvariantMCMC burning stage (total number of executions $(numsamples), final burning chain size $(numsamples_perwalker_burnin * numwalkers))...")
sleep(5)
burninchain, _ = AffineInvariantMCMC.sample(arrayloglikelihood, numwalkers, p0, numsamples_perwalker_burnin, 1; filename="", load=false, save=false, rng=Mads.rng)
burninchain, _ = AffineInvariantMCMC.sample(arrayloglikelihood, numwalkers, p0, numsamples_perwalker_burnin, 1; filename="", load=false, save=false, rng=Mads.rng, type=type, checkoutputs=checkoutputs)
numsamples = numsamples_perwalker * numwalkers
@info("AffineInvariantMCMC exploration stage (total number of executions $(numsamples), final chain size $(div(numsamples_perwalker, thinning) * numwalkers))...")
chain, llhoods = AffineInvariantMCMC.sample(arrayloglikelihood, numwalkers, burninchain[:, :, end], numsamples_perwalker, thinning; filename="", load=false, save=false, rng=Mads.rng)
chain, llhoods = AffineInvariantMCMC.sample(arrayloglikelihood, numwalkers, burninchain[:, :, end], numsamples_perwalker, thinning; filename="", load=false, save=false, rng=Mads.rng, type=type, checkoutputs=checkoutputs)
chain, llhoods = AffineInvariantMCMC.flattenmcmcarray(chain, llhoods)
if save
madsinfo("Saving AffineInvariantMCMC results in $(filename) ...")
Expand Down

0 comments on commit a069b30

Please sign in to comment.