-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrender.py
68 lines (64 loc) · 2.33 KB
/
render.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Copyright 2020 MIT Probabilistic Computing Project.
# See LICENSE.txt
from math import exp
from .spe import AtomicLeaf
from .spe import DiscreteLeaf
from .spe import LeafSPE
from .spe import NominalLeaf
from .spe import ProductSPE
from .spe import RealLeaf
from .spe import SumSPE
def render_nested_lists_concise(spe):
if isinstance(spe, LeafSPE):
return [(str(k), str(v)) for k, v in spe.env.items()]
if isinstance(spe, SumSPE):
return ['+(%d)' % (len(spe.children),),
# [exp(w) for w in spe.weights],
[render_nested_lists_concise(c) for c in spe.children]
]
if isinstance(spe, ProductSPE):
return ['*(%d)' % (len(spe.children),),
[render_nested_lists_concise(c) for c in spe.children]
]
def render_nested_lists(spe):
if isinstance(spe, NominalLeaf):
return ['NominalLeaf', [
['symbol', spe.symbol],
['env', dict(spe.env)],
['dist', {str(x): float(w) for x, w in spe.dist.items()}]]
]
if isinstance(spe, AtomicLeaf):
return ['AtomicLeaf', [
['symbol', spe.symbol],
['value', spe.value],
['env', dict(spe.env)]]
]
if isinstance(spe, RealLeaf):
return ['RealLeaf', [
['symbol', spe.symbol],
['env', dict(spe.env)],
['dist', (spe.dist.dist.name, spe.dist.args, spe.dist.kwds)],
['support', spe.support],
['conditioned', spe.conditioned]]
]
if isinstance(spe, DiscreteLeaf):
return ['DiscreteLeaf', [
['symbol', spe.symbol],
['dist', (spe.dist.dist.name, spe.dist.args, spe.dist.kwds)],
['support', spe.support],
['conditioned', spe.conditioned]]
]
if isinstance(spe, SumSPE):
return ['SumSPE', [
['symbols', list(spe.symbols)],
['weights', [exp(w) for w in spe.weights]],
['n_children', len(spe.children)],
['children', [render_nested_lists(c) for c in spe.children]]]
]
if isinstance(spe, ProductSPE):
return ['ProductSPE', [
['symbols', list(spe.symbols)],
['n_children', len(spe.children)],
['children', [render_nested_lists(c) for c in spe.children]]]
]
assert False, 'Unknown SPE type: %s' % (spe,)