Skip to content

Commit 31e8f1f

Browse files
authored
Merge pull request #37 from pythonhealthdatascience/dev
Dev
2 parents ca446e0 + 5c47321 commit 31e8f1f

File tree

4 files changed

+82
-60
lines changed

4 files changed

+82
-60
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# R DES RAP Template 0.2.0
22

3-
Warm-up period, "unseen" metrics and tests.
3+
Warm-up period, "unseen" metrics and tests. Note that the alteration to record service duration altered the order of random number generation, so results now differ from before.
44

55
## New features
66

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.14980863.svg)](https://doi.org/10.5281/zenodo.14980863)
99
[![R-CMD-check](https://github.com/pythonhealthdatascience/rap_template_r_des/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/pythonhealthdatascience/rap_template_r_des/actions/workflows/R-CMD-check.yaml)
1010
[![Lint](https://github.com/pythonhealthdatascience/rap_template_r_des/actions/workflows/lint.yaml/badge.svg)](https://github.com/pythonhealthdatascience/rap_template_r_des/actions/workflows/lint.yaml)
11+
[![ORCID: Heather](https://img.shields.io/badge/ORCID_Amy_Heather-0000--0002--6596--3479-brightgreen)](https://orcid.org/0000-0002-6596-3479)
1112
<!-- badges: end -->
1213

1314
A template for creating **discrete-event simulation (DES)** models in R within a **reproducible analytical pipeline (RAP)**.<br><br>
@@ -242,10 +243,11 @@ repo/
242243

243244
The overall run time will vary depending on how the template model is used. A few example implementations are provided in `rmarkdown/` and the run times for these were:
244245

245-
* `analysis.Rmd`: 42s
246-
* `choosing_cores.Rmd`: 1m 14s
247-
* `choosing_replications.Rmd`: 2s
248-
* `generate_exp_results.Rmd`: 0s
246+
* `analysis.Rmd`: 1m 56s
247+
* `choosing_cores.Rmd`: 2m 13s
248+
* `choosing_replications.Rmd`: 13s
249+
* `generate_exp_results.Rmd`: 1s
250+
* `logs.Rmd`: 0s
249251

250252
<!--TODO: Check these are up to date -->
251253

rmarkdown/logs.Rmd

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,44 +68,47 @@ print(log_contents, sep = "\n")
6868
The patient arrives at 1.10422 and requests a nurse. There is one available (`SERVE`) so the consultation begins (`Timeout`).
6969

7070
```
71-
## [5] " 1.10422 | arrival: patient0 | activity: Seize | nurse, 1, 0 paths"
72-
## [6] " 1.10422 | resource: nurse | arrival: patient0 | SERVE"
71+
[5] " 1.10422 | arrival: patient0 | activity: Seize | nurse, 1, 0 paths"
72+
[6] " 1.10422 | resource: nurse | arrival: patient0 | SERVE"
7373
...
74-
## [8] " 1.10422 | arrival: patient0 | activity: Timeout | function()"
74+
[10] " 1.10422 | arrival: patient0 | activity: Timeout | function()"
7575
```
7676

77-
The consultation finishes at 2.22258, and the patient leaves:
77+
The consultation finishes at 2.26987, and the patient leaves:
7878

7979
```
80-
## [13] " 2.22258 | arrival: patient0 | activity: Release | nurse, 1"
81-
## [14] " 2.22258 | resource: nurse | arrival: patient0 | DEPART"
82-
## [15] " 2.22258 | task: Post-Release | : | "
80+
[14] " 2.26987 | arrival: patient0 | activity: Release | nurse, 1"
81+
[15] " 2.26987 | resource: nurse | arrival: patient0 | DEPART"
82+
[16] " 2.26987 | task: Post-Release | : | "
8383
```
8484

8585
#### Example B: `patient2`
8686

87-
The patient arrives at 4.59487, requests a nurse and enters a queue (`ENQUEUE`).
87+
The patient arrives at 4.5594, requests a nurse and enters a queue (`ENQUEUE`).
8888

8989
```
90-
[17] " 4.59487 | arrival: patient2 | activity: Seize | nurse, 1, 0 paths"
91-
[18] " 4.59487 | resource: nurse | arrival: patient2 | ENQUEUE"
90+
[13] " 1.94299 | source: patient | new: patient2 | 4.5594"
91+
...
92+
[21] " 4.5594 | arrival: patient2 | activity: Seize | nurse, 1, 0 paths"
93+
[22] " 4.5594 | resource: nurse | arrival: patient2 | ENQUEUE"
9294
```
9395

9496
A nurse becomes available at 25.3823 (`SERVE`) so consultation begins (`Timeout`).
9597

9698
```
97-
[35] " 25.3823 | resource: nurse | arrival: patient2 | SERVE"
98-
[36] " 25.3823 | arrival: patient2 | activity: Timeout | function()"
99+
[39] " 25.4296 | resource: nurse | arrival: patient2 | SERVE"
100+
...
101+
[42] " 25.4296 | arrival: patient2 | activity: Timeout | function()"
99102
```
100103

101-
However, there are no further entries as the simulation ends before the consultation ends.
104+
However, there are no further entries for that patient as the simulation ends before the consultation ends.
102105

103106
### Compare with recorded results
104107

105108
The logs will align with the recorded results of each patient.
106109

107110
```{r}
108-
verbose_run[["arrivals"]]
111+
arrange(verbose_run[["arrivals"]], start_time)
109112
```
110113

111114
## Customising the log messages
@@ -120,18 +123,25 @@ You may find this helpful for interpreting the log messages (for example, with t
120123
# Set the seed
121124
set.seed(0L)
122125
126+
env <- simmer("simulation", verbose = FALSE)
127+
123128
# Define the patient trajectory
124129
patient <- trajectory("appointment") %>%
125130
simmer::log_("🚶 Arrives.") %>%
126131
seize("nurse", 1L) %>%
127-
simmer::log_("🩺 Nurse consultation begins.") %>%
128-
timeout(function() {
132+
set_attribute("nurse_serve_start", function() now(env)) %>%
133+
set_attribute("nurse_serve_length", function() {
129134
rexp(n = 1L, rate = 1L / param[["mean_n_consult_time"]])
130135
}) %>%
136+
simmer::log_(function() {
137+
paste0("🩺 Nurse consultation begins (length: ",
138+
round(get_attribute(env, "nurse_serve_length"), 5L), ")")
139+
}) %>%
140+
timeout(function() get_attribute(env, "nurse_serve_length")) %>%
131141
release("nurse", 1L) %>%
132142
simmer::log_("🚪 Leaves.")
133143
134-
env <- simmer("simulation", verbose = FALSE) %>%
144+
env <- env %>%
135145
add_resource("nurse", param[["number_of_nurses"]]) %>%
136146
add_generator("patient", patient, function() {
137147
rexp(n = 1L, rate = 1L / param[["patient_inter"]])

rmarkdown/logs.md

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Logs
22
================
33
Amy Heather
4-
2025-03-10
4+
2025-03-11
55

66
- [Set up](#set-up)
77
- [Simulation run with logs printed to the
@@ -167,58 +167,61 @@ print(log_contents, sep = "\n")
167167
The patient arrives at 1.10422 and requests a nurse. There is one
168168
available (`SERVE`) so the consultation begins (`Timeout`).
169169

170-
## [5] " 1.10422 | arrival: patient0 | activity: Seize | nurse, 1, 0 paths"
171-
## [6] " 1.10422 | resource: nurse | arrival: patient0 | SERVE"
170+
[5] " 1.10422 | arrival: patient0 | activity: Seize | nurse, 1, 0 paths"
171+
[6] " 1.10422 | resource: nurse | arrival: patient0 | SERVE"
172172
...
173-
## [8] " 1.10422 | arrival: patient0 | activity: Timeout | function()"
173+
[10] " 1.10422 | arrival: patient0 | activity: Timeout | function()"
174174

175-
The consultation finishes at 2.22258, and the patient leaves:
175+
The consultation finishes at 2.26987, and the patient leaves:
176176

177-
## [13] " 2.22258 | arrival: patient0 | activity: Release | nurse, 1"
178-
## [14] " 2.22258 | resource: nurse | arrival: patient0 | DEPART"
179-
## [15] " 2.22258 | task: Post-Release | : | "
177+
[14] " 2.26987 | arrival: patient0 | activity: Release | nurse, 1"
178+
[15] " 2.26987 | resource: nurse | arrival: patient0 | DEPART"
179+
[16] " 2.26987 | task: Post-Release | : | "
180180

181181
#### Example B: `patient2`
182182

183-
The patient arrives at 4.59487, requests a nurse and enters a queue
183+
The patient arrives at 4.5594, requests a nurse and enters a queue
184184
(`ENQUEUE`).
185185

186-
[17] " 4.59487 | arrival: patient2 | activity: Seize | nurse, 1, 0 paths"
187-
[18] " 4.59487 | resource: nurse | arrival: patient2 | ENQUEUE"
186+
[13] " 1.94299 | source: patient | new: patient2 | 4.5594"
187+
...
188+
[21] " 4.5594 | arrival: patient2 | activity: Seize | nurse, 1, 0 paths"
189+
[22] " 4.5594 | resource: nurse | arrival: patient2 | ENQUEUE"
188190

189191
A nurse becomes available at 25.3823 (`SERVE`) so consultation begins
190192
(`Timeout`).
191193

192-
[35] " 25.3823 | resource: nurse | arrival: patient2 | SERVE"
193-
[36] " 25.3823 | arrival: patient2 | activity: Timeout | function()"
194+
[39] " 25.4296 | resource: nurse | arrival: patient2 | SERVE"
195+
...
196+
[42] " 25.4296 | arrival: patient2 | activity: Timeout | function()"
194197

195-
However, there are no further entries as the simulation ends before the
196-
consultation ends.
198+
However, there are no further entries for that patient as the simulation
199+
ends before the consultation ends.
197200

198201
### Compare with recorded results
199202

200203
The logs will align with the recorded results of each patient.
201204

202205
``` r
203-
verbose_run[["arrivals"]]
206+
arrange(verbose_run[["arrivals"]], start_time)
204207
```
205208

206209
## name start_time end_time activity_time resource replication serve_start
207210
## 1 patient0 1.104219 2.269873 1.165654 nurse 0 1.104219
208211
## 2 patient1 1.942991 25.429622 23.159748 nurse 0 2.269873
209-
## 3 patient6 21.796553 NA NA nurse 0 NA
210-
## 4 patient4 15.174872 NA NA nurse 0 NA
211-
## 5 patient3 11.936775 NA NA nurse 0 NA
212+
## 3 patient2 4.559403 NA NA nurse 0 25.429622
213+
## 4 patient3 11.936775 NA NA nurse 0 NA
214+
## 5 patient4 15.174872 NA NA nurse 0 NA
212215
## 6 patient5 20.914277 NA NA nurse 0 NA
213-
## 7 patient2 4.559403 NA NA nurse 0 25.429622
216+
## 7 patient6 21.796553 NA NA nurse 0 NA
214217
## serve_length wait_time wait_time_unseen
215218
## 1 1.165654 0.0000000 NA
216219
## 2 23.159748 0.3268822 NA
217-
## 3 NA NA 8.203447
218-
## 4 NA NA 14.825128
219-
## 5 NA NA 18.063225
220+
## 3 6.096239 20.8702188 NA
221+
## 4 NA NA 18.063225
222+
## 5 NA NA 14.825128
220223
## 6 NA NA 9.085723
221-
## 7 6.096239 20.8702188 NA
224+
## 7 NA NA 8.203447
222225

223226
## Customising the log messages
224227

@@ -237,18 +240,25 @@ distinct).
237240
# Set the seed
238241
set.seed(0L)
239242

243+
env <- simmer("simulation", verbose = FALSE)
244+
240245
# Define the patient trajectory
241246
patient <- trajectory("appointment") %>%
242247
simmer::log_("🚶 Arrives.") %>%
243248
seize("nurse", 1L) %>%
244-
simmer::log_("🩺 Nurse consultation begins.") %>%
245-
timeout(function() {
249+
set_attribute("nurse_serve_start", function() now(env)) %>%
250+
set_attribute("nurse_serve_length", function() {
246251
rexp(n = 1L, rate = 1L / param[["mean_n_consult_time"]])
247252
}) %>%
253+
simmer::log_(function() {
254+
paste0("🩺 Nurse consultation begins (length: ",
255+
round(get_attribute(env, "nurse_serve_length"), 5L), ")")
256+
}) %>%
257+
timeout(function() get_attribute(env, "nurse_serve_length")) %>%
248258
release("nurse", 1L) %>%
249259
simmer::log_("🚪 Leaves.")
250260

251-
env <- simmer("simulation", verbose = FALSE) %>%
261+
env <- env %>%
252262
add_resource("nurse", param[["number_of_nurses"]]) %>%
253263
add_generator("patient", patient, function() {
254264
rexp(n = 1L, rate = 1L / param[["patient_inter"]])
@@ -257,17 +267,17 @@ env <- simmer("simulation", verbose = FALSE) %>%
257267
```
258268

259269
## 1.10422: patient0: 🚶 Arrives.
260-
## 1.10422: patient0: 🩺 Nurse consultation begins.
261-
## 1.97846: patient1: 🚶 Arrives.
262-
## 2.22258: patient0: 🚪 Leaves.
263-
## 2.22258: patient1: 🩺 Nurse consultation begins.
264-
## 4.59487: patient2: 🚶 Arrives.
265-
## 11.9722: patient3: 🚶 Arrives.
266-
## 15.2103: patient4: 🚶 Arrives.
267-
## 20.9497: patient5: 🚶 Arrives.
268-
## 21.832: patient6: 🚶 Arrives.
269-
## 25.3823: patient1: 🚪 Leaves.
270-
## 25.3823: patient2: 🩺 Nurse consultation begins.
270+
## 1.10422: patient0: 🩺 Nurse consultation begins (length: 1.16565)
271+
## 1.94299: patient1: 🚶 Arrives.
272+
## 2.26987: patient0: 🚪 Leaves.
273+
## 2.26987: patient1: 🩺 Nurse consultation begins (length: 23.15975)
274+
## 4.5594: patient2: 🚶 Arrives.
275+
## 11.9368: patient3: 🚶 Arrives.
276+
## 15.1749: patient4: 🚶 Arrives.
277+
## 20.9143: patient5: 🚶 Arrives.
278+
## 21.7966: patient6: 🚶 Arrives.
279+
## 25.4296: patient1: 🚪 Leaves.
280+
## 25.4296: patient2: 🩺 Nurse consultation begins (length: 6.09624)
271281

272282
## Calculate run time
273283

0 commit comments

Comments
 (0)