[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] Python Interface to igraph

From: Tamas Nepusz
Subject: Re: [igraph] Python Interface to igraph
Date: Thu, 7 Feb 2008 16:12:14 +0100

Dear Lorenzo,

Now, I have found out that there is a Python interface, so it does not
make any sense to invoke it from R.
Well, it might - the R interface currently has some features that the Python interface does not (related to parts that are implemented in C+ + in the original library, like walktrap/spinglass community detection and the BLISS isomorphism - however they will be included in 0.5!), and the calling convention of the Python interface is not always the same as one would guess from the R interface.

(1) Is this the correct way of proceeding? I would guess so, since
Python recognizes the library as being installed, but I would like to
be told so by someone acknowledgeable.
As for me, I use python setup.py build && python setup.py install. However, you can simply check whether the installation is correct by invoking Python and typing:

>>> from igraph.test import test
>>> test()
Ran 104 tests in 0.695s

(note that this is v0.5 from the dev tree, v0.4.5 has ~50 tests only)

(2)Say that at some point a version 0.5 is release and I want to
upgrade my installation of igraph. Should I just repeat what I did at
Yes. The new version will simply overwrite the old one in /usr/lib/ python. Just a small note: until up to 0.4.5, the Python extension was statically linked to the C core, so there was no need to install the C libraries in order to use it from Python. This will change with 0.5, you'll have to install the C libraries as well before using igraph from Python. Debian packages for the C library will be made available soon after the release of 0.5 (maybe in a day or so). We have a Debian package repository for igraph releases at http://cneurocvs.rmki.kfki.hu/packages . The binaries are built for the i386 architecture, the source packages build on i386, x86_64 and amd64 architectures. So after the release of igraph 0.5, you'll first have to build the C library packages for your architecture. See http://www.debian.org/doc/manuals/apt-howto/ch-sourcehandling.en.html for instructions on how to build source packages. The two lines that you'll have to add to /etc/apt/sources.list are as follows:
deb http://cneurocvs.rmki.kfki.hu /packages/binary/
deb-src htp://cneurocvs.rmki.kfki.hu /packages/source/

I have plans on building Debian packages for the Python interface as well, but it's unlikely that I'll have time to do that in the near future. :(

Would that be the case now? In other words: what is the procedure to
uninstall igraph if I need to?
The Python interface installs itself in /usr/lib/python2.4/site- packages/ (or .../python2.5/... of course). Either you'll find an igraph subdirectory here that you can safely delete, or a file called igraph-0.4.5-py2.4-linux-x86_64.egg (replace version numbers and architecture names as necessary) which can also be safely deleted. That's all.


How would they translate in Python?
First suppose that dist_mat consists of only zeros and ones. Let's call it dist_mat_01. You can type:

>>> from igraph import *
>>> g=Graph.Adjacency(dist_mat_01, ADJ_UNDIRECTED)
>>> g.simplify()
>>> clustering=g.clusters()
>>> nc=len(clustering)
>>> cluster_sizes=cl.sizes()

Now, unfortunately producing a dist_mat consisting of only zeros and ones is not so easy in Python as in R (at least the syntax is a little bit different). I'd do it as follows:

>>> def clamp(value, threshold):
...   if value<threshold: return 1
...   return 0
>>> dist_mat_01 = [map(clamp, row) for row in dist_mat]

A more powerful matrix datatype is available for Python in a package called NumPy (Numeric Python). You can also use that. If dist_mat is a NumPy matrix, then you can simply type:

>>> g=Graph.Adjacency((dist_mat <= d).tolist(), ADJ_UNDIRECTED)

(dist_mat <= d) works the same as in R, but its result is a NumPy matrix which igraph cannot process directly, since it expects a list of lists. That's why it is converted to a list first by invoking the .tolist() method.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]