Animations with graph-tool#

The drawing capabilities of graph-tool (see draw module) can be harnessed to perform animations in a straightforward manner. Here we show some examples which use GTK+ to display animations in an interactive_window(), as well as offscreen to a file. The idea is to easily generate visualizations which can be used in presentations, and embedded in websites.

Simple interactive animations#

The graph_draw() function can optionally return and be passed a GTK+ window object where drawing can be updated. In this way, simple animations can be done very easily, especially when done in an interactive shell. For example, the following will simulate from an Ising model (see IsingGlauberState) and show the states in an interactive window:

>>> g = gt.collection.data["football"]
>>> state = gt.IsingGlauberState(g, beta=1.5/10)
>>> win = None
>>> for i in range(100):
...     ret = state.iterate_sync(niter=100)
...     win = gt.graph_draw(g, g.vp.pos, vertex_fill_color=state.get_state(),
...                         window=win, return_window=True, main=False)

Although sufficient for many simple animations, the above method is not the most efficient, as it requires a certain amount of redundant processing for each drawing. The examples below show more efficient approaches that can be more suitable in some more elaborate scenarios.

SIRS epidemics#

Here we implement a simple SIRS epidemic model on a network, and we construct an animation showing the time evolution. Nodes which are susceptible (S) are shown in white, whereas infected (I) nodes are shown in black. Recovered ® nodes are removed from the layout, since they cannot propagate the outbreak.

The script which performs the animation is called animation_sirs.py and is shown below.

  1#! /usr/bin/env python
  2# -*- coding: utf-8 -*-
  3
  4# This is a simple example of how to do animations using graph-tool. Here we do a
  5# simple simulation of an S->I->R->S epidemic model, where each vertex can be in
  6# one of the following states: Susceptible (S), infected (I), recovered (R). A
  7# vertex in the S state becomes infected either spontaneously with a probability
  8# 'x' or because a neighbor is infected. An infected node becomes recovered
  9# with probability 'r', and a recovered vertex becomes again susceptible with
 10# probability 's'.
 11
 12# DISCLAIMER: The following code is definitely not the most efficient approach
 13# if you want to simulate this dynamics for very large networks, and/or for very
 14# long times. The main purpose is simply to highlight the animation capabilities
 15# of graph-tool.
 16
 17from graph_tool.all import *
 18from numpy.random import *
 19import sys, os, os.path
 20
 21seed(42)
 22seed_rng(42)
 23
 24# We need some Gtk and gobject functions
 25from gi.repository import Gtk, Gdk, GdkPixbuf, GObject, GLib
 26
 27# We will use the network of network scientists, and filter out the largest
 28# component
 29g = collection.data["netscience"]
 30g = GraphView(g, vfilt=label_largest_component(g), directed=False)
 31g = Graph(g, prune=True)
 32
 33pos = g.vp["pos"]  # layout positions
 34
 35# We will filter out vertices which are in the "Recovered" state, by masking
 36# them using a property map.
 37removed = g.new_vertex_property("bool")
 38
 39# SIRS dynamics parameters:
 40
 41x = 0.001    # spontaneous outbreak probability
 42r = 0.1      # I->R probability
 43s = 0.01     # R->S probability
 44
 45# (Note that the vertices are updated sequentially in random order, and the S->I
 46#  transition happens if a randomly chosen non-recovered neighbor is infected,
 47#  i.e. with a probability equal to the fraction of non-recovered neighbors
 48#  which are infected.)
 49
 50# The states would usually be represented with simple integers, but here we will
 51# use directly the color of the vertices in (R,G,B,A) format.
 52
 53S = [1, 1, 1, 1]           # White color
 54I = [0, 0, 0, 1]           # Black color
 55R = [0.5, 0.5, 0.5, 1.]    # Grey color (will not actually be drawn)
 56
 57# Initialize all vertices to the S state
 58state = g.new_vertex_property("vector<double>")
 59for v in g.vertices():
 60    state[v] = S
 61
 62# Newly infected nodes will be highlighted in red
 63newly_infected = g.new_vertex_property("bool")
 64
 65# If True, the frames will be dumped to disk as images.
 66offscreen = sys.argv[1] == "offscreen" if len(sys.argv) > 1 else False
 67max_count = 500
 68if offscreen and not os.path.exists("./frames"):
 69    os.mkdir("./frames")
 70
 71# This creates a GTK+ window with the initial graph layout
 72if not offscreen:
 73    win = GraphWindow(g, pos, geometry=(500, 400),
 74                      edge_color=[0.6, 0.6, 0.6, 1],
 75                      vertex_fill_color=state,
 76                      vertex_halo=newly_infected,
 77                      vertex_halo_color=[0.8, 0, 0, 0.6])
 78else:
 79    count = 0
 80    win = Gtk.OffscreenWindow()
 81    win.set_default_size(500, 400)
 82    win.graph = GraphWidget(g, pos,
 83                            edge_color=[0.6, 0.6, 0.6, 1],
 84                            vertex_fill_color=state,
 85                            vertex_halo=newly_infected,
 86                            vertex_halo_color=[0.8, 0, 0, 0.6])
 87    win.add(win.graph)
 88
 89
 90# This function will be called repeatedly by the GTK+ main loop, and we use it
 91# to update the state according to the SIRS dynamics.
 92def update_state():
 93    newly_infected.a = False
 94    removed.a = False
 95
 96    # visit the nodes in random order
 97    vs = list(g.vertices())
 98    shuffle(vs)
 99    for v in vs:
