[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [igraph] Import with edge attributes in python
From: |
Tamas Nepusz |
Subject: |
Re: [igraph] Import with edge attributes in python |
Date: |
Mon, 15 Dec 2014 21:39:39 +0100 |
User-agent: |
Mutt/1.5.23 (2014-03-12) |
Hi,
> I'm trying to import a (large) graph from an edgelist (vertices in columns
> 1 and 2, and weights in the third and fourth columns). Since I have two
> sets of weights (different modes of communication), I'm not sure how to
> import them -- read.graph() with format = 'ncol' only seems to accept one
> column of weights, and format = 'edgelist' doesn't seem to tolerate edge
> attributes of any sort. Is there a way to (bulk) load more attributes? Or
> do I need to loop over all the edges and add them manually one at a time?
The easiest is probably to iterate over the lines of the file and construct
a tuple of length 4 from each of them. You can then pass these tuples to the
Graph.TupleList() constructor. Something like:
def tuples(input_file):
for line in open(input_file):
u, v, weight1, weight2 = line.split()
weight1 = float(weight1)
weight2 = float(weight2)
yield u, v, weight1, weight2
g = Graph.TupleList(tuples, edge_attrs=("weight1", "weight2"))
This should yield a graph where the "name" vertex attribute stores the vertex
names (the first and second columns of the input) and the "weight1" and
"weight2" edge attributes store the weights.
T.