-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDAC4.py
More file actions
222 lines (192 loc) · 7.41 KB
/
DAC4.py
File metadata and controls
222 lines (192 loc) · 7.41 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import os
import pandas as pd
import re
from sklearn import tree
from sklearn.metrics import accuracy_score
import argparse
def extract_feature(dataset):
res_df = pd.DataFrame()
label_folders = dataset
labelfile = "labels_modified.txt"
for resp in label_folders:
label_data_path = os.path.join(resp[:-1], labelfile)
if not os.path.exists(label_data_path):
continue
y_dataframe = pd.read_csv(label_data_path, sep=' ', header=None)
y = y_dataframe[1]
chipfaultinfo = resp.split("/")[1:4]
# print(chipfaultinfo)
assert len(chipfaultinfo)==3
root = os.path.join("../", chipfaultinfo[0], chipfaultinfo[1], "????_fail", chipfaultinfo[2].split("_")[0]+"_fail")
fail_data = "all.fail"
fail_data_path = os.path.join(root, fail_data)
x_dataframe = pd.read_csv(fail_data_path, sep=' ', header=None)
if re.search(r'all.bmp', y_dataframe[0].iloc[-1]):
# print("in ")
y = y.iloc[:-1]
x1 = x_dataframe[0]
temp = []
for num in x1:
if len(temp)==0:
temp.append(num)
elif temp[-1]!=num:
temp.append(num)
else:
continue
x1 = pd.Series(temp)
x2 = pd.Series(range(1, len(x1) + 1))
x3 = [(x_dataframe.iloc[:, 0] <= x).values.sum() for x in x1]
x3 = pd.Series(x3)
x4 = [(x_dataframe.iloc[:, 0] == x).values.sum() for x in x1]
x4 = pd.Series(x4)
x5 = [len(set(x_dataframe.iloc[:, 1][x_dataframe.iloc[:, 0] <= x])) for x in x1]
current = x5[0]
x6 = [current]
for i in range(1, len(x5)):
x6.append(x5[i] - current)
current = x5[i]
x5 = pd.Series(x5)
x6 = pd.Series(x6)
x7 = [x1[0] for i in range(len(x1))]
x7 = pd.Series(x7)
data_with_label = pd.concat([x1, x2, x3, x4, x5, x6, x7, y], axis=1)
# print(data_with_label)
res_df = pd.concat([res_df, data_with_label], axis=0)
return res_df
def calacc(model, testset):
# print(testset)
labelfile = "labels_modified.txt"
guard = 3 # 保护带
totalchip = 0
correct = 0
incorrect = 0
id_resp = [-1]*len(testset)
# print(testset)
for p in range(len(testset)):
if not os.path.exists(os.path.join(testset[p][:-1], labelfile)):
# print(testset[p])
continue
totalchip += 1
_, c, f, id = testset[p].split("/")
# print(c, f, id)
# exit()
data_path = os.path.join("data/", c, f)
if os.path.exists(f"{data_path}/{c}_test_{id[:-1]}.csv"):
xy = pd.read_csv(f"{data_path}/{c}_test_{id[:-1]}.csv", sep=',', low_memory=False)
# print("read csv in ")
else:
# print("extracting...")
xy = extract_feature([testset[p]])
xy.columns = ['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7', 'y']
# 存source trainset的特征和标签
if not os.path.exists(data_path):
os.makedirs(data_path)
xy.to_csv(f"{data_path}/{c}_test_{id[:-1]}.csv", index=False)
x = xy.iloc[:, 0:-1].astype('int64')
y = xy.iloc[:, -1].astype('int64')
pred = model.predict(x)
assert len(y)==len(pred)
isStop = False
# 预测标签全为0的情况,也算作correct
if pred.sum()==0:
correct+=1
continue
# for i in range(len(pred)):
# if pred[i]==0:
# continue
# elif pred[i]==1 :
# id_resp[p] = i
# if y[i]==1:
# correct += 1
# break
# else:
# incorrect += 1
# break
for i in range(guard-1, len(pred)):
if pred[i]==0:
continue
elif pred[i]==1:
if sum(pred[i-guard:i+1])==1:
# 满足保护带机制
id_resp[p] = i
isStop = True
if y[i]==1:
correct+=1
break
else:
incorrect+=1
break
else:
# 不满足保护带机制
continue
else:
raise Exception
if not isStop:
correct+=1
continue
if correct+incorrect==totalchip:
return float(correct/totalchip), id_resp
else:
raise Exception
return None
def calDVR(testset, id_resp):
assert len(testset)==len(id_resp)
dvr = 0.0
for i in range(len(testset)):
chipfaultinfo = testset[i].split("/")[1:4]
if id_resp[i]==-1:
continue
failpath = os.path.join("../", chipfaultinfo[0], chipfaultinfo[1], "????_fail", chipfaultinfo[2].split("_")[0]+"_fail", f"{id_resp[i]}.fail")
allfailpath = os.path.join("../", chipfaultinfo[0], chipfaultinfo[1], "????_fail", chipfaultinfo[2].split("_")[0]+"_fail", "all.fail")
fenzi = len(open(failpath, "r").readlines())
fenmu = len(open(allfailpath, "r").readlines())
assert fenzi<=fenmu
dvr = dvr + (1-(fenzi/fenmu))
return dvr/len(testset)
def run(source, target):
source_circuit = source
target_circuit = target
print("************************************")
print(source_circuit, "-->", target_circuit)
col_names = ['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7', 'y']
# source
with open(f"./dataset/{source_circuit}_train_list.txt", "r") as f:
trainset_s = f.readlines()
f.close()
# with open(f"./dataset/{source_circuit}_test_list.txt", "r") as f:
# testset_s = f.readlines()
# f.close()
# trainset_s = ["pic/ctrl/fe/2_resp\n"]
data_path = os.path.join("data/", source_circuit)
if os.path.exists(f"{data_path}/{source_circuit}_train.csv"):
x_y_train = pd.read_csv(f"{data_path}/{source_circuit}_train.csv", sep=',', header=None, low_memory=False)
# print("read features in")
else:
# print("extracting...")
x_y_train = extract_feature(trainset_s)
x_y_train.columns = col_names
# 存source trainset的特征和标签
if not os.path.exists(data_path):
os.makedirs(data_path)
x_y_train.to_csv(f'{data_path}/{source_circuit}_train.csv', index=False)
# print(x_y_train)
x_train_s = x_y_train.iloc[1:, 0:-1].astype('int64')
y_train_s = x_y_train.iloc[1:, -1].astype('int64')
# target
with open(f"./dataset/{target_circuit}_test_list.txt", "r") as f:
testset_t = f.readlines()
f.close()
# Train
dt_clf = tree.DecisionTreeClassifier(min_samples_leaf=3, random_state=10)
dt_clf.fit(x_train_s, y_train_s)
acc_t, id_resp_t = calacc(model=dt_clf, testset=testset_t)
dvr_t = calDVR(testset_t, id_resp_t)
print(f"DT tree acc: {acc_t}".ljust(50), f"DVR : {dvr_t*100}%".ljust(30), f"len of {target_circuit} : {len(testset_t)}")
print("\n")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="decision tree")
parser.add_argument("-s", type=str, default="ctrl", help="source domain")
parser.add_argument("-t", type=str, default="ctrl", help="target domain")
args = parser.parse_args()
print(args)
run(source=args.s, target=args.t)