100        if state[v] == I:
101            if random() < r:
102                state[v] = R
103        elif state[v] == S:
104            if random() < x:
105                state[v] = I
106            else:
107                ns = list(v.out_neighbors())
108                if len(ns) > 0:
109                    w = ns[randint(0, len(ns))]  # choose a random neighbor
110                    if state[w] == I:
111                        state[v] = I
112                        newly_infected[v] = True
113        elif random() < s:
114            state[v] = S
115        if state[v] == R:
116            removed[v] = True
117
118    # Filter out the recovered vertices
119    g.set_vertex_filter(removed.t(lambda x: 1-x))
120
121    # The following will force the re-drawing of the graph, and issue a
122    # re-drawing of the GTK window.
123    win.graph.regenerate_surface()
124    win.graph.queue_draw()
125
126    # if doing an offscreen animation, dump frame to disk
127    if offscreen:
128        global count
129        pixbuf = win.get_pixbuf()
130        pixbuf.savev(r'./frames/sirs%06d.png' % count, 'png', [], [])
131        if count > max_count:
132            sys.exit(0)
133        count += 1
134
135    # We need to return True so that the main loop will call this function more
136    # than once.
137    return True
138
139
140# Bind the function above as an 'idle' callback.
141cid = GLib.idle_add(update_state)
142
143# We will give the user the ability to stop the program by closing the window.
144win.connect("delete_event", Gtk.main_quit)
145
146# Actually show the window, and start the main loop.
147win.show_all()
148Gtk.main()

If called without arguments, the script will show the animation inside an interactive_window(). If the parameter offscreen is passed, individual frames will be saved in the frames directory:

$ ./animation_sirs.py offscreen

These frames can be combined and encoded into the appropriate format. Here we use the mencoder tool from mplayer to combine all the frames into a single file with YUY format, and then we encode this with the WebM format, using vpxenc, so that it can be embedded in a website.

$ mencoder mf://frames/sirs*.png -mf w=500:h=400:type=png -ovc raw -of rawvideo -vf format=i420 -nosound -o sirs.yuy
$ vpxenc sirs.yuy -o sirs.webm -w 500 -h 400 --fps=25/1 --target-bitrate=1000 --good --threads=4

The resulting animation can be downloaded here, or played below.

This type of animation can be extended or customized in many ways, by dynamically modifying the various drawing parameters and vertex/edge properties. For instance, one might want to represent the susceptible state as either ../../_images/face-grin.png or ../../_images/face-surprise.png, depending on whether a neighbor is infected, and the infected state as ../../_images/zombie.png. Properly modifying the script above would lead to the following movie:

The modified script can be downloaded here.

Dynamic layout#

The graph layout can also be updated during an animation. As an illustration, here we consider a very simplistic model for spatial segregation, where the edges of the graph are repeatedly and randomly rewired, as long as the new edge has a shorter euclidean distance.

