forked from umd-mith/geniusing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedge_list.py
executable file
·48 lines (34 loc) · 937 Bytes
/
edge_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
"""
This program will extract an edge list from the songs.csv file for viewing
in a graph utility like Cytoscape or Gephi.
./edge_list.py Artist Producer
will write a file:
artist-producer.csv
"""
import sys
import csv
import networkx
csv_file = sys.argv[1]
col1 = sys.argv[2]
col2 = sys.argv[3]
g = networkx.DiGraph()
input_data = csv.DictReader(open(csv_file))
for row in input_data:
n1 = row[col1]
for n2 in row[col2].split(","):
if not n2 or n1 == n2:
continue
g.add_node(n1, {type: 'artist'})
g.add_node(n2, {type: 'producer'})
g.add_edge(n1, n2)
e = g[n1][n2]
e['weight'] = e.get('weight', 0) + 1
writer = csv.DictWriter(sys.stdout, fieldnames=[col1, col2, 'weight'])
writer.writeheader()
for n1, n2 in g.edges():
writer.writerow({
col1: n1,
col2: n2,
"weight": g[n1][n2]['weight']
})