Skip to content

Commit e89333c

Browse files
committed
feat(logs): add custom logs (as felt this would particularly benefit, as the default are so hard to read, and I wanted to be sure everything was set up right - which it looks to be!) :)
1 parent 72436c7 commit e89333c

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ importFrom(simmer,branch)
1818
importFrom(simmer,get_attribute)
1919
importFrom(simmer,get_mon_arrivals)
2020
importFrom(simmer,get_mon_resources)
21+
importFrom(simmer,log_)
2122
importFrom(simmer,set_attribute)
2223
importFrom(simmer,simmer)
2324
importFrom(simmer,timeout)
2425
importFrom(simmer,trajectory)
2526
importFrom(simmer,wrap)
2627
importFrom(stats,rexp)
2728
importFrom(stats,rlnorm)
29+
importFrom(utils,capture.output)

R/create_asu_trajectory.R

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#' of routing to each destination (e.g.
1414
#' \code{param$asu_routing$stroke$rehab = 0.24}).
1515
#'
16-
#' @importFrom simmer branch trajectory
16+
#' @importFrom simmer branch get_attribute log_ set_attribute timeout trajectory
1717
#' @importFrom stats rlnorm
1818
#'
1919
#' @return Simmer trajectory object. Defines patient journey logic through the
@@ -25,13 +25,23 @@ create_asu_trajectory <- function(env, patient_type, param) {
2525
# Set up simmer trajectory object...
2626
trajectory(paste0("ASU_", patient_type, "_path")) |>
2727

28+
log_("🚶 Arrived at ASU") |>
29+
2830
# Sample destination after ASU (as destination influences length of stay)
2931
set_attribute("post_asu_destination", function() {
3032
sample_routing(prob_list = param[["asu_routing"]][[patient_type]])
3133
}) |>
3234

33-
timeout(function() {
35+
log_(function() {
36+
# Retrieve attribute, and use to get post-ASU destination as a string
37+
dest_index <- get_attribute(env, "post_asu_destination")
38+
dest_names <- names(param[["asu_routing"]][[patient_type]])
39+
dest <- dest_names[dest_index]
40+
# Create log message
41+
paste0("🎯 Planned ASU -> ", dest_index, " (", dest, ")")
42+
}) |>
3443

44+
set_attribute("asu_los", function() {
3545
# Retrieve attribute, and use to get post-ASU destination as a string
3646
dest_index <- get_attribute(env, "post_asu_destination")
3747
dest_names <- names(param[["asu_routing"]][[patient_type]])
@@ -59,6 +69,15 @@ create_asu_trajectory <- function(env, patient_type, param) {
5969
)
6070
}) |>
6171

72+
log_(function() {
73+
paste0("⏳ ASU length of stay: ",
74+
round(get_attribute(env, "asu_los"), 3L))
75+
}) |>
76+
77+
timeout(function() get_attribute(env, "asu_los")) |>
78+
79+
log_("🏁 ASU stay completed") |>
80+
6281
# If that patient's destination is rehab, then start on that trajectory
6382
branch(
6483
option = function() {

R/create_rehab_trajectory.R

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#' probability of routing to each destination (e.g.
1414
#' \code{param$rehab_routing$stroke$esd = 0.40}).
1515
#'
16-
#' @importFrom simmer trajectory
16+
#' @importFrom simmer get_attribute log_ set_attribute timeout trajectory
1717
#' @importFrom stats rlnorm
1818
#'
1919
#' @return Simmer trajectory object. Defines patient journey logic through the
@@ -25,13 +25,23 @@ create_rehab_trajectory <- function(env, patient_type, param) {
2525
# Set up simmer trajectory object...
2626
trajectory(paste0("rehab_", patient_type, "_path")) |>
2727

28+
log_("🚶 Arrived at rehab") |>
29+
2830
# Sample destination after rehab (as destination influences length of stay)
2931
set_attribute("post_rehab_destination", function() {
3032
sample_routing(prob_list = param[["rehab_routing"]][[patient_type]])
3133
}) |>
3234

33-
timeout(function() {
35+
log_(function() {
36+
# Retrieve attribute, and use to get post-rehab destination as a string
37+
dest_index <- get_attribute(env, "post_rehab_destination")
38+
dest_names <- names(param[["rehab_routing"]][[patient_type]])
39+
dest <- dest_names[dest_index]
40+
# Create log message
41+
paste0("🎯 Planned rehab -> ", dest_index, " (", dest, ")")
42+
}) |>
3443

44+
set_attribute("rehab_los", function() {
3545
# Retrieve attribute, and use to get post-rehab destination as a string
3646
dest_index <- get_attribute(env, "post_rehab_destination")
3747
dest_names <- names(param[["rehab_routing"]][[patient_type]])
@@ -56,5 +66,14 @@ create_rehab_trajectory <- function(env, patient_type, param) {
5666
meanlog = los_params[["meanlog"]],
5767
sdlog = los_params[["sdlog"]]
5868
)
59-
})
69+
}) |>
70+
71+
log_(function() {
72+
paste0("⏳ Rehab length of stay: ",
73+
round(get_attribute(env, "rehab_los"), 3L))
74+
}) |>
75+
76+
timeout(function() get_attribute(env, "rehab_los")) |>
77+
78+
log_("🏁 Rehab stay completed")
6079
}

R/model.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ model <- function(run_number, param, set_seed = TRUE) {
2626
param[["asu_los_lnorm"]] <- transform_to_lnorm(param[["asu_los"]])
2727
param[["rehab_los_lnorm"]] <- transform_to_lnorm(param[["rehab_los"]])
2828

29-
# Create simmer environment
30-
env <- simmer("simulation", verbose = verbose)
29+
# Create simmer environment - set verbose to FALSE as using custom logs
30+
env <- simmer("simulation", verbose = FALSE)
3131

3232
# Add ASU and rehab direct admission patient generators
3333
for (unit in c("asu", "rehab")) {
@@ -42,7 +42,7 @@ model <- function(run_number, param, set_seed = TRUE) {
4242

4343
# Add patient generator using the created trajectory
4444
sim_log <- capture.output(
45-
env <- add_patient_generator(
45+
env <- add_patient_generator( # nolint
4646
env = env,
4747
trajectory = traj,
4848
unit = unit,
@@ -55,7 +55,7 @@ model <- function(run_number, param, set_seed = TRUE) {
5555

5656
# Run the model
5757
sim_log <- capture.output(
58-
env <- env |>
58+
env <- env |> # nolint
5959
simmer::run(20L) |>
6060
wrap()
6161
)

0 commit comments

Comments
 (0)