Skip to content

Commit 2323517

Browse files
committed
chore(lint/round): build docs, lint files, and round cores output so doesn't change with every run (as it is time taken, which we would expect to vary slightly)
1 parent 23211d5 commit 2323517

File tree

12 files changed

+60
-52
lines changed

12 files changed

+60
-52
lines changed

DESCRIPTION

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ Imports:
2424
rlang,
2525
tidyr,
2626
R6,
27-
tidyselect
27+
tidyselect,
28+
future,
29+
future.apply
2830
Suggests:
2931
testthat (>= 3.0.0),
3032
lintr,

R/defaults.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Defaults <- R6Class( # nolint
9393
#' the class without needing to run `Defaults[["new"]]()` every time.
9494
#'
9595
#' @return Instance of the Defaults class.
96-
#'
96+
#'
9797
#' @export
9898
#' @rdname defaults
9999

R/model.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ model <- function(run_number, param_class, set_seed = TRUE) {
2828
valid_inputs(run_number, param)
2929

3030
# Set random seed based on run number
31-
if (set_seed == TRUE) {
31+
if (set_seed) {
3232
set.seed(run_number)
3333
}
3434

R/trial.R

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@ trial <- function(param_class) {
3030
# within the function, and we don't want to override future.seed
3131
results <- future_lapply(
3232
1L:n_runs,
33-
function(i) simulation::model(
34-
run_number = i, param_class = param_class, set_seed = FALSE),
35-
future.seed = 123456
33+
function(i) {
34+
simulation::model(run_number = i,
35+
param_class = param_class,
36+
set_seed = FALSE)
37+
},
38+
future.seed = 123456L
3639
)
3740

3841
# Combine the results from multiple replications into just two dataframes

man/model.Rd

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/trial.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

outputs/cores1.png

10.5 KB
Loading

outputs/cores2.png

11.8 KB
Loading

rmarkdown/analysis.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,11 @@ devtools::install()
4545
## checking DESCRIPTION meta-information ... ✔ checking DESCRIPTION meta-information
4646
## ─ checking for LF line-endings in source and make files and shell scripts
4747
## ─ checking for empty or unneeded directories
48-
## Removed empty directory ‘simulation/tests/testthat/_snaps’
49-
## Omitted ‘LazyData’ from DESCRIPTION
48+
## Omitted ‘LazyData’ from DESCRIPTION
5049
## ─ building ‘simulation_0.1.0.tar.gz’
5150
##
5251
## Running /opt/R/4.4.1/lib/R/bin/R CMD INSTALL \
53-
## /tmp/RtmpRZdNqo/simulation_0.1.0.tar.gz --install-tests
52+
## /tmp/Rtmpbqk15J/simulation_0.1.0.tar.gz --install-tests
5453
## * installing to library ‘/home/amy/.cache/R/renv/library/rap_template_r_des-cd7d6844/linux-ubuntu-noble/R-4.4/x86_64-pc-linux-gnu’
5554
## * installing *source* package ‘simulation’ ...
5655
## ** using staged installation
@@ -423,7 +422,7 @@ print(table_latex)
423422
```
424423

425424
## % latex table generated in R 4.4.1 by xtable 1.8-4 package
426-
## % Mon Jan 27 15:52:00 2025
425+
## % Mon Jan 27 16:03:02 2025
427426
## \begin{table}[ht]
428427
## \centering
429428
## \begin{tabular}{rrllll}
@@ -532,7 +531,7 @@ print(sensitivity_table_latex)
532531
```
533532

534533
## % latex table generated in R 4.4.1 by xtable 1.8-4 package
535-
## % Mon Jan 27 15:52:05 2025
534+
## % Mon Jan 27 16:03:07 2025
536535
## \begin{table}[ht]
537536
## \centering
538537
## \begin{tabular}{rrl}
@@ -574,12 +573,12 @@ tail(result[["arrivals"]])
574573
```
575574

576575
## name start_time end_time activity_time resource replication
577-
## 160 patient104 51.17679 NA NA nurse 0
578-
## 161 patient110 53.57293 NA NA nurse 0
579-
## 162 patient148 74.24750 NA NA nurse 0
580-
## 163 patient149 74.50965 NA NA nurse 0
576+
## 160 patient97 47.84534 NA NA nurse 0
577+
## 161 patient155 77.41953 NA NA nurse 0
578+
## 162 patient66 35.72669 NA NA nurse 0
579+
## 163 patient52 27.21852 NA NA nurse 0
581580
## 164 patient156 77.59569 NA NA nurse 0
582-
## 165 patient158 78.34954 NA NA nurse 0
581+
## 165 patient160 78.41585 NA NA nurse 0
583582

584583
## Example run with logs
585584

@@ -818,8 +817,8 @@ verbose_run[["arrivals"]]
818817
## 22 patient12 46.4500010 86.039329 39.5893285 nurse 0
819818
## 23 patient22 77.2295939 87.175152 9.9455579 nurse 0
820819
## 24 patient25 86.1477594 91.726053 5.5782935 nurse 0
821-
## 25 patient24 84.4587897 NA NA nurse 0
822-
## 26 patient26 99.0189155 NA NA nurse 0
820+
## 25 patient26 99.0189155 NA NA nurse 0
821+
## 26 patient24 84.4587897 NA NA nurse 0
823822
## 27 patient23 82.4020925 NA NA nurse 0
824823

825824
## Calculate run time
@@ -835,4 +834,4 @@ seconds <- as.integer(runtime %% 60L)
835834
print(sprintf("Notebook run time: %dm %ds", minutes, seconds))
836835
```
837836

838-
## [1] "Notebook run time: 0m 18s"
837+
## [1] "Notebook run time: 0m 17s"

rmarkdown/choosing_parameters.Rmd

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ confidence_interval_method <- function(replications, desired_precision, metric,
145145
warning("Running ", replications, " replications did not reach ",
146146
"desired precision (", desired_precision, ").")
147147
}
148-
148+
149149
# Plot the cumulative mean and confidence interval
150150
p <- ggplot(cumulative, aes(x = .data[["replications"]],
151151
y = .data[["cumulative_mean"]])) +
@@ -165,7 +165,7 @@ confidence_interval_method <- function(replications, desired_precision, metric,
165165
166166
# Save the plot
167167
ggsave(filename = path, width = 6.5, height = 4L, bg = "white")
168-
168+
169169
return(cumulative)
170170
}
171171
```
@@ -187,8 +187,8 @@ ci_df <- confidence_interval_method(
187187
188188
# View first ten rows were percentage deviation is below 5
189189
ci_df %>%
190-
filter(perc_deviation < 5) %>%
191-
head(10)
190+
filter(perc_deviation < 5L) %>%
191+
head(10L)
192192
193193
# View plot
194194
include_graphics(path)
@@ -211,8 +211,8 @@ ci_df <- confidence_interval_method(
211211
212212
# View first ten rows were percentage deviation is below 5
213213
ci_df %>%
214-
filter(perc_deviation < 5) %>%
215-
head(10)
214+
filter(perc_deviation < 5L) %>%
215+
head(10L)
216216
217217
# View plot
218218
include_graphics(path)
@@ -243,8 +243,9 @@ run_cores <- function(n_cores, file, model_param = NULL) {
243243
param_class[["update"]](list(cores = i))
244244
invisible(trial(param_class))
245245
246-
# Record time taken
247-
cores_time <- as.numeric(Sys.time() - cores_start, units = "secs")
246+
# Record time taken, rounded to nearest .5 dp by running round(x*2)/2
247+
cores_time <- round(
248+
as.numeric(Sys.time() - cores_start, units = "secs")*2)/2
248249
speed[[i]] <- list(cores = i, run_time = round(cores_time, 3L))
249250
}
250251
@@ -254,7 +255,7 @@ run_cores <- function(n_cores, file, model_param = NULL) {
254255
# Generate plot
255256
p <- ggplot(speed_df, aes(x = .data[["cores"]], y = .data[["run_time"]])) +
256257
geom_line() +
257-
labs(x = "Cores", y = "Run time (seconds)") +
258+
labs(x = "Cores", y = "Run time (rounded to nearest .5 seconds)") +
258259
theme_minimal()
259260
260261
# Save plot
@@ -270,7 +271,7 @@ run_cores <- function(n_cores, file, model_param = NULL) {
270271
Setting up and managing a parallel cluster takes extra time. For small tasks or few iterations, this extra time can be more than the time saved by running in parallel.
271272

272273
```{r}
273-
run_cores(5, "cores1.png")
274+
run_cores(5L, "cores1.png")
274275
```
275276

276277
Having increased the simulation length, we now see that parallelisation is decreasing the model run time.
@@ -280,7 +281,7 @@ However, when you use more cores, the data needs to be divided and sent to more
280281
The optimal number of cores will vary depending on your model parameters and machine.
281282

282283
```{r}
283-
run_cores(5, "cores2.png", list(data_collection_period = 100000L))
284+
run_cores(5L, "cores2.png", list(data_collection_period = 100000L))
284285
```
285286

286287
## Run time

0 commit comments

Comments
 (0)