-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata2JSON.2.0.py
80 lines (66 loc) · 2.56 KB
/
data2JSON.2.0.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
import csv
import json
import pandas
from sklearn.preprocessing import MinMaxScaler
benchPath = ['hackerrank', 'hpc_applications']
errorType = ['SDC', "benign", "crash"]
modelType = ["c", "r"]
def getLabel(model, value):
if model == "c":
if value > 0.75:
return 2
elif value > 0.5:
return 2
elif value > 0.25:
return 1
else:
return 1
elif model == "r":
return value
def getDic(i, df, path, model, error):
dictionary = {}
print(df['benmark'][i])
if path == "hackerrank":
codeFile = open(f"{path}/Benchmarks/{df['benmark'][i]}/{df['benmark'][i]}.cpp", 'r')
codeLabel = getLabel(model, df[f'{error}'][i])
dictionary["code"] = codeFile.read()
dictionary["label"] = codeLabel
print(codeLabel)
elif path == "hpc_applications":
codeFile = open(f"{path}/Benchmarks/{df['benmark'][i]}.cpp", 'r')
codeLabel = getLabel(model, df[f'{error}'][i])
dictionary["code"] = codeFile.read()
dictionary["label"] = codeLabel
return(dictionary)
def getData(model, error, file, path):
dataList = []
df = pandas.read_csv(file)
df_min_max_scaled = df.copy()
# apply normalization techniques by Column 1
column = error
df_min_max_scaled[column] = (df_min_max_scaled[column] - df_min_max_scaled[column].min()) / (df_min_max_scaled[column].max() - df_min_max_scaled[column].min())
# view normalized data
#print(df_min_max_scaled)
count_row = df.shape[0]
for i in range (0, count_row):
dataList.append(getDic(i, df_min_max_scaled, path, model, error))
return dataList
if __name__ == '__main__':
for model in modelType:
for error in errorType:
trainData = []
validData = []
trainDataFile = f"./{error}_train_resilience_{model}.jsonl"
for path in benchPath:
testData = []
testDataFile = f"./{error}_test_resilience_{model}.jsonl"
if path == "hackerrank":
csvFile = path+"/errorRate_O0.csv"
trainData = getData(model, error, csvFile, path)
else:
csvFile = path + "/errorRate.csv"
testData = getData(model, error, csvFile, path)
with open(testDataFile, "w") as outfile:
outfile.write("\n".join(json.dumps(i) for i in testData))
with open(trainDataFile, "w") as outfile:
outfile.write("\n".join(json.dumps(i) for i in trainData))