Skip to content

Commit 96f2ecf

Browse files
committed
msg fn to concat automatically; replace str_interp with paste
1 parent 130aef3 commit 96f2ecf

File tree

5 files changed

+34
-44
lines changed

5 files changed

+34
-44
lines changed

backfill_corrections/delphiBackfillCorrection/R/io.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ export_test_result <- function(test_data, coef_data, indicator, signal,
6363
dir.create(file.path(export_dir, signal_dir), showWarnings = FALSE)
6464

6565
if (nrow(test_data) == 0) {
66-
warning(str_interp("No test data available for ${signal_info}"))
66+
warning("No test data available for ", signal_info)
6767
} else {
68-
msg_ts(str_interp("Saving predictions to disk for ${signal_info} "))
69-
pred_output_file <- str_interp("prediction_${base_name}")
68+
msg_ts("Saving predictions to disk for ", signal_info)
69+
pred_output_file <- paste0("prediction_", base_name)
7070

7171
prediction_col <- colnames(test_data)[grepl("^predicted", colnames(test_data))]
7272
expected_col <- c("time_value", "issue_date", "lag", "geo_value",
@@ -75,10 +75,10 @@ export_test_result <- function(test_data, coef_data, indicator, signal,
7575
}
7676

7777
if (nrow(coef_data) == 0) {
78-
warning(str_interp("No coef data available for ${signal_info}"))
78+
warning("No coef data available for ", signal_info)
7979
} else {
80-
msg_ts(str_interp("Saving coefficients to disk for ${signal_info}"))
81-
coef_output_file <- str_interp("coefs_${base_name}")
80+
msg_ts("Saving coefficients to disk for ", signal_info)
81+
coef_output_file <- paste0("coefs_", base_name)
8282
write_csv(coef_data, file.path(export_dir, signal_dir, coef_output_file))
8383
}
8484
}

backfill_corrections/delphiBackfillCorrection/R/main.R

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#' @importFrom dplyr %>% filter select group_by summarize across everything group_split ungroup
1313
#' @importFrom tidyr drop_na
1414
#' @importFrom rlang .data .env
15-
#' @importFrom stringr str_interp
1615
#'
1716
#' @export
1817
run_backfill <- function(df, params,
@@ -28,7 +27,7 @@ run_backfill <- function(df, params,
2827
}
2928

3029
for (geo_level in geo_levels) {
31-
msg_ts(str_interp("geo level ${geo_level}"))
30+
msg_ts("geo level ", geo_level)
3231
# Get full list of interested locations
3332
if (geo_level == "state") {
3433
# Drop county field and make new "geo_value" field from "state_id".
@@ -65,7 +64,7 @@ run_backfill <- function(df, params,
6564
for (subdf in group_dfs) {
6665
geo <- subdf$geo_value[1]
6766

68-
msg_ts(str_interp("Processing ${geo} geo group"))
67+
msg_ts("Processing ", geo, " geo group")
6968

7069
min_refd <- min(subdf[[refd_col]])
7170
max_refd <- max(subdf[[refd_col]])
@@ -78,7 +77,7 @@ run_backfill <- function(df, params,
7877
# process again. Main use case is for quidel which has overall and
7978
# age-based signals.
8079
if (signal_suffix != "") {
81-
msg_ts(str_interp("signal suffix ${signal_suffix}"))
80+
msg_ts("signal suffix ", signal_suffix)
8281
num_col <- paste(params$num_col, signal_suffix, sep = "_")
8382
denom_col <- paste(params$denom_col, signal_suffix, sep = "_")
8483
} else {
@@ -87,7 +86,7 @@ run_backfill <- function(df, params,
8786
}
8887

8988
for (value_type in params$value_types) {
90-
msg_ts(str_interp("value type ${value_type}"))
89+
msg_ts("value type ", value_type)
9190
# Handle different signal types
9291
if (value_type == "count") { # For counts data only
9392
combined_df <- fill_missing_updates(subdf, num_col, refd_col, lag_col)
@@ -154,16 +153,15 @@ run_backfill <- function(df, params,
154153
}
155154
max_raw = sqrt(max(geo_train_data$value_raw))
156155
for (test_lag in params$test_lags) {
157-
msg_ts(str_interp("test lag ${test_lag}"))
156+
msg_ts("test lag ", test_lag)
158157
filtered_data <- data_filteration(test_lag, geo_train_data,
159158
geo_test_data, params$lag_pad)
160159
train_data <- filtered_data[[1]]
161160
test_data <- filtered_data[[2]]
162161

163162
if (nrow(train_data) == 0 || nrow(test_data) == 0) {
164-
msg_ts(str_interp(
165-
"Not enough data to either train or test for test_lag ${test_lag}, skipping"
166-
))
163+
msg_ts("Not enough data to either train or test for test_lag ",
164+
test_lag, ", skipping")
167165
next
168166
}
169167

@@ -288,25 +286,20 @@ main <- function(params,
288286
params$training_start_date <- result$training_start_date
289287
params$training_end_date <- result$training_end_date
290288

291-
msg_ts(paste0(
292-
str_interp("training_start_date is ${params$training_start_date}, "),
293-
str_interp("training_end_date is ${params$training_end_date}")
294-
))
289+
msg_ts("training_start_date is ", params$training_start_date,
290+
", training_end_date is ", params$training_end_date)
295291

296292
# Loop over every indicator + signal combination.
297293
for (group_i in seq_len(nrow(indicators_subset))) {
298294
input_group <- indicators_subset[group_i,]
299-
msg_ts(str_interp(
300-
"Processing indicator ${input_group$indicator} signal ${input_group$signal}"
301-
))
295+
msg_ts("Processing indicator ", input_group$indicator, " signal ", input_group$signal)
302296

303297
files_list <- get_files_list(
304298
input_group$indicator, input_group$signal, params, input_group$sub_dir
305299
)
306300
if (length(files_list) == 0) {
307-
warning(str_interp(
308-
"No files found for indicator ${input_group$indicator} signal ${input_group$signal}, skipping"
309-
))
301+
warning("No files found for indicator indicator ", input_group$indicator,
302+
" signal ", input_group$signal, ", skipping")
310303
next
311304
}
312305

@@ -327,16 +320,15 @@ main <- function(params,
327320
bind_rows()
328321

329322
if (nrow(input_data) == 0) {
330-
warning(str_interp(
331-
"No data available for indicator ${input_group$indicator} signal ${input_group$signal}, skipping"
332-
))
323+
warning("No data available for indicator ", input_group$indicator,
324+
" signal ", input_group$signal, ", skipping")
333325
next
334326
}
335327

336328
# Check data type and required columns
337329
msg_ts("Validating input data")
338330
for (value_type in params$value_types) {
339-
msg_ts(str_interp("for ${value_type}"))
331+
msg_ts("for ", value_type)
340332
result <- validity_checks(
341333
input_data, value_type,
342334
params$num_col, params$denom_col, input_group$name_suffix,

backfill_corrections/delphiBackfillCorrection/R/model.R

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ add_sqrtscale<- function(train_data, test_data, max_raw, value_col) {
9393
#' @template training_start_date-template
9494
#'
9595
#' @importFrom stats predict coef
96-
#' @importFrom stringr str_interp
9796
#'
9897
#' @export
9998
model_training_and_testing <- function(train_data, test_data, taus, covariates,
@@ -130,7 +129,7 @@ model_training_and_testing <- function(train_data, test_data, taus, covariates,
130129

131130
success = success + 1
132131
},
133-
error=function(e) {msg_ts(str_interp("Training failed for ${model_path}"))}
132+
error=function(e) {msg_ts("Training failed for ", model_path)}
134133
)
135134
}
136135
if (success < length(taus)) {return (NULL)}
@@ -202,12 +201,11 @@ exponentiate_preds <- function(test_data, taus) {
202201
#' @template train_models-template
203202
#'
204203
#' @importFrom quantgen quantile_lasso
205-
#' @importFrom stringr str_interp
206204
get_model <- function(model_path, train_data, covariates, tau,
207205
lambda, lp_solver, train_models) {
208206
if (train_models || !file.exists(model_path)) {
209207
if (!train_models && !file.exists(model_path)) {
210-
warning(str_interp("user requested use of cached model but file {model_path}"),
208+
warning("user requested use of cached model but file ", model_path,
211209
" does not exist; training new model")
212210
}
213211
# Quantile regression
@@ -221,7 +219,7 @@ get_model <- function(model_path, train_data, covariates, tau,
221219
} else {
222220
# Load model from cache invisibly. Object has the same name as the original
223221
# model object, `obj`.
224-
msg_ts(str_interp("Loading from ${model_path}"))
222+
msg_ts("Loading from ", model_path)
225223
load(model_path)
226224
}
227225

@@ -248,21 +246,19 @@ get_model <- function(model_path, train_data, covariates, tau,
248246
#'
249247
#' @return path to file containing model object
250248
#'
251-
#' @importFrom stringr str_interp
252-
#'
253249
generate_filename <- function(indicator, signal,
254250
geo_level, signal_suffix, lambda,
255251
training_end_date, training_start_date, geo="",
256252
value_type = "", test_lag="", tau="", dw="",
257253
beta_prior_mode = FALSE, model_mode = TRUE) {
258254
if (lambda != "") {
259-
lambda <- str_interp("lambda${lambda}")
255+
lambda <- paste0("lambda", lambda)
260256
}
261257
if (test_lag != "") {
262-
test_lag <- str_interp("lag${test_lag}")
258+
test_lag <- paste0("lag", test_lag)
263259
}
264260
if (tau != "") {
265-
tau <- str_interp("tau${tau}")
261+
tau <- paste0("tau", tau)
266262
}
267263
if (beta_prior_mode) {
268264
beta_prior <- "beta_prior"

backfill_corrections/delphiBackfillCorrection/R/utils.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,12 @@ get_populous_counties <- function() {
220220

221221
#' Write a message to the console with the current time
222222
#'
223-
#' @param text the body of the message to display
223+
#' @param ... the body of the message to display. Objects should be strings or
224+
#' coercible to strings and are pasted together with no separator.
224225
#'
225226
#' @export
226-
msg_ts <- function(text) {
227-
message(sprintf("%s --- %s", format(Sys.time()), text))
227+
msg_ts <- function(...) {
228+
message(sprintf("%s --- %s", format(Sys.time()), .makeMessage(...)))
228229
}
229230

230231
#' Generate key for identifying a value_type-signal combo

backfill_corrections/delphiBackfillCorrection/man/msg_ts.Rd

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

0 commit comments

Comments
 (0)