-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdata_loader.py
More file actions
180 lines (154 loc) · 5.52 KB
/
data_loader.py
File metadata and controls
180 lines (154 loc) · 5.52 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Third Party Library
import networkx as nx
import numpy as np
import torch
from scipy import io
# First Party Library
import config
np.set_printoptions(threshold=np.inf)
device = config.select_device
def spmat2sptensor(sparse_mat):
# Third Party Library
import torch
dense = sparse_mat.todense()
dense = torch.from_numpy(dense.astype(np.float32)).clone().to(device)
return dense
def spmat2tensor(sparse_mat):
# Third Party Library
import torch
shape = sparse_mat.shape
sparse_mat = sparse_mat.tocoo()
sparse_tensor = torch.sparse.FloatTensor(
torch.LongTensor([sparse_mat.row.tolist(), sparse_mat.col.tolist()]),
torch.FloatTensor(sparse_mat.data.astype(np.float32)),
shape,
)
if torch.cuda.is_available():
sparse_tensor = sparse_tensor.cuda()
return sparse_tensor
class attr_graph_dynamic_spmat_DBLP:
def __init__(self, dirIn="./data/", dataset="DBLP", T=3):
dirIn = dirIn + dataset
# input G
self.T = T
self.G_list = []
self.A_list = []
self.Gmat_list = []
self.Amat_list = []
self.Gtensor_list = []
survive = None
self.len = 0
for t in range(T):
G_matrix = io.loadmat(
dirIn + "/G" + str(t) + ".mat", struct_as_record=True
)["G"]
if survive is None:
survive = np.array(G_matrix.sum(axis=0))
else:
survive = np.multiply(survive, G_matrix.sum(axis=0))
survive = np.ravel(survive > 0)
for t in range(T):
G_matrix = io.loadmat(
dirIn + "/G" + str(t) + ".mat", struct_as_record=True
)["G"]
A_matrix = io.loadmat(
dirIn + "/A" + str(t) + ".mat", struct_as_record=True
)["A"]
G_matrix = G_matrix.T[survive].T
A_matrix = A_matrix.T.dot(G_matrix).T
A = nx.DiGraph()
self.A_list.append(A)
self.Amat_list.append(A_matrix)
G_matrix = G_matrix.T.dot(G_matrix)
G_matrix[G_matrix > 0] = 1.0
G = nx.from_scipy_sparse_matrix(
G_matrix, create_using=nx.DiGraph()
)
self.len = len(G.nodes())
self.G_list.append(G)
self.Gmat_list.append(G_matrix)
self.Gtensor_list.append(spmat2tensor(G_matrix))
class attr_graph_dynamic_spmat_NIPS:
def __init__(self, dirIn="./data/", dataset="NIPS", T=3):
dirIn = dirIn + dataset
# input G
self.T = T
self.G_list = []
self.A_list = []
self.Gmat_list = []
self.Amat_list = []
self.Gtensor_list = []
survive = None
self.len = 0
for t in range(T):
G_matrix = io.loadmat(
dirIn + "/G" + str(t) + ".mat", struct_as_record=True
)["G"]
if survive is None:
survive = np.array(G_matrix.sum(axis=0))
else:
survive = np.multiply(survive, G_matrix.sum(axis=0))
survive = np.ravel(survive > 0)
for t in range(T):
G_matrix = io.loadmat(
dirIn + "/G" + str(t) + ".mat", struct_as_record=True
)["G"]
A_matrix = io.loadmat(
dirIn + "/A" + str(t) + ".mat", struct_as_record=True
)["A"]
G_matrix = G_matrix.T[survive].T
A_matrix = A_matrix.T.dot(G_matrix).T
A = nx.DiGraph()
self.A_list.append(A)
self.Amat_list.append(A_matrix)
G_matrix = G_matrix.T.dot(G_matrix)
G_matrix[G_matrix > 0] = 1.0
# print(G_matrix.getrow(0))
G = nx.from_scipy_sparse_matrix(
G_matrix, create_using=nx.DiGraph()
)
self.len = len(G.nodes())
self.G_list.append(G)
self.Gmat_list.append(G_matrix)
self.Gtensor_list.append(spmat2tensor(G_matrix))
class attr_graph_dynamic_spmat_twitter:
def __init__(self, dirIn="./data/", dataset="twitter", T=1):
n_nodes_fortime = 10000
dirIn = dirIn + dataset
self.T = T
self.len = 0
self.G_list = []
self.A_list = []
self.Gmat_list = []
self.Amat_list = []
survive = None
for t in range(T):
G_matrix = io.loadmat(
dirIn + "/G" + str(t) + ".mat", struct_as_record=True
)["G"]
if survive is None:
survive = np.array(G_matrix.sum(axis=0)) * 1.0 / T
else:
survive += np.array(G_matrix.sum(axis=0)) * 1.0 / T
survive = np.ravel(survive > 0.1)
for t in range(T):
G_matrix = io.loadmat(
dirIn + "/G" + str(t) + ".mat", struct_as_record=True
)["G"]
A_matrix = io.loadmat(
dirIn + "/A" + str(t) + ".mat", struct_as_record=True
)["A"]
G_matrix = G_matrix[survive]
G_matrix = G_matrix[:, survive][:n_nodes_fortime, :n_nodes_fortime]
A_matrix = A_matrix[survive][:n_nodes_fortime, :n_nodes_fortime]
A = nx.DiGraph()
self.A_list.append(A)
self.Amat_list.append(A_matrix)
G_matrix[G_matrix > 0] = 1.0
G = nx.from_scipy_sparse_matrix(
G_matrix, create_using=nx.DiGraph()
)
self.len = len(G.nodes())
print(self.len)
self.G_list.append(G)
self.Gmat_list.append(G_matrix)