cmant
cmant

Reputation: 463

python networkx: cannot use current_flow_betweenness_centrality function

I'm trying to use the current_flow_betweenness_centrality function of the NetworkX library, but it gives me this error when called:

Traceback (most recent call last):   File "D:\IVV\pcg\procsator\__init__.py", line 47, in <module>
    el_val_rel = graph.elements_values(elements, conn_graph, m)   File "D:\IVV\pcg\procsator\graph.py", line 46, in elements_values
    conn_val = metric_func(g)   File "C:\Python27\lib\site-packages\networkx\algorithms\centrality\current_flow_betweenness.py", line 233, in current_flow_betweenness_centrality
    solver=solver):   File "C:\Python27\lib\site-packages\networkx\algorithms\centrality\flow_matrix.py", line 15, in flow_matrix_row
    dtype=dtype, format='csc')   File "C:\Python27\lib\site-packages\networkx\algorithms\centrality\flow_matrix.py", line 135, in laplacian_sparse_matrix
    dtype=dtype, format=format)   File "C:\Python27\lib\site-packages\networkx\convert.py", line 790, in to_scipy_sparse_matrix
    for u,v,d in G.edges_iter(nodelist, data=True) ValueError: need more than 0 values to unpack

This is the code I'm using:

import networkx as nx

g=nx.Graph()
a=object()
b=object()
c=object()
d=object()
g.add_edge(a,b,{'distance': 4.0})
g.add_edge(a,c,{'distance': 1.5})
g.add_edge(b,c,{'distance': 2.2})
g.add_edge(c,d,{'distance': 2.6})
result = nx.current_flow_betweenness_centrality(g, weight='distance')

Where am I wrong? Tried with NetworkX 1.7 with both Python 2.7 and 3.3, on Windows 7 x64.

Upvotes: 0

Views: 605

Answers (1)

Aric
Aric

Reputation: 25289

Update:

This is a bug addressed/fixed in https://github.com/networkx/networkx/pull/856

Original (incorrect) answer:

NetworkX nodes must be Python "hashable" objects. Your object() nodes are not. (The error message you got isn't very helpful).

For example this works

import networkx as nx

g=nx.Graph()
a=1
b=2
c=3
d=4
g.add_edge(a,b,{'distance': 4.0})
g.add_edge(a,c,{'distance': 1.5})
g.add_edge(b,c,{'distance': 2.2})
g.add_edge(c,d,{'distance': 2.6})
result = nx.current_flow_betweenness_centrality(g, weight='distance')

Upvotes: 1

Related Questions