-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgraph.py
111 lines (100 loc) · 3.34 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# from time import time
import networkx as nx
import pickle as pkl
import numpy as np
import scipy.sparse as sp
class Graph(object):
def __init__(self):
self.G = None
self.look_up_dict = {}
self.look_back_list = []
self.node_size = 0
def encode_node(self):
look_up = self.look_up_dict
look_back = self.look_back_list
for node in self.G.nodes():
look_up[node] = self.node_size
look_back.append(node)
self.node_size += 1
self.G.nodes[node]['status'] = ''
def read_g(self, g):
self.G = g
self.encode_node()
def read_adjlist(self, filename):
""" Read graph from adjacency file in which the edge must be unweighted
the format of each line: v1 n1 n2 n3 ... nk
:param filename: the filename of input file
"""
self.G = nx.read_adjlist(filename, create_using=nx.DiGraph())
for i, j in self.G.edges():
self.G[i][j]['weight'] = 1.0
self.encode_node()
def read_edgelist(self, filename, weighted=False, directed=False):
self.G = nx.DiGraph()
if directed:
def read_unweighted(l):
src, dst = l.split()
self.G.add_edge(src, dst)
self.G[src][dst]['weight'] = 1.0
def read_weighted(l):
src, dst, w = l.split()
self.G.add_edge(src, dst)
self.G[src][dst]['weight'] = float(w)
else:
def read_unweighted(l):
src, dst = l.split()
self.G.add_edge(src, dst)
self.G.add_edge(dst, src)
self.G[src][dst]['weight'] = 1.0
self.G[dst][src]['weight'] = 1.0
def read_weighted(l):
src, dst, w = l.split()
self.G.add_edge(src, dst)
self.G.add_edge(dst, src)
self.G[src][dst]['weight'] = float(w)
self.G[dst][src]['weight'] = float(w)
fin = open(filename, 'r')
func = read_unweighted
if weighted:
func = read_weighted
while 1:
l = fin.readline()
if l == '':
break
func(l)
fin.close()
self.encode_node()
def read_node_label(self, filename):
fin = open(filename, 'r')
while 1:
l = fin.readline()
if l == '':
break
vec = l.split()
self.G.nodes[vec[0]]['label'] = vec[1:]
fin.close()
def read_node_features(self, filename):
fin = open(filename, 'r')
for l in fin.readlines():
vec = l.split()
self.G.nodes[vec[0]]['feature'] = np.array(
[float(x) for x in vec[1:]])
fin.close()
def read_node_status(self, filename):
fin = open(filename, 'r')
while 1:
l = fin.readline()
if l == '':
break
vec = l.split()
self.G.nodes[vec[0]]['status'] = vec[1] # train test valid
fin.close()
def read_edge_label(self, filename):
fin = open(filename, 'r')
while 1:
l = fin.readline()
if l == '':
break
vec = l.split()
self.G[vec[0]][vec[1]]['label'] = vec[2:]
fin.close()