-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate_experiments.py
255 lines (217 loc) · 9.75 KB
/
evaluate_experiments.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import json
import os
import time
import fire
import pandas as pd
import yaml
from loguru import logger
from causal_canvas.evaluation_utils import (
extract_all_models,
extract_predictions,
get_best_threshold,
get_bootstrapped_scores,
plot_bootstrapped_scores,
plot_conditionals,
plot_prediction_time,
)
from causal_canvas.script_config import ScriptConfigEvaluation
def predict_and_evaluate_models(config_file: str):
logger.info("Config read")
config = ScriptConfigEvaluation.load_yaml(config_file)
logger.info(config)
model_path = "/bayesian_network/model_fit/bayesian_network.pkl"
cpds_path = (
f"/bayesian_network/dependency_estimates/{config.event_column['name']}.csv"
)
discretiser_path = "/bayesian_network/model_fit/discretiser.pkl"
q_splits_path = "/bayesian_network/model_fit/quantile_splits.json"
for i, train_data_path in enumerate(config.data_train_input_path):
logger.info(f"Reading data in {train_data_path}")
if str(train_data_path).endswith("parquet"):
data = pd.read_parquet(train_data_path)
elif str(train_data_path).endswith("csv"):
data = pd.read_csv(train_data_path)
if config.event_column["target_type"] != "cont":
data[config.event_column["name"]] = data[
config.event_column["name"]
].astype(int)
if data[config.date_column].dtype != "str":
data[config.date_column] = data[config.date_column].astype(str)
if config.test_dates and config.optim_test_dates:
optim_test_set = data[
data[config.date_column].between(*config.optim_test_dates)
]
test_set = data[data[config.date_column].between(*config.test_dates)]
else:
logger.info(f"Reading data in {config.data_test_input_path[i]}")
if str(config.data_test_input_path[i]).endswith("parquet"):
data_for_optim_test = pd.read_parquet(config.data_test_input_path[i])
elif str(config.data_test_input_path[i]).endswith("csv"):
data_for_optim_test = pd.read_csv(config.data_test_input_path[i])
optim_test_set = data_for_optim_test.sample(frac=0.5, random_state=11)
test_set = data_for_optim_test[
~data_for_optim_test.index.isin(optim_test_set.index)
]
all_models_path = (
config.all_models_path
if len(config.data_train_input_path) == 1
else config.all_models_path + f"segment_{i}/inference/"
)
models_list, model_names_list = extract_all_models(
all_models_path=all_models_path,
model_path=model_path,
inference_methods=config.inference_methods,
models_combinations=config.models_combinations,
)
logger.info(f"Models detected for dataset:{model_names_list}")
output_path = (
config.output_path
if len(config.data_train_input_path) == 1
else config.output_path + f"segment_{i}/"
)
all_predictions = dict()
scores = dict()
max_scores = dict()
scores_050_thresh = dict()
times_to_predict = dict()
times_to_predict_test = dict()
test_predictions = dict()
bootstrapped_scores = pd.DataFrame(
columns=["quantile", "precision", "recall", "f1-score", "model"]
)
bootstrapped_scores_test = pd.DataFrame(
columns=["quantile", "precision", "recall", "f1-score", "model"]
)
for name, model in zip(model_names_list, models_list):
reading_path = all_models_path + f"{name[0]}/{name[1]}"
os.makedirs(output_path + f"test_set_predictions/{name[0]}", exist_ok=True)
os.makedirs(output_path + f"optim_set_predictions/{name[0]}", exist_ok=True)
os.makedirs(output_path + "cpd_plots/", exist_ok=True)
logger.info("Plotting CPDs")
plot_conditionals(
cpds_path=reading_path + cpds_path,
target_column_names=config.event_column["target_column_names_cpds"],
target_class=config.event_column["target_class"],
threshold=0.5,
inference_method=name,
target=config.event_column["name"].upper(),
path=output_path + "cpd_plots/",
sub_path=f"{name[0]}_{name[1]}",
)
logger.info("Predicting set for optimisation")
start_time = time.time()
optim_test_set_predictions = extract_predictions(
df=optim_test_set,
chunk_size=1000,
model=model,
features=config.features,
model_name=name,
target_name=config.event_column["name"],
target_column_names_cpds=config.event_column[
"target_column_names_cpds"
],
target_class=config.event_column["target_class"],
discretisation_method=name[1].split("_")[0],
discretisation_path=reading_path + q_splits_path
if name[1].split("_")[0] == "simple"
else reading_path + discretiser_path,
multiprocessing=config.use_multiprocessing,
folder_name=output_path + "optim_set_predictions",
)
end_time = time.time()
times_to_predict[f"{name[0]}_{name[1]}"] = end_time - start_time
all_predictions[name] = optim_test_set_predictions
logger.info(
"Calculating bootstrapped scores for validation set before optimisation"
)
boot_scores = get_bootstrapped_scores(
predictions=all_predictions[name],
target_name=config.event_column["name"],
target_class=config.event_column["target_class"],
threshold=0.5,
boot_iterations=config.boot_iterations,
alpha=config.alpha,
)
boot_scores["model"] = f"{name[0]}_{name[1]}"
bootstrapped_scores = pd.concat([bootstrapped_scores, boot_scores], axis=0)
logger.info("Calculating optimised threshold")
results = get_best_threshold(
predictions=all_predictions[name],
target_name=config.event_column["name"],
target_class=config.event_column["target_class"],
score=config.score_to_optimise,
)
scores[str(name)] = results[0]
max_scores[str(name)] = results[1]
scores_050_thresh[str(name)] = results[2]
logger.info("Calculating test set predictions")
start_time = time.time()
# Predict test set based on optimised threshold
test_predictions[name] = extract_predictions(
df=test_set,
chunk_size=1000,
model=model,
features=config.features,
model_name=name,
target_name=config.event_column["name"],
target_class=config.event_column["target_class"],
target_column_names_cpds=config.event_column[
"target_column_names_cpds"
],
discretisation_method=name[1].split("_")[0],
discretisation_path=reading_path + q_splits_path
if name[1].split("_")[0] == "simple"
else reading_path + discretiser_path,
multiprocessing=config.use_multiprocessing,
folder_name=output_path + "test_set_predictions",
)
end_time = time.time()
times_to_predict_test[f"{name[0]}_{name[1]}"] = end_time - start_time
logger.info(
"Calculating bootstrapped scores for test set with optimised threshold"
)
# Use optimised threshold to extract bootstrap performances
boot_scores = get_bootstrapped_scores(
predictions=all_predictions[name],
target_name=config.event_column["name"],
target_class=config.event_column["target_class"],
threshold=max_scores[str(name)]["best_threshold"],
boot_iterations=config.boot_iterations,
alpha=config.alpha,
)
boot_scores["model"] = f"{name[0]}_{name[1]}"
bootstrapped_scores_test = pd.concat(
[bootstrapped_scores_test, boot_scores], axis=0
)
all_scores_dict = {"best_scores": max_scores, "scores": scores}
out_file = open(output_path + "thresholds.json", "w")
json.dump(all_scores_dict, out_file)
out_file.close()
plot_bootstrapped_scores(
bootstrapped_scores, output_path + "optim_set_predictions/"
)
plot_bootstrapped_scores(
bootstrapped_scores_test, output_path + "test_set_predictions/"
)
if config.test_dates:
plot_prediction_time(
times=times_to_predict,
title="optim set",
path=output_path + "optim_set_predictions/",
)
plot_prediction_time(
times=times_to_predict_test,
title="test set",
path=output_path + "test_set_predictions/",
)
bootstrapped_scores.to_csv(
output_path + "optim_set_predictions/bootstrapped_scores.csv"
)
bootstrapped_scores_test.to_csv(
output_path + "test_set_predictions/bootstrapped_scores.csv"
)
logger.info("Writing config file")
with open(config.output_path + "config.yml", "w") as outfile:
yaml.dump(dict(config), outfile)
if __name__ == "__main__":
fire.Fire(predict_and_evaluate_models)