示例#1
0
def propForeignInterfaceAgreesWithDeterministicSimulate(args_lists, name, sp):
    """Check that the given SP produces the same answer directly and
through the foreign function interface (applied fully uncurried)."""
    answer = carefully(simulate(sp), args_lists[0])
    if isinstance(answer, VentureSPRecord):
        if isinstance(answer.sp.requestPSP, NullRequestPSP):
            if not answer.sp.outputPSP.isRandom():
                ans2 = carefully(simulate(answer.sp),
                                 args_lists[1],
                                 spaux=answer.spAux)
                inner = [v.symbol("test_sp")
                         ] + [val.expressionFor() for val in args_lists[0]]
                expr = [inner] + [val.expressionFor() for val in args_lists[1]]
                assert ans2.equal(
                    carefully(eval_foreign_sp, "test_sp", sp, expr))
            else:
                raise SkipTest(
                    "Putatively deterministic sp %s returned a random SP" %
                    name)
        else:
            raise SkipTest(
                "Putatively deterministic sp %s returned a requesting SP" %
                name)
    else:
        expr = [v.symbol("test_sp")
                ] + [val.expressionFor() for val in args_lists[0]]
        assert answer.equal(carefully(eval_foreign_sp, "test_sp", sp, expr))
示例#2
0
 def _get_directive(self, did):
     import venture.lite.types as t
     directive = copy.copy(
         self.core_sivm.engine.model.traces.at_distinguished(
             'directive', did))
     if directive[0] == 'define':
         ans = {
             'instruction': 'assume',
             'symbol': v.symbol(directive[1]),
             'expression': directive[2]
         }
     elif directive[0] == 'evaluate':
         ans = {'instruction': 'predict', 'expression': directive[1]}
     else:
         assert directive[0] == 'observe'
         ans = {
             'instruction': 'observe',
             'expression': directive[1],
             'value': directive[2]
         }
     label = self.core_sivm.engine.get_directive_label(did)
     if label is not None:
         ans['label'] = v.symbol(label)
     ans['directive_id'] = did
     return ans
示例#3
0
 def p_body_do(self, ss, semi, e):
     assert ast.isloc(ss)
     if e is None:
         e = ast.update_value(semi, val.symbol('pass'))
     assert ast.isloc(e)
     do = ast.locmerge(ss, e, val.symbol('do'))
     return ast.locmerge(ss, e, [do] + ss.value + [e])
示例#4
0
 def new_char(incoming):
   text_box[0] = text_box[0] + incoming
   m = re.match(r" *\(([a-zA-Z]+)\) *\{(.*)\} *\}", text_box[0])
   if m:
     operator = ast.Located([0, 0], vv.symbol('bayesdb_sql'))
     population = ast.Located([0, 0], vv.symbol(m.group(1)))
     sql = ast.Located([0, 0], vv.string(m.group(2)))
     return (True, ast.Located([0,0], [operator, population, sql]))
   else:
     return (False, None)
 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 test_labeled_assume(self):
     inst = {
         'instruction': 'labeled_assume',
         'expression': ['add', v.number(1), v.number(2)],
         'symbol': v.symbol('moo'),
         'label': 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)
示例#7
0
 def _scan_bql_maybe_end(self, text):
   assert text == '}'
   if bql_string_complete_p(self._bql.getvalue()):
     operator = ast.Located([0, self.cur_pos - 1], vv.symbol('bayesdb_bql'))
     population = ast.Located(self._name_position, vv.symbol(self._name))
     bql = ast.Located(
       [self._bql_start, self.cur_pos - 1],
       vv.string(self._bql.getvalue()))
     self.produce(ast.Located([0, self.cur_pos - 1], [operator, population, bql]))
   else:
     self._bql.write(text)
 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_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 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)
示例#11
0
 def _hack_infer_expression_structure(self, exp, syntax, prefix="run"):
     # The engine actually executes an application form around the
     # passed inference program.  Storing this will align the
     # indexes correctly.
     symbol = v.symbol(prefix)
     hacked_exp = [symbol, exp]
     hacked_syntax = macro.ListSyntax([macro.LiteralSyntax(symbol), syntax])
     return (hacked_exp, hacked_syntax)
示例#12
0
 def report(self, label_or_did):
     if isinstance(label_or_did, int):
         d = {'instruction': 'report', 'directive_id': label_or_did}
     else:
         d = {
             'instruction': 'labeled_report',
             'label': v.symbol(label_or_did)
         }
     return self.execute_instruction(d)
 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, [])
示例#14
0
 def p_primary_paren(self, o, es, c):
     assert isinstance(es, list) and all(map(ast.isloc, es))
     if len(es) == 1:
         [e] = es
         return ast.locmerge(o, c, e.value)
     else:
         keyword = ast.update_value(o, val.symbol('values_list'))
         construction = [keyword] + es
         return ast.locmerge(o, c, construction)
