Treatment sim streamlit app

Treatment sim streamlit app#

In this exercise you will incrementally build up a simple streamlit app for the treat_sim package. The app will allow a user to set input parameters using sliders, run the simulation model and view a results summary.

It is recommended that you use basic treat_sim script that has already been provided.

Exercise 1#

Create a a basic streamlit app with a title and information.

  • Create a new file called my_streamlit_app.py; copy and paste in the code from main.py

  • Begin to convert this into a streamlit app

  • Import streamlit

  • Add a streamlit title (e.g. “Treatment Simulation model”)

  • Read in and display the markdown text from resources/overview.md Or create the file yourself. Use the following text:

**A simple simulation model of a urgent care and treatment centre.**
* Change the model parameters and rerun to see the effect on waiting times and utilisation of rooms.
  • Usest.spinner and st.success to provide feedback about the model.

  • Run the app as follows streamlit run my_streamlit_app.py

Basic treat_sim script#

'''
Script to complete a basic run of the model and display a table of
results.

The model is imported from a pypi package 'treat_sim'

Full documentation and source code for `treat_sim` is available as 
* Jupyter Book: https://tommonks.github.io/treatment-centre-sim/
* github: https://github.com/TomMonks/treatment-centre-sim

A conda environment has been provided locally,but the model can be pip installed
`pip install treat_sim==1.0.0`

This will be adapted into a basic streamlit app
'''
from treat_sim.model import Scenario, multiple_replications

# set the variables for the run.
# these are just a subset of the total available for this example...
n_triage = 1
n_exam = 3
n_cubicles_1 = 1

# examination mean
exam_mean = 16.0

# runs
replications = 10

# Setup scenario using supplied variables
args = Scenario(n_triage=n_triage, n_exam=n_exam, n_cubicles_1=n_cubicles_1,
                exam_mean=exam_mean)

# run multiple replications of the model.
results = multiple_replications(args, n_reps=replications)

# print mean results.
print(results.mean().round(1))
00_arrivals                       223.9
01a_triage_wait                    35.3
01b_triage_util                     0.6
02a_registration_wait             107.0
02b_registration_util               0.8
03a_examination_wait               27.5
03b_examination_util                0.8
04a_treatment_wait(non_trauma)    141.6
04b_treatment_util(non_trauma)      0.9
05_total_time(non-trauma)         240.2
06a_trauma_wait                   100.4
06b_trauma_util                     0.8
07a_treatment_wait(trauma)        199.9
07b_treatment_util(trauma)          0.9
08_total_time(trauma)             418.1
09_throughput                     151.5
dtype: float64

Exercise 2:#

Update parameters and run the simulation model via the app

  • Convert the hard coded parameter values into streamlit sliders.

  • Set the script up to that streamlit runs the simulation only when a Run Simulation button is clicked.

  • Using st.table or st.dataframe display the results of the simulation model to a user.

'''
Script to complete a basic run of the model and display a table of
results in a streamlit app.

The model is imported from a pypi package 'treat_sim'

Full documentation and source code for `treat_sim` is available as 
* Jupyter Book: https://tommonks.github.io/treatment-centre-sim/
* github: https://github.com/TomMonks/treatment-centre-sim

A conda environment has been provided locally,but the model can be pip installed
`pip install treat_sim==1.0.0`
'''
from treat_sim.model import Scenario, multiple_replications
import streamlit as st

################################################################################
# MODIFICATION 1: Read in markdown and display
INTRO_FILE = 'resources/overview.md'

def read_file_contents(file_name):
    ''''
    Read the contents of a file.

    Params:
    ------
    file_name: str
        Path to file.

    Returns:
    -------
    str
    '''
    with open(file_name) as f:
        return f.read()

# give the page a title
st.title('Treatment Centre Simulation Model')

# show the introductory markdown
st.markdown(read_file_contents(INTRO_FILE))
################################################################################

