Create edges in networkx out of csv-File

Question:

I work in economics and I’m a newbie in programming, so please pardon my lack of technical knowledge here.

Im using iPython 2.7 and I want to analyze a production system as network.
Therefore I
m trying to create a network out of a csv-file. The csv-file includes two columns. First column is a representation of work orders, the second represents working stations. The working stations have to be nodes of the network.

ORDER_ID;MACHINE_ID;
0;0;
0;1;
1;1;
2;2;
2;3;
2;4;
2;5;
2;6;
2;1;
2;7;
2;2;
3;8;
3;1;
4;9;
5;10;
5;10;
5;5;
5;11;
5;0; 
5;12;

As long as the order ID is the same number in different rows, the nodes of these rows have to be linked as edge. As soon as the Order ID changes, the next row has to checked and linked as edge. Edges are directed for better analysis. I wrote an algorithm, but I have problems with picking row item.

import networkx as nx
import csv 

g = nx.DiGraph()

with open("Data.csv") as dataSet:
    data = csv.reader()
    for OrderID1 in dataSet:
        OrderID = OrderID[0]
        for MachineID1 in dataSet:
            MachineID = MachineID1[1]
                if OrderID[0][i] == OrderID[0][i+1]:  
                    g.add_edge(MachineID[0][i],MachineID[0][i+1])
                elif OrderID[0][i] != row[0][i+1]: 
                    g.add_edge(row[0][i+1],row[0][i+2])
                else:
                    dataSet.close()

Result have to look like this:

g.edges()

>>> (0,1),(2,3),(3,4),(4,5),(5,6),(6,1),(1,7),(7,2),(8,1),(10,10),(10,5),(5,11),(11,0),(0,12)

Many thanks to everyone who takes time to respond!

Asked By: David Kos

||

Answers:

The algorithm as I see it should be as follows:

import csv
import networkx as nx
g = nx.DiGraph()
data_csv = open('Data.csv')
csv_f = csv.reader(data_csv,delimiter=';')
prev_orderID = -1
prev_machineID = -1
for row in csv_f:
    if row[0] == prev_orderID:
        g.add_edge(prev_machineID,row[1])
    prev_orderID = row[0]
    prev_machineID = row[1]
print g.edges()

I have taken the sample you gave into a .csv file and the result was the same as your expectations:

[('11', '0'), ('10', '10'), ('10', '5'), ('1', '7'), ('0', '1'), ('0', '12'), ('3', '4'), ('2', '3'), ('5', '11'), ('5', '6'), ('4', '5'), ('7', '2'), ('6', '1'), ('8', '1')]

Maybe you had problems reading the rows because you did not specify the delimiter.

Answered By: Abdallah Sobehy
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.