-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.py
149 lines (130 loc) · 5.39 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
141
142
143
144
145
146
import os
import sys
import time
import numpy as np
import torch
from torch_geometric.data import Data
from torch_geometric.loader import DataLoader
from libs.nets.PGCN_noAST import PGCN, PGCNTest
from preproc import extract_graphs, construct_graphs
logsPath = './logs/'
testPath = './testdata/'
mdlsPath = './models/'
# parameters
_CLANG_ = 1
_NETXARCHT_ = 'PGCN'
_BATCHSIZE_ = 128
dim_features = 20
start_time = time.time() #mark start time
class Logger(object):
def __init__(self, filename = "log.txt"):
self.terminal = sys.stdout
self.log = open(filename, "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
pass
def RunTime():
pTime = ' [TIME: ' + str(round((time.time() - start_time), 2)) + ' sec]'
return pTime
# extract graphs
def extractgraphs():
cnt = 0
for root, ds, fs in os.walk(testPath):
for file in fs:
if ('.log' == file[-4:]):
filename = os.path.join(root, file).replace('\\', '/')
savename = filename + '_mid.npz'
cnt += 1
nodes, edges, nodes0, edges0, nodes1, edges1 = extract_graphs.ReadFile(filename)
if _CLANG_:
nodes = extract_graphs.ProcNodes(nodes, 'PatchCPG')
nodes0 = extract_graphs.ProcNodes(nodes0, 'PreCPG')
nodes1 = extract_graphs.ProcNodes(nodes1, 'PostCPG')
label = [0]
np.savez(savename, nodes=nodes, edges=edges, nodes0=nodes0, edges0=edges0, nodes1=nodes1, edges1=edges1,
label=label, dtype=object)
print(f'[INFO] <main> save the graph information into numpy file: [{str(cnt)}] ' + savename + RunTime())
print('=====================================================')
return
# construct graphs
def constructgraphs():
cnt = 0
for root, ds, fs in os.walk(testPath):
for file in fs:
if ('_mid.npz' == file[-8:]):
filename = os.path.join(root, file).replace('\\', '/')
savename = filename.replace('_mid.npz', '_np.npz')
cnt += 1
print('[INFO] <main> Process the graph numpy file: [' + str(cnt) + '] ' + filename + RunTime())
nodes, edges, nodes0, edges0, nodes1, edges1, label = construct_graphs.ReadFile(filename)
nodeDict, edgeIndex, edgeAttr = construct_graphs.ProcEdges(edges)
nodeAttr, nodeInvalid = construct_graphs.ProcNodes(nodes, nodeDict)
np.savez(savename, edgeIndex=edgeIndex, edgeAttr=edgeAttr, nodeAttr=nodeAttr, label=label,
nodeDict=nodeDict)
print(
'[INFO] <main> save the graph information into numpy file: [' + str(
cnt) + '] ' + savename + RunTime())
print('-----------------------------------------------------')
return
# get dataset
def GetDataset(path=None):
'''
Get the dataset from numpy data files.
:param path: the path used to store numpy dataset.
:return: dataset - list of torch_geometric.data.Data
'''
# check.
if None == path:
print('[Error] <GetDataset> The method is missing an argument \'path\'!')
return [], []
# contruct the dataset.
dataset = []
files = []
for root, _, filelist in os.walk(path):
for file in filelist:
if file[-7:] == '_np.npz':
# read a numpy graph file.
graph = np.load(os.path.join(root, file), allow_pickle=True)
files.append(os.path.join(root, file[:-7]))
# sparse each element.
edgeIndex = torch.tensor(graph['edgeIndex'], dtype=torch.long)
nodeAttr = torch.tensor(graph['nodeAttr'], dtype=torch.float)
edgeAttr = torch.tensor(graph['edgeAttr'], dtype=torch.float)
label = torch.tensor(graph['label'], dtype=torch.long)
# construct an instance of torch_geometric.data.Data.
data = Data(edge_index=edgeIndex, x=nodeAttr, edge_attr=edgeAttr, y=label)
# append the Data instance to dataset.
dataset.append(data)
if (0 == len(dataset)):
print(f'[ERROR] Fail to load data from {path}')
return dataset, files
# main
def main():
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = PGCN(num_node_features=dim_features)
model.load_state_dict(torch.load(mdlsPath + f'/model_{_NETXARCHT_}_{dim_features}_10.pth'))
dataset, files = GetDataset(path=testPath)
dataloader = DataLoader(dataset, batch_size=_BATCHSIZE_, shuffle=False)
testAcc, testPred, testLabel = PGCNTest(model, dataloader)
filename = logsPath + '/test_results.txt'
fp = open(filename, 'w')
fp.write(f'filename,prediction\n')
for i in range(len(files)):
fp.write(f'{files[i]},{testPred[i]}\n')
fp.close()
return
if __name__ == '__main__':
logfile = 'test.txt'
if os.path.exists(os.path.join(logsPath, logfile)):
os.remove(os.path.join(logsPath, logfile))
elif not os.path.exists(logsPath):
os.makedirs(logsPath)
sys.stdout = Logger(os.path.join(logsPath, logfile))
# --------------------------------------------------
extractgraphs()
# check [mid error]
constructgraphs()
# check [np error]
main()