################################################################################
# MODIFICATION 2: set the variables for the run using sliders
# these are just a subset of the total available for this example...
n_triage = st.slider('Triage bays', 1, 5, 1)
n_exam = st.slider('Exam rooms', 1, 5, 3)
n_cubicles_1 = st.slider('Non-Trauma Treatment cubicles', 1, 5, 1, 
                         help='Set the number of non trauma pathway '
                         + 'treatment cubicles')

# examination mean
exam_mean = st.slider('Mean examination time', 10.0, 45.0, 
                       16.0, 1.0)

# runs
replications = st.slider('No. replications', 1, 50, 10)
################################################################################

# Setup scenario using supplied variables
args = Scenario(n_triage=n_triage, n_exam=n_exam, n_cubicles_1=n_cubicles_1,
                exam_mean=exam_mean)

################################################################################
# MODIFICATION 3: Only execute model if a streamlit button is pressed.
if st.button('Simulate treatment centre'):

    # in this example run a single replication of the model.
    with st.spinner('Simulating the treatment centre...'):
        results = multiple_replications(args, n_reps=replications)

    st.success('Done!')

################################################################################
# MODIFICATION 4: display results using st.table (or st.dataframe)
    st.table(results.mean().round(1))
################################################################################
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[2], line 15
      1 '''
      2 Script to complete a basic run of the model and display a table of
      3 results in a streamlit app.
   (...)
     12 `pip install treat_sim==1.0.0`
     13 '''
     14 from treat_sim.model import Scenario, multiple_replications
---> 15 import streamlit as st
     17 ################################################################################
     18 # MODIFICATION 1: Read in markdown and display
     19 INTRO_FILE = 'resources/overview.md'

ModuleNotFoundError: No module named 'streamlit'

Exercise 3:#

Add in a side bar that display the sliders to a user

  • Create a side bar section in your app and add in the slider.

  • Run your model and check it still works.

'''
Script to complete a basic run of the model and display a table of
results in a streamlit app.

The model is imported from a pypi package 'treat_sim'

Full documentation and source code for `treat_sim` is available as 
* Jupyter Book: https://tommonks.github.io/treatment-centre-sim/
* github: https://github.com/TomMonks/treatment-centre-sim

A conda environment has been provided locally,but the model can be pip installed
`pip install treat_sim==1.0.0`
'''
from treat_sim.model import Scenario, multiple_replications
import streamlit as st

INTRO_FILE = 'resources/overview.md'

def read_file_contents(file_name):
    ''''
    Read the contents of a file.

    Params:
    ------
    file_name: str
        Path to file.

    Returns:
    -------
    str
    '''
    with open(file_name) as f:
        return f.read()

# give the page a title
st.title('Treatment Centre Simulation Model')

# show the introductory markdown
st.markdown(read_file_contents(INTRO_FILE))

################################################################################
# MODIFICATION: create a sidebar for sliders
with st.sidebar:
    n_triage = st.slider('Triage bays', 1, 5, 1)
    n_exam = st.slider('Exam rooms', 1, 5, 3)
    n_cubicles_1 = st.slider('Non-Trauma Treatment cubicles', 1, 5, 1, 
                             help='Set the number of non trauma pathway '
                             + 'treatment cubicles')

    # examination mean
    exam_mean = st.slider('Mean examination time', 10.0, 45.0, 
                           16.0, 1.0)

    # runs
    replications = st.slider('No. replications', 1, 50, 10)
################################################################################

# Setup scenario using supplied variables
args = Scenario(n_triage=n_triage, n_exam=n_exam, n_cubicles_1=n_cubicles_1,
                exam_mean=exam_mean)

# Only execute model if a streamlit button is pressed.
if st.button('Simulate treatment centre'):

    # in this example run a single replication of the model.
    with st.spinner('Simulating the treatment centre...'):
        results = multiple_replications(args, n_reps=replications)

    st.success('Done!')

    # display results using st.table (or st.dataframe)
    st.table(results.mean().round(1))