-
Notifications
You must be signed in to change notification settings - Fork 8
/
batch.py
195 lines (148 loc) · 6.45 KB
/
batch.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
from __future__ import print_function, division
import numpy as np
import time
import random
from collections import Counter
import torch
from torch.utils.data import Dataset
def collate_prot_data(args, data):
prot_fea_list = data
return prot_fea_list
def collate_drug_data(args, data):
drug_node_list, drug_edge_list, drug_n2n_list, drug_e2n_list = data
n_tot_node = np.sum([len(x) for x in drug_node_list])
n_tot_edge = np.sum([len(x) for x in drug_edge_list])
for x in drug_edge_list:
if len(x) == 0:
n_tot_edge += 1
dim_node = drug_node_list[0].shape[1]
dim_edge = drug_edge_list[0].shape[1]
n_batch = len(drug_node_list)
idx_base_node = 0
new_drug_node_list = np.zeros((n_tot_node, dim_node), dtype=np.float32)
idx_drug_node_list = np.zeros(n_tot_node, dtype=np.int)
for i, drug_node in enumerate(drug_node_list):
n_drug_node = len(drug_node)
new_drug_node_list[idx_base_node:idx_base_node+n_drug_node] += drug_node
idx_drug_node_list[idx_base_node:idx_base_node+n_drug_node] += i
idx_base_node += n_drug_node
idx_base_edge = 0
new_drug_edge_list = np.zeros((n_tot_edge, dim_edge), dtype=np.float32)
idx_drug_edge_list = np.zeros(n_tot_edge, dtype=np.int)
for i, drug_edge in enumerate(drug_edge_list):
n_drug_edge = len(drug_edge)
if n_drug_edge == 0:
drug_edge = np.zeros(dim_edge, dtype=np.float32)
n_drug_edge = 1
new_drug_edge_list[idx_base_edge:idx_base_edge+n_drug_edge] += drug_edge
idx_drug_edge_list[idx_base_edge:idx_base_edge+n_drug_edge] += i
idx_base_edge += n_drug_edge
idx_base = 0
new_drug_n2n_list = np.zeros((n_tot_node, n_tot_node), dtype=np.float16)
for i, drug_n2n in enumerate(drug_n2n_list):
n_drug_node = len(drug_n2n)
fancy_index = np.where(drug_n2n == 1)
new_drug_n2n_list[idx_base : idx_base + n_drug_node,
idx_base : idx_base + n_drug_node] += drug_n2n
idx_base += n_drug_node
idx_base_node = 0
idx_base_edge = 0
new_drug_e2n_list = np.zeros((n_tot_node, n_tot_edge), dtype=np.float16)
for i, drug_e2n in enumerate(drug_e2n_list):
n_drug_node, n_drug_edge = np.shape(drug_e2n)
if n_drug_edge == 0:
drug_e2n = np.zeros((n_drug_node, 1), dtype=np.float16)
n_drug_edge = 1
new_drug_e2n_list[idx_base_node : idx_base_node + n_drug_node,
idx_base_edge : idx_base_edge + n_drug_edge] += drug_e2n
idx_base_node += n_drug_node
idx_base_edge += n_drug_edge
return [new_drug_node_list, new_drug_edge_list,
new_drug_n2n_list, new_drug_e2n_list,
idx_drug_node_list, idx_drug_edge_list]
def make_batch(args, data, idx):
prot_data, drug_data, label_data = data
prot_subdata = get_subdata(prot_data, idx)
drug_subdata = get_subdata(drug_data, idx)
label_subdata = get_subdata(label_data, idx)
# Collate batch data
batch_prot_data = collate_prot_data(args, data=prot_subdata)
batch_drug_data = collate_drug_data(args, data=drug_subdata)
batch_label_data = label_subdata
# Cast tensor structure
batch_prot_data = cast_tensor(batch_prot_data, dtype=['f'])
batch_drug_data = cast_tensor(batch_drug_data, dtype=['f', 'f', 'f', 'f', 'd', 'd'])
batch_label_data = cast_tensor(batch_label_data, dtype=['f'])
if args.cuda:
batch_prot_data = cast_cuda(batch_prot_data)
batch_drug_data = cast_cuda(batch_drug_data)
batch_label_data = cast_cuda(batch_label_data)
return [batch_prot_data, batch_drug_data, batch_label_data]
def cast_tensor(data, dtype):
assert isinstance(data, list)
assert len(data) == len(dtype)
cast_data = []
for i, (elem, dt) in enumerate(zip(data, dtype)):
if dt == 'f':
cast_data.append(torch.tensor(elem).float())
elif dt == 'd':
cast_data.append(torch.tensor(elem).long())
else:
"Invalid Data Type"
exit(1)
return cast_data
def cast_cuda(data):
assert isinstance(data, list)
cast_data = [elem.cuda() for elem in data]
if len(cast_data) == 1:
return cast_data[0]
return cast_data
def get_subdata(data, idx):
assert isinstance(data, list)
subdata = [elem[idx] for elem in data]
return subdata
class MoleculeDataset(Dataset):
def __init__(self, args):
# Root dir
#if args.dataset.lower() == 'human':
# dataset_filename = 'data/human/human_simple.npz'
dataset_filename = args.dataset_file
dataset = np.load(dataset_filename, allow_pickle=True)
self.prot_fea_list = dataset['prot_fea']
self.drug_node_list = dataset['drug_node']
self.drug_edge_list = dataset['drug_edge']
self.drug_n2n_list = dataset['drug_n2n']
self.drug_e2n_list = dataset['drug_e2n']
self.label_list = dataset['label']
self.split_list = dataset['split']
self.data = [self.prot_fea_list,
self.drug_node_list, self.drug_edge_list,
self.drug_n2n_list, self.drug_e2n_list,
self.label_list]
self.dim_prot_fea = len(self.prot_fea_list[0])
self.dim_drug_node = self.drug_node_list[0].shape[1]
self.dim_drug_edge = self.drug_edge_list[0].shape[1]
self.preprocess()
def load_data(self, partition, add_noise=False):
if partition == 'train':
data = get_subdata(self.data, self.train_idx_list)
print(Counter(data[-1]))
elif partition == 'val':
data = get_subdata(self.data, self.val_idx_list)
print(Counter(data[-1]))
elif partition == 'test':
data = get_subdata(self.data, self.test_idx_list)
print(Counter(data[-1]))
return data
def preprocess(self):
n_split = len(self.split_list)
self.train_idx_list = []
self.val_idx_list = []
self.test_idx_list = []
self.test_idx_list += list(self.split_list[-1])
self.val_idx_list += list(self.split_list[-2])
for i in range(n_split-2):
self.train_idx_list += list(self.split_list[i])
assert len(set(self.train_idx_list) & set(self.val_idx_list)) == 0
assert len(set(self.val_idx_list) & set(self.test_idx_list)) == 0
assert len(set(self.train_idx_list) & set(self.test_idx_list)) == 0