gds.datasets.pyg_upfd_dataset

gds/datasets/pyg_upfd_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
import os
import os.path as osp
import torch
import numpy as np
import scipy.sparse as sp
from torch_geometric.data import InMemoryDataset, download_url, extract_zip, Data
import torch.nn.functional as F
from torch_sparse import coalesce
from torch_geometric.io import read_txt_array

import pdb

class PyGUPFDDataset(InMemoryDataset):
    names = ['UPFD']
    urls = {
        'UPFD': 'https://www.dropbox.com/s/fgr5uzishps3mv3/UPFD.zip?dl=1'
    }

    def __init__(self, root, name, feature, transform=None,
                 pre_transform=None, pre_filter=None):
        self.root = root
        self.name = name
        assert self.name in self.names
        self.feature = feature
        if name == 'UPFD':
            self.num_tasks = 1
            self.__num_classes__ = 1
        else:
            raise NotImplementedError
        super(PyGUPFDDataset, self).__init__(root, transform, pre_transform, pre_filter)

        path = self.processed_paths[0]
        self.data, self.slices = torch.load(path)

    @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', self.feature)

    @property
    def raw_file_names(self):
        return [
            'node_graph_id.npy', 'graph_labels.npy', 'A.txt', 'train_idx.npy',
            'val_idx.npy', 'test_idx.npy', f'new_{self.feature}_feature.npz'
        ]

    @property
    def processed_file_names(self):
        return ['all.pt']

    def download(self):
        path = download_url(self.urls[self.name], self.raw_dir)
        extract_zip(path, self.raw_dir)
        os.unlink(path)

    def process(self):
        x = sp.load_npz(
            osp.join(self.raw_dir, f'new_{self.feature}_feature.npz'))
        x = torch.from_numpy(x.todense()).to(torch.float)

        edge_index = read_txt_array(osp.join(self.raw_dir, 'A.txt'), sep=',',
                                    dtype=torch.long).t()
        edge_index, _ = coalesce(edge_index, None, x.size(0), x.size(0))

        y = np.load(osp.join(self.raw_dir, 'graph_labels.npy'))
        y = torch.from_numpy(y).to(torch.long)
        _, y = y.unique(sorted=True, return_inverse=True)

        batch = np.load(osp.join(self.raw_dir, 'node_graph_id.npy'))
        batch = torch.from_numpy(batch).to(torch.long)

        node_slice = torch.cumsum(batch.bincount(), 0)
        node_slice = torch.cat([torch.tensor([0]), node_slice])
        edge_slice = torch.cumsum(batch[edge_index[0]].bincount(), 0)
        edge_slice = torch.cat([torch.tensor([0]), edge_slice])
        graph_slice = torch.arange(y.size(0) + 1)
        self.slices = {
            'x': node_slice,
            'edge_index': edge_slice,
            'y': graph_slice
        }

        edge_index -= node_slice[batch[edge_index[0]]].view(1, -1)

        np.random.seed(0)
        x = np.random.randint(8, size=(x.shape[0],))
        x = torch.from_numpy(x).long()

        assert x.dim() == 1
        assert x.dtype == torch.int64

        self.data = Data(x=x, edge_index=edge_index, y=y)

        idx = []
        for split in ['train', 'val', 'test']:
            idx += np.load(osp.join(self.raw_dir, f'{split}_idx.npy')).tolist()
        data_list = [self.get(i) for i in idx]
        if self.pre_filter is not None:
            data_list = [d for d in data_list if self.pre_filter(d)]
        if self.pre_transform is not None:
            data_list = [self.pre_transform(d) for d in data_list]
        torch.save(self.collate(data_list), self.processed_paths[0])

    def __repr__(self):
        return (f'{self.__class__.__name__}({len(self)}, name={self.name}, '
                f'feature={self.feature})')


if __name__ == '__main__' :
    root = '/cmlscratch/kong/datasets/graph_domain'
    PyGUPFDDataset(root, 'UPFD', 'profile')

    import pdb
    pdb.set_trace()



























########################################################################
# class Net(torch.nn.Module):
#     def __init__(self, model, in_channels, hidden_channels, out_channels,
#                  concat=False):
#         super(Net, self).__init__()
#         self.concat = concat
#
#         if model == 'GCN':
#             self.conv1 = GCNConv(in_channels, hidden_channels)
#         elif model == 'SAGE':
#             self.conv1 = SAGEConv(in_channels, hidden_channels)
#         elif model == 'GAT':
#             self.conv1 = GATConv(in_channels, hidden_channels)
#
#         if self.concat:
#             self.lin0 = Linear(in_channels, hidden_channels)
#             self.lin1 = Linear(2 * hidden_channels, hidden_channels)
#
#         self.lin2 = Linear(hidden_channels, out_channels)
#
#     def forward(self, x, edge_index, batch):
#         h = self.conv1(x, edge_index).relu()
#         h = global_max_pool(h, batch)
#
#         if self.concat:
#             # Get the root node (tweet) features of each graph:
#             root = (batch[1:] - batch[:-1]).nonzero(as_tuple=False).view(-1)
#             root = torch.cat([root.new_zeros(1), root + 1], dim=0)
#             news = x[root]
#
#             news = self.lin0(news).relu()
#             h = self.lin1(torch.cat([news, h], dim=-1)).relu()
#
#         h = self.lin2(h)
#         return h.log_softmax(dim=-1)