Skip to main content

Importing your data in Tulip with Python

Importing Data using python scripting

The boilerplate

First, fire up Tulip, create a new Graph (Ctrl+N or from the menu File->New), open the python scripting component (View->Python Script View).

I personally like to tile the windows (Windows->Tile) so they both use as much space as they can.

The python script view will contain a basic script that creates variables for easy access to the graph's properties, such as nodes size, position color and so on.

The basics

Lets use twitter as an example, and admit you want to visualize your twitter friends and followers.

You will need to add a network lib, and a json parsing lib:

import urllib2
import json

Then, get your twitter friend list:

response = urllib2.urlopen('http://api.twitter.com/1/friends/ids.json?user_id=24166202')
friendsHTML = response.read()

This will return a list of IDs in a JSON array, to convert it to python data use:

friends = json.loads(friendsHTML)

then we can iterate on this and add nodes:

for friend in friends:
        friendNode = graph.addNode()


But this does not gets us far.

The interesting part

Let's add a property on those nodes to keep track of the ID of my friends:

identifier= graph.getIntegerProperty("identifier")

This creates a property of integer type, where we the identifiers of your friends will be stored.

Then, to set a value on a node you simply use property[node] = value

lets create a node to represent you:

me = graph.addNode()

and set its ID:

identifier[me] = 24166202

Now in the loop we can add edges from you to your friends (and set their IDs for later use):

for friend in friends:
	friendNode = graph.addNode()
	identifier[friendNode] = friend
	graph.addEdge(me, friendNode)

Hey, why did it add only one node ?

No he didn't. Look at the bottom right corner of Tulip: there are lots of nodes and edges, but you did not specify their location, so they are all at the same place.

You can use a layout algorithm from the GUI (Algorithm->Layout->...) or why not directly from the code ?

graph.computeLayoutProperty("Bubble Tree", viewLayout)

The first parameter is the name of the layout algorithm (the name is precisely what is displayed in the menu entry), the property to apply this algorithm upon (in our case, the displayed layout, or viewLayout, and an optional DataSet to pass options to the algorithm.

 

I attached a slightly enhanced version of this script to this article (twitter.py, that can be found at the bottom of the page), which adds followers and friends, and colors friends green and followers blue.

As I don't have a twitter account, I used Jens Bergensten's ID in this example (Jens is a Minecraft developper, and one of the only persons whose tweets I read. I would have used Markus Persson's, but he has a lot of followers, and I feel it would get be a bit too large a dataset for a simple example like this. Jens' 50.000 followers is already bigger than I'd like but I don't know anybody else on twitter).

 

Now it gets interesting

It is easy to import data into Tulip with this method, and it is excellent for trial-and-error development of an import.

But if you want to use it more easily, and more importantly, share it, you can use a new feature in Tulip 3.6: the python plug-in.

In the Python script view, click the "Tulip plug-in editor" tab, and create a new plugin using the "new" icon.

A wizard prompts you for a file, class name and plugin name, as well as more general informatiosn.

The class name is the name of the python class that will be generated, and the plugin name is the name of the plugin as it will appear in the GUI.

The plugin type is what your plugin does: import data, export data, change the layout, color, ...

In our case, use "Import".

A squeletton will appear with all the required fields, and comments about what each should do.

Just copy/paste the contents of the main function in the previously written import into the importGraph function.

You will need to add a graph variable that points to self.graph, and do not forget to retrieve the properties you might have used.

Aslo mind the imports you made.

Now click on the "Register plugin" at the bottom of the view, and you plugin will appear in the GUI (File->Import->Twitter import in our case).

To have a clean twitter import, it would require adding parameters (the seed ID), and depth of the tree to build, as well as specifying whether to import friends, followers, and so on.

This plugin can be copy-pasted into any Tulip 3.6 installation in the "[tulipRootPath]/lib/tulip/python" folder, and will be loaded automagically when Tulip launches.

But this will be the subject of another tutorial ;)