示例#15
0
 def test_missing_argument(self):
     try:
         self.sivm.execute_instruction({
             'instruction': 'assume',
             'symbol': v.symbol("MOO")
         })
     except VentureException as e:
         self.assertEqual(e.exception, 'missing_argument')
         self.assertEqual(e.data['argument'], 'expression')
示例#16
0
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)
示例#17
0
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_observe(self):
     inst = {
         'instruction': 'labeled_observe',
         'expression': ['normal', v.number(1),
                        v.number(2)],
         'value': v.real(3),
         'label': v.symbol('moo')
     }
     o = self.sivm.execute_instruction(inst)
     self.assertIsInstance(o['directive_id'], (int, float))
示例#19
0
 def predict(self, expression, label=None):
     if label is None:
         d = {'instruction': 'predict', 'expression': expression}
     else:
         d = {
             'instruction': 'labeled_predict',
             'expression': expression,
             'label': v.symbol(label)
         }
     return self.execute_instruction(d)
示例#20
0
 def test_invalid_argument(self):
     try:
         self.sivm.execute_instruction({
             'instruction': 'assume',
             'symbol': v.symbol("9,d"),
             'expression': ['a', 'b', ['c']]
         })
     except VentureException as e:
         self.assertEqual(e.exception, 'invalid_argument')
         self.assertEqual(e.data['argument'], 'symbol')
 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_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_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))
示例#24
0
 def p_directive_assume(self, k, n, e):
     # Fun fact.  This manipulation (and the similar treatment of
     # observe and predict, here and in the VentureScript parser)
     # breaks an invariant that parsing used to satisfy.  To wit,
     # once upon a time it was the case that the string picked out
     # by the location tags of every node in a parse tree was
     # guaranteed to re-parse to an equal node.  This cannot be the
     # case now, because the 'expr' node constructed here is not
     # parsed from the string, but synthesized based on knowing
     # that its constituents appear in an 'assume' directive.
     expr = [
         ast.update_value(k, val.symbol('assume')),
         ast.map_value(val.symbol, n), e
     ]
     return expression_evaluation_instruction(ast.loclist(expr))
示例#25
0
 def observe(self, expression, value, label=None):
     if label is None:
         d = {
             'instruction': 'observe',
             'expression': expression,
             'value': value
         }
     else:
         d = {
             'instruction': 'labeled_observe',
             'expression': expression,
             'value': value,
             'label': v.symbol(label)
         }
     return self.execute_instruction(d)
示例#26
0
 def assume(self, name, expression, label=None):
     if label is None:
         d = {
             'instruction': 'assume',
             'symbol': name,
             'expression': expression
         }
     else:
         label = v.symbol(label)
         d = {
             'instruction': 'labeled_assume',
             'symbol': name,
             'expression': expression,
             'label': label
         }
     return self.execute_instruction(d)
示例#27
0
 def _do_force(self, instruction):
     exp = utils.validate_arg(instruction,
                              'expression',
                              utils.validate_expression,
                              wrap_exception=False)
     val = utils.validate_arg(instruction, 'value', utils.validate_value)
     inst1 = {
         'instruction': 'observe',
         'expression': exp,
         'value': val,
     }
     o1 = self._call_core_sivm_instruction(inst1)
     inst2 = {
         'instruction': 'infer',
         'expression': [v.symbol('incorporate')]
     }
     self._call_core_sivm_instruction(inst2)
     inst3 = {
         'instruction': 'forget',
         'directive_id': o1['directive_id'],
     }
     self._call_core_sivm_instruction(inst3)
     return {"value": o1["value"]}