The script which performs the animation is called animation_dancing.py and is shown below.

  1#! /usr/bin/env python
  2# -*- coding: utf-8 -*-
  3
  4# This is a simple example of how to do animations using graph-tool, where the
  5# layout changes dynamically. We start with some network, and randomly rewire its
  6# edges, and update the layout dynamically, where edges are rewired only if
  7# their euclidean distance is reduced. It is thus a very simplistic model for
  8# spatial segregation.
  9
 10from graph_tool.all import *
 11from numpy.random import *
 12from numpy.linalg import norm
 13import sys, os, os.path
 14
 15seed(42)
 16seed_rng(42)
 17
 18# We need some Gtk and gobject functions
 19from gi.repository import Gtk, Gdk, GdkPixbuf, GObject, GLib
 20
 21# We will generate a small random network
 22g = random_graph(150, lambda: 1 + poisson(5), directed=False)
 23
 24# Parameters for the layout update
 25
 26step = 0.005       # move step
 27K = 0.5            # strength of the repulsion
 28
 29pos = hfd_layout(g, K=K)  # initial layout positions
 30
 31# If True, the frames will be dumped to disk as images.
 32offscreen = sys.argv[1] == "offscreen" if len(sys.argv) > 1 else False
 33max_count = 5000
 34if offscreen and not os.path.exists("./frames"):
 35    os.mkdir("./frames")
 36
 37# This creates a GTK+ window with the initial graph layout
 38if not offscreen:
 39    win = GraphWindow(g, pos, geometry=(500, 400))
 40else:
 41    win = Gtk.OffscreenWindow()
 42    win.set_default_size(500, 400)
 43    win.graph = GraphWidget(g, pos)
 44    win.add(win.graph)
 45
 46# list of edges
 47edges = list(g.edges())
 48
 49count = 0
 50
 51# This function will be called repeatedly by the GTK+ main loop, and we use it
 52# to update the vertex layout and perform the rewiring.
 53def update_state():
 54    global count
 55
 56    # Perform one step of the layout, starting from the previous positions (the
 57    # state is created anew, since the edges change at every frame)
 58    state = HFDState(g, pos=pos, K=K)
 59    state.gradient_sweep(niter=1, step=step, mu=0)
 60
 61    for i in range(100):
 62        # get a chosen edge, and swap one of its end points for a random vertex,
 63        # if it is closer
 64        i = randint(0, len(edges))
 65        e = list(edges[i])
 66        shuffle(e)
 67        s1, t1 = e
 68
 69        t2 = g.vertex(randint(0, g.num_vertices()))
 70
 71        if (norm(pos[s1].a - pos[t2].a) <= norm(pos[s1].a - pos[t1].a) and
 72            s1 != t2 and                      # no self-loops
 73            t1.out_degree() > 1 and           # no isolated vertices
 74            t2 not in s1.out_neighbors()):    # no parallel edges
 75
 76            g.remove_edge(edges[i])
 77            edges[i] = g.add_edge(s1, t2)
 78
 79
 80    # The movement of the vertices may cause them to leave the display area. The
 81    # following function rescales the layout to fit the window to avoid this.
 82    if count > 0 and count % 1000 == 0:
 83        win.graph.fit_to_window(ink=True)
 84
 85    count += 1
 86
 87    # The following will force the re-drawing of the graph, and issue a
 88    # re-drawing of the GTK window.
 89    win.graph.regenerate_surface()
 90    win.graph.queue_draw()
 91
 92    # if doing an offscreen animation, dump frame to disk
 93    if offscreen:
 94        pixbuf = win.get_pixbuf()
 95        pixbuf.savev(r'./frames/dancing%06d.png' % count, 'png', [], [])
 96        if count > max_count:
 97            sys.exit(0)
 98
 99    # We need to return True so that the main loop will call this function more
100    # than once.
101    return True
102
103
104# Bind the function above as an 'idle' callback.
105cid = GLib.idle_add(update_state)
106
107# We will give the user the ability to stop the program by closing the window.
108win.connect("delete_event", Gtk.main_quit)
109
110# Actually show the window, and start the main loop.
111win.show_all()
112Gtk.main()

