def test_case(): node0 = Node(0, 'root') node1 = Node(1, 'left') node2 = Node(2, 'right') node3 = Node(3, 'll-child') node4 = Node(4, 'lr-child') node0.left = node1 node0.right = node2 node1.left = node3 node1.right = node4 ga = Animation() ga.add_edge(node0.name, node1.name) ga.add_edge(node0.name, node2.name) ga.add_edge(node1.name, node3.name) ga.add_edge(node1.name, node4.name) ga.next_step() preorder(node0, ga) graphs = ga.graphs() for g in graphs: print(g) output = render(graphs, 'demo', 'png') gif(output, 'demo', 50)
def main(): parser = ArgumentParser(prog='gvanim') parser.add_argument('animation', nargs='?', type=FileType( 'r'), default=stdin, help='The file containing animation commands (default: stdin)') parser.add_argument('--delay', '-d', default='100', help='The delay (in ticks per second, default: 100)') parser.add_argument('basename', help='The basename of the generated file') args = parser.parse_args() ga = Animation() ga.parse(args.animation) gif(render(ga.graphs(), args.basename, 'png'), args.basename, args.delay)
def __init__(self): # noqa: D107 super().__init__() satisfies_version(EventHandlerExtensionPoint.EXTENSION_POINT_VERSION, '^1.0') self._animation = Animation() self._node_dependencies = {} self._any_started = False self._node_colors = {} self._has_errors = set() self._enabled = os.environ.get( ANIMATION_PROGRESS_ENVIRONMENT_VARIABLE.name, False) # register exit handle to ensure the last status line is cleared atexit.register(self._finish)
def construct(numbers): ga = Animation() # for visualization root = Node(numbers[0]) for n in numbers[1:]: root = root.add_node(Node(n)) # each time rewrite all nodes. this is not good root.set_animation(ga) ga.next_step(clean=True) # print the result root.print() # save the animation if not os.path.isdir("graphs"): os.mkdir("graphs") graphs = ga.graphs() files = render(graphs, "graphs/figure", 'png') gif(files, "graphs/gif-anim", 50)
def build_max_heap(numbers): size = len(numbers) ga = Animation() # add nodes and edges for i in reversed(range(size)): v = numbers[i] ga.label_node(i + 1, v) if i != 0: ga.add_edge((i + 1) // 2, i + 1) ga.next_step() for i in reversed(range(0, size // 2)): max_heapify(numbers, i, ga) # save graphs = ga.graphs() files = render(graphs, "figure/fig", 'png') gif(files, "figure/building-heap", 50)
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # details. # # You should have received a copy of the GNU General Public License along with # "GraphvizAnim". If not, see <http://www.gnu.org/licenses/>. from random import sample from gvanim import Animation, render, gif N = range( 6 ) K = 3 G = dict( ( v, sample( N, K ) ) for v in N ) ga = Animation() for v, adj in G.items(): for u in adj: ga.add_edge( v, u ) ga.label_edge( v, u, '{}:{}'.format(v,u)) ga.next_step() seen = [ False for v in N ] def dfv( v ): ga.highlight_node( v ) ga.next_step() seen[ v ] = True for u in G[ v ]: if not seen[ u ]: ga.highlight_node( v ) ga.highlight_edge( v, u )
def generate_graph(lines, tick=0.5): ''' Given lines from parse_log, return a gvanim.Animate object. The tick determines the frame rate and is in units of the .time value of lines. ''' ga = Animation() last_time = 0 for one in lines: if isinstance(one, Connect): ga.add_edge(one.tail, one.head) last_time = one.time if isinstance(one, State): if one.state == "enter": ga.highlight_node(one.node) if one.state == "exit": ga.add_node(one.node) if one.time - last_time > tick: ga.next_step() last_time = one.time ga.next_step() return ga
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # details. # # You should have received a copy of the GNU General Public License along with # "GraphvizAnim". If not, see <http://www.gnu.org/licenses/>. from random import sample from gvanim import Animation, render, gif N = list(range( 6)) K = 3 G = dict( ( v, sample( N, K ) ) for v in N ) ga = Animation() for v, adj in list(G.items()): for u in adj: ga.add_edge( v, u ) ga.next_step() seen = [ False for v in N ] def dfv( v ): ga.highlight_node( v ) ga.next_step() seen[ v ] = True for u in G[ v ]: if not seen[ u ]: ga.highlight_node( v ) ga.highlight_edge( v, u ) ga.next_step()
} start = N[0] goal = N[5] pre = dict() def color_path(u, animation, path_color="magenta"): try: animation.highlight_edge(pre[u], u, color=path_color) color_path(pre[u], animation) except: pass ga = Animation() gb = Animation() seen = {v: False for v in N} fringe = Queue.Queue() queuename = "" if isinstance(fringe, Queue.LifoQueue): queuename = "Stack" elif isinstance(fringe, Queue.Queue): queuename = "Queue" queueStates = [] for v, adj in G.items(): for u in adj:
def animation(): size = 10 numbers = gen_numbers(size) root = None ga = Animation() # generate tree for n in numbers: root = insert(root, Node(n)) add_nodes(ga, root) ga.highlight_node(n) ga.next_step(clean=True) # delete for n in gen_numbers(size): add_nodes(ga, root) ga.highlight_node(n) ga.next_step(clean=True) root = delete(root, search(root, n)) # save graphs = ga.graphs() files = render(graphs, "figure/figure", 'png') gif(files, "figure/gif-anim", 50)
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # details. # # You should have received a copy of the GNU General Public License along with # "GraphvizAnim". If not, see <http://www.gnu.org/licenses/>. from random import sample from gvanim import Animation, render, to_gif N = range( 6 ) K = 3 G = dict( ( v, sample( N, K ) ) for v in N ) ga = Animation() for v, adj in G.items(): for u in adj: ga.add_edge( v, u ) ga.next_step() seen = [ False for v in N ] def dfv( v ): ga.highlight_node( v ) ga.next_step() seen[ v ] = True for u in G[ v ]: if not seen[ u ]: ga.highlight_node( v ) ga.highlight_edge( v, u ) ga.next_step()
# count events ev_counter = dict() for w_trace in workflow_log: for ev in w_trace: ev_counter[ev] = ev_counter.get(ev, 0) + 1 # count edges occurance edge_count = dict() for w_trace in workflow_log: for i in range(0, len(w_trace) - 1): edge_tuple = (w_trace[i], w_trace[i + 1]) edge_count[edge_tuple] = edge_count.get(edge_tuple, 0) + 1 # draw graph with calculated values ga = Animation() for event in w_net: text = event + ' (' + str(ev_counter[event]) + ")" ga.label_node(event, text) for preceding in w_net[event]: ga.add_edge(event, preceding) ga.next_step() a = 0 for w_trace in workflow_log: color = choice(colors) for i in range(0, len(w_trace) - 1): # cols = [choice(colors) for _ in range(len(w_trace))] for j in range(0, i + 1): ga.highlight_node(w_trace[j], color=color) ga.highlight_edge(w_trace[j], w_trace[j + 1], color=color)
def preorder(root: Node, ga: Animation): if not root: return ga.highlight_node(root.name) if root.left: ga.next_step() ga.highlight_edge(root.name, root.left.name) ga.next_step() preorder(root.left, ga) if root.right: ga.next_step() ga.highlight_edge(root.name, root.right.name) ga.next_step() preorder(root.right, ga)
class GraphvizAnimEventHandler(EventHandlerExtensionPoint): """ Generate a .gif of the task progress. The animation file `graphviz_anim_build.gif` is created in the current working directory. The extension handles events of the following types: - :py:class:`colcon_core.event.job.JobQueued` - :py:class:`colcon_core.event.job.JobStarted` - :py:class:`colcon_core.event.job.JobEnded` - :py:class:`colcon_core.event.output.StderrLine` """ # the priority should be lower than all status and notification extensions # in order to not block them while generating the animation PRIORITY = 10 def __init__(self): # noqa: D107 super().__init__() satisfies_version(EventHandlerExtensionPoint.EXTENSION_POINT_VERSION, '^1.0') self._animation = Animation() self._node_dependencies = {} self._any_started = False self._node_colors = {} self._has_errors = set() self._enabled = os.environ.get( ANIMATION_PROGRESS_ENVIRONMENT_VARIABLE.name, False) # register exit handle to ensure the last status line is cleared atexit.register(self._finish) def __call__(self, event): # noqa: D102 # skip any processing if not explicitly enabled if not self._enabled: return data = event[0] if isinstance(data, JobQueued): self._animation.add_node(data.identifier) self._node_dependencies[data.identifier] = set( data.dependencies.keys()) elif isinstance(data, JobStarted): if not self._any_started: # add nodes with dependency edges # while ignoring unknown dependencies for node, deps in self._node_dependencies.items(): for dep in deps: if dep not in self._node_dependencies: continue self._animation.add_edge(dep, node) self._any_started = True self._animation.next_step() self._node_colors[data.identifier] = 'blue' self._apply_highlights() elif isinstance(data, JobEnded): self._animation.next_step() job = event[1] color = 'green' if data.rc: color = 'red' elif job in self._has_errors: color = 'orange' self._node_colors[data.identifier] = color self._apply_highlights() elif isinstance(data, StderrLine): job = event[1] self._has_errors.add(job) def _apply_highlights(self): for node, color in self._node_colors.items(): self._animation.highlight_node(node, color=color) def _finish(self): if not self._any_started: return graphs = self._animation.graphs() files = render(graphs, 'graphviz_anim_build', fmt='gif', size=1920) gif(files, 'graphviz_anim_build', delay=50)