{ "cells": [ { "cell_type": "markdown", "id": "d73555b4", "metadata": {}, "source": [ "# Python package\n", "\n", "[![Python](https://img.shields.io/pypi/pyversions/treat_sim)](https://pypi.org/project/treat_sim/)\n", "[![PyPI version fury.io](https://badge.fury.io/py/treat-sim.svg)](https://pypi.org/project/treat-sim/)\n", "\n", "The model is available to install from the [Python Package Index (PyPI)](https://pypi.org/project/treat-sim/). If you wish to use the model in your own code or package this is the simplest way to include it. It can be installed from a terminal or included in an conda environment or Python virtual environment. To install at run time use:\n", "\n", "```bash\n", "pip install treat-sim\n", "```" ] }, { "cell_type": "markdown", "id": "f5711433", "metadata": {}, "source": [ "## Running multiple replications of the `treat-sim` model\n", "\n", "To create a custom experiment and run multiple replications of it using the m~odel you need to import the `Scenario` and `multiple_replications` functions from the `model` module. Example code is below" ] }, { "cell_type": "code", "execution_count": 1, "id": "2cdc52f7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
countmeanstdmin25%50%75%max
00_arrivals5.0231.805.07227.00229.00230.00233.00240.00
01a_triage_wait5.030.5115.3917.6824.2824.8028.6657.12
01b_triage_util5.00.610.030.570.610.610.620.64
02a_registration_wait5.0106.1311.7290.00103.24103.61112.24121.54
02b_registration_util5.00.860.010.840.850.850.870.87
03a_examination_wait5.024.847.0714.6921.3725.5131.0931.56
03b_examination_util5.00.860.010.850.860.860.870.88
04a_treatment_wait(non_trauma)5.0134.8727.0894.02120.25150.35152.48157.26
04b_treatment_util(non_trauma)5.00.900.020.870.890.890.910.93
05_total_time(non-trauma)5.0232.0014.87208.36233.30233.88234.76249.69
06a_trauma_wait5.0209.3763.74133.81149.37236.51250.71276.42
06b_trauma_util5.00.900.080.830.860.870.881.03
07a_treatment_wait(trauma)5.011.084.893.729.2312.2513.7016.52
07b_treatment_util(trauma)5.00.500.130.370.420.460.610.66
08_total_time(trauma)5.0350.1363.75281.06301.52346.70380.83440.52
09_throughput5.0167.004.64161.00164.00167.00171.00172.00
\n", "
" ], "text/plain": [ " count mean std min 25% 50% \\\n", "00_arrivals 5.0 231.80 5.07 227.00 229.00 230.00 \n", "01a_triage_wait 5.0 30.51 15.39 17.68 24.28 24.80 \n", "01b_triage_util 5.0 0.61 0.03 0.57 0.61 0.61 \n", "02a_registration_wait 5.0 106.13 11.72 90.00 103.24 103.61 \n", "02b_registration_util 5.0 0.86 0.01 0.84 0.85 0.85 \n", "03a_examination_wait 5.0 24.84 7.07 14.69 21.37 25.51 \n", "03b_examination_util 5.0 0.86 0.01 0.85 0.86 0.86 \n", "04a_treatment_wait(non_trauma) 5.0 134.87 27.08 94.02 120.25 150.35 \n", "04b_treatment_util(non_trauma) 5.0 0.90 0.02 0.87 0.89 0.89 \n", "05_total_time(non-trauma) 5.0 232.00 14.87 208.36 233.30 233.88 \n", "06a_trauma_wait 5.0 209.37 63.74 133.81 149.37 236.51 \n", "06b_trauma_util 5.0 0.90 0.08 0.83 0.86 0.87 \n", "07a_treatment_wait(trauma) 5.0 11.08 4.89 3.72 9.23 12.25 \n", "07b_treatment_util(trauma) 5.0 0.50 0.13 0.37 0.42 0.46 \n", "08_total_time(trauma) 5.0 350.13 63.75 281.06 301.52 346.70 \n", "09_throughput 5.0 167.00 4.64 161.00 164.00 167.00 \n", "\n", " 75% max \n", "00_arrivals 233.00 240.00 \n", "01a_triage_wait 28.66 57.12 \n", "01b_triage_util 0.62 0.64 \n", "02a_registration_wait 112.24 121.54 \n", "02b_registration_util 0.87 0.87 \n", "03a_examination_wait 31.09 31.56 \n", "03b_examination_util 0.87 0.88 \n", "04a_treatment_wait(non_trauma) 152.48 157.26 \n", "04b_treatment_util(non_trauma) 0.91 0.93 \n", "05_total_time(non-trauma) 234.76 249.69 \n", "06a_trauma_wait 250.71 276.42 \n", "06b_trauma_util 0.88 1.03 \n", "07a_treatment_wait(trauma) 13.70 16.52 \n", "07b_treatment_util(trauma) 0.61 0.66 \n", "08_total_time(trauma) 380.83 440.52 \n", "09_throughput 171.00 172.00 " ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from treat_sim.model import Scenario, multiple_replications\n", "\n", "# use all default parameter values\n", "base_case = Scenario()\n", "\n", "# run replications (defaults to 5 if parameter ignored.)\n", "results = multiple_replications(base_case, n_reps=5)\n", "\n", "# show results (use print() if not using Jupyter notebooks)\n", "results.describe().round(2).T" ] }, { "cell_type": "markdown", "id": "81338e1c", "metadata": {}, "source": [ "## Custom experiments\n", "\n", "Simply modify the default parameters of the `Scenario` class if you would like to run different experiments" ] }, { "cell_type": "code", "execution_count": 2, "id": "f5beaec4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
countmeanstdmin25%50%75%max
00_arrivals5.0231.805.07227.00229.00230.00233.00240.00
01a_triage_wait5.030.5115.3917.6824.2824.8028.6657.12
01b_triage_util5.00.610.030.570.610.610.620.64
02a_registration_wait5.0106.1311.7290.00103.24103.61112.24121.54
02b_registration_util5.00.860.010.840.850.850.870.87
03a_examination_wait5.024.847.0714.6921.3725.5131.0931.56
03b_examination_util5.00.860.010.850.860.860.870.88
04a_treatment_wait(non_trauma)5.01.930.141.711.921.951.992.08
04b_treatment_util(non_trauma)5.00.640.020.610.620.640.650.68
05_total_time(non-trauma)5.0188.568.52177.58184.76187.32192.98200.16
06a_trauma_wait5.0209.3763.74133.81149.37236.51250.71276.42
06b_trauma_util5.00.900.080.830.860.870.881.03
07a_treatment_wait(trauma)5.011.084.893.729.2312.2513.7016.52
07b_treatment_util(trauma)5.00.500.130.370.420.460.610.66
08_total_time(trauma)5.0350.1363.75281.06301.52346.70380.83440.52
09_throughput5.0199.404.67193.00198.00200.00200.00206.00
\n", "
" ], "text/plain": [ " count mean std min 25% 50% \\\n", "00_arrivals 5.0 231.80 5.07 227.00 229.00 230.00 \n", "01a_triage_wait 5.0 30.51 15.39 17.68 24.28 24.80 \n", "01b_triage_util 5.0 0.61 0.03 0.57 0.61 0.61 \n", "02a_registration_wait 5.0 106.13 11.72 90.00 103.24 103.61 \n", "02b_registration_util 5.0 0.86 0.01 0.84 0.85 0.85 \n", "03a_examination_wait 5.0 24.84 7.07 14.69 21.37 25.51 \n", "03b_examination_util 5.0 0.86 0.01 0.85 0.86 0.86 \n", "04a_treatment_wait(non_trauma) 5.0 1.93 0.14 1.71 1.92 1.95 \n", "04b_treatment_util(non_trauma) 5.0 0.64 0.02 0.61 0.62 0.64 \n", "05_total_time(non-trauma) 5.0 188.56 8.52 177.58 184.76 187.32 \n", "06a_trauma_wait 5.0 209.37 63.74 133.81 149.37 236.51 \n", "06b_trauma_util 5.0 0.90 0.08 0.83 0.86 0.87 \n", "07a_treatment_wait(trauma) 5.0 11.08 4.89 3.72 9.23 12.25 \n", "07b_treatment_util(trauma) 5.0 0.50 0.13 0.37 0.42 0.46 \n", "08_total_time(trauma) 5.0 350.13 63.75 281.06 301.52 346.70 \n", "09_throughput 5.0 199.40 4.67 193.00 198.00 200.00 \n", "\n", " 75% max \n", "00_arrivals 233.00 240.00 \n", "01a_triage_wait 28.66 57.12 \n", "01b_triage_util 0.62 0.64 \n", "02a_registration_wait 112.24 121.54 \n", "02b_registration_util 0.87 0.87 \n", "03a_examination_wait 31.09 31.56 \n", "03b_examination_util 0.87 0.88 \n", "04a_treatment_wait(non_trauma) 1.99 2.08 \n", "04b_treatment_util(non_trauma) 0.65 0.68 \n", "05_total_time(non-trauma) 192.98 200.16 \n", "06a_trauma_wait 250.71 276.42 \n", "06b_trauma_util 0.88 1.03 \n", "07a_treatment_wait(trauma) 13.70 16.52 \n", "07b_treatment_util(trauma) 0.61 0.66 \n", "08_total_time(trauma) 380.83 440.52 \n", "09_throughput 200.00 206.00 " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# an extra non trauma treatment cubicle\n", "extra_cubicle_experiment = Scenario(n_cubicles_1=2)\n", "\n", "# experiment results\n", "results_extra_cubicle = multiple_replications(extra_cubicle_experiment, n_reps=5)\n", "\n", "# show results of experiment\n", "results_extra_cubicle.describe().round(2).T" ] }, { "cell_type": "markdown", "id": "beed52e9", "metadata": {}, "source": [ "## Pre-configured experiments\n", "\n", "The package also includes a selection of pre-configured experiments to run. The following code runs all experiments and display a summary of the results." ] }, { "cell_type": "code", "execution_count": 3, "id": "de2e6a8e", "metadata": {}, "outputs": [], "source": [ "from treat_sim.model import (get_scenarios, run_scenario_analysis,\n", " scenario_summary_frame, \n", " DEFAULT_RESULTS_COLLECTION_PERIOD)" ] }, { "cell_type": "code", "execution_count": 4, "id": "6f40cba8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Scenario Analysis\n", "No. Scenario: 6\n", "Replications: 50\n", "Running base => done.\n", "\n", "Running triage+1 => done.\n", "\n", "Running exam+1 => done.\n", "\n", "Running treat+1 => done.\n", "\n", "Running swap_exam_treat => done.\n", "\n", "Running short_exam => done.\n", "\n", "Scenario analysis complete.\n" ] } ], "source": [ "results = run_scenario_analysis(get_scenarios(), DEFAULT_RESULTS_COLLECTION_PERIOD,\n", " n_reps=50)" ] }, { "cell_type": "code", "execution_count": 5, "id": "9e94fd9a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
basetriage+1exam+1treat+1swap_exam_treatshort_exam
00_arrivals227.720000227.720000227.720000227.720000227.720000227.720000
01a_triage_wait35.2430191.32517635.24301935.2430191.3251761.325176
01b_triage_util0.6070390.3035190.6070390.6070390.3035190.303519
02a_registration_wait105.572644135.915119105.572644105.572644135.915119135.915119
02b_registration_util0.8406130.8439540.8406130.8406130.8439540.843954
03a_examination_wait25.55252526.6888660.14656525.552525148.86596566.003847
03b_examination_util0.8504480.8535670.6678560.8504480.8936290.868998
04a_treatment_wait(non_trauma)136.660635138.575405151.8082042.28096233.568594111.337103
04b_treatment_util(non_trauma)0.8672380.8693240.8676910.6293460.8363840.871039
05_total_time(non-trauma)234.335783232.133275223.063821192.191909280.729440244.797808
06a_trauma_wait151.680557175.135269151.680557151.680557175.135269175.135269
06b_trauma_util0.8269410.8409770.8269410.8269410.8409770.840977
07a_treatment_wait(trauma)14.30504414.99728714.30504414.30504414.99728714.997287
07b_treatment_util(trauma)0.5010100.5069080.5010100.5010100.5069080.506908
08_total_time(trauma)292.280487282.783517292.280487292.280487282.783517282.783517
09_throughput162.160000162.920000165.880000195.300000138.540000156.600000
\n", "
" ], "text/plain": [ " base triage+1 exam+1 \\\n", "00_arrivals 227.720000 227.720000 227.720000 \n", "01a_triage_wait 35.243019 1.325176 35.243019 \n", "01b_triage_util 0.607039 0.303519 0.607039 \n", "02a_registration_wait 105.572644 135.915119 105.572644 \n", "02b_registration_util 0.840613 0.843954 0.840613 \n", "03a_examination_wait 25.552525 26.688866 0.146565 \n", "03b_examination_util 0.850448 0.853567 0.667856 \n", "04a_treatment_wait(non_trauma) 136.660635 138.575405 151.808204 \n", "04b_treatment_util(non_trauma) 0.867238 0.869324 0.867691 \n", "05_total_time(non-trauma) 234.335783 232.133275 223.063821 \n", "06a_trauma_wait 151.680557 175.135269 151.680557 \n", "06b_trauma_util 0.826941 0.840977 0.826941 \n", "07a_treatment_wait(trauma) 14.305044 14.997287 14.305044 \n", "07b_treatment_util(trauma) 0.501010 0.506908 0.501010 \n", "08_total_time(trauma) 292.280487 282.783517 292.280487 \n", "09_throughput 162.160000 162.920000 165.880000 \n", "\n", " treat+1 swap_exam_treat short_exam \n", "00_arrivals 227.720000 227.720000 227.720000 \n", "01a_triage_wait 35.243019 1.325176 1.325176 \n", "01b_triage_util 0.607039 0.303519 0.303519 \n", "02a_registration_wait 105.572644 135.915119 135.915119 \n", "02b_registration_util 0.840613 0.843954 0.843954 \n", "03a_examination_wait 25.552525 148.865965 66.003847 \n", "03b_examination_util 0.850448 0.893629 0.868998 \n", "04a_treatment_wait(non_trauma) 2.280962 33.568594 111.337103 \n", "04b_treatment_util(non_trauma) 0.629346 0.836384 0.871039 \n", "05_total_time(non-trauma) 192.191909 280.729440 244.797808 \n", "06a_trauma_wait 151.680557 175.135269 175.135269 \n", "06b_trauma_util 0.826941 0.840977 0.840977 \n", "07a_treatment_wait(trauma) 14.305044 14.997287 14.997287 \n", "07b_treatment_util(trauma) 0.501010 0.506908 0.506908 \n", "08_total_time(trauma) 292.280487 282.783517 282.783517 \n", "09_throughput 195.300000 138.540000 156.600000 " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scenario_summary_frame(results)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.14" } }, "nbformat": 4, "nbformat_minor": 5 }