forked from zouxiaochuan/code_ogblsc2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasets.py
195 lines (151 loc) · 7.58 KB
/
datasets.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
import torch
import common_utils
import os
import numpy as np
import pickle
from scipy.spatial.transform import Rotation as R
class SimplePCQM4MDataset(torch.utils.data.Dataset):
def __init__(self, path: str, split_name='train', rotate=False, subset=None, extra_data=None, data_path_name='data',
use_dist=False, use_predict_dist=False, load_dist=False):
self.path = path
self.idx_split = common_utils.load_obj(os.path.join(path, 'idx_split.pkl'))
train_split = self.idx_split['train']
self.idx_split['valid-train'] = train_split[: len(train_split) * 9 // 10]
self.idx_split['valid-test'] = train_split[len(train_split) * 9 // 10:]
self.idx_split['all'] = np.arange(3746620)
self.idx_split['train_valid'] = np.concatenate(
[self.idx_split['train'], self.idx_split['valid']])
for i in range(50):
self.idx_split[f'train_valid_fold{i}'] = self.idx_split['train_valid'][
np.arange(len(self.idx_split['train_valid'])) % 50 != i]
self.idx_split[f'train_valid_fold{i}_test'] = self.idx_split['train_valid'][
np.arange(len(self.idx_split['train_valid'])) % 50 == i]
pass
self.idx_split['train_ex1'] = self.idx_split['train_valid'][:-len(self.idx_split['train_valid']) // 50]
self.idx_split['valid_ex1'] = self.idx_split['train_valid'][-len(self.idx_split['train_valid']) // 50:]
self.data_path = os.path.join(path, data_path_name)
if load_dist:
self.dist_centroids = common_utils.load_obj(os.path.join(path, 'dist_centroids.pkl'))
pass
self.load_dist = load_dist
self.use_dist = use_dist
self.use_predict_dist = use_predict_dist
self.split = self.idx_split[split_name]
self.rotate = rotate
# if split_name == 'train':
# self.split = self.split[:5012]
if subset is not None:
subset = subset[self.split]
self.split = self.split[subset]
pass
self.extra_data = extra_data
pass
def get_data(self, data_idx):
filename = os.path.join(
self.data_path, format(data_idx // 1000, '04d'), format(data_idx, '07d') + '.pkl')
with open(filename, 'rb') as fin:
g, y = pickle.load(fin)
pass
return g, y
# return global_data.DATA[data_idx]
pass
def __getitem__(self, idx):
'''Get datapoint with index'''
if isinstance(idx, (int, np.integer)):
data_idx = self.split[idx]
g, y = self.get_data(data_idx)
if self.rotate:
mat3d = R.random().as_matrix().astype('float32')
g['xyz'] = np.matmul(g['xyz'], mat3d)
pass
num_atom = g['xyz'].shape[0]
g['structure_feat_cate'] = np.stack(
[
g['shortest_path_length'],
g['atom_same_ring_count'],
g['atom_same_ring_min_size'],
# g['angles'],
], axis=2
).astype('int64')
# ijk = ik + jk
# triplet_shortest_path_length = g['shortest_path_length'][:, None, :] + g['shortest_path_length'][None, :, :]
# triplet_shortest_path_min = np.minimum(g['shortest_path_length'][:, None, :], g['shortest_path_length'][None, :, :])
# triplet_shortest_path_max = np.maximum(g['shortest_path_length'][:, None, :], g['shortest_path_length'][None, :, :])
# ijk = ik
triplet_shortest_path_length_src = np.tile(g['shortest_path_length'][:, None, :], (1, num_atom, 1))
# ijk = jk
triplet_shortest_path_length_dst = np.tile(g['shortest_path_length'][None, :, :], (num_atom, 1, 1))
shortest_path = g['shortest_path']
path_idx = np.array([(i, j, k) for i in range(num_atom) for j in range(num_atom) for k in shortest_path[i][j]])
triplet_in_shortest_path = np.zeros((num_atom, num_atom, num_atom), dtype='int64')
if len(path_idx) > 0:
triplet_in_shortest_path[path_idx[:, 0], path_idx[:, 1], path_idx[:, 2]] = 1
pass
g['triplet_feat_cate'] = np.stack(
[triplet_shortest_path_length_src, triplet_shortest_path_length_dst, triplet_in_shortest_path],
axis=3
).astype('int64')
# print(g['xyz'])
g['structure_feat_float'] = np.zeros((num_atom, num_atom, 1), dtype='float32')
if self.load_dist:
shift = g['xyz'][None, :, :] - g['xyz'][:, None, :]
dist = np.linalg.norm(shift, axis=-1)
dist_cls = (dist[:, :, None] <= self.dist_centroids[None, None, :]).astype('float32')
g['dist_class'] = dist_cls
pass
if self.use_dist:
g['structure_feat_float'] = dist_cls
elif self.use_predict_dist:
g['structure_feat_float'] = g['predict_pair_dist_cls']
pass
pass
if self.extra_data is not None:
g['extra_data'] = self.extra_data[data_idx].astype('float32')
pass
return g, y
raise IndexError(
'Only integer is valid index (got {}).'.format(type(idx).__name__))
def __len__(self):
'''Length of the dataset
Returns
-------
int
Length of Dataset
'''
return len(self.split)
pass
def collate_fn(graph_list):
y = [g[1] for g in graph_list]
graph_list = [g[0] for g in graph_list]
atom_feat_cate, atom_mask = common_utils.collate_seq([g['atom_feat_cate'] for g in graph_list])
bond_feat_cate, bond_mask = common_utils.collate_seq([g['bond_feat_cate'] for g in graph_list])
xyz = common_utils.collate_seq([g['xyz'] for g in graph_list])[0]
atom_feat_float, _ = common_utils.collate_seq([g['atom_feat_float'] for g in graph_list])
bond_feat_float, _ = common_utils.collate_seq([g['bond_feat_float'] for g in graph_list])
bond_index, _ = common_utils.collate_seq([g['bond_index'].T for g in graph_list])
bond_index = bond_index.transpose(0, 2, 1).astype('int64')
structure_feat_cate = common_utils.collate_map([g['structure_feat_cate'] for g in graph_list])
structure_feat_float = common_utils.collate_map([g['structure_feat_float'] for g in graph_list])
triplet_feat_cate = common_utils.collate_cube([g['triplet_feat_cate'] for g in graph_list])
result_dict = {
'atom_feat_cate': torch.from_numpy(atom_feat_cate),
'bond_feat_cate': torch.from_numpy(bond_feat_cate),
'atom_feat_float': torch.from_numpy(atom_feat_float),
'bond_feat_float': torch.from_numpy(bond_feat_float),
'bond_index': torch.from_numpy(bond_index),
'atom_mask': torch.from_numpy(atom_mask),
'bond_mask': torch.from_numpy(bond_mask),
'structure_feat_cate': torch.from_numpy(structure_feat_cate),
'structure_feat_float': torch.from_numpy(structure_feat_float),
'triplet_feat_cate': torch.from_numpy(triplet_feat_cate),
'xyz': torch.from_numpy(xyz)
}
if 'extra_data' in graph_list[0]:
result_dict['extra_data'] = torch.from_numpy(
np.stack([g['extra_data'] for g in graph_list]))
pass
if 'dist_class' in graph_list[0]:
result_dict['dist_class'] = torch.from_numpy(
common_utils.collate_map([g['dist_class'] for g in graph_list]))
pass
return result_dict, torch.tensor(y, dtype=torch.float32)