Skip to content

Commit 6b50c19

Browse files
committed
test(scenario): add back test for scenario analysis (#30)
1 parent ada8d9f commit 6b50c19

File tree

3 files changed

+243
-4
lines changed

3 files changed

+243
-4
lines changed

notebooks/generate_exp_results.ipynb

Lines changed: 200 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"import time\n",
5757
"from IPython.display import display\n",
5858
"\n",
59-
"from simulation.model import Param, Runner\n",
59+
"from simulation.model import Param, Runner, run_scenarios\n",
6060
"from simulation.replications import confidence_interval_method"
6161
]
6262
},
@@ -743,6 +743,203 @@
743743
" os.path.join(TESTS, 'overall.csv'), index=True)"
744744
]
745745
},
746+
{
747+
"cell_type": "markdown",
748+
"metadata": {},
749+
"source": [
750+
"## Scenario analysis"
751+
]
752+
},
753+
{
754+
"cell_type": "code",
755+
"execution_count": 10,
756+
"metadata": {},
757+
"outputs": [
758+
{
759+
"name": "stdout",
760+
"output_type": "stream",
761+
"text": [
762+
"There are 4 scenarios. Running:\n",
763+
"{'patient_inter': 3, 'number_of_nurses': 6}\n",
764+
"{'patient_inter': 3, 'number_of_nurses': 7}\n",
765+
"{'patient_inter': 4, 'number_of_nurses': 6}\n",
766+
"{'patient_inter': 4, 'number_of_nurses': 7}\n"
767+
]
768+
},
769+
{
770+
"data": {
771+
"text/html": [
772+
"<div>\n",
773+
"<style scoped>\n",
774+
" .dataframe tbody tr th:only-of-type {\n",
775+
" vertical-align: middle;\n",
776+
" }\n",
777+
"\n",
778+
" .dataframe tbody tr th {\n",
779+
" vertical-align: top;\n",
780+
" }\n",
781+
"\n",
782+
" .dataframe thead th {\n",
783+
" text-align: right;\n",
784+
" }\n",
785+
"</style>\n",
786+
"<table border=\"1\" class=\"dataframe\">\n",
787+
" <thead>\n",
788+
" <tr style=\"text-align: right;\">\n",
789+
" <th></th>\n",
790+
" <th>run_number</th>\n",
791+
" <th>scenario</th>\n",
792+
" <th>arrivals</th>\n",
793+
" <th>mean_q_time_nurse</th>\n",
794+
" <th>mean_time_with_nurse</th>\n",
795+
" <th>mean_nurse_utilisation</th>\n",
796+
" <th>mean_nurse_utilisation_tw</th>\n",
797+
" <th>mean_nurse_q_length</th>\n",
798+
" <th>count_nurse_unseen</th>\n",
799+
" <th>mean_q_time_nurse_unseen</th>\n",
800+
" <th>patient_inter</th>\n",
801+
" <th>number_of_nurses</th>\n",
802+
" </tr>\n",
803+
" </thead>\n",
804+
" <tbody>\n",
805+
" <tr>\n",
806+
" <th>0</th>\n",
807+
" <td>0</td>\n",
808+
" <td>0</td>\n",
809+
" <td>2004</td>\n",
810+
" <td>0.486680</td>\n",
811+
" <td>9.925934</td>\n",
812+
" <td>0.553955</td>\n",
813+
" <td>0.553955</td>\n",
814+
" <td>0.162551</td>\n",
815+
" <td>0</td>\n",
816+
" <td>NaN</td>\n",
817+
" <td>3</td>\n",
818+
" <td>6</td>\n",
819+
" </tr>\n",
820+
" <tr>\n",
821+
" <th>1</th>\n",
822+
" <td>1</td>\n",
823+
" <td>0</td>\n",
824+
" <td>1993</td>\n",
825+
" <td>0.746175</td>\n",
826+
" <td>10.377197</td>\n",
827+
" <td>0.573874</td>\n",
828+
" <td>0.573874</td>\n",
829+
" <td>0.247854</td>\n",
830+
" <td>0</td>\n",
831+
" <td>NaN</td>\n",
832+
" <td>3</td>\n",
833+
" <td>6</td>\n",
834+
" </tr>\n",
835+
" <tr>\n",
836+
" <th>2</th>\n",
837+
" <td>2</td>\n",
838+
" <td>0</td>\n",
839+
" <td>2017</td>\n",
840+
" <td>0.386324</td>\n",
841+
" <td>9.856724</td>\n",
842+
" <td>0.553419</td>\n",
843+
" <td>0.553419</td>\n",
844+
" <td>0.129869</td>\n",
845+
" <td>0</td>\n",
846+
" <td>NaN</td>\n",
847+
" <td>3</td>\n",
848+
" <td>6</td>\n",
849+
" </tr>\n",
850+
" <tr>\n",
851+
" <th>0</th>\n",
852+
" <td>0</td>\n",
853+
" <td>1</td>\n",
854+
" <td>2004</td>\n",
855+
" <td>0.121476</td>\n",
856+
" <td>9.925934</td>\n",
857+
" <td>0.474819</td>\n",
858+
" <td>0.474819</td>\n",
859+
" <td>0.040573</td>\n",
860+
" <td>0</td>\n",
861+
" <td>NaN</td>\n",
862+
" <td>3</td>\n",
863+
" <td>7</td>\n",
864+
" </tr>\n",
865+
" <tr>\n",
866+
" <th>1</th>\n",
867+
" <td>1</td>\n",
868+
" <td>1</td>\n",
869+
" <td>1993</td>\n",
870+
" <td>0.226682</td>\n",
871+
" <td>10.377197</td>\n",
872+
" <td>0.491892</td>\n",
873+
" <td>0.491892</td>\n",
874+
" <td>0.075296</td>\n",
875+
" <td>0</td>\n",
876+
" <td>NaN</td>\n",
877+
" <td>3</td>\n",
878+
" <td>7</td>\n",
879+
" </tr>\n",
880+
" </tbody>\n",
881+
"</table>\n",
882+
"</div>"
883+
],
884+
"text/plain": [
885+
" run_number scenario arrivals mean_q_time_nurse mean_time_with_nurse \\\n",
886+
"0 0 0 2004 0.486680 9.925934 \n",
887+
"1 1 0 1993 0.746175 10.377197 \n",
888+
"2 2 0 2017 0.386324 9.856724 \n",
889+
"0 0 1 2004 0.121476 9.925934 \n",
890+
"1 1 1 1993 0.226682 10.377197 \n",
891+
"\n",
892+
" mean_nurse_utilisation mean_nurse_utilisation_tw mean_nurse_q_length \\\n",
893+
"0 0.553955 0.553955 0.162551 \n",
894+
"1 0.573874 0.573874 0.247854 \n",
895+
"2 0.553419 0.553419 0.129869 \n",
896+
"0 0.474819 0.474819 0.040573 \n",
897+
"1 0.491892 0.491892 0.075296 \n",
898+
"\n",
899+
" count_nurse_unseen mean_q_time_nurse_unseen patient_inter \\\n",
900+
"0 0 NaN 3 \n",
901+
"1 0 NaN 3 \n",
902+
"2 0 NaN 3 \n",
903+
"0 0 NaN 3 \n",
904+
"1 0 NaN 3 \n",
905+
"\n",
906+
" number_of_nurses \n",
907+
"0 6 \n",
908+
"1 6 \n",
909+
"2 6 \n",
910+
"0 7 \n",
911+
"1 7 "
912+
]
913+
},
914+
"metadata": {},
915+
"output_type": "display_data"
916+
}
917+
],
918+
"source": [
919+
"# Run scenarios\n",
920+
"param = Param(\n",
921+
" patient_inter=4,\n",
922+
" mean_n_consult_time=10,\n",
923+
" number_of_nurses=5,\n",
924+
" warm_up_period=2000,\n",
925+
" data_collection_period=6000,\n",
926+
" number_of_runs=3,\n",
927+
" audit_interval=120,\n",
928+
" cores=1\n",
929+
")\n",
930+
"scenario_results = run_scenarios(\n",
931+
" scenarios={'patient_inter': [3, 4],\n",
932+
" 'number_of_nurses': [6, 7]},\n",
933+
" param=param\n",
934+
")\n",
935+
"\n",
936+
"# Preview\n",
937+
"display(scenario_results.head())\n",
938+
"\n",
939+
"# Save to csv\n",
940+
"scenario_results.to_csv(os.path.join(TESTS, 'scenario.csv'), index=True)"
941+
]
942+
},
746943
{
747944
"cell_type": "markdown",
748945
"metadata": {},
@@ -777,7 +974,7 @@
777974
},
778975
{
779976
"cell_type": "code",
780-
"execution_count": 10,
977+
"execution_count": 11,
781978
"metadata": {},
782979
"outputs": [
783980
{
@@ -1009,7 +1206,7 @@
10091206
},
10101207
{
10111208
"cell_type": "code",
1012-
"execution_count": 11,
1209+
"execution_count": 12,
10131210
"metadata": {},
10141211
"outputs": [
10151212
{

tests/exp_results/scenario.csv

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
,run_number,scenario,arrivals,mean_q_time_nurse,mean_time_with_nurse,mean_nurse_utilisation,mean_nurse_utilisation_tw,mean_nurse_q_length,count_nurse_unseen,mean_q_time_nurse_unseen,patient_inter,number_of_nurses
2+
0,0,0,2004,0.48667967755432595,9.925933909873555,0.5539549536279254,0.5539549536279247,0.16255101230314487,0,,3,6
3+
1,1,0,1993,0.7461746423993406,10.377197279111792,0.5738739636111149,0.5738739636111151,0.24785434371698098,0,,3,6
4+
2,2,0,2017,0.38632400402627576,9.856723933105343,0.5534193173586592,0.5534193173586589,0.12986925268683305,0,,3,6
5+
0,0,1,2004,0.12147605450060947,9.925933909873555,0.47481853168107885,0.47481853168107835,0.04057300220320356,0,,3,7
6+
1,1,1,1993,0.2266816332232325,10.377197279111792,0.491891968809527,0.4918919688095272,0.07529608250231706,0,,3,7
7+
2,2,1,2017,0.11960199026500552,9.856723933105343,0.47435941487885075,0.47435941487885047,0.040206202394086024,0,,3,7
8+
0,0,2,1492,0.08184145365169877,10.04701005331526,0.41472102750962625,0.4147210275096257,0.020351241474722427,0,,4,6
9+
1,1,2,1515,0.15440193319353135,10.296390547789509,0.4336195261552104,0.4336195261552114,0.03898648813136667,0,,4,6
10+
2,2,2,1525,0.11498914338292632,9.877249497718147,0.4180848124362831,0.41808481243628204,0.029226407276493775,0,,4,6
11+
0,0,3,1492,0.02076347209562216,10.04701005331526,0.35547516643682253,0.35547516643682203,0.00516318339444471,0,,4,7
12+
1,1,3,1515,0.03494302435135906,10.296390547789509,0.3716738795616089,0.37167387956160974,0.008823113648718163,0,,4,7
13+
2,2,3,1525,0.03358631740445475,9.877249497718147,0.3583584106596712,0.35835841065967033,0.008536522340298915,0,,4,7

tests/test_backtest.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
from pathlib import Path
1616
import pandas as pd
17-
from simulation.model import Param, Runner
17+
from simulation.model import Param, Runner, run_scenarios
1818

1919

2020
def test_reproduction():
@@ -58,3 +58,32 @@ def test_reproduction():
5858
exp_overall = pd.read_csv(
5959
Path(__file__).parent.joinpath('exp_results/overall.csv'), index_col=0)
6060
pd.testing.assert_frame_equal(experiment.overall_results_df, exp_overall)
61+
62+
63+
def test_scenarios():
64+
"""
65+
Check that results from run with scenarios are consistent with those
66+
previously generated.
67+
"""
68+
# Run scenarios
69+
param = Param(
70+
patient_inter=4,
71+
mean_n_consult_time=10,
72+
number_of_nurses=5,
73+
warm_up_period=2000,
74+
data_collection_period=6000,
75+
number_of_runs=3,
76+
audit_interval=120,
77+
cores=1
78+
)
79+
results = run_scenarios(
80+
scenarios={'patient_inter': [3, 4],
81+
'number_of_nurses': [6, 7]},
82+
param=param
83+
)
84+
85+
# Compare to expected results
86+
exp_results = pd.read_csv(
87+
Path(__file__).parent.joinpath('exp_results/scenario.csv'),
88+
index_col=0)
89+
pd.testing.assert_frame_equal(results, exp_results)

0 commit comments

Comments
 (0)