igraph-help
[Top][All Lists]
Advanced

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

Re: [igraph] Starting with python version


From: Tamas Nepusz
Subject: Re: [igraph] Starting with python version
Date: Sat, 17 Nov 2007 13:15:58 +0100

Hi Simone,

I'm trying to start using Igraph python version. I'm getting some troubles both because i'm a newbye programmer and Ican't find a really good user guide like those written for Igraph C version.
The documentation for the Python interface is self-contained in the Python package, so you can simply use the Python help facility to get documentation on the objects and methods supported by the Python interface. E.g. try:

from igraph import *
help(Graph)

The HTML version of the documentation is available here:

http://cneurocvs.rmki.kfki.hu/igraph/doc/python/index.html

but it doesn't work ... I get this message error:

"Traceback (most recent call last):
File "getting_net.py", line 17, in ?
g=Graph(A,edges,directed=False)
TypeError: List elements must be non-negative integer pairs"
The reason is that igraph expects the edge list to contain integer pairs. You are supplying strings instead of integers. Try the following line:

edges.append((int(split_line[0]), int(split_line[1]))

By the way, a more Pythonic way to do this is the following:

from igraph import *

SP_data = open("test_graph.dat", "r")
edgelist, weights = [], []
for line in SP_data:
  parts = line.strip().split()
  edgelist.append((int(parts[0]), int(parts[1])))
  weights.append(float(parts[2]))
SP_data.close()

g = Graph(edgelist)    # directed=False is the default
g.es["weight"] = weights

I haven't tried it, but it should work. You don't have to supply the number of vertices in the constructor of the graph, since igraph is able to infer it from the edge list. An even easier way to do this is to recognize that the format you mentioned is exactly the NCOL format (see http://cneurocvs.rmki.kfki.hu/igraphbook/igraphbook- foreign.html, "Large Graph Layout Formats"), so theoretically the following should also work:

from igraph import *
g = Graph.Read_NCOL("test_graph.dat")

--
T.





reply via email to

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