-
Notifications
You must be signed in to change notification settings - Fork 1
/
input_data.py
146 lines (111 loc) · 4.87 KB
/
input_data.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
import networkx as nx
import scipy.sparse as sp
import os
import numpy as np
import torch
from torch_geometric.data import Data
from torch_geometric.utils import to_networkx, to_undirected
from Citation import load_npz_dataset, train_test_split
from torch_geometric.datasets import WebKB
def load_data(dataset_name = 'cora_ml', directed=True):
if dataset_name == 'wisconsin':
dataset = WebKB(root='./data', name='Wisconsin')
data = dataset[0]
if not directed:
data.edge_index = to_undirected(data.edge_index)
elif dataset_name == 'cornell':
dataset = WebKB(root='./data', name='Cornell')
data = dataset[0]
if not directed:
data.edge_index = to_undirected(data.edge_index)
elif dataset_name == 'texas':
dataset = WebKB(root='./data', name='Texas')
data = dataset[0]
if not directed:
data.edge_index = to_undirected(data.edge_index)
else:
data = load_data_from_npz(dataset_name, directed)
return data
def load_data_from_npz(dataset_name = 'cora_ml', directed=True):
dataset_path = os.path.join('./data/{}/raw'.format(dataset_name), '{}.npz'.format(dataset_name))
g = load_npz_dataset(dataset_path)
adj, features, labels = g['A'], g['X'], g['z']
if not directed:
adj = (adj + adj.T) / 2.0
mask = train_test_split(labels, seed=1020, train_examples_per_class=20, val_size=500, test_size=None)
mask['train'] = torch.from_numpy(mask['train']).bool()
mask['val'] = torch.from_numpy(mask['val']).bool()
mask['test'] = torch.from_numpy(mask['test']).bool()
coo = adj.tocoo()
indices = np.vstack((coo.row, coo.col))
indices = torch.from_numpy(indices).long()
values = coo.data
values = torch.from_numpy(values).float()
features = torch.from_numpy(features.todense()).float()
labels = torch.from_numpy(labels).long()
edge_index = indices
edge_weight = values
data = Data(x=features, edge_index=edge_index, edge_weight=edge_weight, y=labels)
data.train_mask = mask['train']
data.val_mask = mask['val']
data.test_mask = mask['test']
return data
def gravity_load_data(dataset, load_features=True):
if dataset == 'cora_ml':
data = load_data(dataset, directed=True)
adj = nx.to_scipy_sparse_matrix(to_networkx(data))
if load_features:
feature_array = data.x.detach().clone().numpy()
else:
feature_array = np.identity(adj.shape[0])
elif dataset == 'citeseer':
data = load_data(dataset, directed=True)
adj = nx.to_scipy_sparse_matrix(to_networkx(data))
if load_features:
feature_array = data.x.detach().clone().numpy()
else:
feature_array = np.identity(adj.shape[0])
elif dataset == 'wisconsin':
data = load_data(dataset, directed=True)
adj = nx.to_scipy_sparse_matrix(to_networkx(data))
if load_features:
feature_array = data.x.detach().clone().numpy()
else:
feature_array = np.identity(adj.shape[0])
elif dataset == 'cornell':
data = load_data(dataset, directed=True)
adj = nx.to_scipy_sparse_matrix(to_networkx(data))
if load_features:
feature_array = data.x.detach().clone().numpy()
else:
feature_array = np.identity(adj.shape[0])
elif dataset == 'texas':
data = load_data(dataset, directed=True)
adj = nx.to_scipy_sparse_matrix(to_networkx(data))
if load_features:
feature_array = data.x.detach().clone().numpy()
else:
feature_array = np.identity(adj.shape[0])
return adj, feature_array
# Adapted/copied from:
# https://github.com/deezer/gravity_graph_autoencoders
def original_gravity_load_data(dataset):
if dataset == 'cora_ml':
adj = nx.adjacency_matrix(nx.read_edgelist("./data/cora.cites",
delimiter='\t',
create_using=nx.DiGraph()))
# Transpose the adjacency matrix, as Cora raw dataset comes with a
# <ID of cited paper> <ID of citing paper> edgelist format.
adj = adj.T
features = sp.identity(adj.shape[0])
elif dataset == 'citeseer':
adj = nx.adjacency_matrix(nx.read_edgelist("./data/citeseer.cites",
delimiter='\t',
create_using=nx.DiGraph()))
# Transpose the adjacency matrix, as Citeseer raw dataset comes with a
# <ID of cited paper> <ID of citing paper> edgelist format.
adj = adj.T
features = sp.identity(adj.shape[0])
else:
raise ValueError('Undefined dataset!')
return adj, features