Skip to content

Commit 5c1a2df

Browse files
committed
Fix doctests
1 parent fd0ee1d commit 5c1a2df

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

src/contexts.jl

Lines changed: 22 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,16 @@ 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> collapse_prefix_stack(c2)
706+
ConditionContext(Dict{VarName{:a}, Int64}(a.b.y => 2, a.x => 1), DefaultContext())
695707
```
696708
"""
697709
function collapse_prefix_stack(context::PrefixContext{Prefix}) where {Prefix}
@@ -703,7 +715,14 @@ function collapse_prefix_stack(context::PrefixContext{Prefix}) where {Prefix}
703715
# depth of the context stack.
704716
return prefix_cond_and_fixed_variables(collapsed, VarName{Prefix}())
705717
end
706-
collapse_prefix_stack(context::AbstractContext) = context
718+
function collapse_prefix_stack(context::AbstractContext)
719+
return collapse_prefix_stack(NodeTrait(collapse_prefix_stack, context), context)
720+
end
721+
collapse_prefix_stack(::IsLeaf, context) = context
722+
function collapse_prefix_stack(::IsParent, context)
723+
new_child_context = collapse_prefix_stack(childcontext(context))
724+
return setchildcontext(context, new_child_context)
725+
end
707726

708727
"""
709728
prefix_cond_and_fixed_variables(context::AbstractContext, prefix::VarName)

src/model.jl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -770,29 +770,29 @@ julia> # Returns all the variables we have fixed on + their values.
770770
fixed(fix(m, x=100.0, m=1.0))
771771
(x = 100.0, m = 1.0)
772772
773-
julia> # Nested ones also work (note that `PrefixContext` does nothing to the result).
773+
julia> # The rest of this is the same as the `condition` example above.
774774
cm = fix(contextualize(m, PrefixContext{:a}(fix(m=1.0))), x=100.0);
775775
776776
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
777+
Dict{VarName, Any} with 2 entries:
778+
a.m => 1.0
779+
x => 100.0
784780
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);
781+
julia> keys(VarInfo(cm))
782+
1-element Vector{VarName{:a, Accessors.PropertyLens{:x}}}:
783+
a.x
787784
788-
julia> fixed(cm)[@varname(x)]
789-
100.0
785+
julia> # We can also condition on `a.m` _outside_ of the PrefixContext:
786+
cm = fix(contextualize(m, PrefixContext{:a}(DefaultContext())), (@varname(a.m) => 1.0));
790787
791-
julia> fixed(cm)[@varname(a.m)]
792-
1.0
788+
julia> fixed(cm)
789+
Dict{VarName{:a, Accessors.PropertyLens{:m}}, Float64} with 1 entry:
790+
a.m => 1.0
793791
794-
julia> keys(VarInfo(cm)) # <= no variables are sampled
795-
VarName[]
792+
julia> # Now `a.x` will be sampled.
793+
keys(VarInfo(cm))
794+
1-element Vector{VarName{:a, Accessors.PropertyLens{:x}}}:
795+
a.x
796796
```
797797
"""
798798
fixed(model::Model) = fixed(model.context)

0 commit comments

Comments
 (0)