-
Notifications
You must be signed in to change notification settings - Fork 1
/
streamlit_app_asdm.py
57 lines (42 loc) · 2.11 KB
/
streamlit_app_asdm.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
import pandas as pd
import streamlit as st
import plotly.graph_objects as go
from ASDM.ASDM import Structure
def load_model(model_path):
try:
model = Structure(from_xmile=model_path)
except FileNotFoundError:
st.error(f"File {model_path} not found.")
return None
return model
def run_simulation(model, simulation_time, re_investment):
model.sim_specs['initial_time'] = 0
model.sim_specs['current_time'] = 0
model.sim_specs['dt'] = 1
model.sim_specs['simulation_time'] = simulation_time
model.sim_specs['time_units'] = 'Months'
model.clear_last_run()
model.replace_element_equation('Percentage_of_savings_spent_on_cessation', re_investment)
model.simulate()
results = model.export_simulation_result()
results_df = pd.DataFrame.from_dict(results)
columns_to_plot = ["Current_smokers", "Ex_smokers", "Ex_smokers_starting_again"]
return results_df['Months'], results_df[columns_to_plot]
st.title('Smoking Cessation')
st.markdown("""
This simulation estimates the effects of various reinvestment levels in a smoking cessation service within a population of 900 smokers.
By varying the proportion of savings that are reinvested into the service, we can observe different outcomes in terms of current smokers, ex-smokers,
and ex-smokers who start smoking again over time.
""")
st.subheader('Slide the Slider to Vary Re-Investment Levels')
model = load_model('models/smoking cessation demo.stmx')
if model is not None:
re_investment = st.slider("Proportion of Savings Spent on Cessation", 0, 100, 45)
simulation_time = st.slider("Select the number of months to simulate:", min_value=1, max_value=36, value=24)
x_values, y_values = run_simulation(model, simulation_time, re_investment)
st.subheader('Effects of Re-Investment on Smoking Levels')
fig = go.Figure()
for column in y_values.columns:
fig.add_trace(go.Scatter(x=x_values, y=y_values[column], mode='lines', name=column))
fig.update_layout(xaxis_title='Months', yaxis_title='Number of Smokers', autosize=False, width=800, height=500)
st.plotly_chart(fig)