Skip to content

Commit 784839e

Browse files
committed
refactor(verbose): prevent parameter printing when running tests by setting verbose = FALSE
1 parent 495e0b3 commit 784839e

File tree

5 files changed

+36
-18
lines changed

5 files changed

+36
-18
lines changed

R/choose_replications.R

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ ReplicationsAlgorithm <- R6Class("ReplicationsAlgorithm", list( # nolint: object
257257
#' @param initial_replications Number of initial replications to perform.
258258
#' @param look_ahead Minimum additional replications to look ahead.
259259
#' @param replication_budget Maximum allowed replications.
260+
#' @param verbose Boolean, whether to print messages about parameters.
260261
initialize = function(
261262
param,
262263
metrics = c("mean_waiting_time_nurse",
@@ -265,7 +266,8 @@ ReplicationsAlgorithm <- R6Class("ReplicationsAlgorithm", list( # nolint: object
265266
desired_precision = 0.1,
266267
initial_replications = 3L,
267268
look_ahead = 5L,
268-
replication_budget = 1000L
269+
replication_budget = 1000L,
270+
verbose = TRUE
269271
) {
270272
self$param <- param
271273
self$metrics <- metrics
@@ -278,8 +280,10 @@ ReplicationsAlgorithm <- R6Class("ReplicationsAlgorithm", list( # nolint: object
278280
self$reps <- initial_replications
279281

280282
# Print the parameters
281-
print("Model parameters:") # nolint: print_linter
282-
print(self$param)
283+
if (isTRUE(verbose)) {
284+
print("Model parameters:") # nolint: print_linter
285+
print(self$param)
286+
}
283287

284288
# Check validity of provided parameters
285289
self$valid_inputs()
@@ -499,17 +503,20 @@ ReplicationsAlgorithm <- R6Class("ReplicationsAlgorithm", list( # nolint: object
499503
#' @param replications Number of times to run the model.
500504
#' @param desired_precision Desired mean deviation from confidence interval.
501505
#' @param metric Name of performance metric to assess.
506+
#' @param verbose Boolean, whether to print messages about parameters.
502507
#'
503508
#' @importFrom utils tail
504509
#'
505510
#' @return Dataframe with results from each replication.
506511
#' @export
507512

508513
confidence_interval_method <- function(replications, desired_precision,
509-
metric) {
514+
metric, verbose = TRUE) {
510515
# Run model for specified number of replications
511516
param <- parameters(number_of_runs = replications)
512-
print(param)
517+
if (isTRUE(verbose)) {
518+
print(param)
519+
}
513520
results <- runner(param, use_future_seeding = FALSE)[["run_results"]]
514521

515522
# Initialise list to store the results

tests/testthat/test-backtest-replications.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ test_that("results from confidence_interval_method are consistent", {
1414
rep_results <- suppressWarnings(confidence_interval_method(
1515
replications = 15L,
1616
desired_precision = 0.1,
17-
metric = "mean_serve_time_nurse"
17+
metric = "mean_serve_time_nurse",
18+
verbose = FALSE
1819
))
1920

2021
# Import the expected results
@@ -42,7 +43,8 @@ test_that("results from ReplicationsAlgorithm are consistent", {
4243
desired_precision = 0.1,
4344
initial_replications = 15L,
4445
look_ahead = 0L,
45-
replication_budget = 15L
46+
replication_budget = 15L,
47+
verbose = FALSE
4648
)
4749
suppressWarnings(alg$select())
4850
rep_results <- alg$summary_table

tests/testthat/test-backtest.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ test_that("results from scenario analysis match those previously generated", {
8181
)
8282

8383
scenario_results <- as.data.frame(
84-
run_scenarios(scenarios, base_list = param)
84+
run_scenarios(scenarios, base_list = param, verbose = FALSE)
8585
)
8686

8787
# Import the expected results

tests/testthat/test-functionaltest-replications.R

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ test_that("output from confidence_interval_method is as expected", {
88
ci_df <- suppressWarnings(confidence_interval_method(
99
replications = reps,
1010
desired_precision = 0.1,
11-
metric = "mean_waiting_time_nurse"
11+
metric = "mean_waiting_time_nurse",
12+
verbose = FALSE
1213
))
1314

1415
# Check that the results dataframe has the right number of rows
@@ -33,7 +34,8 @@ test_that(
3334
man_df <- suppressWarnings(confidence_interval_method(
3435
replications = reps,
3536
desired_precision = desired_precision,
36-
metric = metric
37+
metric = metric,
38+
verbose = FALSE
3739
))
3840

3941
# Run the algorithm (ignoring unsolved warnings)
@@ -43,7 +45,8 @@ test_that(
4345
desired_precision = desired_precision,
4446
initial_replications = reps,
4547
look_ahead = 0L,
46-
replication_budget = reps
48+
replication_budget = reps,
49+
verbose = FALSE
4750
)
4851
suppressWarnings(alg$select())
4952

@@ -63,7 +66,8 @@ test_that("ReplicationsAlgorithm initial_replications consistent to without", {
6366
desired_precision = 0.1,
6467
initial_replications = initial_replications,
6568
look_ahead = 10L,
66-
replication_budget = 10L
69+
replication_budget = 10L,
70+
verbose = FALSE
6771
)
6872
suppressWarnings(alg$select())
6973
head(alg$summary_table, 10L)
@@ -83,7 +87,8 @@ test_that("running algorithm with < 3 replications has no solution", {
8387
alg <- ReplicationsAlgorithm$new(param = parameters(),
8488
initial_replications = 0L,
8589
replication_budget = 2L,
86-
look_ahead = 0L)
90+
look_ahead = 0L,
91+
verbose = FALSE)
8792
# Check that it runs with a warning
8893
expect_warning(alg$select())
8994
# Check that there is no solution

tests/testthat/test-unittest-replications.R

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ patrick::with_parameters_test_that(
66
# Calculate klimit
77
calc <- ReplicationsAlgorithm$new(param = parameters(),
88
look_ahead = look_ahead,
9-
initial_replications = n)$klimit()
9+
initial_replications = n,
10+
verbose = FALSE)$klimit()
1011
# Check that it meets our expected value
1112
expect_identical(calc, exp)
1213
},
@@ -21,7 +22,8 @@ patrick::with_parameters_test_that(
2122
patrick::with_parameters_test_that(
2223
"ReplicationsAlgorithm responds appropriately to invalid parameters",
2324
{
24-
inputs <- c(list(param = parameters()), setNames(list(value), arg))
25+
inputs <- c(list(param = parameters()), setNames(list(value), arg),
26+
verbose = FALSE)
2527
expect_error(do.call(ReplicationsAlgorithm$new, inputs), msg)
2628
},
2729
patrick::cases(
@@ -47,7 +49,8 @@ test_that(
4749
expect_error(
4850
ReplicationsAlgorithm$new(param = parameters(),
4951
initial_replications = 10L,
50-
replication_budget = 9L),
52+
replication_budget = 9L,
53+
verbose = FALSE),
5154
"replication_budget must be less than initial_replications."
5255
)
5356
}
@@ -138,7 +141,8 @@ patrick::with_parameters_test_that(
138141
# Set threshold to 0.5, with provided look_ahead
139142
alg <- ReplicationsAlgorithm$new(param = parameters(),
140143
desired_precision = 0.5,
141-
look_ahead = look_ahead)
144+
look_ahead = look_ahead,
145+
verbose = FALSE)
142146
# Get result from algorithm and compare to expected
143147
result <- alg$find_position(lst)
144148
expect_identical(result, exp)
@@ -170,6 +174,6 @@ patrick::with_parameters_test_that(
170174

171175

172176
test_that("find_position() fails if not supplied a list", {
173-
alg <- ReplicationsAlgorithm$new(param = parameters())
177+
alg <- ReplicationsAlgorithm$new(param = parameters(), verbose = FALSE)
174178
expect_error(alg$find_position(c(1L, 2L, 3L)))
175179
})

0 commit comments

Comments
 (0)