|
From: | Nick Eubank |
Subject: | Re: [igraph] Import with edge attributes in python |
Date: | Wed, 17 Dec 2014 19:58:44 +0000 |
Message: 1
Date: Mon, 15 Dec 2014 21:39:39 +0100
From: Tamas Nepusz <address@hidden>
To: Help for igraph users <address@hidden>
Subject: Re: [igraph] Import with edge attributes in python
Message-ID: <20141215203939.GY24137@porcupine.local>
Content-Type: text/plain; charset=us-ascii
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.
[Prev in Thread] | Current Thread | [Next in Thread] |