Skip to content

Commit 5f4e1bf

Browse files
committed
feat(init_cond): extra trace info for exp
1 parent c6fa0c3 commit 5f4e1bf

File tree

1 file changed

+48
-115
lines changed

1 file changed

+48
-115
lines changed

content/14_initial_conditions.ipynb

Lines changed: 48 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,12 @@
112112
"\n",
113113
"INIT_COND_PARAMS = {\n",
114114
" \"mode\": \"fixed\",\n",
115-
" \"fixed\": 8,\n",
115+
" \"fixed\": 10,\n",
116116
" \"rnd\": {\n",
117117
" \"values\":[8, 9, 10, 11, 12, 13],\n",
118-
" \"freq\":[25, 25, 2, 1, 1, 1]\n",
119-
" }\n",
118+
" \"freq\":[15, 15, 25, 20, 5, 5]\n",
119+
" },\n",
120+
" \"collect_results\": False\n",
120121
"}\n",
121122
" \n",
122123
"# sampling settings\n",
@@ -178,6 +179,9 @@
178179
" \"\"\"\n",
179180
" Encapsulates the concept of an experiment 🧪 for the stroke pathway\n",
180181
" bed blocking simulator. Manages parameters, PRNG streams and results.\n",
182+
"\n",
183+
" There is a new parameter `init_cond_params` that stores the initial\n",
184+
" conditions to use in an experiment.\n",
181185
" \"\"\"\n",
182186
" def __init__(\n",
183187
" self,\n",
@@ -201,7 +205,7 @@
201205
" self.acute_los_mean = acute_los_mean\n",
202206
" self.acute_los_std = acute_los_std\n",
203207
"\n",
204-
" # stored initial conditions\n",
208+
" # ---- stored initial conditions -------\n",
205209
" self.init_cond_params = init_cond_params\n",
206210
"\n",
207211
" # place holder for resources\n",
@@ -217,8 +221,9 @@
217221
" def set_random_no_set(self, random_number_set):\n",
218222
" \"\"\"\n",
219223
" Controls the random sampling\n",
224+
" \n",
220225
" Parameters:\n",
221-
" ----------\n",
226+
" -----------\n",
222227
" random_number_set: int\n",
223228
" Used to control the set of pseudo random numbers used by\n",
224229
" the distributions in the simulation.\n",
@@ -250,12 +255,14 @@
250255
" self.init_conds = FixedDistribution(\n",
251256
" self.init_cond_params[\"fixed\"]\n",
252257
" )\n",
253-
" else:\n",
258+
" elif self.init_cond_params[\"mode\"] == \"rnd\":\n",
254259
" self.init_conds = DiscreteEmpirical(\n",
255260
" values = self.init_cond_params[\"rnd\"][\"values\"],\n",
256261
" freq = self.init_cond_params[\"rnd\"][\"freq\"],\n",
257262
" random_seed=self.seeds[2]\n",
258263
" )\n",
264+
" else:\n",
265+
" raise ValueError(\"Initial conditions mode must be 'fixed' or 'rnd'\")\n",
259266
" \n",
260267
"\n",
261268
" def init_results_variables(self):\n",
@@ -326,8 +333,7 @@
326333
"outputs": [],
327334
"source": [
328335
"def acute_stroke_pathway(patient_id, env, args, collect_results=True):\n",
329-
" \"\"\"Process a patient through the acute ward\n",
330-
" Simpy generator function.\n",
336+
" \"\"\"Process a patient through the acute ward. Simpy generator function.\n",
331337
" \n",
332338
" Parameters:\n",
333339
" -----------\n",
@@ -348,6 +354,7 @@
348354
" acute_admit_time = env.now\n",
349355
" wait_for_acute = acute_admit_time - arrival_time\n",
350356
"\n",
357+
" # used to avoid collecting stats from initial conditions...\n",
351358
" if collect_results:\n",
352359
" args.results['waiting_acute'].append(wait_for_acute)\n",
353360
" \n",
@@ -415,27 +422,25 @@
415422
"source": [
416423
"def setup_initial_conditions(\n",
417424
" env: simpy.Environment, \n",
418-
" args: Experiment,\n",
419-
" collect_results: bool = False,\n",
425+
" args: Experiment\n",
420426
"):\n",
421-
" \"\"\"Set up initial conditions with patients already in the acute\n",
422-
" stroke queue\n",
427+
" \"\"\"Set up initial conditions with patients already in the acute stroke queue\n",
423428
"\n",
424429
" Parameters:\n",
425430
" -----------\n",
426431
" env: simpy.Environment\n",
427432
" The simpy environment for the simulation\n",
428433
"\n",
429434
" args: Experiment\n",
430-
" The settings and input parameters for the simulation.\n",
431-
"\n",
432-
" collect_results: bool, optional (default = False)#\n",
433-
" Should results be collected for initial conditions patients?\n",
434-
" \n",
435+
" The settings and input parameters for the simulation. \n",
435436
" \"\"\"\n",
436437
" # sample the no. patients to load into queue\n",
437438
" patients_to_load = args.init_conds.sample()\n",
439+
" trace(f\"Patient to load: {patients_to_load}\")\n",
438440
"\n",
441+
" # collect results or not?\n",
442+
" collect_results = args.init_cond_params[\"collect_results\"]\n",
443+
" \n",
439444
" for initial_id in range(1, patients_to_load+1):\n",
440445
" # Create patients with negative IDs to distinguish them as init cond.\n",
441446
" # we may or may not want collect results for initial conditions\n",
@@ -496,15 +501,19 @@
496501
" # simpy resources\n",
497502
" experiment.acute_ward = simpy.Resource(env, experiment.n_acute_beds)\n",
498503
"\n",
499-
" # load the acute stroke queue\n",
500-
" setup_initial_conditions(env, experiment)\n",
504+
" trace(\"--- Pre-Simulation Actions ---\")\n",
501505
"\n",
502506
" # schedule a warm_up period\n",
503507
" env.process(warmup_complete(wu_period, env, experiment))\n",
504508
" \n",
509+
" # load the acute stroke queue\n",
510+
" setup_initial_conditions(env, experiment)\n",
511+
" \n",
505512
" # we pass all arrival generators to simpy \n",
506513
" env.process(stroke_arrivals_generator(env, experiment))\n",
507514
"\n",
515+
" trace(\"--- Start Simulation ---\")\n",
516+
" \n",
508517
" # run model\n",
509518
" env.run(until=wu_period+rc_period)\n",
510519
"\n",
@@ -520,123 +529,47 @@
520529
},
521530
{
522531
"cell_type": "code",
523-
"execution_count": 11,
532+
"execution_count": null,
524533
"id": "caf52390-5455-4fa1-bb22-60b5b91ad8d0",
525534
"metadata": {},
526-
"outputs": [
527-
{
528-
"name": "stdout",
529-
"output_type": "stream",
530-
"text": [
531-
"0.00: Patient -1 loaded into queue\n",
532-
"0.00: Patient -2 loaded into queue\n",
533-
"0.00: Patient -3 loaded into queue\n",
534-
"0.00: Patient -4 loaded into queue\n",
535-
"0.00: Patient -5 loaded into queue\n",
536-
"0.00: Patient -6 loaded into queue\n",
537-
"0.00: Patient -7 loaded into queue\n",
538-
"0.00: Patient -8 loaded into queue\n",
539-
"0.00: Patient -9 loaded into queue\n",
540-
"0.00: Patient -10 loaded into queue\n",
541-
"0.00: Patient -1 admitted to acute ward.(waited 0.00 days)\n",
542-
"0.00: Patient -2 admitted to acute ward.(waited 0.00 days)\n",
543-
"0.00: Patient -3 admitted to acute ward.(waited 0.00 days)\n",
544-
"0.00: Patient -4 admitted to acute ward.(waited 0.00 days)\n",
545-
"0.00: Patient -5 admitted to acute ward.(waited 0.00 days)\n",
546-
"0.00: Patient -6 admitted to acute ward.(waited 0.00 days)\n",
547-
"0.00: Patient -7 admitted to acute ward.(waited 0.00 days)\n",
548-
"0.00: Patient -8 admitted to acute ward.(waited 0.00 days)\n",
549-
"0.00: Patient -9 admitted to acute ward.(waited 0.00 days)\n",
550-
"0.00: 🥵 Warm up complete.\n",
551-
"2.74: Patient 1. Stroke arrival.\n",
552-
"2.78: Patient 2. Stroke arrival.\n",
553-
"3.36: Patient 3. Stroke arrival.\n",
554-
"4.42: Patient 4. Stroke arrival.\n",
555-
"4.68: Patient 5. Stroke arrival.\n",
556-
"5.80: Patient -3 discharged.\n",
557-
"5.80: Patient -10 admitted to acute ward.(waited 5.80 days)\n",
558-
"5.80: Patient -8 discharged.\n",
559-
"5.80: Patient 1 admitted to acute ward.(waited 3.06 days)\n",
560-
"5.97: Patient 6. Stroke arrival.\n",
561-
"6.08: Patient -6 discharged.\n",
562-
"6.08: Patient 2 admitted to acute ward.(waited 3.30 days)\n",
563-
"6.22: Patient -7 discharged.\n",
564-
"6.22: Patient 3 admitted to acute ward.(waited 2.86 days)\n",
565-
"6.33: Patient 7. Stroke arrival.\n",
566-
"7.28: Patient 8. Stroke arrival.\n",
567-
"7.41: Patient -4 discharged.\n",
568-
"7.41: Patient 4 admitted to acute ward.(waited 2.99 days)\n",
569-
"7.43: Patient 9. Stroke arrival.\n",
570-
"7.55: Patient 10. Stroke arrival.\n",
571-
"7.94: Patient -5 discharged.\n",
572-
"7.94: Patient 5 admitted to acute ward.(waited 3.26 days)\n",
573-
"8.11: Patient -2 discharged.\n",
574-
"8.11: Patient 6 admitted to acute ward.(waited 2.14 days)\n",
575-
"8.26: Patient 11. Stroke arrival.\n",
576-
"8.42: Patient 12. Stroke arrival.\n",
577-
"8.68: Patient 13. Stroke arrival.\n",
578-
"8.72: Patient 14. Stroke arrival.\n",
579-
"8.82: Patient -9 discharged.\n",
580-
"8.82: Patient 7 admitted to acute ward.(waited 2.50 days)\n",
581-
"8.95: Patient 15. Stroke arrival.\n",
582-
"9.18: Patient 16. Stroke arrival.\n",
583-
"9.87: Patient -1 discharged.\n",
584-
"9.87: Patient 8 admitted to acute ward.(waited 2.58 days)\n",
585-
"9.92: Patient 17. Stroke arrival.\n"
586-
]
587-
},
588-
{
589-
"data": {
590-
"text/plain": [
591-
"{'mean_acute_wait': 2.8362718457918676}"
592-
]
593-
},
594-
"execution_count": 11,
595-
"metadata": {},
596-
"output_type": "execute_result"
597-
}
598-
],
535+
"outputs": [],
599536
"source": [
600537
"TRACE = True\n",
538+
"\n",
539+
"# settings dictionary\n",
601540
"init_cond_params = INIT_COND_PARAMS.copy()\n",
602541
"init_cond_params[\"mode\"] = \"fixed\"\n",
603542
"\n",
604-
"# uncomment to vary the fixed amount 10 = 1 in queue.\n",
543+
"# vary the fixed amount. Interpretation 10 = 1 in queue.\n",
605544
"init_cond_params[\"fixed\"] = 10\n",
606545
"\n",
546+
"# vary if we collect results from initial condition stroke patients\n",
547+
"init_cond_params[\"collect_results\"] = False\n",
548+
"\n",
549+
"# create experiment and pass in initial conditions\n",
607550
"experiment = Experiment(init_cond_params=init_cond_params)\n",
551+
"\n",
608552
"results = single_run(experiment, rep=1, wu_period=0.0, rc_period=10.0)\n",
609553
"results"
610554
]
611555
},
612556
{
613557
"cell_type": "code",
614-
"execution_count": 12,
558+
"execution_count": null,
615559
"id": "0aaef408-09ca-49e0-8d39-f4a088a4ef1b",
616560
"metadata": {},
617-
"outputs": [
618-
{
619-
"data": {
620-
"text/plain": [
621-
"{'n_arrivals': 17,\n",
622-
" 'waiting_acute': [3.0586175577655674,\n",
623-
" 3.303449502025928,\n",
624-
" 2.857579305401165,\n",
625-
" 2.9908615153438225,\n",
626-
" 3.2622390398417513,\n",
627-
" 2.136685286636955,\n",
628-
" 2.496306611009193,\n",
629-
" 2.58443594831056]}"
630-
]
631-
},
632-
"execution_count": 12,
633-
"metadata": {},
634-
"output_type": "execute_result"
635-
}
636-
],
561+
"outputs": [],
637562
"source": [
638563
"experiment.results"
639564
]
565+
},
566+
{
567+
"cell_type": "code",
568+
"execution_count": null,
569+
"id": "9d5266e1-d8f3-4755-838b-89b9b416faef",
570+
"metadata": {},
571+
"outputs": [],
572+
"source": []
640573
}
641574
],
642575
"metadata": {

0 commit comments

Comments
 (0)