-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstream.py
62 lines (54 loc) · 2.14 KB
/
stream.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
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 1 20:56:07 2019
@author: kees.brekelmans
"""
import sys; sys.executable
import streamlit as st
import pandas as pd
import numpy as np
import pickle
import time
import monitor
import explore
import diagnose
import support
import shap
import shap2
import os
#define relative path
path = os.path.dirname(os.path.abspath(__file__))
data = support.load_data()
lower_model, model, upper_model = support.load_gb_model()
#get SHAP generator by passing the predictive model
explainer = shap.TreeExplainer(model)
#select box to switch between modes
interface = st.sidebar.selectbox("Choose mode:", ["monitor", "what-if"])
#from monitor mode, one can go to diagnose mode. To keep track of whether the user selected to go to or exit diagnose mode, pickles have to be generated and read, since streamlit runs the whole script from the beginning and all local variables are lost.
if interface == "monitor":
title = st.empty()
diagnose_mode = st.button("Go to diagnose mode")
with open(path + "/diagnose_mode", 'rb') as handle:
diagnose_mode2 = pickle.load(handle)
if diagnose_mode == True or diagnose_mode2 == True:
title.title("Diagnose mode")
with open(path + "/diagnose_mode", 'wb') as handle:
pickle.dump(True, handle)
diagnose_exit = st.button("Ready with diagnose?")
#the if-else below causes the program to require you to press "Ready with diagnose?" twice.
if diagnose_exit:
with open(path + "/diagnose_mode", 'wb') as handle:
pickle.dump(False, handle)
else:
diagnose.run(data, upper_model, model, lower_model, explainer)
else:
title.title("Monitor mode")
monitor.run(data, upper_model, model, lower_model, explainer)
diagnose_start = st.button("Diagnose mode")
if diagnose_start:
with open(path + "/diagnose_mode", 'wb') as handle:
pickle.dump(True, handle)
#go to what-if mode. Code can be found in explore.py
if interface == "what-if":
title = st.title("What-if mode")
explore.run(data, upper_model, model, lower_model, explainer)