示例#28
0
def _expToDict(exp):
    if isinstance(exp, int):
        return {
            "kernel": "resimulation_mh",
            "scope": "default",
            "block": "one",
            "transitions": exp
        }

    scope = _ensure_stack_dict(exp[1])
    block = _ensure_stack_dict(exp[2])

    exp = map(_unwrapVentureValue, exp)

    tag = exp[0]
    # Silly pylint, I intentionally write x <= (foo) and (foo) <= y below.
    # pylint:disable=misplaced-comparison-constant
    if tag == "resimulation_mh":
        assert len(exp) == 4
        return {
            "kernel": "resimulation_mh",
            "scope": scope,
            "block": block,
            "transitions": int(exp[3])
        }
    elif tag == "bogo_possibilize":
        assert len(exp) == 4
        return {
            "kernel": "bogo_possibilize",
            "scope": scope,
            "block": block,
            "transitions": int(exp[3])
        }
    elif tag == "func_mh":
        assert len(exp) == 4
        return {
            "kernel": "resimulation_mh",
            "scope": scope,
            "block": block,
            "transitions": int(exp[3])
        }
    elif tag == "gibbs":
        assert 4 <= len(exp) and len(exp) <= 5
        ans = {
            "kernel": "gibbs",
            "scope": scope,
            "block": block,
            "transitions": int(exp[3])
        }
        if len(exp) == 5:
            ans["in_parallel"] = exp[4]
        else:
            ans["in_parallel"] = True
        return ans
    elif tag == "emap":
        assert 4 <= len(exp) and len(exp) <= 5
        ans = {
            "kernel": "emap",
            "scope": scope,
            "block": block,
            "transitions": int(exp[3])
        }
        if len(exp) == 5:
            ans["in_parallel"] = exp[4]
        else:
            ans["in_parallel"] = True
        return ans
    elif tag == "slice":
        assert len(exp) == 6
        return {
            "kernel": "slice",
            "scope": scope,
            "block": block,
            "w": exp[3],
            "m": int(exp[4]),
            "transitions": int(exp[5])
        }
    elif tag == "slice_doubling":
        assert len(exp) == 6
        return {
            "kernel": "slice_doubling",
            "scope": scope,
            "block": block,
            "w": exp[3],
            "p": int(exp[4]),
            "transitions": int(exp[5])
        }
    # [FIXME] expedient hack for now to allow windowing with pgibbs.
    elif tag == "pgibbs":
        assert 5 <= len(exp) and len(exp) <= 6
        if isinstance(block["value"], list):
            range_spec = block["value"]
            assert range_spec[0]["value"] == "ordered_range"
            ans = {
                "kernel": "pgibbs",
                "scope": scope,
                "block": v.symbol("ordered_range"),
                "min_block": range_spec[1],
                "max_block": range_spec[2],
                "particles": int(exp[3]),
                "transitions": int(exp[4])
            }
        else:
            ans = {
                "kernel": "pgibbs",
                "scope": scope,
                "block": block,
                "particles": int(exp[3]),
                "transitions": int(exp[4])
            }
        if len(exp) == 6:
            ans["in_parallel"] = exp[5]
        else:
            ans["in_parallel"] = True
        return ans
    elif tag == "func_pgibbs":
        assert 5 <= len(exp) and len(exp) <= 6
        ans = {
            "kernel": "pgibbs",
            "scope": scope,
            "block": block,
            "particles": int(exp[3]),
            "transitions": int(exp[4])
        }
        if len(exp) == 6:
            ans["in_parallel"] = exp[5]
        else:
            ans["in_parallel"] = True
        return ans
    elif tag == "meanfield":
        assert len(exp) == 5
        return {
            "kernel": "meanfield",
            "scope": scope,
            "block": block,
            "steps": int(exp[3]),
            "transitions": int(exp[4])
        }
    elif tag == "hmc":
        assert len(exp) == 6
        return {
            "kernel": "hmc",
            "scope": scope,
            "block": block,
            "epsilon": exp[3],
            "L": int(exp[4]),
            "transitions": int(exp[5])
        }
    elif tag == "map":
        assert len(exp) == 6
        return {
            "kernel": "map",
            "scope": scope,
            "block": block,
            "rate": exp[3],
            "steps": int(exp[4]),
            "transitions": int(exp[5])
        }
    elif tag == "nesterov":
        assert len(exp) == 6
        return {
            "kernel": "nesterov",
            "scope": scope,
            "block": block,
            "rate": exp[3],
            "steps": int(exp[4]),
            "transitions": int(exp[5])
        }
    elif tag == "latents":
        assert len(exp) == 4
        return {
            "kernel": "latents",
            "scope": scope,
            "block": block,
            "transitions": int(exp[3])
        }
    elif tag == "rejection":
        assert len(exp) >= 3
        assert len(exp) <= 4
        if len(exp) == 4:
            return {
                "kernel": "rejection",
                "scope": scope,
                "block": block,
                "transitions": int(exp[3])
            }
        else:
            return {
                "kernel": "rejection",
                "scope": scope,
                "block": block,
                "transitions": 1
            }
    else:
        raise Exception(
            "The Puma backend does not support the %s inference primitive" %
            (tag, ))
示例#29
0
 def observe(i, d):
   r.observe(expr.app(expr.symbol("datum"), expr.integer(i), d), dataset[i][d])
示例#30
0
 def p_primary_array(self, o, a, c):
     assert isinstance(a, list)
     construction = [ast.update_value(o, val.symbol('array'))] + a
     return ast.locmerge(o, c, construction)
示例#31
0
 def test_modify_symbol(self):
     val = 'add'
     s = v.symbol('add')
     self.assertEqual(module._modify_symbol(val), s)
示例#32
0
def symbol_prepend(prefix, symbol):
    if isinstance(symbol, basestring):
        return prefix + symbol
    else:
        return v.symbol(prefix + symbol["value"])
示例#33
0
 def p_hash_tag_tag_val(self, e, h, tag, colon, value):
     tag_proc = ast.update_value(h, val.symbol('tag'))
     name = locquoted(h, tag, val.quasiquote)
     app = [tag_proc, name, value, e]
     return ast.locmerge(e, value, app)