user1043144
user1043144

Reputation: 2710

graph from weighted edge list from a database

I have list of weighted edges stored in database. How can I easily create a Graph from it (without writing it in a file and reading it)

Here the reproductible

import sqlite3 
con = sqlite3.connect(":memory:")

with con:
    cur = con.cursor()    
    cur.execute("CREATE TABLE DATEN(Source TEXT, Target TEXT, Weight REAL)")
    cur.execute("INSERT INTO DATEN VALUES('X33', 'X20', 0.014)") 
    cur.execute("INSERT INTO DATEN VALUES('X32', 'X20', 0.024)") 
    cur.execute("INSERT INTO DATEN VALUES('X23', 'X20', 0.167)") 
    cur.execute("INSERT INTO DATEN VALUES('X23', 'X32', 0.015)") 
    cur.execute("INSERT INTO DATEN VALUES('X32', 'X33', 0.003)") 
    cur.execute("INSERT INTO DATEN VALUES('X23', 'X33', 0.035)")


cur.execute('SELECT * FROM DATEN')
data = cur.fetchall()

my attempt to create a graph fails:

import networkx as nx
G = nx.Graph()
for x in data:
     x1 = {'source': data[0][0], 'target': data[0][1], 'weight':  data[0][2]} 
     print x1 

     G.add_edge(x1)  # THIS IS NOT WORKING 

Is there an easier way to do this?

Upvotes: 4

Views: 798

Answers (4)

Asiis Pradhan
Asiis Pradhan

Reputation: 37

This works

f=open('G_train.txt','r')
data=f.read()
EDGES=ast.literal_eval(data)

g = nx.DiGraph((x, y, {'weights': v}) for (x, y), v in Counter(EDGES).items())

print("Edges", g.edges(data=True), sep='\n')

Upvotes: 0

unutbu
unutbu

Reputation: 880877

The cursor is an iterator that yields rows after a SELECT statement is executed. For example, instead of fetching all the results into a list with

rows = cur.fetchall()

You can iterate over the rows with

for row in cur:

However, since the NetworkX add_weighted_edges_from method accepts an iterator, you can pass cur directly:

G.add_weighted_edges_from(cur)

This is a nice example of polymorphism. The designers of NetworkX did not have to do anything special or even anticipate that sqlite3 cursors might be passed as an argument, they only had to write the code with the assumption that the first argument was an iterator.


import networkx as nx
import sqlite3 
import matplotlib.pyplot as plt

with sqlite3.connect(":memory:") as con:
    cur = con.cursor()    
    cur.execute("CREATE TABLE DATEN(Source TEXT, Target TEXT, Weight REAL)")
    cur.execute("INSERT INTO DATEN VALUES('X33', 'X20', 0.014)") 
    cur.execute("INSERT INTO DATEN VALUES('X32', 'X20', 0.024)") 
    cur.execute("INSERT INTO DATEN VALUES('X23', 'X20', 0.167)") 
    cur.execute("INSERT INTO DATEN VALUES('X23', 'X32', 0.015)") 
    cur.execute("INSERT INTO DATEN VALUES('X32', 'X33', 0.003)") 
    cur.execute("INSERT INTO DATEN VALUES('X23', 'X33', 0.035)")

    G = nx.Graph()
    cur.execute('SELECT Source, Target, Weight FROM DATEN')
    G.add_weighted_edges_from(cur)    

nx.draw(G)
plt.show()

enter image description here

Upvotes: 5

Guillaume Frichet
Guillaume Frichet

Reputation: 1

First, could you give us the error message ?

Then, you don't seem to use add_edge properly (see : http://networkx.github.io/documentation/latest/reference/generated/networkx.Graph.add_edge.html#networkx.Graph.add_edge )

you should try to add your edges like this:

import networkx as nx
G = nx.Graph()
for x in data:
    G.add_edge(x[0], x[1], weight=x[2])

Upvotes: 0

mata
mata

Reputation: 69092

Graph.add_edge() doesn't take a dict as argument, it takes at least two nodes and optional keyword arguments. Try this:

import networkx as nx
G = nx.Graph()
for x in data:
     G.add_edge(x[0], x[1], weight=x[2])

Upvotes: 0

Related Questions