-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathmag_dataset.py
179 lines (150 loc) · 7.07 KB
/
mag_dataset.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
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
import os
import numpy as np
import scipy.sparse as sp
import torch
import dgl
from dgl.data import download, extract_archive
from . import BaseDataset, register_dataset
@register_dataset('mag_dataset')
class MagDataset(BaseDataset):
def __init__(self, *args, **kwargs):
train_percent = kwargs.pop('train_percent', 0.1)
super(MagDataset, self).__init__(*args, **kwargs)
self.data_path = 'openhgnn/dataset/data/mag.zip'
self.raw_dir = 'openhgnn/dataset/data/'
self.g_path = 'openhgnn/dataset/data/mag/mag/'
self.name = 'mag'
self.url = 'https://raw.githubusercontent.com/MoonLight-Sherry/OpenHGNN/main/openhgnn/dataset/data/test/mag.zip'
# self.url = 'http://localhost:8000/mag.zip'
self.train_percent = train_percent
self.download()
self.load_odbmag_4017(self.train_percent)
def load_odbmag_4017(self, train_percent):
feats = np.load(self.g_path + 'feats.npz', allow_pickle=True)
p_ft = feats['p_ft']
a_ft = feats['a_ft']
i_ft = feats['i_ft']
f_ft = feats['f_ft']
ft_dict = {}
ft_dict['p'] = torch.FloatTensor(p_ft)
ft_dict['a'] = torch.FloatTensor(a_ft)
ft_dict['i'] = torch.FloatTensor(i_ft)
ft_dict['f'] = torch.FloatTensor(f_ft)
p_label = np.load(self.g_path + 'p_label.npy', allow_pickle=True)
p_label = torch.LongTensor(p_label)
idx_train_p, idx_val_p, idx_test_p = train_val_test_split(p_label.shape[0], train_percent)
label = {}
label['p'] = [p_label, idx_train_p, idx_val_p, idx_test_p]
sp_a_i = sp.load_npz(self.g_path + 'norm_sp_a_i.npz')
sp_i_a = sp.load_npz(self.g_path + 'norm_sp_i_a.npz')
sp_a_p = sp.load_npz(self.g_path + 'norm_sp_a_p.npz')
sp_p_a = sp.load_npz(self.g_path + 'norm_sp_p_a.npz')
sp_p_f = sp.load_npz(self.g_path + 'norm_sp_p_f.npz')
sp_f_p = sp.load_npz(self.g_path + 'norm_sp_f_p.npz')
sp_p_cp = sp.load_npz(self.g_path + 'norm_sp_p_cp.npz')
sp_cp_p = sp.load_npz(self.g_path + 'norm_sp_cp_p.npz')
adj_dict = {'p': {}, 'a': {}, 'i': {}, 'f': {}}
adj_dict['a']['i'] = sp_coo_2_sp_tensor(sp_a_i.tocoo())
adj_dict['a']['p'] = sp_coo_2_sp_tensor(sp_a_p.tocoo())
adj_dict['i']['a'] = sp_coo_2_sp_tensor(sp_i_a.tocoo())
adj_dict['f']['p'] = sp_coo_2_sp_tensor(sp_f_p.tocoo())
adj_dict['p']['a'] = sp_coo_2_sp_tensor(sp_p_a.tocoo())
adj_dict['p']['f'] = sp_coo_2_sp_tensor(sp_p_f.tocoo())
adj_dict['p']['citing_p'] = sp_coo_2_sp_tensor(sp_p_cp.tocoo())
adj_dict['p']['cited_p'] = sp_coo_2_sp_tensor(sp_cp_p.tocoo())
self.label = label
self.ft_dict = ft_dict
self.adj_dict = adj_dict
graph_data = {}
# Add entries for 'a' -> 'i' relationship
source_type = 'a'
edge_type = 'i'
target_type = 'i'
coo_matrix = sp_a_i.tocoo()
source_nodes = torch.from_numpy(coo_matrix.row)
target_nodes = torch.from_numpy(coo_matrix.col)
graph_data[(source_type, edge_type, target_type)] = (source_nodes, target_nodes)
# Add entries for 'a' -> 'p' relationship
source_type = 'a'
edge_type = 'p'
target_type = 'p'
coo_matrix = sp_a_p.tocoo()
source_nodes = torch.from_numpy(coo_matrix.row)
target_nodes = torch.from_numpy(coo_matrix.col)
graph_data[(source_type, edge_type, target_type)] = (source_nodes, target_nodes)
# Add entries for 'i' -> 'a' relationship
source_type = 'i'
edge_type = 'a'
target_type = 'a'
coo_matrix = sp_i_a.tocoo()
source_nodes = torch.from_numpy(coo_matrix.row)
target_nodes = torch.from_numpy(coo_matrix.col)
graph_data[(source_type, edge_type, target_type)] = (source_nodes, target_nodes)
# Add entries for 'f' -> 'p' relationship
source_type = 'f'
edge_type = 'p'
target_type = 'p'
coo_matrix = sp_f_p.tocoo()
source_nodes = torch.from_numpy(coo_matrix.row)
target_nodes = torch.from_numpy(coo_matrix.col)
graph_data[(source_type, edge_type, target_type)] = (source_nodes, target_nodes)
# Add entries for 'p' -> 'a' relationship
source_type = 'p'
edge_type = 'a'
target_type = 'a'
coo_matrix = sp_p_a.tocoo()
source_nodes = torch.from_numpy(coo_matrix.row)
target_nodes = torch.from_numpy(coo_matrix.col)
graph_data[(source_type, edge_type, target_type)] = (source_nodes, target_nodes)
# Add entries for 'p' -> 'f' relationship
source_type = 'p'
edge_type = 'f'
target_type = 'f'
coo_matrix = sp_p_f.tocoo()
source_nodes = torch.from_numpy(coo_matrix.row)
target_nodes = torch.from_numpy(coo_matrix.col)
graph_data[(source_type, edge_type, target_type)] = (source_nodes, target_nodes)
# Add entries for 'p' -> 'citing_p' relationship
source_type = 'p'
edge_type = 'citing_p'
target_type = 'p'
coo_matrix = sp_p_cp.tocoo()
source_nodes = torch.from_numpy(coo_matrix.row)
target_nodes = torch.from_numpy(coo_matrix.col)
graph_data[(source_type, edge_type, target_type)] = (source_nodes, target_nodes)
# Add entries for 'p' -> 'cited_p' relationship
source_type = 'p'
edge_type = 'cited_p'
target_type = 'p'
coo_matrix = sp_cp_p.tocoo()
source_nodes = torch.from_numpy(coo_matrix.row)
target_nodes = torch.from_numpy(coo_matrix.col)
graph_data[(source_type, edge_type, target_type)] = (source_nodes, target_nodes)
# 转换成 DGL 的异构图数据格式
self.g = dgl.heterograph(graph_data)
# 将特征数据添加到异构图的节点上
for ntype in self.ft_dict:
self.g.nodes[ntype].data['features'] = self.ft_dict[ntype]
def download(self):
# download raw data to local disk
# path to store the file
if os.path.exists(self.data_path): # pragma: no cover
pass
else:
file_path = os.path.join(self.raw_dir)
# download file
download(self.url, path=file_path)
extract_archive(self.data_path, os.path.join(self.raw_dir, self.name))
def train_val_test_split(label_shape, train_percent):
rand_idx = np.random.permutation(label_shape)
val_percent = (1.0 - train_percent) / 2
idx_train = torch.LongTensor(rand_idx[int(label_shape * 0.0): int(label_shape * train_percent)])
idx_val = torch.LongTensor(
rand_idx[int(label_shape * train_percent): int(label_shape * (train_percent + val_percent))])
idx_test = torch.LongTensor(rand_idx[int(label_shape * (train_percent + val_percent)): int(label_shape * 1.0)])
return idx_train, idx_val, idx_test
def sp_coo_2_sp_tensor(sp_coo_mat):
indices = torch.from_numpy(np.vstack((sp_coo_mat.row, sp_coo_mat.col)).astype(np.int64))
values = torch.from_numpy(sp_coo_mat.data)
shape = torch.Size(sp_coo_mat.shape)
return torch.sparse.FloatTensor(indices, values, shape)