@@ -11,6 +11,7 @@ using DynamicPPL:
11
11
PointwiseLogdensityContext,
12
12
contextual_isassumption,
13
13
ConditionContext,
14
+ decondition_context,
14
15
hasconditioned,
15
16
getconditioned,
16
17
hasconditioned_nested,
196
197
@test EnzymeCore. EnzymeRules. inactive_type (typeof (context))
197
198
end
198
199
200
+ @testset " ConditionContext" begin
201
+ @testset " Nesting" begin
202
+ @testset " NamedTuple" begin
203
+ n1 = (x= 1 , y= 2 )
204
+ n2 = (x= 3 ,)
205
+ # Values from outer context should override inner one
206
+ ctx1 = ConditionContext (n1, ConditionContext (n2))
207
+ @test ctx1. values == (x= 1 , y= 2 )
208
+ # Check that the two ConditionContexts are collapsed
209
+ @test childcontext (ctx1) isa DefaultContext
210
+ # Then test the nesting the other way round
211
+ ctx2 = ConditionContext (n2, ConditionContext (n1))
212
+ @test ctx2. values == (x= 3 , y= 2 )
213
+ @test childcontext (ctx2) isa DefaultContext
214
+ end
215
+
216
+ @testset " Dict" begin
217
+ # Same tests as NamedTuple above
218
+ d1 = Dict (@varname (x) => 1 , @varname (y) => 2 )
219
+ d2 = Dict (@varname (x) => 3 )
220
+ ctx1 = ConditionContext (d1, ConditionContext (d2))
221
+ @test ctx1. values == Dict (@varname (x) => 1 , @varname (y) => 2 )
222
+ @test childcontext (ctx1) isa DefaultContext
223
+ ctx2 = ConditionContext (d2, ConditionContext (d1))
224
+ @test ctx2. values == Dict (@varname (x) => 3 , @varname (y) => 2 )
225
+ @test childcontext (ctx2) isa DefaultContext
226
+ end
227
+ end
228
+
229
+ @testset " decondition_context" begin
230
+ @testset " NamedTuple" begin
231
+ ctx = ConditionContext ((x= 1 , y= 2 , z= 3 ))
232
+ # Decondition all variables
233
+ @test decondition_context (ctx) isa DefaultContext
234
+ # Decondition only some variables
235
+ dctx = decondition_context (ctx, :x )
236
+ @test dctx isa ConditionContext
237
+ @test dctx. values == (y= 2 , z= 3 )
238
+ dctx = decondition_context (ctx, :y , :z )
239
+ @test dctx isa ConditionContext
240
+ @test dctx. values == (x= 1 ,)
241
+ # Decondition all variables manually
242
+ @test decondition_context (ctx, :x , :y , :z ) isa DefaultContext
243
+ end
244
+
245
+ @testset " Dict" begin
246
+ ctx = ConditionContext (Dict (@varname (x) => 1 , @varname (y) => 2 , @varname (z) => 3 ))
247
+ # Decondition all variables
248
+ @test decondition_context (ctx) isa DefaultContext
249
+ # Decondition only some variables
250
+ dctx = decondition_context (ctx, @varname (x))
251
+ @test dctx isa ConditionContext
252
+ @test dctx. values == Dict (@varname (y) => 2 , @varname (z) => 3 )
253
+ dctx = decondition_context (ctx, @varname (y), @varname (z))
254
+ @test dctx isa ConditionContext
255
+ @test dctx. values == Dict (@varname (x) => 1 )
256
+ # Decondition all variables manually
257
+ @test decondition_context (ctx, @varname (x), @varname (y), @varname (z)) isa DefaultContext
258
+ end
259
+ end
260
+ end
261
+
199
262
@testset " FixedContext" begin
200
263
@testset " $(model. f) " for model in DynamicPPL. TestUtils. DEMO_MODELS
201
264
retval = model ()
0 commit comments