-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict_log.py
More file actions
195 lines (177 loc) · 8.29 KB
/
predict_log.py
File metadata and controls
195 lines (177 loc) · 8.29 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
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 21 21:36:01 2018
@author: Manuel Camargo
"""
import sys
import json
import os
import math
import datetime
from datetime import timedelta
from tensorflow.keras.models import load_model
import pandas as pd
import numpy as np
from support_modules.analyzers import generalization as gen
from support_modules import support as sup
MAX = sys.maxsize
START_TIMEFORMAT = ''
INDEX_AC = None
INDEX_RL = None
DIM = dict()
TBTW = dict()
EXP = dict()
def predict(timeformat, parameters, is_single_exec=True):
"""Main function of the event log generation module.
Args:
timeformat (str): event-log date-time format.
parameters (dict): parameters used in the training step.
is_single_exec (boolean): generate measurments stand alone or share
results with other runing experiments (optional)
"""
global START_TIMEFORMAT
global INDEX_AC
global INDEX_RL
global DIM
global TBTW
global EXP
START_TIMEFORMAT = timeformat
output_route = os.path.join('output_files', parameters['folder'])
model_name, _ = os.path.splitext(parameters['model_file'])
# Loading of testing dataframe
df_test = pd.read_csv(os.path.join(output_route, 'parameters', 'test_log.csv'))
df_test['start_timestamp'] = pd.to_datetime(df_test['start_timestamp'])
df_test['end_timestamp'] = pd.to_datetime(df_test['end_timestamp'])
df_test = df_test.drop(columns=['user'])
df_test = df_test.rename(index=str, columns={"role": "user"})
# Loading of parameters from training
with open(os.path.join(output_route, 'parameters', 'model_parameters.json')) as file:
data = json.load(file)
EXP = {k: v for k, v in data['exp_desc'].items()}
print(EXP)
DIM['samples'] = int(data['dim']['samples'])
DIM['time_dim'] = int(data['dim']['time_dim'])
DIM['features'] = int(data['dim']['features'])
TBTW['max_tbtw'] = float(data['max_tbtw'])
INDEX_AC = {int(k): v for k, v in data['index_ac'].items()}
INDEX_RL = {int(k): v for k, v in data['index_rl'].items()}
file.close()
# Next event selection method and numbers of repetitions
variants = [{'imp': 'Random Choice', 'rep': 1},
{'imp': 'Arg Max', 'rep': 0}]
# Generation of predictions
model = load_model(os.path.join(output_route, parameters['model_file']))
df_test_log = df_test.to_dict('records')
for var in variants:
for _ in range(0, var['rep']):
generated_event_log = generate_traces(model, var['imp'],
len(df_test.caseid.unique()),
200)
sim_task = gen.gen_mesurement(df_test_log,
generated_event_log, 'task')
sim_role = gen.gen_mesurement(df_test_log,
generated_event_log, 'user')
if is_single_exec:
sup.create_csv_file_header(sim_task,
os.path.join(output_route,
model_name +'_similarity.csv'))
sup.create_csv_file_header(generated_event_log,
os.path.join(output_route, model_name +'_log.csv'))
# Save results
measurements = list()
measurements.append({**dict(model=os.path.join(output_route, parameters['model_file']),
implementation=var['imp'],
dl_task=np.mean([x['sim_score'] for x in sim_task]),
dl_user=np.mean([x['sim_score'] for x in sim_role]),
mae=np.mean([x['abs_err'] for x in sim_task]),
dlt=np.mean([x['sim_score_t'] for x in sim_task])),
**EXP})
if is_single_exec:
sup.create_csv_file_header(measurements,
os.path.join('output_files',
model_name +'_measures.csv'))
else:
if os.path.exists(os.path.join('output_files', 'total_measures.csv')):
sup.create_csv_file(measurements,
os.path.join('output_files',
'total_measures.csv'), mode='a')
else:
sup.create_csv_file_header(measurements,
os.path.join('output_files', 'total_measures.csv'))
# =============================================================================
# Predic traces
# =============================================================================
def generate_traces(model, imp, num_cases, max_trace_size):
"""Generate business process traces using a keras trained model.
Args:
model (keras model): keras trained model.
imp (str): method of next event selection.
num_cases (int): number of traces to generate.
max_trace_size (int): max size of the trace
"""
sup.print_performed_task('Generating traces')
generated_event_log = list()
for case in range(0, num_cases):
x_trace = list()
x_ac_ngram = np.zeros((1, DIM['time_dim']), dtype=np.float32)
x_rl_ngram = np.zeros((1, DIM['time_dim']), dtype=np.float32)
x_t_ngram = np.zeros((1, DIM['time_dim'], 1), dtype=np.float32)
for _ in range(1, max_trace_size):
predictions = model.predict([x_ac_ngram, x_rl_ngram, x_t_ngram])
if imp == 'Random Choice':
# Use this to get a random choice following as PDF the predictions
pos = np.random.choice(np.arange(0, len(predictions[0][0])), p=predictions[0][0])
pos1 = np.random.choice(np.arange(0, len(predictions[1][0])), p=predictions[1][0])
elif imp == 'Arg Max':
# Use this to get the max prediction
pos = np.argmax(predictions[0][0])
pos1 = np.argmax(predictions[1][0])
x_trace.append([pos, pos1, predictions[2][0][0]])
# # Add prediction to n_gram
x_ac_ngram = np.append(x_ac_ngram, [[pos]], axis=1)
x_ac_ngram = np.delete(x_ac_ngram, 0, 1)
x_rl_ngram = np.append(x_rl_ngram, [[pos1]], axis=1)
x_rl_ngram = np.delete(x_rl_ngram, 0, 1)
x_t_ngram = np.append(x_t_ngram, [predictions[2]], axis=1)
x_t_ngram = np.delete(x_t_ngram, 0, 1)
# # Stop if the next prediction is the end of the trace
# # otherwise until the defined max_size
if INDEX_AC[pos] == 'end':
break
generated_event_log.extend(decode_trace(x_trace, case))
sup.print_done_task()
return generated_event_log
# =============================================================================
# Decoding
# =============================================================================
def decode_trace(trace, case):
"""Example function with types documented in the docstring.
Args:
trace (list): trace of predicted events.
case (int): case number.
Returns:
list: predicted business trace decoded.
"""
log_trace = list()
for i, _ in enumerate(trace):
event = trace[i]
if INDEX_AC[event[0]] != 'end':
if EXP['norm_method'] == 'activity':
tbtw = (event[2] * TBTW['max_tbtw'][event[0]])
elif EXP['norm_method'] == 'lognorm':
tbtw = math.expm1(event[2] * TBTW['max_tbtw'])
else:
tbtw = np.rint(event[2] * TBTW['max_tbtw'])
if i == 0:
now = datetime.datetime.now()
now.strftime(START_TIMEFORMAT)
time = now
else:
time = log_trace[i-1]['start_timestamp'] + timedelta(minutes=tbtw)
log_trace.append(dict(caseid=case,
task=INDEX_AC[event[0]],
user=INDEX_RL[event[1]],
start_timestamp=time,
tbtw_raw=event[2],
tbtw=tbtw))
return log_trace