From db1f96c1c9e9b7378f6d7eabe2f0f05ed83f0bcf Mon Sep 17 00:00:00 2001 From: Penelope Yong Date: Sun, 4 May 2025 00:12:09 +0100 Subject: [PATCH] Add pretty-printing for context stacks --- Project.toml | 2 +- src/contexts.jl | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 83b1fe335..860ece8b3 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "DynamicPPL" uuid = "366bfd00-2699-11ea-058f-f148b4cae6d8" -version = "0.36.1" +version = "0.36.2" [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))