def tagged_value_to_string(v): assert 'type' in v assert 'value' in v if v['type'] == 'boolean': assert v['value'] in set([True, False]) return 'true' if v['value'] else 'false' elif v['type'] == 'number': assert isinstance(v['value'], int) or isinstance(v['value'], float) return str(v['value']) elif v['type'] == 'symbol': assert isinstance(v['value'], basestring) return v['value'] elif v['type'] == 'string': assert isinstance(v['value'], basestring) return quote_string(v['value']) elif v['type'] == 'array_unboxed': # first problem: the JSON representation for this isn't one-to-one, # because it doesn't include a way to specify subtype. # second: the value might be a numpy array, which JSON doesn't like. # solution: convert to lite's VentureValue, unbox, convert back. # XXX is there a better way? from venture.lite.value import VentureValue, VentureArray v = VentureValue.fromStackDict(v) v = VentureArray(v.getArray()) v = v.asStackDict() return '%s<%s>' % (v['type'], json.dumps(v['value'])) else: return '%s<%s>' % (v['type'], json.dumps(v['value']))
def fromStackDict(thing): # proxy for VentureValue.fromStackDict that handles SPs by unwrapping them # TODO: should foreign_sp be a recognized stack dict type? # should this become the normal stack representation for SPs? if thing is None: return None elif thing["type"] == "foreign_sp": return VentureSPRecord(thing["sp"].sp, thing["aux"]) elif thing["type"] == "request": return Request(thing["value"]["esrs"], thing["value"]["lsrs"]) else: return VentureValue.fromStackDict(thing)
def unboxExpression(self, exp): return ExpressionType().asPython(VentureValue.fromStackDict(exp))
def unboxValue(val): return VentureValue.fromStackDict(val)
def eval_in_ripl(expr): ripl = get_ripl() ripl.predict(expr, label="thing") return VentureValue.fromStackDict(ripl.report("thing", type=True))
def eval_foreign_sp(name, sp, expr): ripl = get_ripl() ripl.bind_foreign_sp(name, sp) ripl.predict(expr, label="thing") return VentureValue.fromStackDict(ripl.report("thing", type=True))
def extract_stats(self, exp): sp_dict = self.engine.sample(exp.asStackDict()) return VentureValue.fromStackDict(sp_dict["aux"])
def sample_all(self, exp): return [ VentureValue.fromStackDict(val) for val in self.engine.sample_all(exp.asStackDict()) ]
def sample(self, exp): return VentureValue.fromStackDict(self.engine.sample( exp.asStackDict()))