-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmm_dashboard.py
152 lines (131 loc) · 5.4 KB
/
smm_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
"""Heart Failure Virtual Clinic - Simple Markov Modelling Dashboard.
@author: T.D. Medina
"""
import dash_bootstrap_components as dbc
import pandas as pd
from dash import Dash, html, dash_table, Output, Input, dcc
from hfvc import processing, modelling
patient_db = processing.main()
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.title = "HFVC Markov Modelling"
app.layout = html.Div(children=[
dbc.NavbarSimple(
brand="HFVC Markov Modelling",
brand_href="#",
color="primary",
dark=True,
),
# dcc.Slider(id="n_state_slider", min=2, max=10, step=1, value=5),
dbc.Accordion(always_open=True, children=[
dbc.AccordionItem(title="Parameters", children=[
dbc.Row(align="top", justify="start", children=[
dbc.Col(width="auto", children=[
html.Label(dcc.Markdown("**Interval:**"))
]),
dbc.Col(width="auto", children=[
dcc.RadioItems(id="interval_buttons", options=["day", "month", "year"],
labelStyle={"display": "inline-block", "margin-right": "10px"},
value="month")
])
]),
]),
dbc.AccordionItem(title="Models by Patient Type", children=[
html.H2("Models by Patient Type"),
dcc.Loading(children=html.Div(id="individual_model_tables"))
]),
dbc.AccordionItem(title="Compare Patient Type vs. Others", children=[
html.H2("Compare Patient Type vs. Others"),
dcc.Dropdown(list({patient.type for patient in patient_db}),
id="patient_type_dropdown",
placeholder="Select patient type"),
dcc.Loading(children=html.Div(id="comparison_model_tables"))
])
]),
])
def make_sd_table(sd_array):
size = sd_array.shape[0]
sd_table = pd.DataFrame(data=(sd_array*100).round(1).reshape((1, size)),
columns=[f"S{i}" for i in range(1, size+1)],
)
table = dash_table.DataTable(
data=sd_table.to_dict("records"),
style_header={"fontWeight": "bold",
"backgroundColor": "#b0c4de",
"textAlign": "center"}
)
return table
def make_tpm_table(tpm_array):
size = tpm_array.shape[0]
cols = [f"S{i}" for i in range(1, size+1)]
tpm_table = pd.DataFrame(data=(tpm_array*100).round(1),
columns=cols,
)
tpm_table[""] = cols
table = dash_table.DataTable(
data=tpm_table.to_dict("records"),
columns=[dict(name=x, id=x) for x in [""] + cols],
style_header={"fontWeight": "bold",
"backgroundColor": "#b0c4de",
"textAlign": "center"},
style_cell_conditional=[{"if": {"column_id": ""},
"fontWeight": "bold",
"backgroundColor": "#b0c4de"}],
)
return table
def make_model_tables(model, name):
if not name:
name = "None"
shadow_style = "0px 0px 5px 2px rgba(0, 0, 0, 0.2)"
sd_table = make_sd_table(model[0])
tpm_table = make_tpm_table(model[1])
# table_layout = dbc.Col(children=[
# html.Label(name),
# sd_table,
# html.Br(),
# tpm_table
# ])
table_layout = dbc.Col(children=[
dbc.Card(style={"margin": "10px", "box-shadow": shadow_style}, children=[
dbc.CardHeader(html.B(name)),
dbc.CardBody(children=[
html.Label(html.B("Initial Distribution")),
sd_table,
html.Br(),
html.Label(html.B("Transition Probabilities")),
tpm_table
]),
])
])
return table_layout
def make_model_table_layout(patient_database, n_states, interval):
models = modelling.make_all_models(patient_database, n_states, interval)
row_children = [make_model_tables(model, name) for name, model in models.items()]
row = dbc.Row(children=row_children)
return row
def make_comparison_model_layout(patient_database, patient_type, n_states, interval):
models = modelling.smm_patient_type_against_others(patient_database, patient_type,
n_states, interval)
row_children = [make_model_tables(model, name) for name, model in models.items()]
row = dbc.Row(children=row_children)
return row
@app.callback(
Output("individual_model_tables", "children"),
# Input("n_state_slider", "value"),
Input("interval_buttons", "value")
)
# def make_individual_tables(n_states, interval):
# individual_tables = make_model_table_layout(patient_db, n_states, interval)
def make_individual_tables(interval):
individual_tables = make_model_table_layout(patient_db, 5, interval)
return individual_tables
@app.callback(
Output("comparison_model_tables", "children"),
Input("patient_type_dropdown", "value"),
Input("interval_buttons", "value")
)
def make_comparison_tables(patient_type, interval):
comparison_tables = make_comparison_model_layout(patient_db, patient_type,
5, interval)
return comparison_tables
if __name__ == "__main__":
app.run_server(debug=True)