-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutation_utils.py
executable file
·135 lines (110 loc) · 3.82 KB
/
mutation_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
133
134
135
import csv
import json
import os
import numpy as np
from properties import RESULTS_ROOT, ROOT_DIR
def create_folder(folder):
if not os.path.exists(folder):
try:
os.makedirs(name=folder, exist_ok=True)
except OSError as e:
print("Unable to create folder for mutated models:" + str(e))
raise Exception()
def create_results_folders(algo, env):
create_folder(RESULTS_ROOT)
algo_path = os.path.join(RESULTS_ROOT, algo) # ALGO
create_folder(algo_path)
subj_path = os.path.join(algo_path, env) # SUBJ
create_folder(subj_path)
raw_res_path = os.path.join(subj_path, "raw_results")
create_folder(raw_res_path)
return subj_path, raw_res_path
def write_to_csv(rows, dir, file_name):
out_csv_file = os.path.join(dir, file_name)
with open(out_csv_file, "w") as file:
writer = csv.writer(
file,
delimiter=",",
lineterminator="\n",
)
writer.writerows(rows)
def get_operator_trained_confs(algo, env, operator_name):
path = os.path.join(ROOT_DIR, "logs", algo)
conf_list = [
item
for item in os.listdir(path)
if os.path.isdir(os.path.join(path, item))
and env in item
and operator_name in item
]
conf_list = [
item.replace(env + "_" + "mutant" + "_" + operator_name + "_", "")
for item in conf_list
]
return conf_list
def get_agent_stats(
algo,
env,
data_type,
agent_type,
operator,
conf,
num_runs,
get_raw_results,
threshold_failure,
):
path = os.path.join(ROOT_DIR, "logs", algo, str(env) + "_" + str(agent_type))
if agent_type == "mutant":
path = "_".join((path, str(operator), str(conf)))
results_stats = []
results_raw = []
results = []
if not os.path.isdir(path):
raise Exception(f"Path {path} is not a directory")
else:
for i in range(num_runs):
stats_folder = os.path.join(path, "_".join(("run", str(i))))
if data_type == "strong" and agent_type == "original":
stats_file = [
filename
for filename in os.listdir(stats_folder)
if filename.endswith(
str(operator)
+ "-"
+ str(conf)
+ "-"
+ str(data_type)
+ "-statistics.json"
)
]
if len(stats_file) == 0:
stats_file = [
filename
for filename in os.listdir(stats_folder)
if filename.endswith(str(data_type) + "-statistics.json")
]
else:
stats_file = [
filename
for filename in os.listdir(stats_folder)
if filename.endswith(str(data_type) + "-statistics.json")
]
if len(stats_file) == 0:
raise Exception(
f"No statistics file that ends with {str(data_type)}-statistics.json in {stats_folder}"
)
with open(os.path.join(stats_folder, stats_file[0])) as f:
stats = json.load(f)
failure_probabilities = (
(np.asarray(stats["failure_probabilities"]) > threshold_failure)
.astype(int)
.tolist()
)
num_failures = failure_probabilities.count(1)
num_success = failure_probabilities.count(0)
results_stats.append([num_success, num_failures])
results_raw.append(failure_probabilities)
results.append(results_stats)
if get_raw_results:
results.append(results_raw)
return results