Skip to content

Commit 7de0975

Browse files
authored
Merge pull request #6 from pythonhealthdatascience/dev
Dev
2 parents 345f216 + 79a2746 commit 7de0975

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+16078
-165
lines changed

DESCRIPTION

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ Imports:
2121
dplyr,
2222
future,
2323
future.apply,
24+
rlang,
2425
simmer
2526
Suggests:
2627
devtools,
2728
ggplot2,
28-
scales
29+
lintr,
30+
mockery,
31+
patrick,
32+
scales,
33+
testthat (>= 3.0.0)
2934
Config/testthat/edition: 3

NAMESPACE

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(add_patient_generator)
4+
export(check_all_param_names)
5+
export(check_all_positive)
6+
export(check_nonneg_integer)
7+
export(check_param_names)
8+
export(check_param_values)
9+
export(check_positive_integer)
10+
export(check_prob_vector)
11+
export(check_run_number)
412
export(create_asu_arrivals)
513
export(create_asu_los)
614
export(create_asu_routing)
@@ -10,20 +18,24 @@ export(create_rehab_arrivals)
1018
export(create_rehab_los)
1119
export(create_rehab_routing)
1220
export(create_rehab_trajectory)
21+
export(filter_warmup)
1322
export(get_occupancy_stats)
1423
export(model)
1524
export(runner)
1625
export(sample_routing)
1726
export(transform_to_lnorm)
27+
export(valid_inputs)
1828
importFrom(dplyr,bind_rows)
1929
importFrom(dplyr,filter)
30+
importFrom(dplyr,group_by)
2031
importFrom(dplyr,mutate)
2132
importFrom(dplyr,rowwise)
2233
importFrom(dplyr,ungroup)
2334
importFrom(future,multisession)
2435
importFrom(future,plan)
2536
importFrom(future,sequential)
2637
importFrom(future.apply,future_lapply)
38+
importFrom(rlang,.data)
2739
importFrom(simmer,add_generator)
2840
importFrom(simmer,add_resource)
2941
importFrom(simmer,branch)

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", level = 1L) |>
29+
log_("\U0001F6B6 Arrived at ASU", level = 1L) |>
3030

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

@@ -41,7 +41,7 @@ create_asu_trajectory <- function(env, patient_type, param) {
4141
dest_names <- names(param[["asu_routing"]][[patient_type]])
4242
dest <- dest_names[dest_index]
4343
# Create log message
44-
paste0("🎯 Planned ASU -> ", dest_index, " (", dest, ")")
44+
paste0("\U0001F3AF Planned ASU -> ", dest_index, " (", dest, ")")
4545
}, level = 1L) |>
4646

4747
set_attribute("asu_los", function() {
@@ -73,13 +73,13 @@ create_asu_trajectory <- function(env, patient_type, param) {
7373
}) |>
7474

7575
log_(function() {
76-
paste0(" ASU length of stay: ",
76+
paste0("\U000023F3 ASU length of stay: ",
7777
round(get_attribute(env, "asu_los"), 3L))
7878
}, level = 1L) |>
7979

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

82-
log_("🏁 ASU stay completed", level = 1L) |>
82+
log_("\U0001F3C1 ASU stay completed", level = 1L) |>
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", level = 1L) |>
29+
log_("\U0001F6B6 Arrived at rehab", level = 1L) |>
3030

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

@@ -41,7 +41,7 @@ create_rehab_trajectory <- function(env, patient_type, param) {
4141
dest_names <- names(param[["rehab_routing"]][[patient_type]])
4242
dest <- dest_names[dest_index]
4343
# Create log message
44-
paste0("🎯 Planned rehab -> ", dest_index, " (", dest, ")")
44+
paste0("\U0001F3AF Planned rehab -> ", dest_index, " (", dest, ")")
4545
}, level = 1L) |>
4646

4747
set_attribute("rehab_los", function() {
@@ -72,13 +72,13 @@ create_rehab_trajectory <- function(env, patient_type, param) {
7272
}) |>
7373

7474
log_(function() {
75-
paste0(" Rehab length of stay: ",
75+
paste0("\U000023F3 Rehab length of stay: ",
7676
round(get_attribute(env, "rehab_los"), 3L))
7777
}, level = 1L) |>
7878

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

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

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

R/filter_warmup.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#' Filters arrivals and occupancy to remove warm-up patients.
2+
#'
3+
#' @param result Named list with two tables: arrivals & occupancy.
4+
#' @param warm_up_period Length of warm-up period.
5+
#'
6+
#' @importFrom dplyr filter group_by ungroup
7+
#'
8+
#' @return The name list `result`, but with the tables (`arrivals` and
9+
#' `occupancy`) filtered to remove warm-up patients.
10+
#' @export
11+
12+
filter_warmup <- function(result, warm_up_period) {
13+
if (warm_up_period > 0L) {
14+
result[["arrivals"]] <- result[["arrivals"]] |>
15+
group_by(.data[["name"]]) |>
16+
filter(all(.data[["start_time"]] >= warm_up_period)) |>
17+
ungroup()
18+
result[["occupancy"]] <- filter(result[["occupancy"]],
19+
time >= warm_up_period)
20+
}
21+
result
22+
}

R/get_occupancy_stats.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
#' \item The "1 in every n" patients delayed (inverse of probability of delay)
1313
#' }
1414
#'
15+
#'
1516
#' @param occupancy DataFrame with three columns: \code{resource}, \code{time},
1617
#' and \code{occupancy}.
1718
#'
19+
#' @importFrom rlang .data
20+
#'
1821
#' @return A list of data frames, one per resource, each containing occupancy
1922
#' statistics.
2023
#' @export

R/model.R

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# To avoid package build warning (name in expand.grid())
2+
utils::globalVariables("time")
3+
14
#' Run simulation.
25
#'
36
#' @param run_number Integer representing index of current simulation run.
@@ -6,16 +9,20 @@
69
#' may not wish to do if being set elsewhere - such as done in \code{runner()}).
710
#' Default is TRUE.
811
#'
9-
#' @importFrom dplyr filter mutate rowwise ungroup
12+
#' @importFrom dplyr filter group_by mutate rowwise ungroup
13+
#' @importFrom rlang .data
1014
#' @importFrom simmer add_resource get_mon_arrivals get_mon_resources simmer
1115
#' @importFrom simmer wrap
1216
#' @importFrom utils capture.output
1317
#'
14-
#' @return TBC
18+
#' @return Named list with two tables: arrivals and occupancy.
1519
#' @export
1620

1721
model <- function(run_number, param, set_seed = TRUE) {
1822

23+
# Check all inputs are valid
24+
valid_inputs(run_number, param)
25+
1926
# Set random seed based on run number
2027
if (set_seed) {
2128
set.seed(run_number)
@@ -120,5 +127,12 @@ model <- function(run_number, param, set_seed = TRUE) {
120127
arrivals <- mutate(arrivals, replication = run_number)
121128
occupancy <- mutate(occupancy, replication = run_number)
122129

123-
return(list(arrivals = arrivals, occupancy = occupancy))
130+
result <- list(arrivals = arrivals, occupancy = occupancy)
131+
132+
# Filter the output results if a warm-up period was specified...
133+
result <- filter_warmup(
134+
result = result, warm_up_period = param[["warm_up_period"]]
135+
)
136+
137+
return(result)
124138
}

0 commit comments

Comments
 (0)