This repository has been archived by the owner on Apr 30, 2020. It is now read-only.
HitLuca
/
Incremental_predictive_monitoring_of_Business_Processes_with_a_priori_knowledge
Public archive
forked from kaurjvpld/Incremental-Predictive-Monitoring-of-Business-Processes-with-A-priori-knowledge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_method_cfr.py
executable file
·319 lines (268 loc) · 16.2 KB
/
new_method_cfr.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"""
This file is build based on the code by Anton Yeshchenko found in evaluate_suffix_only.py
here the beam search algorithm is implemented with a modified queueing algorithm.
Author: Kaur Jarvpold
"""
from __future__ import division
import csv
import os.path
import time
from Queue import PriorityQueue
from datetime import datetime, timedelta
from itertools import izip
import distance
from keras.models import load_model
from sklearn import metrics
import shared_variables
from evaluation.prepare_data_resource import amplify, select_declare_verified_traces, \
encode, prepare_testing_data, create_queue, adjust_probabilities
from shared_variables import get_int_from_unicode
def run_experiments(server_replayer, log_name, models_folder, fold):
class NodePrediction:
def __init__(self, data, trace_id, crop_line, crop_line_group, crop_times,
tot_predicted_time, probability_of=0):
self.data = data
self.trace_id = trace_id
self.cropped_line = crop_line
self.cropped_line_group = crop_line_group
self.cropped_times = crop_times
self.total_predicted_time = tot_predicted_time
self.probability_of = probability_of
model_filename = shared_variables.extract_last_model_checkpoint(log_name, models_folder, fold, 'CFR')
declare_model_filename = shared_variables.extract_declare_model_filename(log_name)
log_settings_dictionary = shared_variables.log_settings[log_name]
prefix_size_pred_from = log_settings_dictionary['prefix_size_pred_from']
prefix_size_pred_to = log_settings_dictionary['prefix_size_pred_to']
start_time = time.time()
beam_size = 10
# prepare the data N.B. maxlen == predict_size
lines, \
lines_id, \
lines_group, \
lines_t, \
lines_t2, \
lines_t3, \
lines_t4, \
maxlen, \
chars, \
chars_group, \
char_indices, \
char_indices_group, \
divisor, \
divisor2, \
divisor3, \
predict_size, \
target_indices_char, \
target_indices_char_group, \
target_char_indices, \
target_char_indices_group = prepare_testing_data(log_name)
# find cycles and modify the probability functionality goes here
stop_symbol_probability_amplifier_current = 1
# load model, set this to the model generated by train.py
model = load_model(model_filename)
folder_path = shared_variables.outputs_folder + models_folder + '/' + str(fold) + '/results/declare/'
if not os.path.exists(folder_path):
os.makedirs(folder_path)
output_filename = folder_path + '%s_%s.csv' % (log_name, 'CFR')
with open(output_filename, 'wb') as csvfile:
csv_writer = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
# headers for the new file
csv_writer.writerow(["Prefix length",
"Ground truth",
"Predicted",
"Damerau-Levenshtein",
"Jaccard",
"Ground truth times",
"Predicted times",
"RMSE",
"MAE",
"Median AE",
"Ground Truth Resource",
"Predicted Resource",
"Damerau-Levenshtein Resource"])
# make predictions for different prefix sizes as specified in 'shared variables'
for prefix_size in range(prefix_size_pred_from, prefix_size_pred_to):
print("prefix size: " + str(prefix_size))
curr_time = time.time()
lines_s, \
lines_id_s, \
lines_group_s, \
lines_t_s, \
lines_t2_s, \
lines_t3_s, \
lines_t4_s = select_declare_verified_traces(server_replayer,
declare_model_filename,
lines,
lines_id,
lines_group,
lines_t,
lines_t2,
lines_t3,
lines_t4,
prefix_size)
print("formulas verified: " + str(len(lines_s)) + " out of : " + str(len(lines)))
print('elapsed_time:', time.time() - curr_time)
counter = 0
for line, line_id, line_group, times, times2, times3, times4 in izip(lines_s,
lines_id_s,
lines_group_s,
lines_t_s,
lines_t2_s,
lines_t3_s,
lines_t4_s):
times.append(0)
cropped_line_id = line_id
cropped_line = ''.join(line[:prefix_size])
cropped_line_group = ''.join(line_group[:prefix_size])
cropped_times = times[:prefix_size]
cropped_times3 = times3[:prefix_size]
cropped_times4 = times4[:prefix_size]
if len(times2) < prefix_size:
continue # make no prediction for this case, since this case has ended already
# initialize root of the tree for beam search
total_predicted_time_initialization = 0
search_node_root = NodePrediction(encode(cropped_line, cropped_line_group, cropped_times,
cropped_times3, maxlen, chars, chars_group,
char_indices, char_indices_group, divisor, divisor2),
cropped_line_id,
cropped_line,
cropped_line_group,
cropped_times4,
total_predicted_time_initialization)
ground_truth = ''.join(line[prefix_size:prefix_size + predict_size])
ground_truth_group = ''.join(line_group[prefix_size:prefix_size + predict_size])
ground_truth_t = times2[prefix_size - 1]
case_end_time = times2[len(times2) - 1]
ground_truth_t = case_end_time - ground_truth_t
queue_next_steps = PriorityQueue()
queue_next_steps.put((-search_node_root.probability_of, search_node_root))
queue_next_steps_future = PriorityQueue()
start_of_the_cycle_symbol = " "
found_satisfying_constraint = False
current_beam_size = beam_size
current_prediction_premis = None
for i in range(predict_size):
for k in range(current_beam_size):
if queue_next_steps.empty():
break
_, current_prediction_premis = queue_next_steps.get()
if not found_satisfying_constraint:
if server_replayer.verify_with_data(declare_model_filename,
current_prediction_premis.trace_id,
current_prediction_premis.cropped_line,
current_prediction_premis.cropped_line_group,
current_prediction_premis.cropped_times,
prefix_size):
# the formula verified and we can just finish the predictions
# beam size is 1 because predict only sequence of events
activity = map(lambda x: str(get_int_from_unicode(x)),
current_prediction_premis.cropped_line[prefix_size:])
group = map(lambda x: str(get_int_from_unicode(x)),
current_prediction_premis.cropped_line_group[prefix_size:])
print 'changed beam size ' + str(activity) + ' ' + str(group)
current_prediction_premis.probability_of = 0.0
current_beam_size = 1
# overwrite new queue
queue_next_steps_future = PriorityQueue()
found_satisfying_constraint = True
enc = current_prediction_premis.data
current_trace_id = current_prediction_premis.trace_id
temp_cropped_line = current_prediction_premis.cropped_line
temp_cropped_line_group = current_prediction_premis.cropped_line_group
temp_cropped_times = current_prediction_premis.cropped_times
y = model.predict(enc, verbose=0) # make predictions
# split predictions into separate activity and time predictions
y_char = y[0][0]
y_group = y[1][0]
y_t = y[2][0][0]
if not i == 0:
stop_symbol_probability_amplifier_current, start_of_the_cycle_symbol = \
amplify(temp_cropped_line)
y_char_adjusted = adjust_probabilities(y_char, target_char_indices, start_of_the_cycle_symbol,
stop_symbol_probability_amplifier_current)
the_queue = create_queue(y_char_adjusted, y_group)
if y_t < 0:
y_t = 0
cropped_times.append(y_t)
y_t = y_t * divisor3
cropped_times3.append(cropped_times3[-1] + timedelta(seconds=y_t))
for j in range(current_beam_size):
probability_this, indexes = the_queue.get()
temp_prediction = target_indices_char[indexes[0]]
temp_prediction_group = target_indices_char_group[indexes[1]]
# end of case was just predicted, therefore, stop predicting further into the future
if temp_prediction == '!':
if server_replayer.verify_with_data(declare_model_filename,
current_trace_id,
temp_cropped_line,
temp_cropped_line_group,
temp_cropped_times,
prefix_size):
stop_symbol_probability_amplifier_current = 1
# print '! predicted, end case'
queue_next_steps = PriorityQueue()
break
else:
continue
temp_cropped_line = current_prediction_premis.cropped_line + temp_prediction
temp_cropped_line_group = \
current_prediction_premis.cropped_line_group + temp_prediction_group
# adds a fake timestamp to the list
t = time.strptime(cropped_times4[-1], "%Y-%m-%d %H:%M:%S")
new_timestamp = datetime.fromtimestamp(time.mktime(t)) + timedelta(0, 2000)
cropped_times4.append(new_timestamp.strftime("%Y-%m-%d %H:%M:%S"))
temp_total_predicted_time = current_prediction_premis.total_predicted_time + y_t
temp_state_data = encode(temp_cropped_line,
temp_cropped_line_group,
cropped_times,
cropped_times3,
maxlen,
chars,
chars_group,
char_indices,
char_indices_group,
divisor,
divisor2)
temp = NodePrediction(temp_state_data,
cropped_line_id,
temp_cropped_line,
temp_cropped_line_group,
cropped_times4,
temp_total_predicted_time,
current_prediction_premis.probability_of + probability_this)
queue_next_steps_future.put((temp.probability_of, temp))
# print 'INFORMATION: ' + str(counter) + ' ' + str(i) + ' ' + str(k) + ' ' + str(j) + ' ' + \
# temp_cropped_line[prefix_size:] + " " + ground_truth + " " + \
# temp_cropped_line_group[prefix_size:] + " " + ground_truth_group + " " + \
# str(temp.probability_of) + ' ' + str(current_beam_size)
queue_next_steps = queue_next_steps_future
queue_next_steps_future = PriorityQueue()
counter += 1
if current_prediction_premis is None:
print "Cannot find any trace that is compliant with formula given current beam size"
break
output = []
if current_prediction_premis is None:
predicted = u""
predicted_group = u""
total_predicted_time = 0
else:
predicted = (current_prediction_premis.cropped_line[prefix_size:])
predicted_group = (current_prediction_premis.cropped_line_group[prefix_size:])
total_predicted_time = current_prediction_premis.total_predicted_time
if len(ground_truth) > 0:
output.append(prefix_size)
output.append(unicode(ground_truth).encode("utf-8"))
output.append(unicode(predicted).encode("utf-8"))
output.append(1 - distance.nlevenshtein(predicted, ground_truth))
output.append(1 - distance.jaccard(predicted, ground_truth))
output.append(ground_truth_t)
output.append(total_predicted_time)
output.append('')
output.append(metrics.mean_absolute_error([ground_truth_t], [total_predicted_time]))
output.append(metrics.median_absolute_error([ground_truth_t], [total_predicted_time]))
output.append(unicode(ground_truth_group).encode("utf-8"))
output.append(unicode(predicted_group).encode("utf-8"))
output.append(1 - distance.nlevenshtein(predicted_group, ground_truth_group))
csv_writer.writerow(output)
print("TIME TO FINISH --- %s seconds ---" % (time.time() - start_time))