-
Notifications
You must be signed in to change notification settings - Fork 10
/
magics.py
64 lines (52 loc) · 1.97 KB
/
magics.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
# Copyright 2020 MIT Probabilistic Computing Project.
# See LICENSE.txt
import sys
from collections import namedtuple
from IPython.core.magic import Magics
from IPython.core.magic import cell_magic
from IPython.core.magic import line_magic
from IPython.core.magic import magics_class
from IPython.core.magic import needs_local_scope
from sppl.compilers.sppl_to_python import SPPL_Compiler
from .render import render_graphviz
Model = namedtuple('Model', ['source', 'compiler', 'namespace'])
@magics_class
class SPPL_Magics(Magics):
def __init__(self, shell):
super().__init__(shell)
self.programs = {}
@line_magic
def sppl_get_spe(self, line):
assert line in self.programs, 'unknown program %s' % (line,)
return getattr(self.programs[line].namespace, line)
@cell_magic
def sppl(self, line, cell):
if not line:
sys.stderr.write('specify model name after %%sppl')
return
if line in self.programs:
del self.programs[line]
compiler = SPPL_Compiler(cell, line)
namespace = compiler.execute_module()
self.programs[line] = Model(cell, compiler, namespace)
@line_magic
def sppl_to_python(self, line):
assert line in self.programs, 'unknown program %s' % (line,)
print(self.programs[line].compiler.render_module())
@needs_local_scope
@line_magic
def sppl_to_graph(self, line, local_ns):
tokens = line.strip().split(' ')
line = tokens[0]
filename = tokens[1] if len(tokens) == 2 else None
if line in self.programs:
spe = self.sppl_get_spe(line)
elif line in local_ns:
spe = local_ns[line]
else:
assert False, 'unknown program %s' % (line,)
return render_graphviz(spe, filename=filename)
@line_magic
def sppl_get_namespace(self, line):
assert line in self.programs, 'unknown program %s' % (line,)
return self.programs[line].namespace