-
Notifications
You must be signed in to change notification settings - Fork 2
/
import_graph.py
58 lines (55 loc) · 1.71 KB
/
import_graph.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
58
import networkx as nx
import os
def import_graph(dataset):
"""Import a graph stored in text files into a NetworkX graph."""
try:
f = open( os.path.join(dataset , "type.txt"), "r")
type_of_graph = f.readline()[0:-1]
f.close()
if type_of_graph == "DW":
G = nx.read_weighted_edgelist(
os.path.join(dataset , "edge.txt"),
nodetype=int,
create_using=nx.DiGraph())
elif type_of_graph == "UW":
G = nx.read_weighted_edgelist(
os.path.join(dataset , "edge.txt"),
nodetype=int)
elif type_of_graph == "DU":
G = nx.read_edgelist(
os.path.join(dataset , "edge.txt"),
nodetype=int,
create_using=nx.DiGraph())
else:
G = nx.read_edgelist(
os.path.join(dataset , "edge.txt"),
nodetype=int)
G.name = dataset
except:
G = nx.Graph(name = "Empty graph")
return G
def import_position(dataset):
"""Import the position layout associated with a graph."""
pos = {}
try:
f = open(os.path.join(dataset , "position.txt"), "r")
u = 0
for line in f:
s = line.split()
pos[u] = (float(s[0]),float(s[1]))
u += 1
f.close()
except:
pass
return pos
def import_label(dataset):
"""Import the labels associated with a graph."""
label = []
try:
f = open(os.path.join(dataset , "label.txt"), "r")
for line in f:
label.append(line[0:-1])
f.close()
except:
pass
return label