This example works like the SIRS example above, and if we pass the offscreen parameter, the frames will be dumped to disk, otherwise the animation is displayed inside an interactive_window().

$ ./animation_dancing.py offscreen

Also like the previous example, we can encode the animation with the WebM format:

$ mencoder mf://frames/dancing*.png -mf w=500:h=400:type=png -ovc raw -of rawvideo -vf format=i420 -nosound -o dancing.yuy
$ vpxenc dancing.yuy -o dancing.webm -w 500 -h 400 --fps=100/1 --target-bitrate=2000 --good --threads=16

The resulting animation can be downloaded here, or played below.

Interactive visualizations#

Here we show an example of interactive visualization where the BFS tree of the currently selected vertex is highlighted with a different color.

The script which performs the visualization is called interactive_bst.py and is shown below. When called, it will open an interactive window.

 1#! /usr/bin/env python
 2# -*- coding: utf-8 -*-
 3
 4# This is a simple example of how to do interactive animations using graph-tool.
 5# Here we show the BFS tree of length 3 for the currently selected vertex.
 6
 7from graph_tool.all import *
 8
 9# We need some Gtk functions
10from gi.repository import Gtk, Gdk
11import sys
12
13offscreen = sys.argv[1] == "offscreen" if len(sys.argv) > 1 else False
14
15# We will use the network of network scientists, and filter out the largest
16# component
17g = collection.data["netscience"]
18g = GraphView(g, vfilt=label_largest_component(g), directed=False)
19g = Graph(g, prune=True)
20
21pos = g.vp["pos"]  # layout positions
22
23ecolor = g.new_edge_property("vector<double>")
24for e in g.edges():
25    ecolor[e] = [0.6, 0.6, 0.6, 1]
26vcolor = g.new_vertex_property("vector<double>")
27for v in g.vertices():
28    vcolor[v] = [0.6, 0.6, 0.6, 1]
29
30win = GraphWindow(g, pos, geometry=(500, 400),
31                  edge_color=ecolor,
32                  vertex_fill_color=vcolor)
33
34orange = [0.807843137254902, 0.3607843137254902, 0.0, 1.0]
35old_src = None
36count = 0
37def update_bfs(widget, event):
38    global old_src, g, count, win
39    src = widget.picked
40    if src is None:
41        return True
42    if isinstance(src, PropertyMap):
43        src = [v for v in g.vertices() if src[v]]
44        if len(src) == 0:
45            return True
46        src = src[0]
47    if src == old_src:
48        return True
49    old_src = src
50    pred = shortest_distance(g, src, max_dist=3, pred_map=True)[1]
51    for e in g.edges():
52        ecolor[e] = [0.6, 0.6, 0.6, 1]
53    for v in g.vertices():
54        vcolor[v] = [0.6, 0.6, 0.6, 1]
55    for v in g.vertices():
56        w = g.vertex(pred[v])
57        if w < g.num_vertices():
58            e = g.edge(w, v)
59            if e is not None:
60                ecolor[e] = orange
61                vcolor[v] = vcolor[w] = orange
62    widget.regenerate_surface()
63    widget.queue_draw()
64
65    if offscreen:
66        window = widget.get_window()
67        pixbuf = Gdk.pixbuf_get_from_window(window, 0, 0, 500, 400)
68        pixbuf.savev(r'./frames/bfs%06d.png' % count, 'png', [], [])
69        count += 1
70
71# Bind the function above as a motion-notify handler
72win.graph.connect("motion_notify_event", update_bfs)
73
74# We will give the user the ability to stop the program by closing the window.
75win.connect("delete_event", Gtk.main_quit)
76
77# Actually show the window, and start the main loop.
78win.show_all()
79Gtk.main()
$ ./interactive_bst.py offscreen

The above script is interactive, i.e. it expects a reaction from the user. But for the purpose of this demo, it also saves the frames to a file, so we can encode the animation with the WebM format:

$ mencoder mf://frames/bfs*.png -mf type=png -ovc raw -of rawvideo -vf format=i420,scale=500:400 -nosound -o bfs.yuy
$ vpxenc bfs.yuy -o bfs.webm -w 500 -h 400 --fps=5/1 --target-bitrate=2000 --good --threads=16

The resulting animation can be downloaded here, or played below.