-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataloader.py
86 lines (72 loc) · 3.17 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
import torch
from torch_geometric.data import InMemoryDataset
from torch_geometric.io import read_tu_data
import os.path as osp
class GraphDataset(InMemoryDataset):
def __init__(self, root, name, transform=None, pre_transform=None,
pre_filter=None, use_node_attr=False, use_edge_attr=False):
self.name = name
super(GraphDataset, self).__init__(root, transform, pre_transform,
pre_filter)
self.data, self.slices = torch.load(self.processed_paths[0])
if self.data.x is not None and not use_node_attr:
num_node_attributes = self.num_node_attributes
self.data.x = self.data.x[:, num_node_attributes:]
if self.data.edge_attr is not None and not use_edge_attr:
num_edge_attributes = self.num_edge_attributes
self.data.edge_attr = self.data.edge_attr[:, num_edge_attributes:]
@property
def raw_dir(self):
name = 'raw{}'.format('')
return osp.join(self.root, self.name, name)
@property
def processed_dir(self):
name = 'processed{}'.format('')
return osp.join(self.root, self.name, name)
@property
def num_node_labels(self):
if self.data.x is None:
return 0
for i in range(self.data.x.size(1)):
x = self.data.x[:, i:]
if ((x == 0) | (x == 1)).all() and (x.sum(dim=1) == 1).all():
return self.data.x.size(1) - i
return 0
@property
def num_node_attributes(self):
if self.data.x is None:
return 0
return self.data.x.size(1) - self.num_node_labels
@property
def num_edge_labels(self):
if self.data.edge_attr is None:
return 0
for i in range(self.data.edge_attr.size(1)):
if self.data.edge_attr[:, i:].sum() == self.data.edge_attr.size(0):
return self.data.edge_attr.size(1) - i
return 0
@property
def num_edge_attributes(self):
if self.data.edge_attr is None:
return 0
return self.data.edge_attr.size(1) - self.num_edge_labels
@property
def raw_file_names(self):
names = ['A', 'graph_indicator','graph_labels','node_attributes','node_labels']
return ['{}_{}.txt'.format(self.name, name) for name in names]
@property
def processed_file_names(self):
return 'data.pt'
def process(self):
self.data, self.slices = read_tu_data(self.raw_dir, self.name)
if self.pre_filter is not None:
data_list = [self.get(idx) for idx in range(len(self))]
data_list = [data for data in data_list if self.pre_filter(data)]
self.data, self.slices = self.collate(data_list)
if self.pre_transform is not None:
data_list = [self.get(idx) for idx in range(len(self))]
data_list = [self.pre_transform(data) for data in data_list]
self.data, self.slices = self.collate(data_list)
torch.save((self.data, self.slices), self.processed_paths[0])
def __repr__(self):
return '{}({})'.format(self.name, len(self))