-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutil.py
57 lines (47 loc) · 1.3 KB
/
util.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
49
50
51
52
53
54
55
56
57
"""
Alex Levenson
[email protected] | www.isnotinvain.com
(c) Reya Group | http://www.reyagroup.com
Friday July 23rd 2010
"""
import networkx as nx
import csv
import igraph
def writeDict(dict,file,headerRow=None):
"""
Writes dict to file in CSV format
file: a file object or a filepath
headerRow: a list containing the headers
"""
file = nx.utils._get_fh(file, mode='w')
writer = csv.writer(file)
if headerRow: writer.writerow(headerRow)
for k,v in dict.iteritems():
writer.writerow([k,v])
file.close()
def writeNestedDict(dict,file,headerRow):
file = nx.utils._get_fh(file, mode='w')
writer = csv.writer(file)
writer.writerow(headerRow)
for k,d in dict.iteritems():
row = [k]
row.extend([d[prop] for prop in headerRow[1:]])
writer.writerow(row)
file.close()
def writeList(lyst,file,headerRow=None):
"""
Writes lyst to file in CSV format
file: a file object or a filepath
headerRow: a list containing the headers
"""
file = nx.utils._get_fh(file, mode='w')
writer = csv.writer(file)
if headerRow: writer.writerow(headerRow)
for row in lyst:
writer.writerow(row)
file.close()
def nxToIgraph(graph,directed=None):
ig = igraph.Graph(directed=directed)
ig.add_vertices(len(graph)-1)
ig.add_edges([(g.nodes().index(edge[0]),g.nodes().index(edge[1])) for edge in g.edges()])
return ig