Exemple #1
0
def testScopeObservedThroughMem1():
    r = get_ripl()
    r.assume("frob", "(mem (lambda (x) (flip 0.5)))")
    r.observe("(frob 1)", True)
    r.predict("(tag (quote foo) 0 (frob 1))")
    trace = r.sivm.core_sivm.engine.getDistinguishedTrace()
    scope = trace._normalizeEvaluatedScopeOrBlock(val.VentureSymbol("foo"))  # pylint:disable=protected-access
    # TODO test for when auto-incorporation is disabled
    eq_(0, len(trace.getAllNodesInScope(scope)))
Exemple #2
0
 def init_inference_trace(self):
   import venture.untraced.trace as trace
   ans = trace.Trace(self._py_rng.randint(1, 2**31 - 1))
   for name,sp in self.inferenceSPsList():
     ans.bindPrimitiveSP(name, sp)
   for word in inf.inferenceKeywords:
     if not ans.boundInGlobalEnv(word):
       ans.bindPrimitiveName(word, vv.VentureSymbol(word))
   ans.bindPrimitiveName("__the_inferrer__", vv.VentureForeignBlob(Infer(self)))
   self.install_inference_prelude(ans)
   return ans
Exemple #3
0
def testScopeObservedThroughMem2():
    # The way resample happened to be implemented in Lite when I wrote
    # this test, it had the effect of undoing [infer (incorporate)] for
    # all observations.  This was detected through a horrible mess
    # involving mem.
    r = get_ripl()
    r.assume("frob", "(mem (lambda (x) (flip 0.5)))")
    r.observe("(frob 1)", True)
    trace = r.sivm.core_sivm.engine.getDistinguishedTrace()
    scope = trace._normalizeEvaluatedScopeOrBlock(val.VentureSymbol("foo"))  # pylint:disable=protected-access
    eq_(0, trace.numBlocksInScope(scope))
    r.infer("(incorporate)")
    r.predict("(frob 1)")
    r.infer("(resample 1)")
    r.predict("(tag (quote foo) 0 (frob 1))")
    trace = r.sivm.core_sivm.engine.getDistinguishedTrace()
    eq_(0, len(trace.getAllNodesInScope(scope)))
Exemple #4
0
 def asVentureValue(self, thing):
     if isinstance(thing, bool) or isinstance(thing, np.bool_):
         return vv.VentureBool(thing)
     if isinstance(thing, int):
         return vv.VentureInteger(thing)
     if isinstance(thing, numbers.Number):
         return vv.VentureNumber(thing)
     if isinstance(thing, vv.VentureAtom):
         return thing
     if isinstance(thing, str):
         return vv.VentureSymbol(thing)
     if hasattr(thing, "__getitem__"):  # Already not a string
         return vv.VentureArray([self.asVentureValue(val) for val in thing])
     if isinstance(thing, vv.VentureValue):
         return thing
     else:
         raise Exception("Cannot convert Python object %r to a Venture " \
                         "Expression" % thing)
Exemple #5
0
 def asPython(self, thing):
     if isinstance(thing, vv.VentureBool):
         return thing.getBool()
     if isinstance(thing, vv.VentureInteger):
         return thing.getInteger()
     if isinstance(thing, vv.VentureNumber):
         return thing.getNumber()
     if isinstance(thing, vv.VentureAtom):
         return thing  # Atoms are valid elements of expressions
     if isinstance(thing, vv.VentureSymbol):
         return thing.getSymbol()
     if thing.isValidCompoundForm():
         # Leave quoted data as they are, on the grounds that (quote
         # <thing>) should evaluate to exactly that <thing>, even if
         # constructed programmatically from a <thing> that does not
         # normally appear in expressions.
         if thing.size() == 2 \
            and thing.lookup(vv.VentureNumber(0)) == vv.VentureSymbol("quote"):
             return ["quote", thing.lookup(vv.VentureNumber(1))]
         else:
             return thing.asPythonList(self)
     # Most other things are represented as themselves.
     return thing
 def symbol(self, length=None, **_kwargs):
     if length is None:
         length = npr.randint(1, 10)
     alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
     return v.VentureSymbol(''.join(npr.choice(list(alphabet), length)))
Exemple #7
0
 def __hash__(self):
   return vv.sequenceHash([vv.VentureSymbol(self.tag)] + self.fields)