@@ -47,7 +47,7 @@ effectively updating the child context.
4747```jldoctest
4848julia> using DynamicPPL: DynamicTransformationContext
4949
50- julia> ctx = SamplingContext( );
50+ julia> ctx = ConditionContext((; a = 1 );
5151
5252julia> DynamicPPL.childcontext(ctx)
5353DefaultContext()
@@ -121,73 +121,6 @@ setleafcontext(::IsLeaf, ::IsParent, left, right) = right
121121setleafcontext (:: IsLeaf , :: IsLeaf , left, right) = right
122122
123123# Contexts
124- """
125- SamplingContext(
126- [rng::Random.AbstractRNG=Random.default_rng()],
127- [sampler::AbstractSampler=SampleFromPrior()],
128- [context::AbstractContext=DefaultContext()],
129- )
130-
131- Create a context that allows you to sample parameters with the `sampler` when running the model.
132- The `context` determines how the returned log density is computed when running the model.
133-
134- See also: [`DefaultContext`](@ref)
135- """
136- struct SamplingContext{S<: AbstractSampler ,C<: AbstractContext ,R} <: AbstractContext
137- rng:: R
138- sampler:: S
139- context:: C
140- end
141-
142- function SamplingContext (
143- rng:: Random.AbstractRNG = Random. default_rng (), sampler:: AbstractSampler = SampleFromPrior ()
144- )
145- return SamplingContext (rng, sampler, DefaultContext ())
146- end
147-
148- function SamplingContext (
149- sampler:: AbstractSampler , context:: AbstractContext = DefaultContext ()
150- )
151- return SamplingContext (Random. default_rng (), sampler, context)
152- end
153-
154- function SamplingContext (rng:: Random.AbstractRNG , context:: AbstractContext )
155- return SamplingContext (rng, SampleFromPrior (), context)
156- end
157-
158- function SamplingContext (context:: AbstractContext )
159- return SamplingContext (Random. default_rng (), SampleFromPrior (), context)
160- end
161-
162- NodeTrait (context:: SamplingContext ) = IsParent ()
163- childcontext (context:: SamplingContext ) = context. context
164- function setchildcontext (parent:: SamplingContext , child)
165- return SamplingContext (parent. rng, parent. sampler, child)
166- end
167-
168- """
169- hassampler(context)
170-
171- Return `true` if `context` has a sampler.
172- """
173- hassampler (:: SamplingContext ) = true
174- hassampler (context:: AbstractContext ) = hassampler (NodeTrait (context), context)
175- hassampler (:: IsLeaf , context:: AbstractContext ) = false
176- hassampler (:: IsParent , context:: AbstractContext ) = hassampler (childcontext (context))
177-
178- """
179- getsampler(context)
180-
181- Return the sampler of the context `context`.
182-
183- This will traverse the context tree until it reaches the first [`SamplingContext`](@ref),
184- at which point it will return the sampler of that context.
185- """
186- getsampler (context:: SamplingContext ) = context. sampler
187- getsampler (context:: AbstractContext ) = getsampler (NodeTrait (context), context)
188- getsampler (:: IsParent , context:: AbstractContext ) = getsampler (childcontext (context))
189- getsampler (:: IsLeaf , :: AbstractContext ) = error (" No sampler found in context" )
190-
191124"""
192125 struct DefaultContext <: AbstractContext end
193126
@@ -280,41 +213,6 @@ function prefix_and_strip_contexts(::IsParent, ctx::AbstractContext, vn::VarName
280213 return vn, setchildcontext (ctx, new_ctx)
281214end
282215
283- """
284- prefix(model::Model, x::VarName)
285- prefix(model::Model, x::Val{sym})
286- prefix(model::Model, x::Any)
287-
288- Return `model` but with all random variables prefixed by `x`, where `x` is either:
289- - a `VarName` (e.g. `@varname(a)`),
290- - a `Val{sym}` (e.g. `Val(:a)`), or
291- - for any other type, `x` is converted to a Symbol and then to a `VarName`. Note that
292- this will introduce runtime overheads so is not recommended unless absolutely
293- necessary.
294-
295- # Examples
296-
297- ```jldoctest
298- julia> using DynamicPPL: prefix
299-
300- julia> @model demo() = x ~ Dirac(1)
301- demo (generic function with 2 methods)
302-
303- julia> rand(prefix(demo(), @varname(my_prefix)))
304- (var"my_prefix.x" = 1,)
305-
306- julia> rand(prefix(demo(), Val(:my_prefix)))
307- (var"my_prefix.x" = 1,)
308- ```
309- """
310- prefix (model:: Model , x:: VarName ) = contextualize (model, PrefixContext (x, model. context))
311- function prefix (model:: Model , x:: Val{sym} ) where {sym}
312- return contextualize (model, PrefixContext (VarName {sym} (), model. context))
313- end
314- function prefix (model:: Model , x)
315- return contextualize (model, PrefixContext (VarName {Symbol(x)} (), model. context))
316- end
317-
318216"""
319217
320218 ConditionContext{Values<:Union{NamedTuple,AbstractDict},Ctx<:AbstractContext}
0 commit comments