Skip to content

Commit eedab28

Browse files
committed
feat(speed): set log_() level so only record when verbose - massive speed increase! 1m16s -> 0m33s
1 parent 934e395 commit eedab28

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

R/create_asu_trajectory.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ create_asu_trajectory <- function(env, patient_type, param) {
2626
# Set up simmer trajectory object...
2727
trajectory(paste0("ASU_", patient_type, "_path")) |>
2828

29-
log_("🚶 Arrived at ASU") |>
29+
log_("🚶 Arrived at ASU", level = 1) |>
3030

3131
seize("asu_bed", 1L) |>
3232

@@ -42,7 +42,7 @@ create_asu_trajectory <- function(env, patient_type, param) {
4242
dest <- dest_names[dest_index]
4343
# Create log message
4444
paste0("🎯 Planned ASU -> ", dest_index, " (", dest, ")")
45-
}) |>
45+
}, level = 1) |>
4646

4747
set_attribute("asu_los", function() {
4848
# Retrieve attribute, and use to get post-ASU destination as a string
@@ -75,11 +75,11 @@ create_asu_trajectory <- function(env, patient_type, param) {
7575
log_(function() {
7676
paste0("⏳ ASU length of stay: ",
7777
round(get_attribute(env, "asu_los"), 3L))
78-
}) |>
78+
}, level = 1) |>
7979

8080
timeout(function() get_attribute(env, "asu_los")) |>
8181

82-
log_("🏁 ASU stay completed") |>
82+
log_("🏁 ASU stay completed", level = 1) |>
8383

8484
release("asu_bed", 1L) |>
8585

R/create_rehab_trajectory.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ create_rehab_trajectory <- function(env, patient_type, param) {
2626
# Set up simmer trajectory object...
2727
trajectory(paste0("rehab_", patient_type, "_path")) |>
2828

29-
log_("🚶 Arrived at rehab") |>
29+
log_("🚶 Arrived at rehab", level = 1) |>
3030

3131
seize("rehab_bed", 1L) |>
3232

@@ -42,7 +42,7 @@ create_rehab_trajectory <- function(env, patient_type, param) {
4242
dest <- dest_names[dest_index]
4343
# Create log message
4444
paste0("🎯 Planned rehab -> ", dest_index, " (", dest, ")")
45-
}) |>
45+
}, level = 1) |>
4646

4747
set_attribute("rehab_los", function() {
4848
# Retrieve attribute, and use to get post-rehab destination as a string
@@ -74,11 +74,11 @@ create_rehab_trajectory <- function(env, patient_type, param) {
7474
log_(function() {
7575
paste0("⏳ Rehab length of stay: ",
7676
round(get_attribute(env, "rehab_los"), 3L))
77-
}) |>
77+
}, level = 1) |>
7878

7979
timeout(function() get_attribute(env, "rehab_los")) |>
8080

81-
log_("🏁 Rehab stay completed") |>
81+
log_("🏁 Rehab stay completed", level = 1) |>
8282

8383
release("rehab_bed", 1L)
8484
}

R/get_occupancy_stats.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ get_occupancy_stats <- function(occupancy) {
5454
occ_stats[["pct"]] <- occ_stats[["freq"]] / sum(occ_stats[["freq"]])
5555
occ_stats[["c_pct"]] <- cumsum(occ_stats[["pct"]])
5656

57-
# Calculate probability of delay
57+
# Calculate probability of delay using the Erlang loss formula
5858
occ_stats[["prob_delay"]] <- occ_stats[["pct"]] / occ_stats[["c_pct"]]
5959

6060
# Calculate 1 in every n patients delayed

R/model.R

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ model <- function(run_number, param, set_seed = TRUE) {
2222
}
2323

2424
# Determine whether to get verbose activity logs
25-
verbose <- any(c(param[["log_to_console"]], param[["log_to_file"]]))
25+
param[["verbose"]] <- any(c(param[["log_to_console"]],
26+
param[["log_to_file"]]))
2627

2728
# Transform LOS parameters to lognormal scale
2829
param[["asu_los_lnorm"]] <- transform_to_lnorm(param[["asu_los"]])
2930
param[["rehab_los_lnorm"]] <- transform_to_lnorm(param[["rehab_los"]])
3031

3132
# Create simmer environment - set verbose to FALSE as using custom logs
3233
# (but can change to TRUE if want to see default simmer logs as well)
33-
env <- simmer("simulation", verbose = FALSE)
34+
env <- simmer("simulation", verbose = FALSE,
35+
log_level = if (param[["verbose"]]) 1 else 0)
3436

3537
# Add ASU and rehab direct admission patient generators
3638
for (unit in c("asu", "rehab")) {
@@ -70,7 +72,7 @@ model <- function(run_number, param, set_seed = TRUE) {
7072
)
7173

7274
# Save and/or display the log
73-
if (isTRUE(verbose)) {
75+
if (isTRUE(param[["verbose"]])) {
7476
# Create full log message by adding parameters
7577
param_string <- paste(names(param), param, sep = "=", collapse = ";\n ")
7678
full_log <- append(c("Parameters:", param_string, "Log:"), sim_log)

0 commit comments

Comments
 (0)