-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
163 lines (149 loc) · 4.8 KB
/
app.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
from pathlib import Path
import numpy as np
import pandas as pd
import pyrsm as rsm
import io
from contextlib import redirect_stdout
import sys
from shiny import App, render, ui, reactive, Inputs, Outputs, Session
from shiny.types import FileInfo
app_ui = ui.page_fluid(
ui.layout_sidebar(
ui.panel_sidebar(
ui.panel_well(
ui.input_file(
id="logistic_data",
label="Upload a Pickle file",
accept=[".pkl"],
multiple=False,
),
),
ui.panel_conditional(
"input.tabs_logistic == 'Summary'",
ui.panel_well(
ui.input_action_button(
"run", "Estimate model", class_="btn-success", width="100%"
),
),
ui.panel_well(
ui.output_ui("ui_rvar"),
ui.output_ui("ui_evar"),
width=3,
),
),
ui.panel_conditional(
"input.tabs_logistic == 'Plot'",
ui.panel_well(
ui.input_action_button(
"plot", "Create plot", class_="btn-success", width="100%"
),
),
ui.panel_well(
ui.input_select(
id="logistic_plots",
label="Plots",
selected=None,
choices={
"or": "OR plot",
"pred": "Prediction plot",
"vimp": "Permutation importance",
},
),
width=3,
),
),
),
ui.panel_main(
ui.navset_tab_card(
ui.nav("Data", ui.output_ui("show_data")),
ui.nav("Summary", ui.output_text_verbatim("logistic_summary")),
ui.nav("Plot", ui.output_plot("logistic_plot")),
id="tabs_logistic",
)
),
),
)
def server(input: Inputs, output: Outputs, session: Session):
@reactive.Calc
def load_data():
if input.logistic_data() is None:
return "Please upload a Pickle file"
f: list[FileInfo] = input.logistic_data()
return pd.read_pickle(f[0]["datapath"])
@output(id="show_data")
@render.ui
def show_data():
if input.logistic_data() is None:
return "Please upload a Pickle file"
else:
return ui.HTML(
load_data()
.head()
.to_html(classes="table table-striped data_preview", index=False)
)
@output(id="ui_rvar")
@render.ui
def ui_rvar():
if isinstance(load_data(), str):
df_cols = []
else:
df_cols = list(load_data().columns)
return ui.input_select(
id="rvar",
label="Response Variable",
selected=None,
choices=df_cols,
)
@output(id="ui_evar")
@render.ui
def ui_evar():
if isinstance(load_data(), str):
df_cols = []
else:
df_cols = list(load_data().columns)
if (input.rvar() is not None) and (input.rvar() in df_cols):
df_cols.remove(input.rvar())
return ui.input_select(
id="evar",
label="Explanatory Variables",
selected=None,
choices=df_cols,
multiple=True,
selectize=False,
)
@reactive.Calc
def logistic_regression():
return rsm.logistic(
dataset=load_data(), rvar=input.rvar(), evar=list(input.evar())
)
@output(id="logistic_summary")
@render.text
@reactive.event(input.run, ignore_none=True)
def logistic_summary():
out = io.StringIO()
with redirect_stdout(out):
logistic_regression().summary()
return out.getvalue()
@output(id="logistic_plot")
@render.plot()
@reactive.event(input.plot, ignore_none=True)
def logistic_plot():
return logistic_regression().plot(plots=input.logistic_plots())
@output(id="code_snippet")
@render.text
@reactive.event(input.copy_to_clipboard, ignore_none=False)
def code_snippet():
"""Generate Python code for logistic regression using the 'pyrsm' package."""
rvar = input.rvar
evar = input.evar
cmd = f"""import pyrsm as rsm
lr = rsm.logistic_(dataset='{df_name}', rvar='{rvar}', evars={evar})
lr.summary()
"""
try:
return exec(cmd)
except Exception as e:
return f"Error: {str(e)}"
# pyperclip.copy(cmd)
return cmd
app = App(app_ui, server)