Skip to content

Add show_context_stack(::AbstractContext) #909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -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()`.
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
30 changes: 30 additions & 0 deletions src/contexts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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