Graph from the list of weighted edges from the database

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

Here reproducible

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 failed:

 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?

+4
source share
4 answers

A cursor is an iterator that gives rows after executing a SELECT statement. For example, instead of retrieving all the results in a list with

 rows = cur.fetchall() 

You can iterate over strings with

 for row in cur: 

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

 G.add_weighted_edges_from(cur) 

This is a good example of polymorphism. NetworkX designers did not need to do anything special or even expect sqlite3 cursors to be passed as an argument; they only needed to write code with the assumption that the iterator was the first argument.


 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

+5
source

Graph.add_edge() does not accept the dict argument as an argument, it takes at least two nodes and optional keyword arguments. Try the following:

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

Firstly, could you give us an error message?

Then you don't seem to use add_edge correctly (see http://networkx.imtqy.com/documentation/latest/reference/generated/networkx.Graph.add_edge.html#networkx.Graph.add_edge )

you should try adding your edges as follows:

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

It 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') 
0
source

All Articles