-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
140 lines (120 loc) · 5.94 KB
/
test.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
import torch
import torch.nn as nn
import torch.nn.init as init
from sklearn.metrics import roc_curve
from torch import optim
from torch.utils.data import DataLoader
from losses import LOSS_FUNCTIONS
from log import LOG
from Encoder.deal_data import MolGraphDataset, molgraph_collate_fn
# from Result_vis import vis_loss, vis_roc
from Model.MVA import MVA
import argparse
import numpy as np
import pandas as pd
from log import all_evaluate
common_args_parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter, add_help=False)
common_args_parser.add_argument('--train-set', type=str, default='./Data/v1.csv', help='Training dataset path')
common_args_parser.add_argument('--valid-set', type=str, default='./Data/v1.csv', help='Validation dataset path')
common_args_parser.add_argument('--test-set', type=str, default='./Data/v1.csv', help='Testing dataset path')
# common_args_parser.add_argument('--train-set', type=str, default='./Data/atc.csv', help='Training dataset path')
# common_args_parser.add_argument('--valid-set', type=str, default='./Data/atc.csv', help='Validation dataset path')
# common_args_parser.add_argument('--test-set', type=str, default='./Data/atc.csv', help='Testing dataset path')
# common_args_parser.add_argument('--train-set', type=str, default='./Data/MVADDI_train.csv', help='Training dataset path')
# common_args_parser.add_argument('--valid-set', type=str, default='./Data/MVADDI_valid.csv', help='Validation dataset path')
# common_args_parser.add_argument('--test-set', type=str, default='./Data/MVADDI_test.csv', help='Testing dataset path')
common_args_parser.add_argument('--loss', type=str, default='CrossEntropy', choices=[k for k, v in LOSS_FUNCTIONS.items()])
common_args_parser.add_argument('--score', type=str, default='All', help='roc-auc or MSE or All')
common_args_parser.add_argument('--epochs', type=int, default=200, help='Number of training epochs')
common_args_parser.add_argument('--batch-size', type=int, default=256, help='Number of graphs in a mini-batch')
common_args_parser.add_argument('--learn-rate', type=float, default=0.0001) #0.001-inf
common_args_parser.add_argument('--savemodel', action='store_true', default=False, help='Saves model with highest validation score')
common_args_parser.add_argument('--logging', type=str, default='less')
common_args_parser.add_argument('--gcn_in_size', type=int, default=75, help='gcn input size')
common_args_parser.add_argument('--gcn_out_size', type=int, default=128, help='gcn output size')
common_args_parser.add_argument('--random_factor', type=bool, default=False, help='Whether to fix the random factor')
class EarlyStopping:
def __init__(self, patience=10, delta=0.01):
self.patience = patience
self.delta = delta
self.counter = 0
self.best_loss = None
self.early_stop = False
def __call__(self, val_loss):
if self.best_loss is None:
self.best_loss = val_loss
elif val_loss > self.best_loss + self.delta:
self.counter += 1
if self.counter >= self.patience:
self.early_stop = True
else:
self.best_loss = val_loss
self.counter = 0
return self.early_stop
def main():
global args
args = common_args_parser.parse_args()
print(args)
# train_dataset = MolGraphDataset(args.train_set)
# train_dataloader = DataLoader(train_dataset, batch_size=args.batch_size,
# shuffle=True, collate_fn=molgraph_collate_fn)
#
# validation_dataset = MolGraphDataset(args.valid_set)
# validation_dataloader = DataLoader(validation_dataset, batch_size=args.batch_size,
# shuffle=False, collate_fn=molgraph_collate_fn)
test_dataset = MolGraphDataset(args.test_set)
test_dataloader = DataLoader(test_dataset, batch_size=args.batch_size,
shuffle=True, collate_fn=molgraph_collate_fn)
def init_weights(m):
if isinstance(m, nn.Linear):
init.xavier_uniform_(m.weight)
if m.bias is not None:
init.constant_(m.bias, 0)
elif isinstance(m, nn.Conv2d):
init.xavier_uniform_(m.weight)
if m.bias is not None:
init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
init.constant_(m.weight, 1)
init.constant_(m.bias, 0)
elif isinstance(m, nn.Embedding):
init.xavier_uniform_(m.weight)
elif isinstance(m, nn.LayerNorm):
init.normal_(m.weight, mean=1.0, std=0.02)
init.constant_(m.bias, 0.0)
net = torch.load('Out/MAV-DDI.pt', map_location=torch.device('cpu'))
if args.random_factor:
seed = 1
torch.manual_seed(seed)
np.random.seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
net.apply(init_weights)
print('----------------')
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
net.to(device)
net.eval()
for i_batch, batch in enumerate(test_dataloader):
ft1, adj1, ft2, adj2, num_size1, target1, d11, d21, mask_1, mask_2 = batch
# ft1 = ft1.cuda()
# adj1 = adj1.cuda()
# ft2 = ft2.cuda()
# adj2 = adj2.cuda()
# num_size1 = num_size1.cuda()
# d11 = d11.cuda()
# d21 = d21.cuda()
output, _ = net(ft1, adj1, ft2, adj2, num_size1, d11, d21)
scores = torch.sigmoid(output)
# print(scores)
# scores = scores * 10000
# print(scores)
# scores = torch.round(scores)
# print(scores)
# scores = scores / 10000
print(scores)
# F1, accuracy, recall, precision, auroc, aupr = all_evaluate(output, target1)
# print("test answer: acc {}, auc {}".format(accuracy, auroc))
if __name__ == '__main__':
main()