]> hydra-www.ietfng.org Git - dyna2/commitdiff
Minor tweaks to crash handler. Post-processor can render animates of text and
authortimv <tim.f.vieira@gmail.com>
Fri, 14 Jun 2013 18:37:07 +0000 (14:37 -0400)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Tue, 18 Jun 2013 01:22:32 +0000 (21:22 -0400)
lines. Instead of hard coded nodes, edge, pos. See `example/force.dyna`

examples/force.dyna
src/Dyna/Backend/Python/defn.py
src/Dyna/Backend/Python/graph.py
src/Dyna/Backend/Python/repl.py
src/Dyna/Backend/Python/utils.py

index fb9885b4b98c780bfda62f9f0fc7efa2f3e64a91..4d0c1dd85eecca1a8922974adf861db2c4d39ed9 100644 (file)
@@ -14,21 +14,35 @@ dist(U,V,T) := (x(U,T) - x(V,T))**2 + (y(U,T) - y(V,T))**2
 shortestpath(U,U) min= 0 for node(U).
 shortestpath(U,V) min= shortestpath(U,W) + edge(W,V).
 
-% "the unit edge length"
-edgelen := 4.0.
-
+% Compute attractive-replusive forces
 f(U,V,T) := (U != V), dist(U,V,T) / (shortestpath(U,V) * edgelen) - 1.
-
 forceX(V,T) += f(U,V,T) * (x(U,T) - x(V,T)).
 forceY(V,T) += f(U,V,T) * (y(U,T) - y(V,T)).
 
+% Constants
 a := 0.15.
-niter := 100.
+niter := 200.
+edgelen := 4.0.   % "the unit edge length"
 
 % should `a` be negative?
 x(U,T) += a * forceX(U,T-1).
 y(U,T) += a * forceY(U,T-1).
 
+% make graph symmetric.
+edge(A,B) := 1 for edge(B,A).
+
+% collect nodes.
+node(U) := true for edge(U,_).
+node(U) := true for edge(_,U).
+
+% pack x and y into a tuple
+pos(U,T) := tuple(x(U, T), y(U, T)).
+
+% visualization
+frame(T, Item) := node(Name), Item is &text(Name, pos(Name, T)).
+frame(T, Item) := edge(U,V), Item is &line(pos(U, T), pos(V, T)).
+
+% declare some edges
 edge("a", "b") := 1.
 edge("a", "c") := 1.
 edge("a", "d") := 1.
@@ -40,12 +54,6 @@ edge("e", "f") := 1.
 edge("e", "g") := 1.
 edge("a", "i") := 1.
 
-edge(A,B) := 1 for edge(B,A). % make graph symmetric.
-
-% collect nodes.
-node(U) := true for edge(U,_).
-node(U) := true for edge(_,U).
-
 % randomly initialize node positions.
 x("a",0) += uniform(0,1).  y("a",0) += uniform(0,1).
 x("b",0) += uniform(0,1).  y("b",0) += uniform(0,1).
@@ -57,5 +65,3 @@ x("g",0) += uniform(0,1).  y("g",0) += uniform(0,1).
 x("h",0) += uniform(0,1).  y("h",0) += uniform(0,1).
 x("i",0) += uniform(0,1).  y("i",0) += uniform(0,1).
 x("j",0) += uniform(0,1).  y("j",0) += uniform(0,1).
-
-pos(U,T) := tuple(x(U, T), y(U, T)).
index 8d39cc6178175119347d6bcd40b7d834465f6604..a014199981edd706bf987ad6f6b9ffeac2563432 100644 (file)
@@ -17,6 +17,8 @@ class Aggregator(object):
 
 
 class BAggregator(Counter, Aggregator):
+    def __init__(self):   
+        super(BAggregator, self).__init__()
     def inc(self, val, ruleix, variables):
         self[val] += 1
     def dec(self, val, ruleix, variables):
index f69cd2d191ce50a6f9d8b9f8d4e99687468630f1..44f3628ceafe7d9a6d4f220e0f765f5467d82019 100644 (file)
@@ -1,40 +1,55 @@
-import pylab as pl
-from matplotlib.animation import FuncAnimation
-from collections import defaultdict
+"""
+Postprocessor for animated visualization of basic elements such as lines and
+text.
 
-def g(nodes, edges, t, ax, interp):
-    ax.cla()
-    ax.set_title(t)
-    ax.set_xlim(-2,2)
-    ax.set_ylim(-2,2)
+We look for the following patterns in the dynabase
 
-    pos = defaultdict(lambda: (0,0))
-    pos.update({node: p for _, (node, _), p in interp.chart['pos/2'][:,t,:]})
+         visual element
+               v
+    frame(T, &text(String, tuple(X, Y))).
+          ^
+       time index
 
-    for u,v in edges:
-        if u < v:
-            (a,b), (c,d) = pos[u], pos[v]
-            ax.plot([a,c], [b,d])
+Frames should have value true. The example above places a text element reading
+`String` at position `(X,Y)` in a frame at time `T`. This element can be
+specified by dyna rule.
+"""
 
