Problems to construct a graph with multiple edges between nodes

251 views
Skip to first unread message

Marcio Luis Acencio

unread,
Jan 28, 2010, 6:42:17 PM1/28/10
to networkx...@googlegroups.com
Hey everyone!

I have tried to construct a directed graph with multiple edges between nodes (MultiDiGraph) but I have been unable to succeed with this. As I want to construct a network from a a edge list file, first I have tried to use the following code:

import networkx as nx
lista = open('net1','r')
G = nx.read_edgelist(lista,create_using=nx.MultiDiGraph(),delimiter=' ',edgetype=dict)

Below is the edge list file "net1":
A B {'inter':'reg'}
A C {'inter':'ppi'}
C A {'inter':'ppi'}
B D {'inter':'met'}
B A {'inter': 'ppi'}
B D {'inter': 'ppi'}
D B {'inter': 'ppi'}
E B {'inter': 'ppi'}
A B {'inter':'ppi'}
B E {'inter': 'ppi'}

Result:
Traceback (most recent call last):
  File "centralidades.py", line 9, in <module>
    G = nx.read_edgelist(lista,create_using=nx.MultiDiGraph(),delimiter=' ',edgetype=dict)
  File "/usr/local/lib/python2.6/dist-packages/networkx-1.0.1-py2.6.egg/networkx/readwrite/edgelist.py", line 189, in read_edgelist
TypeError: Failed to convert edge data ({'inter':'reg'}) to type <type 'dict'>

After that, I have tried to constructed my network by using the following code (note that "gtest" is the same network shown above) :
import networkx as nx
gtest = {'A':{'B':{'inter': 'reg'}},'A':{'C':{'inter': 'ppi'}},'C':{'A':{'inter': 'ppi'}},'B':{'D':{'inter': 'met'}},'B':{'A':{'inter': 'ppi'}},'B':{'D':{'inter': 'ppi'}},'D':{'B':{'inter': 'ppi'}},'E':{'B':{'inter': 'ppi'}},'A':{'B':{'inter': 'ppi'}},'B':{'E':{'inter': 'ppi'}}}
G = nx.from_dict_of_dicts(gtest,create_using=nx.MultiDiGraph(),multigraph_input=True)

Result:
Traceback (most recent call last):
  File "centralidades.py", line 12, in <module>
    G = nx.from_dict_of_dicts(gteste,create_using=nx.MultiDiGraph(),multigraph_input=True)
  File "/usr/local/lib/python2.6/dist-packages/networkx-1.0.1-py2.6.egg/networkx/convert.py", line 324, in from_dict_of_dicts
  File "/usr/local/lib/python2.6/dist-packages/networkx-1.0.1-py2.6.egg/networkx/classes/multigraph.py", line 350, in add_edges_from
ValueError: dictionary update sequence element #0 has length 1; 2 is required

Please, let me know which mistake(s) I have committed.

Cheers,

Marcio

Aric Hagberg

unread,
Jan 28, 2010, 7:32:28 PM1/28/10
to networkx...@googlegroups.com
On Thu, Jan 28, 2010 at 4:42 PM, Marcio Luis Acencio
<mlac...@gmail.com> wrote:
> Hey everyone!
>
> I have tried to construct a directed graph with multiple edges between nodes
> (MultiDiGraph) but I have been unable to succeed with this. As I want to
> construct a network from a a edge list file, first I have tried to use the
> following code:
>
> import networkx as nx
> lista = open('net1','r')
> G = nx.read_edgelist(lista,create_using=nx.MultiDiGraph(),delimiter='
> ',edgetype=dict)
>
> Below is the edge list file "net1":
> A B {'inter':'reg'}
> A C {'inter':'ppi'}
> C A {'inter':'ppi'}
> B D {'inter':'met'}
> B A {'inter': 'ppi'}
> B D {'inter': 'ppi'}
> D B {'inter': 'ppi'}
> E B {'inter': 'ppi'}
> A B {'inter':'ppi'}
> B E {'inter': 'ppi'}
>

Drop the "edgetype" keyword and use

>>> G=nx.read_edgelist('net',create_using=nx.MultiGraph())

and it should work.

If it makes sense you might want to relabel "inter" as "key"
since that is an internal NetworkX dictionary name to identify multiple
edges. If you don't specify a key an integer key will be assigned.
e.g.
In [78]: G['A']['B']
Out[78]: {0: {'inter': 'reg'}, 1: {'inter': 'ppi'}}
vs
In [80]: G['A']['B']
Out[80]: {'ppi': {}, 'reg': {}}


> After that, I have tried to constructed my network by using the following
> code (note that "gtest" is the same network shown above) :
> import networkx as nx
> gtest = {'A':{'B':{'inter': 'reg'}},'A':{'C':{'inter':
> 'ppi'}},'C':{'A':{'inter': 'ppi'}},'B':{'D':{'inter':
> 'met'}},'B':{'A':{'inter': 'ppi'}},'B':{'D':{'inter':
> 'ppi'}},'D':{'B':{'inter': 'ppi'}},'E':{'B':{'inter':
> 'ppi'}},'A':{'B':{'inter': 'ppi'}},'B':{'E':{'inter': 'ppi'}}}
> G =
> nx.from_dict_of_dicts(gtest,create_using=nx.MultiDiGraph(),multigraph_input=True)
>
> Result:
> Traceback (most recent call last):
>   File "centralidades.py", line 12, in <module>
>     G =
> nx.from_dict_of_dicts(gteste,create_using=nx.MultiDiGraph(),multigraph_input=True)
>   File
> "/usr/local/lib/python2.6/dist-packages/networkx-1.0.1-py2.6.egg/networkx/convert.py",
> line 324, in from_dict_of_dicts
>   File
> "/usr/local/lib/python2.6/dist-packages/networkx-1.0.1-py2.6.egg/networkx/classes/multigraph.py",
> line 350, in add_edges_from
> ValueError: dictionary update sequence element #0 has length 1; 2 is
> required

I think this should work - I'll look into it.

Aric

Reply all
Reply to author
Forward
0 new messages