Esempio n. 1
0
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)
Esempio n. 2
0
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 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)
Esempio n. 4
0
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)
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)
Esempio n. 6
0
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 )
            ga.next_step()
            dfv( u )

dfv( 0 )

graphs = ga.graphs()
files = render( graphs, 'dfv', 'png' )
gif( files, 'dfv', 50 )
Esempio n. 7
0
        gb.next_step()

        for u in G[v]:
            if not seen[u]:
                gb.add_node(u)
                gb.add_edge(v, u)

                ga.highlight_node(u, color='blue')
                gb.highlight_node(u, color='blue')

                fringe.put(u)
                seen[u] = True
                pre[u] = v

        color_path(v, ga)
        color_path(v, gb)

        addQueueState(
            fringe, queueStates,
            "3. Kindknoten von *" + v + "* auf " + queuename + " legen.")

        ga.next_step()
        gb.next_step()

graphs_a = ga.graphs("circo")
files1 = render(graphs_a, os.path.basename(__file__)[:-3] + '_graph', 'svg')

graphs_b = gb.graphs("dot")
files2 = render(graphs_b, os.path.basename(__file__)[:-3] + '_tree', 'svg')

slides(files1, files2, queueStates, os.path.basename(__file__)[:-3])
Esempio n. 8
0
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)
Esempio n. 9
0
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.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()
            dfv( u )

dfv( 0 )

graphs = ga.graphs()
files = render( graphs, 'dfv', 'png' )
gif( files, 'dfv', 50 )