-    for s in nodes:
-        x,y = pos[s]
-        ax.text(x,y,s)
+import pylab as pl
+from matplotlib.animation import FuncAnimation
+from collections import defaultdict
 
-def animate(interp):
-    [(_, _, niter)] = interp.chart['niter/0'][:,]
+def main(interp):
 
-    nodes = [name for _, [name], _ in interp.chart['node/1'][:,:]]
-    edges = [(u,v) for _, [u,v] ,_ in interp.chart['edge/2'][:,:,:]]
+    frame = defaultdict(list)
+    for _, [t, item], val in interp.chart['frame/2'][:,:,:]:
+        if val:
+            frame[t].append(item)
+
+    nframes = max(frame)
+
+    def draw_frame(t):
+        ax.cla()
+        ax.set_title(t)
+        ax.set_xlim(-2,2)   # TODO: this isn't right...
+        ax.set_ylim(-2,2)
+        if t not in frame:
+            print 'frame', t, 'missing.'
+        for item in frame[t]:
+            if item.fn == 'line/2':
+                [(a,b), (c,d)] = item.args
+                ax.plot([a,c], [b,d], color='b', alpha=0.5)
+            elif item.fn == 'text/2':
+                (s,(x,y)) = item.args
+                ax.text(x,y,s)
+            else:
+                print 'dont know how to render', item
 
     fig = pl.figure()
     ax = pl.axes()
 
     print 'creating animation..'
-    anim = FuncAnimation(fig, lambda t: g(nodes, edges, t % niter, ax, interp), frames=niter)
+    anim = FuncAnimation(fig, draw_frame, frames=nframes)
     print 'saving...'
     anim.save('examples/force.dyna.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
     print 'wrote examples/force.dyna.mp4'
-
-
-def main(interp):
-    animate(interp)
index 7758c8b6298c6d3ccf1003d9f0e2840e4b02771a..b766b23baf7d29b0e4dbdc572a0d3cfc15b9511a 100644 (file)
@@ -1,7 +1,7 @@
 import os, cmd, readline
 
 import debug, interpreter
-from utils import DynaCompilerError, DynaInitializerException, ip, crash_handler
+from utils import DynaCompilerError, DynaInitializerException, ip
 from chart import _repr
 from config import dotdynadir
 
@@ -127,9 +127,6 @@ class REPL(cmd.Cmd, object):
         except KeyboardInterrupt:
             # Catch Control-C and resume REPL.
             print '^C'
-            self.cmdloop()   
-        except:
-            # report uncatch exception.
-            crash_handler()
+            self.cmdloop()
         finally:
             readline.write_history_file(self.hist)
index 4a78017e7b5977b538935e61280864f5e0716437..e2d1250cfffd4636f8d9c55a29b227be8a14b132 100644 (file)
@@ -46,23 +46,25 @@ def dynac(f, out):
         raise DynaCompilerError(stderr)
 
 
-def crash_handler():
-    exception_handler(*sys.exc_info())
-
-
 def exception_handler(etype, evalue, tb):
 
     # once for the log file.
     with file(dotdynadir / 'crash.log', 'wb') as crashreport:
-        h = VerboseTB(color_scheme='Linux', call_pdb=False,
+        h = VerboseTB(color_scheme='Linux',
+                      call_pdb=False,
                       ostream=crashreport,
-                      long_header=True, include_vars=True,
+                      long_header=True,
+                      include_vars=True,
                       check_cache=None)
         h(etype, evalue, tb)
 
     # once for the user
-    h = VerboseTB(color_scheme='Linux', call_pdb=False, ostream=None,
-                  tb_offset=0, long_header=False, include_vars=True,
+    h = VerboseTB(color_scheme='Linux',
+                  call_pdb=False,
+                  ostream=None,
+                  tb_offset=0,
+                  long_header=False,
+                  include_vars=False,
                   check_cache=None)
     h(etype, evalue, tb)