-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.py
222 lines (179 loc) · 7.55 KB
/
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import torch
import torch_geometric.transforms as T
from torch_geometric.datasets import Amazon, Coauthor, HeterophilousGraphDataset, WikiCS
from ogb.nodeproppred import NodePropPredDataset
import numpy as np
import scipy.sparse as sp
from os import path
#from google_drive_downloader import GoogleDriveDownloader as gdd
import gdown
import scipy
from data_utils import dataset_drive_url
class NCDataset(object):
def __init__(self, name):
"""
based off of ogb NodePropPredDataset
https://github.com/snap-stanford/ogb/blob/master/ogb/nodeproppred/dataset.py
Gives torch tensors instead of numpy arrays
- name (str): name of the dataset
- root (str): root directory to store the dataset folder
- meta_dict: dictionary that stores all the meta-information about data. Default is None,
but when something is passed, it uses its information. Useful for debugging for external contributers.
Usage after construction:
split_idx = dataset.get_idx_split()
train_idx, valid_idx, test_idx = split_idx["train"], split_idx["valid"], split_idx["test"]
graph, label = dataset[0]
Where the graph is a dictionary of the following form:
dataset.graph = {'edge_index': edge_index,
'edge_feat': None,
'node_feat': node_feat,
'num_nodes': num_nodes}
For additional documentation, see OGB Library-Agnostic Loader https://ogb.stanford.edu/docs/nodeprop/
"""
self.name = name # original name, e.g., ogbn-proteins
self.graph = {}
self.label = None
def get_idx_split(self, split_type='random', train_prop=.6, valid_prop=.2, label_num_per_class=20):
"""
train_prop: The proportion of dataset for train split. Between 0 and 1.
valid_prop: The proportion of dataset for validation split. Between 0 and 1.
"""
split_idx = None
return split_idx
def __getitem__(self, idx):
assert idx == 0, 'This dataset has only one graph'
return self.graph, self.label
def __len__(self):
return 1
def __repr__(self):
return '{}({})'.format(self.__class__.__name__, len(self))
def load_dataset(data_dir, dataname, sub_dataname=''):
""" Loader for NCDataset
Returns NCDataset
"""
print(dataname)
if dataname in ('amazon-photo', 'amazon-computer'):
dataset = load_amazon_dataset(data_dir, dataname)
elif dataname in ('coauthor-cs', 'coauthor-physics'):
dataset = load_coauthor_dataset(data_dir, dataname)
elif dataname in ('roman-empire', 'amazon-ratings', 'minesweeper', 'tolokers', 'questions'):
dataset = load_hetero_dataset(data_dir, dataname)
elif dataname == 'wikics':
dataset = load_wikics_dataset(data_dir)
elif dataname in ('ogbn-arxiv', 'ogbn-products'):
dataset = load_ogb_dataset(data_dir, dataname)
elif dataname == 'pokec':
dataset = load_pokec_mat(data_dir)
else:
raise ValueError('Invalid dataname')
return dataset
def load_wikics_dataset(data_dir):
wikics_dataset = WikiCS(root=f'{data_dir}/wikics/')
data = wikics_dataset[0]
edge_index = data.edge_index
node_feat = data.x
label = data.y
num_nodes = data.num_nodes
dataset = NCDataset('wikics')
dataset.graph = {'edge_index': edge_index,
'node_feat': node_feat,
'edge_feat': None,
'num_nodes': num_nodes}
dataset.label = label
return dataset
def load_hetero_dataset(data_dir, name):
#transform = T.NormalizeFeatures()
torch_dataset = HeterophilousGraphDataset(name=name.capitalize(), root=data_dir)
data = torch_dataset[0]
edge_index = data.edge_index
node_feat = data.x
label = data.y
num_nodes = data.num_nodes
dataset = NCDataset(name)
## dataset splits are implemented in data_utils.py
'''
dataset.train_idx = torch.where(data.train_mask[:,0])[0]
dataset.valid_idx = torch.where(data.val_mask[:,0])[0]
dataset.test_idx = torch.where(data.test_mask[:,0])[0]
'''
dataset.graph = {'edge_index': edge_index,
'node_feat': node_feat,
'edge_feat': None,
'num_nodes': num_nodes}
dataset.label = label
return dataset
def load_amazon_dataset(data_dir, name):
transform = T.NormalizeFeatures()
if name == 'amazon-photo':
torch_dataset = Amazon(root=f'{data_dir}Amazon',
name='Photo', transform=transform)
elif name == 'amazon-computer':
torch_dataset = Amazon(root=f'{data_dir}Amazon',
name='Computers', transform=transform)
data = torch_dataset[0]
edge_index = data.edge_index
node_feat = data.x
label = data.y
num_nodes = data.num_nodes
dataset = NCDataset(name)
dataset.graph = {'edge_index': edge_index,
'node_feat': node_feat,
'edge_feat': None,
'num_nodes': num_nodes}
dataset.label = label
return dataset
def load_coauthor_dataset(data_dir, name):
transform = T.NormalizeFeatures()
if name == 'coauthor-cs':
torch_dataset = Coauthor(root=f'{data_dir}Coauthor',
name='CS', transform=transform)
elif name == 'coauthor-physics':
torch_dataset = Coauthor(root=f'{data_dir}Coauthor',
name='Physics', transform=transform)
data = torch_dataset[0]
edge_index = data.edge_index
node_feat = data.x
label = data.y
num_nodes = data.num_nodes
dataset = NCDataset(name)
dataset.graph = {'edge_index': edge_index,
'node_feat': node_feat,
'edge_feat': None,
'num_nodes': num_nodes}
dataset.label = label
return dataset
def load_ogb_dataset(data_dir, name):
dataset = NCDataset(name)
ogb_dataset = NodePropPredDataset(name=name, root=f'{data_dir}/ogb')
dataset.graph = ogb_dataset.graph
dataset.graph['edge_index'] = torch.as_tensor(dataset.graph['edge_index'])
dataset.graph['node_feat'] = torch.as_tensor(dataset.graph['node_feat'])
def ogb_idx_to_tensor():
split_idx = ogb_dataset.get_idx_split()
tensor_split_idx = {key: torch.as_tensor(
split_idx[key]) for key in split_idx}
return tensor_split_idx
dataset.load_fixed_splits = ogb_idx_to_tensor # ogb_dataset.get_idx_split
dataset.label = torch.as_tensor(ogb_dataset.labels).reshape(-1, 1)
return dataset
def load_pokec_mat(data_dir):
""" requires pokec.mat """
if not path.exists(f'{data_dir}/pokec/pokec.mat'):
drive_id = '1575QYJwJlj7AWuOKMlwVmMz8FcslUncu'
gdown.download(id=drive_id, output="data/pokec/")
#import sys; sys.exit()
#gdd.download_file_from_google_drive(
# file_id= drive_id, \
# dest_path=f'{data_dir}/pokec/pokec.mat', showsize=True)
fulldata = scipy.io.loadmat(f'{data_dir}/pokec/pokec.mat')
dataset = NCDataset('pokec')
edge_index = torch.tensor(fulldata['edge_index'], dtype=torch.long)
node_feat = torch.tensor(fulldata['node_feat']).float()
num_nodes = int(fulldata['num_nodes'])
dataset.graph = {'edge_index': edge_index,
'edge_feat': None,
'node_feat': node_feat,
'num_nodes': num_nodes}
label = fulldata['label'].flatten()
dataset.label = torch.tensor(label, dtype=torch.long)
return dataset