Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
It’s a bit out of scope to take a dive into graph-layout algorithms, but it’s hard to pass up an opportunity to introduce Ubigraph, a 3D interactive graph-visualization tool. While there’s not really any analytical value in visualizing a clique, it’s nonetheless a good exercise to learn the ropes. Ubigraph is trivial to install and comes with bindings for a number of popular programming languages, including Python. Example 4-15 illustrates an example usage of Ubigraph for graphing the common members among the maximum cliques for a network. The bad news is that this is one of those opportunities where Windows folks will probably need to consider running a Linux-based virtual machine, since it is unlikely that there will be a build of the Ubigraph server anytime soon. The good news is that it’s not very difficult to pull this together, even if you don’t have advanced knowledge of Linux.
Example 4-15. Visualizing graph data with Ubigraph (friends_followers__ubigraph.py)
# -*- coding: utf-8 -*-
import sys
import json
import networkx as nx
# Packaged with Ubigraph in the examples/Python directory
import ubigraph
SCREEN_NAME = sys.argv[1]
FRIEND = sys.argv[2]
g = nx.read_gpickle(SCREEN_NAME + '.gpickle')
cliques = [c for c in nx.find_cliques(g) if FRIEND in c]
max_clique_size = max([len(c) for c in cliques])
max_cliques = [c for c in cliques if len(c) == max_clique_size]
print 'Found %s max cliques' % len(max_cliques)
print json.dumps(max_cliques, indent=4)
U = ubigraph.Ubigraph()
U.clear()
small = U.newVertexStyle(shape='sphere', color='#ffff00', size='0.2')
largeRed = U.newVertexStyle(shape='sphere', color='#ff0000', size='1.0')
# find the people who are common to all cliques for visualization
vertices = list(set([v for c in max_cliques for v in c]))
vertices = dict([(v, U.newVertex(style=small, label=v)) for v in vertices if v
not in (SCREEN_NAME, FRIEND)])
vertices[SCREEN_NAME] = U.newVertex(style=largeRed, label=SCREEN_NAME)
vertices[FRIEND] = U.newVertex(style=largeRed, label=FRIEND)
for v1 in vertices:
for v2 in vertices:
if v1 == v2:
continue
U.newEdge(vertices[v1], vertices[v2])
All in all, you just create a graph and add vertices and edges to
it. Note that unlike in NetworkX, however, nodes are not defined by
their labels, so you do have to take care not to add duplicate nodes to
the same graph. The preceding example demonstrates building a graph by
iterating through a dictionary of vertices with minor customizations so
that some nodes are easier to see than others. Following the pattern of
the previous examples, this particular listing would visualize the
common members of the maximum cliques shared between a particular user
(Tim O’Reilly, in this case) and a friend (defined by
FRIEND)—in other words, the largest clique containing two
constrained nodes. Figure 4-3 shows a
screenshot of Ubigraph at work. Like most everything else in this book,
this is just the bare minimum of an introduction, intended to inspire
you to go out and do great things with data.
[28] An Infochimps “Who Is” API is forthcoming and will provide a means of resolving screen names from user IDs.