-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataloader.py
413 lines (338 loc) · 13.9 KB
/
dataloader.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import os
import os.path as osp
import torch
from torch_geometric.data import Dataset
from torch_geometric.data import Data
from torch.utils.data import Dataset as TorchDataset
from torch_geometric.loader import DataLoader
from torch_geometric.data import Data, Batch, Dataset
from view_functions import *
import pandas as pd
class GraphTextDataset(Dataset):
def __init__(self, root, gt, split, tokenizer=None, transform=None, pre_transform=None):
self.root = root
self.gt = gt
self.split = split
self.tokenizer = tokenizer
self.description = pd.read_csv(os.path.join(self.root, split+'.tsv'), sep='\t', header=None)
self.description = self.description.set_index(0).to_dict()
self.cids = list(self.description[1].keys())
self.idx_to_cid = {}
i = 0
for cid in self.cids:
self.idx_to_cid[i] = cid
i += 1
super(GraphTextDataset, self).__init__(root, transform, pre_transform)
@property
def raw_file_names(self):
return [str(cid) + ".graph" for cid in self.cids]
@property
def processed_file_names(self):
return ['data_{}.pt'.format(cid) for cid in self.cids]
@property
def raw_dir(self) -> str:
return osp.join(self.root, 'raw')
@property
def processed_dir(self) -> str:
return osp.join(self.root, 'processed/', self.split)
def download(self):
pass
def process_graph(self, raw_path):
edge_index = []
x = []
with open(raw_path, 'r') as f:
next(f)
for line in f:
if line != "\n":
edge = *map(int, line.split()),
edge_index.append(edge)
else:
break
next(f)
for line in f: #get mol2vec features:
substruct_id = line.strip().split()[-1]
if substruct_id in self.gt.keys():
x.append(self.gt[substruct_id])
else:
x.append(self.gt['UNK'])
return torch.LongTensor(edge_index).T, torch.FloatTensor(x)
def process(self):
i = 0
for raw_path in self.raw_paths:
cid = int(raw_path.split('/')[-1][:-6])
text_input = self.tokenizer([self.description[1][cid]],
return_tensors="pt",
truncation=True,
max_length=256,
padding="max_length",
add_special_tokens=True,)
edge_index, x = self.process_graph(raw_path)
data = Data(x=x, edge_index=edge_index, input_ids=text_input['input_ids'], attention_mask=text_input['attention_mask'])
torch.save(data, osp.join(self.processed_dir, 'data_{}.pt'.format(cid)))
i += 1
def len(self):
return len(self.processed_file_names)
def get(self, idx):
data = torch.load(osp.join(self.processed_dir, 'data_{}.pt'.format(self.idx_to_cid[idx])))
return data
def get_cid(self, cid):
data = torch.load(osp.join(self.processed_dir, 'data_{}.pt'.format(cid)))
return data
class GraphDataset(Dataset):
def __init__(self, root, gt, split, transform=None, pre_transform=None):
self.root = root
self.gt = gt
self.split = split
self.description = pd.read_csv(os.path.join(self.root, split+'.txt'), sep='\t', header=None)
self.cids = self.description[0].tolist()
self.idx_to_cid = {}
i = 0
for cid in self.cids:
self.idx_to_cid[i] = cid
i += 1
super(GraphDataset, self).__init__(root, transform, pre_transform)
@property
def raw_file_names(self):
return [str(cid) + ".graph" for cid in self.cids]
@property
def processed_file_names(self):
return ['data_{}.pt'.format(cid) for cid in self.cids]
@property
def raw_dir(self) -> str:
return osp.join(self.root, 'raw')
@property
def processed_dir(self) -> str:
return osp.join(self.root, 'processed/', self.split)
def download(self):
pass
def process_graph(self, raw_path):
edge_index = []
x = []
with open(raw_path, 'r') as f:
next(f)
for line in f:
if line != "\n":
edge = *map(int, line.split()),
edge_index.append(edge)
else:
break
next(f)
for line in f:
substruct_id = line.strip().split()[-1]
if substruct_id in self.gt.keys():
x.append(self.gt[substruct_id])
else:
x.append(self.gt['UNK'])
return torch.LongTensor(edge_index).T, torch.FloatTensor(x)
def process(self):
i = 0
for raw_path in self.raw_paths:
cid = int(raw_path.split('/')[-1][:-6])
edge_index, x = self.process_graph(raw_path)
data = Data(x=x, edge_index=edge_index)
torch.save(data, osp.join(self.processed_dir, 'data_{}.pt'.format(cid)))
i += 1
def len(self):
return len(self.processed_file_names)
def get(self, idx):
data = torch.load(osp.join(self.processed_dir, 'data_{}.pt'.format(self.idx_to_cid[idx])))
return data
def get_cid(self, cid):
data = torch.load(osp.join(self.processed_dir, 'data_{}.pt'.format(cid)))
return data
def get_idx_to_cid(self):
return self.idx_to_cid
class TextDataset(TorchDataset):
def __init__(self, file_path, tokenizer, max_length=256):
self.tokenizer = tokenizer
self.max_length = max_length
self.sentences = self.load_sentences(file_path)
def load_sentences(self, file_path):
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
return [line.strip() for line in lines]
def __len__(self):
return len(self.sentences)
def __getitem__(self, idx):
sentence = self.sentences[idx]
encoding = self.tokenizer.encode_plus(
sentence,
add_special_tokens=True,
max_length=self.max_length,
padding='max_length',
truncation=True,
return_tensors='pt'
)
return {
'input_ids': encoding['input_ids'].squeeze(),
'attention_mask': encoding['attention_mask'].squeeze()
}
#----------- Code for pretraining graph encoders -----------#
DATA_SPLIT = [0.7, 0.2, 0.1] # Train / val / test split ratio
class GraphDatasetPretrain(Dataset):
def __init__(self, root, gt, split, transform=None, pre_transform=None):
self.root = root
self.gt = gt
self.split = split
self.description = pd.read_csv(os.path.join(self.root, split+'.txt'), sep='\t', header=None)
self.cids = self.description[0].tolist()
self.idx_to_cid = {}
i = 0
for cid in self.cids:
self.idx_to_cid[i] = cid
i += 1
super(GraphDatasetPretrain, self).__init__(root, transform, pre_transform)
@property
def raw_file_names(self):
return [str(cid) + ".graph" for cid in self.cids]
@property
def processed_file_names(self):
return ['data_{}.pt'.format(cid) for cid in self.cids]
@property
def raw_dir(self) -> str:
return osp.join(self.root, 'raw')
@property
def processed_dir(self) -> str:
return osp.join(self.root, 'processed/', self.split)
def download(self):
pass
def update_split_file(self):
# Update the split file with the successfully processed cids
updated_split_path = os.path.join(self.root, self.split + '.txt')
self.description.to_csv(updated_split_path, sep='\t', index=False, header=None)
def process_graph(self, raw_path):
edge_index = []
x = []
with open(raw_path, 'r') as f:
next(f)
for line in f:
if line != "\n":
edge = *map(int, line.split()),
edge_index.append(edge)
else:
break
next(f)
for line in f:
substruct_id = line.strip().split()[-1]
if substruct_id in self.gt.keys():
x.append(self.gt[substruct_id])
else:
x.append(self.gt['UNK'])
return torch.LongTensor(edge_index).T, torch.FloatTensor(x)
def process(self):
i = 0
processed_cids = []
for raw_path in self.raw_paths:
cid = int(raw_path.split('/')[-1][:-6])
edge_index, x = self.process_graph(raw_path)
# Check if the graph has at least two edges
if edge_index.size(0) == 2 and edge_index.size(1) >= 2: # Ensuring the correct shape
data = Data(x=x, edge_index=edge_index)
torch.save(data, osp.join(self.processed_dir, 'data_{}.pt'.format(cid)))
processed_cids.append(cid)
i += 1
else:
print(f"Skipping graph {cid} due to insufficient edges: {edge_index.size(), edge_index} edges found.")
self.cids = processed_cids
self.description = pd.DataFrame(self.cids)
self.update_split_file()
self.idx_to_cid = {i: cid for i, cid in enumerate(self.cids)}
def len(self):
return len(self.processed_file_names)
def get(self, idx):
data = torch.load(osp.join(self.processed_dir, 'data_{}.pt'.format(self.idx_to_cid[idx])))
return data
def get_cid(self, cid):
data = torch.load(osp.join(self.processed_dir, 'data_{}.pt'.format(cid)))
return data
def get_idx_to_cid(self):
return self.idx_to_cid
def split_dataset(dataset, train_data_percent=1.0):
"""
Splits the data into train / val / test sets.
Args:
dataset (list): all graphs in the dataset.
train_data_percent (float): Fraction of training data which is labelled. (default 1.0)
"""
# random.shuffle(dataset)
n = len(dataset)
train_split, val_split, test_split = DATA_SPLIT
train_end = int(n * DATA_SPLIT[0])
val_end = train_end + int(n * DATA_SPLIT[1])
train_label_percent = int(train_end * train_data_percent)
train_dataset, val_dataset, test_dataset = [i for i in dataset[:train_label_percent]], [i for i in dataset[train_end:val_end]], [i for i in dataset[val_end:]]
return train_dataset, val_dataset, test_dataset
def build_loader(args, dataset, subset):
shuffle = (subset != "test")
loader = DataLoader(MyDataset(dataset, subset, args.augment_list),
num_workers=args.num_workers, batch_size=args.batch_size,
shuffle=shuffle, follow_batch=["x_anchor", "x_pos"])
return loader
def build_classification_loader(args, dataset, subset):
shuffle = (subset != "test")
loader = DataLoader(dataset, num_workers=args.num_workers, batch_size=args.batch_size, shuffle=shuffle)
return loader
class MyDataset(Dataset):
"""
Dataset class that returns a graph and its augmented view in get() call.
Augmentations are applied sequentially based on the augment_list.
"""
def __init__(self, dataset, subset, augment_list):
super(MyDataset, self).__init__()
self.dataset = dataset
self.augment_list = augment_list
self.augment_functions = []
for augment in self.augment_list:
if augment == "edge_perturbation":
function = EdgePerturbation()
elif augment == "diffusion":
function = Diffusion()
elif augment == "diffusion_with_sample":
function = DiffusionWithSample()
elif augment == "node_dropping":
function = UniformSample()
elif augment == "random_walk_subgraph":
function = RWSample()
elif augment == "node_attr_mask":
function = NodeAttrMask()
self.augment_functions.append(function)
print("# samples in {} subset: {}".format(subset, len(self.dataset)))
def get_positive_sample(self, current_graph):
"""
Possible augmentations include the following:
edge_perturbation()
diffusion()
diffusion_with_sample()
node_dropping()
random_walk_subgraph()
node_attr_mask()
"""
graph_temp = current_graph
for function in self.augment_functions:
graph_temp = function.views_fn(graph_temp)
return graph_temp
def get(self, idx):
graph_anchor = self.dataset[idx]
assert graph_anchor.edge_index.size(0) == 2 or graph_anchor.edge_index.numel() == 0, f"Edge index shape mismatch: {graph_anchor.edge_index.size()}"
graph_pos = self.get_positive_sample(graph_anchor)
return PairData(graph_anchor.edge_index, graph_anchor.x, graph_pos.edge_index, graph_pos.x)
def len(self):
return len(self.dataset)
class PairData(Data):
"""
Utility function to return a pair of graphs in dataloader.
Adapted from https://pytorch-geometric.readthedocs.io/en/latest/notes/batching.html
"""
def __init__(self, edge_index_anchor = None, x_anchor = None, edge_index_pos = None, x_pos = None):
super().__init__()
self.edge_index_anchor = edge_index_anchor
self.x_anchor = x_anchor
self.edge_index_pos = edge_index_pos
self.x_pos = x_pos
def __inc__(self, key, value, *args, **kwargs):
if key == "edge_index_anchor":
return self.x_anchor.size(0)
if key == "edge_index_pos":
return self.x_pos.size(0)
else:
return super().__inc__(key, value, *args, **kwargs)