def test_lists(): sampler = ScenicSampler.fromScenicCode( 'ego = Object with foo [1, -1, 3.3]', maxIterations=1) sample = sampler.nextSample() foo = sample.objects.object0.foo assert type(foo) is tuple assert foo == pytest.approx((1, -1, 3.3))
def test_quoted_param(): sampler = ScenicSampler.fromScenicCode( 'param "x/y" = (3, 5)\n' 'ego = Object', maxIterations=1) sample = sampler.nextSample() v = sampler.paramDictForSample(sample)['x/y'] assert type(v) is float assert 3 <= v <= 5
def test_params(): sampler = ScenicSampler.fromScenicCode('param x = (3, 5)\n' 'ego = Object', maxIterations=1) sample = sampler.nextSample() x = sample.params.x assert type(x) is float assert 3 <= x <= 5
def test_objects(): sampler = ScenicSampler.fromScenicCode('ego = Object at 4 @ 9', maxIterations=1) sample = sampler.nextSample() objects = sample.objects assert len(objects) == 1 pos = objects.object0.position assert type(pos) is tuple assert pos == (4, 9)
def test_object_order(): sampler = ScenicSampler.fromScenicCode( 'ego = Object at 0 @ 0\n' 'for i in range(1, 11):\n' ' Object at 2*i @ 0', maxIterations=1) sample = sampler.nextSample() objects = sample.objects assert len(objects) == 11 for i, obj in enumerate(objects): assert obj.position == pytest.approx((2 * i, 0))
def test_active_sampling(): sampler = ScenicSampler.fromScenicCode( 'from dotmap import DotMap\n' 'param verifaiSamplerType = "ce"\n' 'ce_params = DotMap(alpha=0.9)\n' 'ce_params.cont.buckets = 2\n' 'param verifaiSamplerParams = ce_params\n' 'ego = Object at VerifaiRange(-1, 1) @ 0', maxIterations=1) def f(sample): return -1 if sample.objects.object0.position[0] < 0 else 1 samples = sampleWithFeedback(sampler, 120, f) xs = [sample.objects.object0.position[0] for sample in samples] assert all(-1 <= x <= 1 for x in xs) assert any(x > 0 for x in xs) assert 66 <= sum(x < 0 for x in xs[50:])
def test_active_save_restore(tmpdir): sampler = ScenicSampler.fromScenicCode( 'param verifaiSamplerType = "halton"\n' 'ego = Object at VerifaiRange(-1, 1) @ 0', maxIterations=1) checkSaveRestore(sampler, tmpdir)
def test_save_restore(tmpdir): sampler = ScenicSampler.fromScenicCode('ego = Object at (-1, 1) @ 0', maxIterations=1) checkSaveRestore(sampler, tmpdir)