-
Notifications
You must be signed in to change notification settings - Fork 12
/
dataset_utils.py
222 lines (178 loc) · 7.9 KB
/
dataset_utils.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
# copied from https://github.com/ivam-he/BernNet
# load the real-world dataset from the `"BernNet: Learning Arbitrary Graph Spectral Filters via Bernstein Approximation" paper
import torch
import pickle
import os.path as osp
import os
import torch_geometric.transforms as T
from torch_geometric.data import InMemoryDataset, download_url, Data
from torch_geometric.datasets import Planetoid, Amazon, WikipediaNetwork, Actor
from torch_sparse import coalesce
from torch_geometric.utils.undirected import to_undirected
def index_to_mask(index, size):
mask = torch.zeros(size, dtype=torch.bool, device=index.device)
mask[index] = 1
return mask
# GPRGNN
def random_planetoid_splits(data,
num_classes,
percls_trn=20,
val_lb=500,
Flag=0):
indices = []
for i in range(num_classes):
index = (data.y == i).nonzero().view(-1)
index = index[torch.randperm(index.size(0), device=index.device)]
indices.append(index)
train_index = torch.cat([i[:percls_trn] for i in indices], dim=0)
if Flag == 0:
rest_index = torch.cat([i[percls_trn:] for i in indices], dim=0)
rest_index = rest_index[torch.randperm(rest_index.size(0))]
train_mask = index_to_mask(train_index, size=data.num_nodes)
val_mask = index_to_mask(rest_index[:val_lb], size=data.num_nodes)
test_mask = index_to_mask(rest_index[val_lb:], size=data.num_nodes)
else:
val_index = torch.cat(
[i[percls_trn:percls_trn + val_lb] for i in indices], dim=0)
rest_index = torch.cat([i[percls_trn + val_lb:] for i in indices],
dim=0)
rest_index = rest_index[torch.randperm(rest_index.size(0))]
train_mask = index_to_mask(train_index, size=data.num_nodes)
val_mask = index_to_mask(val_index, size=data.num_nodes)
test_mask = index_to_mask(rest_index, size=data.num_nodes)
return train_mask, val_mask, test_mask
class dataset_heterophily(InMemoryDataset):
def __init__(self,
root='data/',
name=None,
p2raw=None,
train_percent=0.01,
transform=None,
pre_transform=None):
existing_dataset = ['chameleon', 'film', 'squirrel']
if name not in existing_dataset:
raise ValueError(
f'name of hypergraph dataset must be one of: {existing_dataset}'
)
else:
self.name = name
self._train_percent = train_percent
if (p2raw is not None) and osp.isdir(p2raw):
self.p2raw = p2raw
elif p2raw is None:
self.p2raw = None
elif not osp.isdir(p2raw):
raise ValueError(
f'path to raw hypergraph dataset "{p2raw}" does not exist!')
if not osp.isdir(root):
os.makedirs(root)
self.root = root
super(dataset_heterophily, self).__init__(root, transform,
pre_transform)
self.data, self.slices = torch.load(self.processed_paths[0])
self.train_percent = self.data.train_percent
@property
def raw_dir(self):
return osp.join(self.root, self.name, 'raw')
@property
def processed_dir(self):
return osp.join(self.root, self.name, 'processed')
@property
def raw_file_names(self):
file_names = [self.name]
return file_names
@property
def processed_file_names(self):
return ['data.pt']
def download(self):
pass
def process(self):
p2f = osp.join(self.raw_dir, self.name)
with open(p2f, 'rb') as f:
data = pickle.load(f)
data = data if self.pre_transform is None else self.pre_transform(data)
torch.save(self.collate([data]), self.processed_paths[0])
def __repr__(self):
return '{}()'.format(self.name)
class WebKB(InMemoryDataset):
r"""The WebKB datasets used in the
`"Geom-GCN: Geometric Graph Convolutional Networks"
<https://openreview.net/forum?id=S1e2agrFvS>`_ paper.
Nodes represent web pages and edges represent hyperlinks between them.
Node features are the bag-of-words representation of web pages.
The task is to classify the nodes into one of the five categories, student,
project, course, staff, and faculty.
Args:
root (string): Root directory where the dataset should be saved.
name (string): The name of the dataset (:obj:`"Cornell"`,
:obj:`"Texas"` :obj:`"Washington"`, :obj:`"Wisconsin"`).
transform (callable, optional): A function/transform that takes in an
:obj:`torch_geometric.data.Data` object and returns a transformed
version. The data object will be transformed before every access.
(default: :obj:`None`)
pre_transform (callable, optional): A function/transform that takes in
an :obj:`torch_geometric.data.Data` object and returns a
transformed version. The data object will be transformed before
being saved to disk. (default: :obj:`None`)
"""
url = (
'https://raw.githubusercontent.com/graphdml-uiuc-jlu/geom-gcn/master/new_data'
)
def __init__(self, root, name, transform=None, pre_transform=None):
self.name = name.lower()
assert self.name in ['cornell', 'texas', 'washington', 'wisconsin']
super(WebKB, self).__init__(root, transform, pre_transform)
self.data, self.slices = torch.load(self.processed_paths[0])
@property
def raw_dir(self):
return osp.join(self.root, self.name, 'raw')
@property
def processed_dir(self):
return osp.join(self.root, self.name, 'processed')
@property
def raw_file_names(self):
return ['out1_node_feature_label.txt', 'out1_graph_edges.txt']
@property
def processed_file_names(self):
return 'data.pt'
def download(self):
for name in self.raw_file_names:
download_url(f'{self.url}/{self.name}/{name}', self.raw_dir)
def process(self):
with open(self.raw_paths[0], 'r') as f:
data = f.read().split('\n')[1:-1]
x = [[float(v) for v in r.split('\t')[1].split(',')] for r in data]
x = torch.tensor(x, dtype=torch.float)
y = [int(r.split('\t')[2]) for r in data]
y = torch.tensor(y, dtype=torch.long)
with open(self.raw_paths[1], 'r') as f:
data = f.read().split('\n')[1:-1]
data = [[int(v) for v in r.split('\t')] for r in data]
edge_index = torch.tensor(data, dtype=torch.long).t().contiguous()
edge_index = to_undirected(edge_index)
edge_index, _ = coalesce(edge_index, None, x.size(0), x.size(0))
data = Data(x=x, edge_index=edge_index, y=y)
data = data if self.pre_transform is None else self.pre_transform(data)
torch.save(self.collate([data]), self.processed_paths[0])
def __repr__(self):
return '{}()'.format(self.name)
def DataLoader(name):
if name in ['cora', 'citeseer', 'pubmed']:
root_path = './'
path = osp.join(root_path, 'data', name)
dataset = Planetoid(path, name, transform=T.NormalizeFeatures())
elif name in ['computers', 'photo']:
root_path = './'
path = osp.join(root_path, 'data', name)
dataset = Amazon(path, name, T.NormalizeFeatures())
elif name in ['chameleon', 'film', 'squirrel']:
dataset = dataset_heterophily(root='./data/',
name=name,
transform=T.NormalizeFeatures())
elif name in ['texas', 'cornell']:
dataset = WebKB(root='./data/',
name=name,
transform=T.NormalizeFeatures())
else:
raise ValueError(f'dataset {name} not supported in dataloader')
return dataset