diff --git a/HISTORY.md b/HISTORY.md index a0f91a494..46d84fe8a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,9 @@ # DynamicPPL Changelog +## 0.36.12 + +Added an unexported method, `DynamicPPL.show_context_stack`, for minimalistic printing of context stacks (which is useful when debugging e.g. submodels). + ## 0.36.11 Make `ThreadSafeVarInfo` hold a total of `Threads.nthreads() * 2` logp values, instead of just `Threads.nthreads()`. diff --git a/Project.toml b/Project.toml index 362035eb7..5108d9aa9 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "DynamicPPL" uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8" -version = "0.36.11" +version = "0.36.12" [deps] ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b" diff --git a/src/contexts.jl b/src/contexts.jl index 8ac085663..5badd8a2a 100644 --- a/src/contexts.jl +++ b/src/contexts.jl @@ -817,3 +817,33 @@ function prefix_cond_and_fixed_variables( context, prefix_cond_and_fixed_variables(childcontext(context), prefix) ) end + +## Pretty-printing context stacks + +# splitting on the dot gets rid of module prefixes +_pretty_context(ctx::AbstractContext) = split(split(string(ctx), "Context")[1], ".")[end] + +""" + show_context_stack(ctx::AbstractContext) + +Return a minimalistic string representation of the context stack `ctx`. Useful +for debugging complicated context problems, e.g. with submodels. + +For example, `SamplingContext(ConditionContext(..., DefaultContext())` will +print as `Sampling->Condition->Default`. + +```jldoctest +julia> using DynamicPPL: @varname, show_context_stack, ConditionContext, PrefixContext + +julia> c1 = ConditionContext((a=1, )); show_context_stack(c1) +"Condition->Default" + +julia> p1 = PrefixContext(@varname(y), c1); show_context_stack(p1) +"Prefix->Condition->Default" +``` +""" +show_context_stack(ctx::AbstractContext) = show_context_stack(NodeTrait(ctx), ctx) +show_context_stack(::IsLeaf, ctx) = _pretty_context(ctx) +function show_context_stack(::IsParent, ctx) + return _pretty_context(ctx) * "->" * show_context_stack(childcontext(ctx)) +end