Skip to content

Commit 523f411

Browse files
committed
Fix doctests
1 parent 32c6a37 commit 523f411

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

src/contexts.jl

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ function conditioned(context::ConditionContext)
502502
# precedence over decendants of `context`.
503503
return _merge(context.values, conditioned(childcontext(context)))
504504
end
505-
function conditioned(context::PrefixContext{Prefix}) where {Prefix}
505+
function conditioned(context::PrefixContext)
506506
return conditioned(collapse_prefix_stack(context))
507507
end
508508

@@ -681,6 +681,9 @@ function fixed(context::FixedContext)
681681
# precedence over decendants of `context`.
682682
return _merge(context.values, fixed(childcontext(context)))
683683
end
684+
function fixed(context::PrefixContext)
685+
return fixed(collapse_prefix_stack(context))
686+
end
684687

685688
"""
686689
collapse_prefix_stack(context::AbstractContext)
@@ -691,7 +694,22 @@ the `PrefixContext`s from the context stack.
691694
```jldoctest
692695
julia> using DynamicPPL: collapse_prefix_stack
693696
694-
julia> c1 = PrefixContext({:a}(ConditionContext((x=1, )))
697+
julia> c1 = PrefixContext{:a}(ConditionContext((x=1, )));
698+
699+
julia> collapse_prefix_stack(c1)
700+
ConditionContext(Dict(a.x => 1), DefaultContext())
701+
702+
julia> # Here, `x` gets prefixed only with `a`, whereas `y` is prefixed with both.
703+
c2 = PrefixContext{:a}(ConditionContext((x=1, ), PrefixContext{:b}(ConditionContext((y=2,)))));
704+
705+
julia> collapsed = collapse_prefix_stack(c2);
706+
707+
julia> # `collapsed` really looks something like this:
708+
# ConditionContext(Dict{VarName{:a}, Int64}(a.b.y => 2, a.x => 1), DefaultContext())
709+
# To avoid fragility arising from the order of the keys in the doctest, we test
710+
# this indirectly:
711+
collapsed.values[@varname(a.x)], collapsed.values[@varname(a.b.y)]
712+
(1, 2)
695713
```
696714
"""
697715
function collapse_prefix_stack(context::PrefixContext{Prefix}) where {Prefix}
@@ -703,7 +721,14 @@ function collapse_prefix_stack(context::PrefixContext{Prefix}) where {Prefix}
703721
# depth of the context stack.
704722
return prefix_cond_and_fixed_variables(collapsed, VarName{Prefix}())
705723
end
706-
collapse_prefix_stack(context::AbstractContext) = context
724+
function collapse_prefix_stack(context::AbstractContext)
725+
return collapse_prefix_stack(NodeTrait(collapse_prefix_stack, context), context)
726+
end
727+
collapse_prefix_stack(::IsLeaf, context) = context
728+
function collapse_prefix_stack(::IsParent, context)
729+
new_child_context = collapse_prefix_stack(childcontext(context))
730+
return setchildcontext(context, new_child_context)
731+
end
707732

708733
"""
709734
prefix_cond_and_fixed_variables(context::AbstractContext, prefix::VarName)

src/model.jl

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,8 @@ julia> # Nested ones also work.
431431
# container has to be broadened to a `Dict`.)
432432
cm = condition(contextualize(m, PrefixContext{:a}(ConditionContext((m=1.0,)))), x=100.0);
433433
434-
julia> conditioned(cm)
435-
Dict{VarName, Any} with 2 entries:
436-
a.m => 1.0
437-
x => 100.0
434+
julia> Set(keys(conditioned(cm))) == Set([@varname(a.m), @varname(x)])
435+
true
438436
439437
julia> # Since we conditioned on `a.m`, it is not treated as a random variable.
440438
# However, `a.x` will still be a random variable.
@@ -770,29 +768,27 @@ julia> # Returns all the variables we have fixed on + their values.
770768
fixed(fix(m, x=100.0, m=1.0))
771769
(x = 100.0, m = 1.0)
772770
773-
julia> # Nested ones also work (note that `PrefixContext` does nothing to the result).
771+
julia> # The rest of this is the same as the `condition` example above.
774772
cm = fix(contextualize(m, PrefixContext{:a}(fix(m=1.0))), x=100.0);
775773
776-
julia> fixed(cm)
777-
(x = 100.0, m = 1.0)
778-
779-
julia> # Since we fixed on `m`, not `a.m` as it will appear after prefixed,
780-
# `a.m` is treated as a random variable.
781-
keys(VarInfo(cm))
782-
1-element Vector{VarName{:a, Accessors.PropertyLens{:m}}}:
783-
a.m
774+
julia> Set(keys(fixed(cm))) == Set([@varname(a.m), @varname(x)])
775+
true
784776
785-
julia> # If we instead fix on `a.m`, `m` in the model will be considered an observation.
786-
cm = fix(contextualize(m, PrefixContext{:a}(fix(@varname(a.m) => 1.0,))), x=100.0);
777+
julia> keys(VarInfo(cm))
778+
1-element Vector{VarName{:a, Accessors.PropertyLens{:x}}}:
779+
a.x
787780
788-
julia> fixed(cm)[@varname(x)]
789-
100.0
781+
julia> # We can also condition on `a.m` _outside_ of the PrefixContext:
782+
cm = fix(contextualize(m, PrefixContext{:a}(DefaultContext())), (@varname(a.m) => 1.0));
790783
791-
julia> fixed(cm)[@varname(a.m)]
792-
1.0
784+
julia> fixed(cm)
785+
Dict{VarName{:a, Accessors.PropertyLens{:m}}, Float64} with 1 entry:
786+
a.m => 1.0
793787
794-
julia> keys(VarInfo(cm)) # <= no variables are sampled
795-
VarName[]
788+
julia> # Now `a.x` will be sampled.
789+
keys(VarInfo(cm))
790+
1-element Vector{VarName{:a, Accessors.PropertyLens{:x}}}:
791+
a.x
796792
```
797793
"""
798794
fixed(model::Model) = fixed(model.context)

0 commit comments

Comments
 (0)