def testObserve(self): self.sivm.observe( [v.symbol('normal'), v.number(0), v.number(1)], v.number(1), label='obs') assert_equal(self.extractValue(self.sivm.report('obs')), 1)
def test_observe(self): inst = { 'instruction':'observe', 'expression': ['normal',v.number(1),v.number(2)], 'value': v.real(3) } o = self.sivm.execute_instruction(inst) self.assertIsInstance(o['directive_id'],(int,float))
def test_predict(self): inst = { 'instruction': 'predict', 'expression': ['add', v.number(1), v.number(2)], } val = v.number(3) o = self.sivm.execute_instruction(inst) self.assertIsInstance(o['directive_id'], (int, float)) self.assertEquals(o['value'], val)
def test_force(self): inst = { 'instruction': 'force', 'expression': ['normal', v.number(1), v.number(2)], 'value': v.real(3) } self.sivm.execute_instruction(inst) o2 = self.sivm.list_directives() self.assertEquals(o2, [])
def test_sample(self): inst = { 'instruction': 'sample', 'expression': ['add', v.number(1), v.number(2)], } val = v.number(3) o = self.sivm.execute_instruction(inst) self.assertEquals(o['value'], val) o2 = self.sivm.list_directives() self.assertEquals(o2, [])
def test_assume(self): inst = { 'instruction': 'assume', 'expression': ['add', v.number(1), v.number(2)], 'symbol': v.symbol('moo') } val = v.number(3) o = self.sivm.execute_instruction(inst) self.assertIsInstance(o['directive_id'], (int, float)) self.assertEquals(o['value'], val)
def test_observe_fail(self): raise SkipTest("Engine should report a polite exception on constraint of a deterministic choice. Issue: https://app.asana.com/0/9277419963067/9940667562268") inst = { 'instruction':'observe', 'expression': ['add',v.number(1),v.number(2)], 'value': v.real(4) } try: self.sivm.execute_instruction(inst) except VentureException as e: self.assertEquals(e.exception, 'invalid_constraint')
def test_get_directive(self): inst1 = { 'instruction': 'predict', 'expression': [v.symbol('add'), v.number(1), v.number(2)], } o1 = self.sivm.execute_instruction(inst1) inst1['directive_id'] = o1['directive_id'] o2 = self.sivm.get_directive(o1['directive_id']) self.assertEquals(o2, inst1)
def test_double_symbol(self): output = self.p.parse_instruction('[predict (>= 1 1)]') expected = { 'expression': [ v.symbol('predict'), [v.symbol('gte'), v.number(1.0), v.number(1.0)] ], 'instruction': 'evaluate' } self.assertEqual(output, expected)
def test_report(self): inst1 = { 'instruction': 'predict', 'expression': ['add', v.number(1), v.number(2)], } o1 = self.sivm.execute_instruction(inst1) inst2 = { 'instruction': 'report', 'directive_id': o1['directive_id'], } o2 = self.sivm.execute_instruction(inst2) self.assertEquals(o2['value'], v.number(3))
def test_invalid_label(self): inst = { 'instruction': 'labeled_assume', 'expression': ['add', v.number(1), v.number(2)], 'symbol': v.symbol('moo'), 'label': v.symbol('123moo') } try: self.sivm.execute_instruction(inst) except VentureException as e: self.assertEqual(e.exception, 'invalid_argument') self.assertEqual(e.data['argument'], 'label')
def test_labeled_report(self): inst1 = { 'instruction': 'labeled_predict', 'expression': ['add', v.number(1), v.number(2)], 'label': 'moo', } self.sivm.execute_instruction(inst1) inst2 = { 'instruction': 'labeled_report', 'label': v.symbol('moo'), } o2 = self.sivm.execute_instruction(inst2) self.assertEquals(o2['value'], v.number(3))
def _ensure_parsed_expression(self, expr): languages = self._languages if isinstance(expr, basestring): answer = self._cur_parser().parse_expression(expr, languages) if isinstance(answer, basestring): # Was a symbol; wrap it in a stack dict to prevent it # from being processed again. return {'type': 'symbol', 'value': answer} else: return answer elif isinstance(expr, list) or isinstance(expr, tuple): return [self._ensure_parsed_expression(e) for e in expr] elif isinstance(expr, dict): # A literal value as a stack dict. These are all assumed # fully parsed. return expr elif isinstance(expr, int): return v.integer(expr) elif isinstance(expr, numbers.Number): return v.number(expr) elif isinstance(expr, VentureValue): # A literal value as a Venture Value return expr.asStackDict(None) else: raise Exception("Unknown partially parsed expression type %s" % expr)
def testInferReturn(): # Make sure that execute_program returns results from infer commands ripl = get_ripl() prog = '[INFER (return (+ 5 3))]' ripl.infer('(resample 3)') res = ripl.execute_program(prog)[-1]['value'] eq_(res, v.number(8.0))
def test_execute_instruction(self): f = self.ripl.execute_instruction f("[assume a 1]") f("[assume b (+ 1 2)]") f("[assume c (- b a)]") ret_value= f("[predict c]") self.assertEqual(ret_value['value'], v.number(2))
def test_infer(self): # Infer # self.run_test( ' infer 132', [{'loc':j(1,5,7,3), 'value':{ 'instruction' : {'loc':j(1,5), 'value':'infer'}, 'expression' : {'loc':j(7,3), 'value':v.number(132.0)}, }}])
def test_forget(self): inst1 = { 'instruction': 'predict', 'expression': ['add', v.number(1), v.number(2)], } o1 = self.sivm.execute_instruction(inst1) inst2 = { 'instruction': 'forget', 'directive_id': o1['directive_id'], } self.sivm.execute_instruction(inst2) try: self.sivm.execute_instruction(inst2) except VentureException as e: self.assertEquals(e.exception, 'invalid_argument')
def test_labeled_forget(self): inst1 = { 'instruction': 'labeled_predict', 'expression': ['add', v.number(1), v.number(2)], 'label': v.symbol('moo'), } self.sivm.execute_instruction(inst1) inst2 = { 'instruction': 'labeled_forget', 'label': v.symbol('moo'), } self.sivm.execute_instruction(inst2) try: self.sivm.execute_instruction(inst2) except VentureException as e: self.assertEquals(e.exception, 'invalid_argument') o3 = self.sivm.list_directives() self.assertEquals(o3, [])
def toVenture(thing): if isinstance(thing, dict): return venturedicts.val("dict", [(toVenture(k),toVenture(v)) for k, v in thing.iteritems()]) if isinstance(thing, (list, tuple)): return venturedicts.val("array", [toVenture(v) for v in thing]) if isinstance(thing, (int, float)): return venturedicts.number(thing) if isinstance(thing, str): return venturedicts.symbol(thing)
def test_program(self): self.run_test( 'define blah = count<132>;infer 132', [{'loc':j(0,6,7,4,12,1,14,10), 'value':{ 'instruction' : {'loc':j(0,6), 'value':'define'}, 'symbol' : {'loc':j(7,4), 'value':v.sym('blah')}, 'expression' : {'loc':j(14,10), 'value':{'type':'count', 'value':132.0}}, }},{'loc':j(25,5,31,3), 'value':{ 'instruction' : {'loc':j(25,5), 'value':'infer'}, 'expression' : {'loc':j(31,3), 'value':v.number(132.0)}, }}])
def test_empty_lambda(self): output = self.p.parse_instruction('[predict (lambda () 0)]') expected = { 'instruction': 'evaluate', 'expression': [v.symbol('predict'), [v.symbol('lambda'), [], v.number(0.0)]] } self.assertEqual(output, expected)
def toVenture(thing): if isinstance(thing, dict): return venturedicts.val("dict", [(toVenture(k), toVenture(v)) for k, v in thing.iteritems()]) if isinstance(thing, (list, tuple)): return venturedicts.val("array", [toVenture(v) for v in thing]) if isinstance(thing, (int, float)): return venturedicts.number(thing) if isinstance(thing, str): return venturedicts.symbol(thing)
def test_labeled_get_directive(self): inst1 = { 'instruction': 'labeled_predict', 'expression': [v.symbol('add'), v.number(1), v.number(2)], 'label': v.symbol('moo'), } o1 = self.sivm.execute_instruction(inst1) del inst1['label'] o2 = self.sivm.labeled_get_directive(v.symbol('moo')) output = { 'directive_id': o1['directive_id'], 'instruction': 'predict', 'expression': [v.symbol('add'), v.number(1), v.number(2)], 'label': v.symbol('moo'), } self.assertEquals(o2, output)
def test_observe(self): # Observe # full_loc = j(0,7,8,4,13,1,15,3) self.run_test( 'obServe blah = 1.3', [{'loc':full_loc, 'value':{ 'instruction' : {'loc':full_loc, 'value': 'evaluate'}, 'expression' : {'loc': full_loc, 'value': [{'loc':j(0,7), 'value':v.sym('observe')}, {'loc':j(8,4), 'value':v.sym('blah')}, {'loc': j(15,3), 'value':v.number(1.3)}]} }}])
def test_expression(self): with self.assertRaises(VentureException): self.p.parse_locexpression('') self.assertEqual( self.p.parse_locexpression('(a b (c number<1>))'), { 'loc': [0, 18], 'value': [{ 'loc': [1, 1], 'value': v.sym('a') }, { 'loc': [3, 3], 'value': v.sym('b') }, { 'loc': [5, 17], 'value': [{ 'loc': [6, 6], 'value': v.sym('c') }, { 'loc': [8, 16], 'value': v.number(1.0) }] }] })
def p_unary_neg(self, op, e): return self._p_binop(ast.update_value(op, val.number(0)), op, e)
def testForceAndSample(self): self.sivm.assume('x', [v.symbol('normal'), v.number(0), v.number(1)]) self.sivm.force('x', v.number(-2)) assert_equal(self.extractValue(self.sivm.sample('x')), -2)
def testForget(self): did1 = self.sivm.predict(v.number(1))['directive_id'] self.sivm.predict(v.number(2), label='xx2') self.sivm.forget(did1) self.sivm.forget('xx2') assert_equal(len(self.sivm.list_directives()), 0)
def testListDirectives(self): self.sivm.predict(v.number(1)) self.sivm.predict(v.number(2), label='xx2') assert_equal(len(self.sivm.list_directives()), 2)
def testPredict(self): did1 = self.sivm.predict(v.number(1))['directive_id'] self.sivm.predict(v.number(2), label='xx2') assert_equal(self.extractValue(self.sivm.report(did1)), 1) assert_equal(self.extractValue(self.sivm.report('xx2')), 2)
def testAssume(self): did1 = self.sivm.assume('x1', v.number(1))['directive_id'] self.sivm.assume('x2', v.number(2), label='xx2') assert_equal(self.extractValue(self.sivm.report(did1)), 1) assert_equal(self.extractValue(self.sivm.report('xx2')), 2)