From 917057b6af30f168e881bb0fa1145bf4715f12a7 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Tue, 3 Jun 2025 11:36:52 +0100 Subject: [PATCH 1/2] Add pretty-printing for context stacks --- HISTORY.md | 4 ++++ Project.toml | 2 +- src/contexts.jl | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) 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..b7bac0066 100644 --- a/src/contexts.jl +++ b/src/contexts.jl @@ -817,3 +817,17 @@ function prefix_cond_and_fixed_variables( context, prefix_cond_and_fixed_variables(childcontext(context), prefix) ) end + +_pretty(ctx::AbstractContext) = split(string(ctx), "Context")[1] +""" + show_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`. +""" +show_stack(ctx::AbstractContext) = show_stack(NodeTrait(ctx), ctx) +show_stack(::IsLeaf, ctx) = _pretty(ctx) +show_stack(::IsParent, ctx) = _pretty(ctx) * "->" * show_stack(childcontext(ctx)) From 00a4b6b33079877f5d4275a22c17f2b9dfe2ebe8 Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Tue, 3 Jun 2025 11:35:35 +0100 Subject: [PATCH 2/2] Remove module prefixes, add doctests --- src/contexts.jl | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/contexts.jl b/src/contexts.jl index b7bac0066..5badd8a2a 100644 --- a/src/contexts.jl +++ b/src/contexts.jl @@ -818,16 +818,32 @@ function prefix_cond_and_fixed_variables( ) end -_pretty(ctx::AbstractContext) = split(string(ctx), "Context")[1] +## 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_stack(ctx::AbstractContext) + 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_stack(ctx::AbstractContext) = show_stack(NodeTrait(ctx), ctx) -show_stack(::IsLeaf, ctx) = _pretty(ctx) -show_stack(::IsParent, ctx) = _pretty(ctx) * "->" * show_stack(childcontext(ctx)) +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