diff --git a/src/TOMLInterface.jl b/src/TOMLInterface.jl index f233ce297..5f613a31d 100644 --- a/src/TOMLInterface.jl +++ b/src/TOMLInterface.jl @@ -56,6 +56,10 @@ function get_parameter_distribution(param_dict::Dict, name::AbstractString) # Constructing a parameter distribution requires a prior distribution, # a constraint, and a name. prior = construct_prior(param_dict[name]) + # If constrained_gaussian, then prior is already a ParameterDistribution + if prior isa ParameterDistribution + return prior + end constraint = construct_constraint(param_dict[name]) return ParameterDistribution(prior, constraint, name) @@ -243,6 +247,14 @@ function get_distribution_from_expr(d::Expr) return dist_type(dist_args) + elseif dist_type_symb == Symbol("constrained_gaussian") + # Check for non-positional kwargs + if d.args[2] isa Expr && d.args[2].args[1].head == :kw + d.args[3] = string(d.args[3]) + else + d.args[2] = string(d.args[2]) + end + return eval(d) else throw(ArgumentError("Unknown distribution type from symbol: $(dist_type_symb)")) end diff --git a/test/TOMLInterface/runtests.jl b/test/TOMLInterface/runtests.jl index 4824e3a95..875c8feed 100644 --- a/test/TOMLInterface/runtests.jl +++ b/test/TOMLInterface/runtests.jl @@ -51,6 +51,26 @@ const EKP = EnsembleKalmanProcesses [no_constraint(), no_constraint(), bounded_below(-5.0)], "uq_param_8", ), + "uq_param_9" => + ParameterDistribution(Parameterized(Normal(4.0, 0.17881264846405112)), [bounded(0, Inf)], "uq_param_9"), + "uq_param_10" => ParameterDistribution( + VectorOfParameterized([ + Normal(4.0, 0.17881264846405112), + Normal(4.0, 0.17881264846405112), + Normal(4.0, 0.17881264846405112), + ]), + [bounded(0, Inf), bounded(0, Inf), bounded(0, Inf)], + "uq_param_10", + ), + "uq_param_11" => ParameterDistribution( + VectorOfParameterized([ + Normal(4.0, 0.17881264846405112), + Normal(4.0, 0.17881264846405112), + Normal(4.0, 0.17881264846405112), + ]), + [bounded(0, Inf), bounded(0, Inf), bounded(0, Inf)], + "uq_param_11", + ), ) # Get all `ParameterDistribution`s. We also add dummy (key, value) pairs diff --git a/test/TOMLInterface/toml/uq_test_parameters.toml b/test/TOMLInterface/toml/uq_test_parameters.toml index 5ebfc33ac..3ec7ce887 100644 --- a/test/TOMLInterface/toml/uq_test_parameters.toml +++ b/test/TOMLInterface/toml/uq_test_parameters.toml @@ -40,6 +40,19 @@ constraint = "repeat([no_constraint()], 3)" prior = "VectorOfParameterized([Gamma(2.0, 3.0), LogNormal(0.1, 0.1), Normal(0.0, 10.0)])" constraint = "[no_constraint(), no_constraint(), bounded_below(-5.0)]" +# Test for `constrained_gaussian` constructor +# Basic +[uq_param_9] +prior = "constrained_gaussian(uq_param_9, 55.47802418037957, 10, 0, Inf)" + +# Non-positional kwarg +[uq_param_10] +prior = "constrained_gaussian(uq_param_10, 55.47802418037957, 10, 0, Inf; repeats = 3, optim_algorithm = NelderMead())" + +# Positional kwarg +[uq_param_11] +prior = "constrained_gaussian(uq_param_11, 55.47802418037957, 10, 0, Inf, repeats = 3, optim_algorithm = NelderMead())" + # The six parameters below are interpreted as "regular" (non-UQ) parameters, as they # they either have no key "prior", or a key "prior" that is set to "fixed" [mean_sea_level_pressure]