-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard.py
780 lines (657 loc) · 44.9 KB
/
dashboard.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# -*- coding: utf-8 -*-
"""
@author: Rehan Fazal
"""
import os
os.environ['TF_XLA_FLAGS'] = '--tf_xla_enable_xla_devices'
os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
import sys
import json
import time
import getopt
import pandas as pd
import streamlit as st
# ---Workaround for "tensorflow.python.framework.errors_impl.UnknownError: Fail to find the dnn implementation."
# from tensorflow.compat.v1 import ConfigProto
# from tensorflow.compat.v1 import InteractiveSession
#
# config = ConfigProto()
# config.gpu_options.allow_growth = True # dynamically grow the memory used on the GPU
# # config.inter_op_parallelism_threads = 4
# config.intra_op_parallelism_threads = 4
# session = InteractiveSession(config=config)
# --------------------------------------------------------------------------------------------------------------
from model_prediction import model_predictor as pr
from model_training import model_trainer as tr
# ----------------------------------------------------------------------------------
# -- Dashboard Global View -- #
# ----------------------------------------------------------------------------------
# -- Page Config
st.set_page_config(layout="wide", initial_sidebar_state="auto", page_title='NxEventPred', page_icon="🌌")
# -- Page Customization
max_width_str = f"max-width: 1500px;"
st.markdown(f""" <style>
.reportview-container .main .block-container {{{max_width_str}
}} </style> """, unsafe_allow_html=True)
# -- Condense the layout
padding = 1
st.markdown(f""" <style>
.reportview-container .main .block-container{{
padding-top: {padding}rem;
padding-right: {padding}rem;
padding-left: {padding}rem;
padding-bottom: {padding}rem;
}} </style> """, unsafe_allow_html=True)
# -- hides both menu and footer watermark
# #Hide the menu button
# st.markdown(""" <style>
# #MainMenu {visibility: hidden;}
# footer {visibility: hidden;}
# </style> """, unsafe_allow_html=True)
# hide_menu_style = """
# <style>
# #MainMenu {visibility: hidden; }
# footer {visibility: hidden;}
# </style>
# """
# -- hides footer watermark
hide_menu_style = """
<style>
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_menu_style, unsafe_allow_html=True)
# Dashboard Name
html_temp = """
<div style="background-color:tomato;padding:1.5px">
<h1 style="color:white;text-align:center;">⏭️Next Event Prediction Dashboard </h1>
</div><br>"""
st.markdown(html_temp, unsafe_allow_html=True)
# -- Control Panel Name
st.sidebar.title("🎛️ App Control Menu") # Adding the header to the sidebar as well as the sidebar
def catch_parameter(opt):
"""Change the captured parameters names"""
switch = {'-h': 'help', '-o': 'one_timestamp', '-a': 'activity',
'-f': 'file_name', '-i': 'imp', '-l': 'lstm_act',
'-d': 'dense_act', '-p': 'optim', '-n': 'norm_method',
'-m': 'model_type', '-z': 'n_size', '-y': 'l_size',
'-c': 'folder', '-b': 'model_file', '-x': 'is_single_exec',
'-t': 'max_trace_size', '-e': 'splits', '-g': 'sub_group',
'-v': 'variant', '-r': 'rep'}
try:
return switch[opt]
except:
raise Exception('Invalid option ' + opt)
def clear_cache():
st.caching.clear_cache()
session_state_key_list = ['multi_pred_ss', 'pos_tm_ss', 'initial_prediction',
'prediction_choice_idx', 'prediction_choice_name', 'history_of_choice',
'process_multi_pred_ss', 'process_tm_ss', 'initial_process_prediction']
_temp_sessionstate_keys = [*st.session_state]
for key in _temp_sessionstate_keys:
if key in session_state_key_list:
del st.session_state[key]
def main(argv):
parameters = dict()
column_names = {'Case ID': 'caseid',
'Activity': 'task',
'lifecycle:transition': 'event_type',
'Resource': 'user'}
if not argv:
parameters['one_timestamp'] = True # Only one timestamp
parameters['read_options'] = {
'timeformat': '%Y-%m-%dT%H:%M:%S.%f',
# 'timeformat': '%Y-%m-%dT%H:%M:%S',
'column_names': column_names,
'one_timestamp': parameters['one_timestamp'],
'ns_include': True}
# -- Navigate Between Different Views
navigate_page = st.sidebar.selectbox("🔗 Navigate", ["Home", "Training"], key="navigation_page",
on_change=clear_cache)
# if navigate_page == "About":
#
# st.write("View coming Up")
# ----------------------------------------------------------------------------------
# -- Dashboard Training Control-View -- #
# ----------------------------------------------------------------------------------
if navigate_page == "Training":
parameters['activity'] = 'training'
formt = st.sidebar.form(key="my_training_form")
role_threshold = formt.number_input("Role Similarity Threshold", min_value=0.50, max_value=0.95, value=0.85,
step=0.05)
batch_size_threshold = formt.number_input("Batch Size", min_value=16, max_value=512, value=32, step=16)
epochs_size_threshold = formt.number_input("Epochs", min_value=20, max_value=2000, value=200, step=10)
parameters['rp_sim'] = role_threshold # Default 0.85
parameters['batch_size'] = batch_size_threshold # Usually 16/32/64/128/256, 0 for Automatic Determine
parameters['epochs'] = epochs_size_threshold # 200, for embedded training it's 100.
if parameters['activity'] in ['training']:
# -- Event Log
train_file_name = formt.selectbox("Training File Name", ["Sepsis Dataset"], index=0)
if train_file_name == "Sepsis Dataset":
train_file_name = 'sepsis_cases.csv'
# -- Add more files
else:
st.write('Invalid file')
parameters['file_name'] = train_file_name
# -- Type of LSTM Model
train_model = formt.selectbox("Training Model",
['Fully Shared', 'Shared Categorical', 'Intercase Fully Shared',
'Intercase Shared Categorical'], index=2)
if train_model == 'Fully Shared':
parameters['model_type'] = 'concatenated_base'
elif train_model == 'Shared Categorical':
parameters['model_type'] = 'shared_cat_base'
elif train_model == 'Intercase Fully Shared':
parameters['model_type'] = 'concatenated_inter'
elif train_model == 'Intercase Shared Categorical':
parameters['model_type'] = 'shared_cat_inter'
# -- Hardware to use for training / keras lstm implementation 1 cpu,2 gpu
type_of_execution = formt.selectbox("Execute On", ["CPU", "GPU"], index=1)
if type_of_execution == "CPU":
parameters['imp'] = 1
elif type_of_execution == "GPU":
parameters['imp'] = 2
# -- Activation Function
lstm_activation = formt.selectbox("LSTM Activation Layer",
[None, "tanh", "selu", "relu", "sigmoid", "softmax"], index=1)
parameters['lstm_act'] = lstm_activation # optimization function Keras
dense_activation = formt.selectbox("Dense Layer", [None, 'sigmoid', 'linear', 'tanh', 'selu', 'relu'],
index=0)
parameters['dense_act'] = dense_activation
# -- Optimization Function
optimization_func = formt.selectbox("Optimization Function", ['Nadam', 'Adam', 'SGD', 'Adagrad'],
index=0)
parameters['optim'] = optimization_func # 'Nadam' # optimization function Keras
norm_method_opt = formt.selectbox("Normalization Method", ['lognorm', 'max', 'standard', 'normal'],
index=1)
# -- Normalization Method to be used for Time and some Intercase Features
parameters['norm_method'] = norm_method_opt
# -- N-Gram
n_gram_value = formt.number_input("N-Gram Size", min_value=5, max_value=30, value=10, step=5)
lstm_layer_size = formt.number_input("LSTM Layer Size", min_value=25, max_value=300, value=50, step=25)
parameters['n_size'] = n_gram_value
parameters['l_size'] = lstm_layer_size
else:
raise ValueError(parameters['activity'])
# Execution
train_button = formt.form_submit_button("Train Model")
if train_button:
if parameters['activity'] == 'training':
start = time.time()
trainer = tr.ModelTrainer(parameters)
print(trainer.output, trainer.model, sep=' ')
st.write("Trained Model Location : ", trainer.output)
end = time.time()
st.sidebar.write("Elapsed Time (in minutes) : ", (end - start) / 60)
st.success('Done')
# ----------------------------------------------------------------------------------
# -- Dashboard Home (Simulation) Control View -- #
# ----------------------------------------------------------------------------------
elif navigate_page == "Home":
parameters['activity'] = 'predict_next'
# --Hard Coded Value
parameters['is_single_exec'] = True
parameters['rep'] = 1
# --- Common Functions Used Across different sub-page
@st.cache(persist=True)
def _list2dictConvert(_a):
_it = iter(_a)
_res_dct = dict(zip(_it, _it))
return _res_dct
def common_batch_varient(actcount, rolecount):
with st.sidebar.expander('Variant'):
st.info("Select **Max Probability** for the most probable events,"
" **Multiple Prediction** for the prediction of the multiple events, "
"and **Random Prediction** for prediction of random recommendation of events")
variant_opt = st.selectbox("", (
'Max Probability', 'Multiple Prediction', 'Random Prediction',
'Multiple Random Prediction'),
key='variant_opt', on_change=clear_cache)
if variant_opt == 'Max Probability':
variant_opt = 'arg_max'
slider = 1
elif variant_opt == 'Random Prediction':
variant_opt = 'random_choice'
slider = 1
elif variant_opt in ['Multiple Prediction', 'Multiple Random Prediction']:
if variant_opt == 'Multiple Prediction':
variant_opt = 'multi_pred'
elif variant_opt == 'Multiple Random Prediction':
variant_opt = 'multi_pred_rand'
# variant_opt = 'multi_pred'
if actcount > rolecount:
_maxmulti = rolecount
elif rolecount > actcount:
_maxmulti = actcount
else:
_maxmulti = actcount
with st.sidebar.expander('Number of Predictions'):
st.info(
"**Multiple Predictions : ** minimum 2 predictions and maximum value based on slider")
slider = st.slider(
label='', min_value=2,
max_value=_maxmulti, key='my_number_prediction_slider_batch', on_change=clear_cache)
return variant_opt, slider
@st.cache(persist=True)
def read_next_testlog():
input_file = os.path.join('output_files', parameters['folder'], 'parameters', 'test_log.csv')
parameter_file = os.path.join('output_files', parameters['folder'], 'parameters',
'model_parameters.json')
filter_log = pd.read_csv(input_file, dtype={'user': str})
# Standard Code based on log_reader
filter_log = filter_log.rename(columns=column_names)
filter_log = filter_log.astype({'caseid': object})
filter_log = (
filter_log[(filter_log.task != 'Start') & (filter_log.task != 'End')].reset_index(drop=True))
filter_log_columns = filter_log.columns
with open(parameter_file) as pfile:
parameter_data = json.load(pfile)
file_name = parameter_data["file_name"]
pfile.close()
return filter_log, filter_log_columns, file_name
def next_columns(filter_log, display_columns, index=None, flag=None):
# Dashboard selection of Case ID
if index == None:
filter_caseid = st.selectbox("🆔 Select Case ID", filter_log["caseid"].unique(),
key="caseid_select", on_change=clear_cache)
filter_caseid_index = filter_log["caseid"].unique().tolist().index(filter_caseid)
else:
if flag != None:
filter_caseid = st.selectbox("🆔 Select Case ID", filter_log["caseid"].unique()[index],
key="caseid_select", on_change=clear_cache)
filter_caseid_index = filter_log["caseid"].unique().tolist().index(filter_caseid)
flag = None
filter_caseid_attr_df = filter_log.loc[filter_log["caseid"].isin([filter_caseid])]
filter_attr_display = filter_caseid_attr_df[display_columns]
return filter_attr_display, filter_caseid, filter_caseid_attr_df, filter_caseid_index
def num_predictions(_df):
if _df['task'].nunique() > _df['role'].nunique():
_dfmax = _df['role'].nunique()
elif _df['role'].nunique() > _df['task'].nunique():
_dfmax = _df['task'].nunique()
else:
_dfmax = _df['task'].nunique()
with st.sidebar.expander('Variant'):
st.info("Select the slider value **equal to 1** for **Max Probability** "
"otherwise choose the number **greater than 1** for "
"**Multiple Predictions** sorted in decreasing order based on probability of it's ocurrance")
slider = st.slider(
label='', min_value=1,
max_value=_dfmax, key='my_number_prediction_slider', on_change=clear_cache)
return slider
def label_identifier(file_name):
# --- Logic to be added for other dataset
with st.sidebar.expander('Label Checker'):
if "sepsis" in file_name:
st.info("Select the respective labeling mode for Sepsis Patient Condition")
label_indicator = ["Returns to Emergency Room", "Admitted to Intensive Care",
"Discharged for other Reason"]
_labelsel = st.radio("Labelling Indicator", label_indicator, key="radio_select_label",
on_change=clear_cache)
if _labelsel == "Returns to Emergency Room":
parameters['label_activity'] = "Return ER"
elif _labelsel == "Admitted to Intensive Care":
parameters['label_activity'] = "Admission IC"
elif _labelsel == "Discharged for other Reason":
parameters['label_activity'] = "Release A"
parameters['label_check_event'] = st.number_input("Check after number of events",
min_value=1, max_value=25, value=5,
# These are estimate numbers
step=1, key="radio_number_label",
on_change=clear_cache)
else:
st.error("Label Logic for dataset is not defined")
# ----------------------------------------------------
# Folder picker / provide name from output_files
if parameters['activity'] in ['predict_next']:
with st.sidebar.expander('Folder Name'):
st.info("Provide the folder for which the prediction has to be simulated")
_folder_name = st.text_input('', key="folder_name", on_change=clear_cache)
parameters['folder'] = _folder_name
if parameters['folder'] != "":
# --Selecting Model
_model_directory = os.path.join('output_files', _folder_name)
_model_name = []
for file in os.listdir(_model_directory):
# check the files which are end with specific extention
if file.endswith(".h5"):
_model_name.append(file)
parameters['model_file'] = _model_name[-1]
# --Selecting Mode
with st.sidebar.expander('Type of Prediction Processing Mode'):
st.info("Select **Batch Processing** for the prediction of eintire log file,"
" **Single Processing** to simulate the prediction for each Case Id individually")
# next_option = st.sidebar.radio('', ['Execution Mode', 'Evaluation Mode'])
# st.sidebar.subheader("Choose Mode of Prediction")
_mode_sel = st.radio('Processing', ['Batch Processing', 'Single Event Processing'],
key="processing_type", on_change=clear_cache)
# st.sidebar.markdown("""---""")
if _mode_sel == 'Batch Processing':
_mode_sel = 'batch'
elif _mode_sel == 'Single Event Processing':
_mode_sel = 'next'
parameters['mode'] = _mode_sel
# if parameters['folder'] != "":
if parameters['activity'] in ['predict_next']:
# ----------------------------------------------------------------------------------
# -- Dashboard Simulation Single Mode Control View -- #
# ----------------------------------------------------------------------------------
if parameters['mode'] in ['next']:
# ---batch default values
parameters['batch_mode'] = ''
parameters['batchprefixnum'] = ''
parameters['batchpredchoice'] = ''
parameters['batchlogrange'] = ''
# Saves the result in the URL in the Next mode
app_state = st.experimental_get_query_params()
if "my_saved_result" in app_state:
saved_result = app_state["my_saved_result"][0]
nxt_button_idx = int(saved_result)
# st.write("Here is your result", saved_result)
else:
# st.write("No result to display, compute a value first.")
nxt_button_idx = 0
# next_option = st.sidebar.selectbox("Type of Single Event Processing", ('Prediction of Next Event', 'Prediction of Next Events with Suffix'), key='next_dropdown_opt')
with st.sidebar.expander('Type of Single Event Processing'):
st.info(
"Select **Execution Mode** for simulating the dashboard for the Users, **Evaluation Mode** to judge the trustworthiness of the ML model prediction")
next_option = st.radio('', ['Execution Mode', 'Evaluation Mode', 'What-If Mode'],
key="single_event_processing", on_change=clear_cache)
# st.sidebar.markdown("""---""")
if next_option == 'Execution Mode':
next_option = 'history_with_next'
elif next_option == 'Evaluation Mode':
next_option = 'next_action'
elif next_option == 'What-If Mode':
next_option = 'what_if'
# Read the Test Log
filter_log, filter_log_columns, file_name = read_next_testlog()
essential_columns = ['task', 'role', 'end_timestamp']
norm_cols = [col for col in filter_log.columns if '_norm' in col]
index_cols = [col for col in filter_log.columns if '_index' in col]
ord_cols = [col for col in filter_log.columns if '_ord' in col]
ohe_cols = [col for col in filter_log.columns if '_ohe' in col]
extra_columns = ['caseid', 'label', 'dur', 'acc_cycle', 'daytime', 'user',
# 'dur_norm', 'ac_index', 'rl_index', 'label_index',
# 'wait_norm', 'open_cases_norm', 'daytime_norm',
'acc_cycle'] # Add the Columns here which you don't want to display
display_columns = list(set(filter_log_columns) - set(
essential_columns + extra_columns + norm_cols + index_cols + ord_cols + ohe_cols))
filter_attr_display, filter_caseid, filter_caseid_attr_df, filter_caseid_index = next_columns(
filter_log, display_columns)
parameters['nextcaseid'] = filter_caseid
st.subheader('🔦 State of the Process')
statecols, _, buttoncols = st.columns([2, 0.25, 0.2])
with statecols:
state_of_theprocess = st.empty()
with buttoncols:
button_of_theprocess = st.empty()
parameters['multiprednum'] = num_predictions(filter_caseid_attr_df)
if parameters['multiprednum'] == 1:
variant_opt = 'arg_max'
elif parameters['multiprednum'] > 1:
variant_opt = 'multi_pred'
parameters['variant'] = variant_opt
parameters['next_mode'] = next_option
filter_caseid_attr_df = filter_caseid_attr_df[essential_columns].values.tolist()
# ---Logic to select the label
label_identifier(file_name)
# ----------------------------------------------------------------------------------
# -- Dashboard Simulation Evaluation Sub-Control View -- #
# ----------------------------------------------------------------------------------
if next_option == 'next_action':
# with st.sidebar.expander('Choose Prefix Source'):
# st.info("Select **SME** for simulating the input to prediction using the log, "
# "**Prediction** to use the respective prediction as the input for subsequent prediction")
# parameters['nextactpredchoice'] = st.radio('', ['SME', 'Prediction'], key="radio_select_pred_next_act", on_change=clear_cache)
filter_caseid_attr_list = st.select_slider("Choose [Activity, User, Time]",
options=filter_caseid_attr_df,
key="caseid_attr_slider",
on_change=clear_cache)
_idx = filter_caseid_attr_df.index(filter_caseid_attr_list)
filter_caseid_attr_list.append(_idx)
# Selected suffix key, Converting list to dictionary
filter_key_attr = ["filter_acitivity", "filter_role", "filter_time", "filter_index"]
filter_key_pos = [0, 1, 2, 3]
assert (len(filter_key_attr) == len(filter_key_pos))
_acc_val = 0
for i in range(len(filter_key_attr)):
filter_caseid_attr_list.insert(filter_key_pos[i] + _acc_val, filter_key_attr[i])
_acc_val += 1
filter_caseid_attr_dict = _list2dictConvert(filter_caseid_attr_list)
# print("Value of Slider :", filter_caseid_attr_dict)
# Passing the respective paramter to Parameters
parameters['nextcaseid_attr'] = filter_caseid_attr_dict
if (_idx + 1) < len(
filter_caseid_attr_df): # Index starts from 0 so added 1 to equate with length value
_filterdf = filter_attr_display.iloc[[_idx]]
_filterdf.index = [""] * len(_filterdf)
state_of_theprocess.dataframe(_filterdf)
st.sidebar.markdown("""---""")
# if st.sidebar.button("Process", key='next_process'):
if button_of_theprocess.button("Process", key='next_process'):
with st.spinner(text='In progress'):
predictor = pr.ModelPredictor(parameters)
# print("predictor : ", predictor.acc)
st.success('Done')
else:
st.error('Reselect the Suffix to a lower Value')
# ----------------------------------------------------------------------------------
# -- Dashboard Simulation Execution Sub-Control View -- #
# ----------------------------------------------------------------------------------
elif next_option == 'history_with_next':
st.experimental_set_query_params(my_saved_caseid=filter_caseid)
st.sidebar.markdown("""---""")
# next_button = st.sidebar.button("Process", key='next_process_action')
next_button = button_of_theprocess.button("Process", key='next_process_action')
# _filterdf = filter_attr_display.iloc[[nxt_button_idx]]
# _filterdf.index = [""] * len(_filterdf)
# state_of_theprocess.dataframe(_filterdf)
# if (next_button) and ((nxt_button_idx) < len(filter_caseid_attr_df) + 1):
parameters['execution_trace_size'] = len(filter_caseid_attr_df)
if (next_button) and ((nxt_button_idx) < len(filter_caseid_attr_df)):
_filterdf = filter_attr_display.iloc[[nxt_button_idx]]
_filterdf.index = [""] * len(_filterdf)
state_of_theprocess.dataframe(_filterdf)
nxt_button_idx += 1
st.experimental_set_query_params(my_saved_result=nxt_button_idx,
my_saved_caseid=filter_caseid) # Save value
filter_caseid_attr_list = [nxt_button_idx - 1]
filter_key_attr = ["filter_index"]
filter_key_pos = [0]
assert (len(filter_key_attr) == len(filter_key_pos))
_acc_val = 0
for i in range(len(filter_key_attr)):
filter_caseid_attr_list.insert(filter_key_pos[i] + _acc_val, filter_key_attr[i])
_acc_val += 1
filter_caseid_attr_dict = _list2dictConvert(filter_caseid_attr_list)
parameters['nextcaseid_attr'] = filter_caseid_attr_dict
with st.spinner(text='In progress'):
predictor = pr.ModelPredictor(parameters)
# print("predictor : ", predictor.acc)
st.success('Done')
if (nxt_button_idx) >= len(filter_caseid_attr_df) + 1:
# next_button.enabled = False
st.experimental_set_query_params(my_saved_result=0)
# ---Adding logic to skip to next caseid (Not Working)
# filter_caseid_index = filter_caseid_index + 1
# next_case_id_flag = 1
# filter_attr_display, filter_caseid, filter_caseid_attr_df, filter_caseid_index = next_columns(filter_log, display_columns, filter_caseid_index, next_case_id_flag)
st.error('End of Current Case Id, Select the Next Case ID')
# elif ((nxt_button_idx) >= len(filter_caseid_attr_df) + 1):
elif ((nxt_button_idx) >= len(filter_caseid_attr_df)):
st.experimental_set_query_params(my_saved_result=0) # reset value
st.error('End of Current Case Id, Select the Next Case ID')
# ----------------------------------------------------------------------------------
# -- Dashboard Simulation What-If Sub-Control View -- #
# ----------------------------------------------------------------------------------
elif next_option == 'what_if':
form = st.form(key="my_whatif_form")
form.subheader("What-IF Prediction Choose Box")
# # --------------------------------------------------------------------------
# Creating prediction selection radio button
choose_pred_lst = ['SME']
for _dx in range(parameters['multiprednum']):
_dx += 1
choose_pred_lst.append("Prediction " + str(_dx))
st.write('<style>div.row-widget.stRadio > div{flex-direction:row;}</style>',
unsafe_allow_html=True)
# # --------------------------------------------------------------------------
# with st.expander('Choice of Prediction'):
form.info(
"Choose the Prediction according to which System generates the next prediction, "
"**SME** (Subject Matter Expert): decision solely based on users instinct and knowledge of business process about the process, "
"**Prediction n** : decision solely based on the respective ranked confidence of the process")
wcols1, _, wcols2 = form.columns(
[2, 0.25, 0.2]) # --To add prediction radio button and process button in a line
parameters['predchoice'] = wcols1.radio('', choose_pred_lst, key="radio_select_whatif")
# # --------------------------------------------------------------------------
st.experimental_set_query_params(my_saved_caseid=filter_caseid)
st.sidebar.markdown("""---""")
next_button = wcols2.form_submit_button("Process")
# _filterdf = filter_attr_display.iloc[[nxt_button_idx]]
# _filterdf.index = [""] * len(_filterdf)
# state_of_theprocess.dataframe(_filterdf)
# # print("Prediction Choice : ", parameters['predchoice'])
if (next_button) and ((nxt_button_idx) < len(filter_caseid_attr_df)):
_filterdf = filter_attr_display.iloc[[nxt_button_idx]]
_filterdf.index = [""] * len(_filterdf)
state_of_theprocess.dataframe(_filterdf)
# print("Prediction Choice : ", parameters['predchoice'])
nxt_button_idx += 1
st.experimental_set_query_params(my_saved_result=nxt_button_idx,
my_saved_caseid=filter_caseid) # Save value
filter_caseid_attr_list = [nxt_button_idx - 1]
filter_key_attr = ["filter_index"]
filter_key_pos = [0]
assert (len(filter_key_attr) == len(filter_key_pos))
_acc_val = 0
for i in range(len(filter_key_attr)):
filter_caseid_attr_list.insert(filter_key_pos[i] + _acc_val, filter_key_attr[i])
_acc_val += 1
filter_caseid_attr_dict = _list2dictConvert(filter_caseid_attr_list)
parameters['nextcaseid_attr'] = filter_caseid_attr_dict
with st.spinner(text='In progress'):
predictor = pr.ModelPredictor(parameters)
# print("predictor : ", predictor.acc)
st.success('Done')
if (nxt_button_idx) >= len(filter_caseid_attr_df) + 1:
# next_button.enabled = False
st.experimental_set_query_params(my_saved_result=0)
st.error('End of Current Case Id, Select the Next Case ID')
elif ((nxt_button_idx) >= len(filter_caseid_attr_df)):
st.experimental_set_query_params(my_saved_result=0) # reset value
st.error('End of Current Case Id, Select the Next Case ID')
# ----------------------------------------------------------------------------------
# -- Dashboard Simulation Batch Mode Control View -- #
# ----------------------------------------------------------------------------------
elif parameters['mode'] in ['batch']:
_log, _, _ = read_next_testlog()
rolecount = min(_log.groupby('caseid')['role'].nunique())
actcount = min(_log.groupby('caseid')['task'].nunique())
# logic to count the max number of suffix which can be used for the eintire log
_count = _log['caseid'].value_counts().to_frame()
_count.reset_index(level=0, inplace=True)
_count.sort_values(by=['caseid'], inplace=True)
_log_event_select = list(set(_count['caseid'].values.tolist()))
# _count = (_count['caseid'].iloc[:1].values.tolist()[0]) - 1
with st.sidebar.expander('Select Range of Number of Events'):
st.info("Select the range of Number of events need to be evaluated")
range_event_number = st.slider('', int(min(_log_event_select)),
int(max(_log_event_select)),
(int(min(_log_event_select)),
int(max(_log_event_select))),
key="range_of_event_number", on_change=clear_cache)
# range_event_number = st.select_slider('', min(_log_event_select), max(_log_event_select),
# _log_event_select, key="range_of_event_number", on_change=clear_cache)
_log_check = _log.groupby("caseid").filter(
lambda x: len(x) >= min(range_event_number) and len(x) <= max(range_event_number))
if _log_check.empty is True:
st.error("The Range of Event Number doesn't have any Case Id's")
raise ValueError(range_event_number)
else:
parameters['batchlogrange'] = range_event_number
with st.sidebar.expander('Type of Batch Event Processing'):
st.info(
"Select **Execution Mode** for simulating the dashboard for the Users, **Evaluation Mode** to judge the trustworthiness of the ML model prediction")
batch_option = st.radio('', ['Base Mode', 'Pre-Select Prefix Mode'],
key="batch_event_processing", on_change=clear_cache)
if batch_option == 'Base Mode':
batch_option = 'base_batch'
elif batch_option == 'Pre-Select Prefix Mode':
batch_option = 'pre_prefix'
if batch_option == 'base_batch':
variant_opt, slider = common_batch_varient(actcount, rolecount)
parameters['batchpredchoice'] = ''
parameters['batchprefixnum'] = ''
elif batch_option == 'pre_prefix':
with st.sidebar.expander('Choose Prefix Source'):
st.info("Select **SME** for simulating the input to prediction using the log, "
"**Prediction** to use the respective prediction as the input for subsequent prediction, "
"**Generative** to generate the model predictions")
parameters['batchpredchoice'] = st.radio('', ['SME', 'Prediction', 'Generative'],
key="radio_select_pred_batch",
on_change=clear_cache)
with st.sidebar.expander('Select the Number of Prefix'):
st.info("Prefix for each caseid in the batch mode")
# -- Dynamic Slider based on number of event number range
prefix_slider = st.slider(
label='', min_value=0,
max_value=(min(range_event_number) - 1), value=0, step=1,
key='my_number_prefix_slider_batch', on_change=clear_cache)
# -- Dynamic Slider Logic based on entire event log : Issue with automatic update of prefix
# prefix_slider = st.select_slider(
# label='', options=list(range(min(range_event_number))),
# value=min(list(range(min(range_event_number)))),
# key='my_number_prefix_select_slider_batch', on_change=clear_cache)
parameters['batchprefixnum'] = prefix_slider
variant_opt, slider = common_batch_varient(actcount, rolecount)
parameters['multiprednum'] = slider #
parameters[
'variant'] = variant_opt # random_choice, arg_max for variants and repetitions to be tested
parameters['batch_mode'] = batch_option
parameters['next_mode'] = ''
parameters['predchoice'] = ''
st.sidebar.markdown("""---""")
if st.sidebar.button("Process", key='batch_process'):
start = time.time()
with st.spinner(text='In progress'):
predictor = pr.ModelPredictor(parameters)
end = time.time()
st.sidebar.write("Elapsed Time (in minutes) : ", (end - start) / 60)
st.success('Done')
else:
# Catch parameters
try:
opts, _ = getopt.getopt(
argv,
"ho:a:f:i:l:d:p:n:m:z:y:c:b:x:t:e:v:r:",
['one_timestamp=', 'activity=',
'file_name=', 'imp=', 'lstm_act=',
'dense_act=', 'optim=', 'norm_method=',
'model_type=', 'n_size=', 'l_size=',
'folder=', 'model_file=', 'is_single_exec=',
'max_trace_size=', 'splits=', 'sub_group=',
'variant=', 'rep='])
for opt, arg in opts:
key = catch_parameter(opt)
if arg in ['None', 'none']:
parameters[key] = None
elif key in ['is_single_exec', 'one_timestamp']:
parameters[key] = arg in ['True', 'true', 1]
elif key in ['imp', 'n_size', 'l_size',
'max_trace_size', 'splits', 'rep']:
parameters[key] = int(arg)
else:
parameters[key] = arg
parameters['read_options'] = {'timeformat': '%Y-%m-%dT%H:%M:%S.%f',
'column_names': column_names,
'one_timestamp':
parameters['one_timestamp'],
'ns_include': True}
except getopt.GetoptError:
print('Invalid option')
sys.exit(2)
if __name__ == "__main__":
main(sys.argv[1:])
# streamlit run dashboard.py