forked from oxpig/AEV-PLIG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
132 lines (105 loc) · 4.61 KB
/
utils.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
import os
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import numpy as np
from torch_geometric.data import InMemoryDataset, Data
import torch
from sklearn.preprocessing import StandardScaler
def init_weights(layer):
if hasattr(layer, "weight") and "BatchNorm" not in str(layer):
torch.nn.init.xavier_normal_(layer.weight)
if hasattr(layer, "bias"):
if layer.bias is True:
torch.nn.init.zeros_(layer.bias)
class GraphDataset(InMemoryDataset):
def __init__(self, root='data', dataset=None,
ids=None, y=None, graphs_dict=None, y_scaler=None):
super(GraphDataset, self).__init__(root)
self.dataset = dataset
#torch.serialization.add_safe_globals([GraphDataset])
torch.serialization.add_safe_globals([Data])
if os.path.isfile(self.processed_paths[0]):
#self.data, self.slices = torch.load(self.processed_paths[0])
self.load(self.processed_paths[0])
print("processed paths:")
print(self.processed_paths[0])
else:
self.process(ids, y, graphs_dict)
#self.data, self.slices = torch.load(self.processed_paths[0])
self.load(self.processed_paths[0])
if y_scaler is None:
y_scaler = StandardScaler()
y_scaler.fit(np.reshape(self._data.y, (self.__len__(),1)))
self.y_scaler = y_scaler
self._data.y = [torch.tensor(element[0]).float() for element in self.y_scaler.transform(np.reshape(self._data.y, (self.__len__(),1)))]
@property
def raw_file_names(self):
pass
@property
def processed_file_names(self):
return [self.dataset + '.pt']
def download(self):
pass
def _download(self):
pass
def _process(self):
if not os.path.exists(self.processed_dir):
os.makedirs(self.processed_dir)
def process(self, ids, y, graphs_dict):
assert (len(ids) == len(y)), 'Number of datapoints and labels must be the same'
data_list = []
data_len = len(ids)
for i in range(data_len):
pdbcode = ids[i]
label = y[i]
c_size, features, edge_index, edge_features = graphs_dict[pdbcode]
data_point = Data(x=torch.Tensor(np.array(features)),
edge_index=torch.LongTensor(np.array(edge_index)).T,
edge_attr=torch.Tensor(np.array(edge_features)),
y=torch.FloatTensor(np.array([label])))
data_list.append(data_point)
print('Graph construction done. Saving to file.')
#self.data, self.slices = self.collate(data_list)
self.save(data_list, self.processed_paths[0])
#torch.save((self.data, self.slices), self.processed_paths[0])
class GraphDatasetPredict(InMemoryDataset):
def __init__(self, root='data', dataset=None,
ids=None, graph_ids=None, graphs_dict=None):
super(GraphDatasetPredict, self).__init__(root)
self.dataset = dataset
torch.serialization.add_safe_globals([Data])
if os.path.isfile(self.processed_paths[0]):
self.load(self.processed_paths[0])
print("processed paths:")
print(self.processed_paths[0])
else:
self.process(ids, graph_ids, graphs_dict)
self.load(self.processed_paths[0])
@property
def raw_file_names(self):
pass
@property
def processed_file_names(self):
return [self.dataset + '.pt']
def download(self):
pass
def _download(self):
pass
def _process(self):
if not os.path.exists(self.processed_dir):
os.makedirs(self.processed_dir)
def process(self, ids, graph_ids, graphs_dict):
assert (len(ids) == len(graph_ids)), 'Number of datapoints and labels must be the same'
data_list = []
data_len = len(ids)
for i in range(data_len):
pdbcode = ids[i]
graph_id = graph_ids[i]
c_size, features, edge_index, edge_features = graphs_dict[pdbcode]
data_point = Data(x=torch.Tensor(np.array(features)),
edge_index=torch.LongTensor(np.array(edge_index)).T,
edge_attr=torch.Tensor(np.array(edge_features)),
y=torch.IntTensor(np.array([graph_id])))
data_list.append(data_point)
print('Graph construction done. Saving to file.')
self.save(data_list, self.processed_paths[0])